KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ChatComThread


1 /* Chat
2  * Copyright (C) 1998-2004 Alexis de Bernis
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  *
19  */

20
21 import java.io.*;
22 import java.net.*;
23 import java.util.*;
24
25 public class ChatComThread extends Thread JavaDoc
26 {
27     private Chat parent;
28     private InetAddress address;
29     private Socket sock;
30     private BufferedReader from_server;
31     private BufferedWriter for_server;
32     private String JavaDoc received, my_nick;
33     private ResourceBundle captions;
34
35     ChatComThread(Chat parent, InetAddress address, Socket sock)
36     {
37         this.parent=parent;
38         this.address=address;
39         this.sock=sock;
40         captions = parent.get_translated_messages();
41         my_nick = parent.get_my_nick();
42
43         try
44         {
45             from_server=new BufferedReader(new InputStreamReader(
46              sock.getInputStream()));
47             for_server=new BufferedWriter(new OutputStreamWriter(
48              sock.getOutputStream()));
49         }catch(Exception JavaDoc e)
50         {
51             return;
52         }
53     }
54
55     public void run()
56     {
57         try {
58             while(true) {
59                 received=from_server.readLine();
60
61                 if(received==null)
62                     return;
63
64                 dispatch_msg(received);
65             }
66         }catch(Exception JavaDoc e) {
67             return;
68         }
69     }
70
71     public void dispatch_msg(String JavaDoc msg)
72     {
73         String JavaDoc tmp;
74         
75         msg = msg.substring(msg.indexOf("<|>")+4).trim();
76
77 // parent.disp_message("*** DEBUG : Dispatching message "+msg+"\n");
78

79         if(msg.startsWith("TALK <|>")) {
80             msg=msg.substring(msg.indexOf("<|>")+4).trim();
81             tmp=msg.substring(msg.indexOf("<|>")+4).trim();
82             
83             msg=msg.substring(0,msg.indexOf("<|>")).trim();
84             tmp=tmp.substring(0,tmp.indexOf("<|>")).trim();
85             
86             if(!msg.equals(my_nick))
87                 parent.disp_message("<"+msg+"> "+tmp+"\n");
88
89             return;
90         }
91
92         if(msg.startsWith("ACTION <|>")) {
93             msg=msg.substring(msg.indexOf("<|>")+4).trim();
94             tmp=msg.substring(msg.indexOf("<|>")+4).trim();
95             
96             msg=msg.substring(0,msg.indexOf("<|>")).trim();
97             tmp=tmp.substring(0,tmp.indexOf("<|>")).trim();
98             
99             parent.disp_message("* "+msg+" "+tmp+"\n");
100             return;
101         }
102
103         if(msg.startsWith("CONNECTION <|>")) {
104             msg=msg.substring(msg.indexOf("<|>")+4).trim();
105             parent.add_user(msg.substring(0,msg.indexOf("<|>")).trim());
106         }
107         
108         if(msg.startsWith("DECONNECTION <|>")) {
109             msg=msg.substring(msg.indexOf("<|>")+4).trim();
110             tmp=msg.substring(msg.indexOf("<|>")+4).trim();
111             
112             msg=msg.substring(0,msg.indexOf("<|>")).trim();
113             tmp=tmp.substring(0,tmp.indexOf("<|>")).trim();
114             parent.remove_user(msg, tmp);
115         }
116
117         if(msg.startsWith("ERROR <|>")) {
118             msg=msg.substring(msg.indexOf("<|>")+4).trim();
119             parent.disp_message("*** "+captions.getString("MSG_ERROR")+" : "+msg.substring(0,msg.indexOf("<|>"))+"\n");
120         }
121
122         if(msg.startsWith("MSG <|>")) {
123             msg=msg.substring(msg.indexOf("<|>")+4).trim();
124             parent.disp_message("** "+msg.substring(0,msg.indexOf("<|>"))+"\n");
125         }
126
127         if(msg.startsWith("PING")) {
128             tmp = msg.substring(msg.indexOf("<|>")+4).trim();
129             tmp = tmp.substring(0,tmp.indexOf("<|>")).trim();
130             send_msg("CLIENT <|> PONG <|> " + tmp + " <|> CLIENT");
131         }
132
133         if(msg.startsWith("PMSG")) {
134             msg=msg.substring(msg.indexOf("<|>")+4).trim();
135             tmp=msg.substring(msg.indexOf("<|>")+4).trim();
136             
137             msg=msg.substring(0,msg.indexOf("<|>")).trim();
138             tmp=tmp.substring(0,tmp.indexOf("<|>")).trim();
139             
140             parent.disp_private_msg(msg, tmp);
141             return;
142         }
143     }
144
145     public void send_msg(String JavaDoc msg)
146     {
147         try {
148             for_server.write(msg+"\n");
149             for_server.flush();
150         }catch(Exception JavaDoc e) {
151             System.out.println("Error :: send_msg() : " + e);
152             e.printStackTrace();
153         }
154     }
155
156     public void send_message(String JavaDoc nick, String JavaDoc text)
157     {
158         try
159         {
160             for_server.write("CLIENT <|> TALK <|> " + text + " <|> CLIENT\n");
161             for_server.flush();
162         } catch(Exception JavaDoc e)
163         {
164             return;
165         }
166     }
167
168     public void finish_all()
169     {
170         try
171         {
172             sock.close();
173         }catch(Exception JavaDoc e)
174         {
175             return;
176         }
177     }
178 }
179
Popular Tags