KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > client > Listener


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2002 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.client;
20
21 import org.lucane.client.util.*;
22 import org.lucane.common.*;
23 import org.lucane.common.net.ObjectConnection;
24 import org.lucane.common.net.ServerSocketFactory;
25
26 import java.net.*;
27 import java.io.*;
28 import java.util.*;
29
30
31 /**
32  * A Listener is the client's "server" part
33  */

34 class Listener
35 extends Thread JavaDoc
36 {
37     
38     private ServerSocket socket;
39     private Client parent;
40     private boolean stop;
41     private int port;
42     
43     /**
44      * Constructor. Initialize the ServerSocket
45      */

46     public Listener(String JavaDoc privateKey, String JavaDoc userName, String JavaDoc passwd)
47     {
48         Random rnd = new Random();
49         boolean ok = false;
50         this.parent = Client.getInstance();
51         
52         port = parent.getConfig().getListenerPort();
53         if(port == 0)
54             port = rnd.nextInt(62000) + 1025;
55         
56         while(! ok)
57         {
58             try
59             {
60                 if(Communicator.getInstance().useSSL())
61                 {
62                     this.socket = ServerSocketFactory.getServerSocket(port, privateKey, userName, passwd);
63                     Logging.getLogger().info("Listener port (ssl) : " + port);
64                 }
65                 else
66                 {
67                     this.socket = ServerSocketFactory.getServerSocket(port);
68                     Logging.getLogger().info("Listener port (plain) : " + port);
69                 }
70                 ok = true;
71             }
72             catch(IOException ex)
73             {
74                 ex.printStackTrace();
75                 port = rnd.nextInt(62000) + 1025;
76             }
77             catch(Exception JavaDoc ex)
78             {
79                 ex.printStackTrace();
80             }
81         }
82     }
83     
84     /**
85      * Accept all connections. Start a new Thread.
86      */

87     public void listen()
88     {
89         this.start();
90     }
91     
92     /**
93      * Called by listen(). You don't need to start the thread by yourself.
94      */

95     public void run()
96     {
97         Socket client = null;
98         
99         while(! stop)
100         {
101             try
102             {
103                 client = socket.accept();
104                 
105                 InetAddress ia = client.getInetAddress();
106                 Logging.getLogger().finer("Listener::accept() FROM:" + ia.getHostName() + " (" +
107                         ia.getHostAddress() + ") : " + client.getPort());
108                 this.getMessage(client);
109             }
110             catch(IOException e)
111             {
112                 Logging.getLogger().warning(Translation.tr("listenerCantAcceptConnection") + e);
113             }
114         }
115     }
116     
117     /**
118      * Read messages from the network
119      *
120      * @param s the Socket to read
121      */

122     private void getMessage(Socket s)
123     {
124         ObjectConnection oc = new ObjectConnection(s);
125         Message message = null;
126         
127         try
128         {
129             message = (Message)oc.read();
130             Logging.getLogger().fine("Listener::getMessage(): " + message);
131         }
132         catch(Exception JavaDoc e)
133         {
134             Logging.getLogger().warning(Translation.tr("listenerCantReadMessage") + e);
135             e.printStackTrace();
136             oc.close();
137             return;
138         }
139         
140         if(message != null)
141         {
142             //client command
143
if(message.getApplication().equals("Client"))
144             {
145                 try {
146                     oc.write("OK");
147                 } catch(Exception JavaDoc e) {}
148                 
149                 Logging.getLogger().finest("Listener::getMessage() COMMAND: client");
150                 internalCommand(message);
151             }
152             
153             //plugin command
154
else
155             {
156                 
157                 if(PluginManager.getInstance().isAvailable(message.getApplication()))
158                 {
159                     try {
160                         oc.write("OK");
161                     } catch(Exception JavaDoc e) {}
162                     
163                     PluginManager.getInstance().load(oc, message);
164                 }
165                 else
166                 {
167                     try {
168                         oc.write("FAILED " + Translation.tr("listenerNoSuchApplication"));
169                     } catch(Exception JavaDoc e) {}
170                     
171                     Logging.getLogger().finer("Listener::getMessage() UNKNOWN: " + message.getApplication());
172                 }
173             }
174         }
175     }
176     
177     /**
178      * Handles internal commands
179      *
180      */

181     private void internalCommand(Message message)
182     {
183         Map map = (Map)message.getData();
184         String JavaDoc command = (String JavaDoc)map.get("command");
185
186         if(command.equals("USER_LIST"))
187             parent.setUserList((ArrayList)map.get("users"));
188         else if(command.equals("DISCONNECT"))
189             disconnect();
190     }
191
192     /**
193      * Disconnects and exit
194      */

195     private void disconnect()
196     {
197         Client.getInstance().disconnect();
198     }
199     
200     /**
201      * Stop the listener.
202      */

203     public void pleaseStop()
204     {
205         this.stop = true;
206         
207         try
208         {
209             this.socket.close();
210         }
211         catch(Exception JavaDoc e)
212         {
213             //oh well
214
}
215     }
216     
217     /**
218      * Get the listening port
219      *
220      * @return the port
221      */

222     public int getPort()
223     {
224         return port;
225     }
226 }
227
Popular Tags