KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > dcc > Chat


1 /*
2     jerk.irc.dcc.ChatClient [ created - 1/2/02 ]
3
4     Author: Raphael Mudge (rsmudge@mtu.edu)
5     
6     Description:
7     handles the details of talking in a DCC connection, nothing to do
8     with actually establishing the connection. Talks to a JerkEngine
9     reference.
10
11     Documentation:
12     n/a
13
14     Changelog:
15        
16 */

17
18 package rero.dcc;
19
20 import java.util.*;
21
22 import java.net.*;
23 import java.io.*;
24
25 import rero.ircfw.*;
26 import rero.ircfw.interfaces.*;
27
28 import rero.config.*;
29
30 public class Chat extends ProtocolDCC implements ClientStateListener
31 {
32     protected BufferedReader input;
33     protected PrintStream output;
34
35     public Chat(String JavaDoc _nickname)
36     {
37        nickname = _nickname;
38        ClientState.getClientState().addClientStateListener("client.encoding", this);
39    }
40
41     public void propertyChanged(String JavaDoc property, String JavaDoc value)
42     {
43       try
44       {
45          if (input != null && socket.isConnected())
46          {
47             input = new BufferedReader( ClientState.getClientState().getProperInputStream( socket.getInputStream() ) );
48             output = ClientState.getClientState().getProperPrintStream( socket.getOutputStream() );
49          }
50       }
51       catch (Exception JavaDoc ex)
52       {
53          System.out.println("Unable to switch encodings...");
54          ex.printStackTrace();
55       }
56     }
57   
58     /** returns the nickname of who we are having a *chat* with */
59     public String JavaDoc getNickname()
60     {
61        return nickname;
62     }
63
64     /** sends a message to the chat */
65     public void sendln(String JavaDoc text)
66     {
67        output.println(text);
68     }
69     
70     public int getTypeOfDCC()
71     {
72        return DCC_CHAT;
73     }
74
75     public void run()
76     {
77        if (socket == null || !socket.isConnected())
78        {
79           return;
80        }
81
82        try
83        {
84           socket.setKeepAlive(true);
85        }
86        catch (Exception JavaDoc ex)
87        {
88           ex.printStackTrace();
89        }
90
91        fireEvent("CHAT_OPEN", null);
92
93        String JavaDoc text;
94
95        try
96        {
97           output = ClientState.getClientState().getProperPrintStream( socket.getOutputStream() );
98           input = new BufferedReader( ClientState.getClientState().getProperInputStream( socket.getInputStream() ) );
99
100           while (socket.isConnected())
101           {
102              text = input.readLine();
103              if (text == null)
104              {
105                 fireEvent("CHAT_CLOSE", "closed");
106                 return;
107              }
108
109              idleTime = System.currentTimeMillis();
110
111              fireEvent("CHAT", text);
112           }
113          
114           fireEvent("CHAT_CLOSE", "closed");
115        }
116        catch (Exception JavaDoc ex)
117        {
118           ex.printStackTrace();
119
120           fireError(ex.getMessage());
121        }
122     }
123
124     public void fireEvent(String JavaDoc event, String JavaDoc description)
125     {
126        eventData.clear();
127
128        eventData.put(FrameworkConstants.$NICK$, getNickname());
129        eventData.put(FrameworkConstants.$EVENT$, event);
130        eventData.put("$this", this.toString());
131
132  
133        if (description != null)
134        {
135           eventData.put(FrameworkConstants.$DATA$, getNickname() + " " + description);
136           eventData.put(FrameworkConstants.$PARMS$, description);
137        }
138
139        dispatcher.dispatchEvent(eventData);
140     }
141
142     public void fireError(String JavaDoc description)
143     {
144        eventData.put(FrameworkConstants.$NICK$, getNickname());
145        eventData.put(FrameworkConstants.$EVENT$, "CHAT_CLOSE");
146        eventData.put(FrameworkConstants.$DATA$, getNickname() + " " + description);
147        eventData.put(FrameworkConstants.$PARMS$, description);
148        eventData.put("$this", this.toString());
149
150        dispatcher.dispatchEvent(eventData);
151     }
152 }
153
Popular Tags