KickJava   Java API By Example, From Geeks To Geeks.

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


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.client.widgets.*;
23 import org.lucane.common.*;
24 import org.lucane.common.net.ClientSocketFactory;
25 import org.lucane.common.net.ObjectConnection;
26 import java.io.*;
27 import java.net.*;
28 import java.util.StringTokenizer JavaDoc;
29 import java.util.Vector JavaDoc;
30
31
32 /**
33  * Allows communication with the server and the other clients
34  * Used to ask connections informations and to download plugins.
35  */

36 public class Communicator
37 {
38     private ConnectInfo ci_server;
39     private ConnectInfo ci_proxy;
40     private Vector JavaDoc infos;
41     private Client parent;
42     private static Communicator instance;
43     
44     /**
45      * Get the signleton instance
46      *
47      * @return the unique Communicator instance
48      */

49     public static Communicator getInstance()
50     {
51         return instance;
52     }
53     
54     /**
55      * Constructor.
56      *
57      * @param serverName the main server
58      */

59     protected Communicator()
60     {
61         this.parent = Client.getInstance();
62         this.ci_server = new ConnectInfo("Server", parent.getConfig().getServerHost(),
63                 parent.getConfig().getServerPort(), "nokey", "Server");
64         
65         //if a proxy is used
66
if(parent.getConfig().getProxyHost() != null)
67         {
68             this.ci_proxy = new ConnectInfo("Proxy",
69                     parent.getConfig().getProxyHost(),
70                     parent.getConfig().getProxyPort(), "nokey", "proxy");
71         }
72         else
73             this.ci_proxy = null;
74
75         this.getServerConnectInfo();
76         
77         this.infos = new Vector JavaDoc();
78         Communicator.instance = this;
79     }
80     
81     /**
82      * Update the plugins from the server
83      *
84      * @return the number of updated plugins
85      */

86     protected int updatePlugins()
87     {
88         int nupdated = 0;
89         Vector JavaDoc list = new Vector JavaDoc();
90         
91         try
92         {
93             ObjectConnection oc = this.sendMessageTo(this.ci_server, "Server", "PLUGIN_LIST");
94             
95             list = (Vector JavaDoc)oc.read();
96             parent.getConnectBox().setProgressMax(list.size());
97             
98             oc.close();
99         }
100         catch(Exception JavaDoc e)
101         {
102             DialogBox.error(Translation.tr("communicatorPluginUpdateError"));
103         }
104         
105         for(int i = 0; i<list.size(); i++)
106         {
107             StringTokenizer JavaDoc stk = new StringTokenizer JavaDoc((String JavaDoc)list.elementAt(i), " ");
108             String JavaDoc plugin = stk.nextToken();
109             String JavaDoc version = stk.nextToken();
110             
111             parent.getConnectBox().setProgressValue(i, plugin + " v" + version);
112             
113             /* download the plugin if necessary */
114             if(! PluginManager.getInstance().isAvailable(plugin, version, true))
115             {
116                 this.downloadPlugin(plugin);
117                 
118                 if(! PluginManager.getInstance().isAvailable(plugin, version, true))
119                     DialogBox.error(Translation.tr("communicatorPluginLoadError") + plugin);
120                 else
121                     nupdated++;
122             }
123             
124         }
125         
126         Logging.getLogger().info("updated " + nupdated + " plugins.");
127         
128         return nupdated;
129     }
130     
131     /**
132      * Download a new Plugin or a new version from the server
133      *
134      * @param name the Plugin name
135      */

136     protected void downloadPlugin(String JavaDoc name)
137     {
138         String JavaDoc subdirectory = Client.APPLICATIONS_DIRECTORY;
139         Logging.getLogger().info("downloading plugin : " + name);
140         
141         ObjectConnection oc = null;
142         DataOutputStream dos = null;
143         
144         try {
145             oc = this.sendMessageTo(this.ci_server, "Server", "PLUGIN_GET " + name);
146             dos = new DataOutputStream(new FileOutputStream(subdirectory + name + ".jar"));
147             byte[] buf = (byte[])oc.read();
148             oc.close();
149             dos.write(buf);
150         } catch(Exception JavaDoc e) {
151             DialogBox.error(Translation.tr("communicatorGetFileError") + name);
152             e.printStackTrace();
153         } finally {
154             try {
155                 if(dos != null)
156                     dos.close();
157             } catch(IOException ioe) {}
158         }
159     }
160     
161     /**
162      * Set the user connection informations to the proxy
163      * if necessary
164      */

165     protected void setProxyInfo() throws IOException
166     {
167         //proxy
168
if(this.ci_proxy != null)
169         {
170             ObjectConnection oc = this.sendMessageTo(this.ci_proxy, "Proxy",
171                     "CONNECT_SET " + parent.getMyInfos(true).getRepresentation());
172             
173             if(oc == null)
174                 throw new IOException(Translation.tr("communicatorProxySendError"));
175             
176             oc.close();
177             Logging.getLogger().finer("using proxy for : " +
178                     parent.getMyInfos(true).getRepresentation());
179         }
180     }
181     
182     /**
183      * Clean the cache to force connectInfo loading
184      * Usefull when someone disconnected and reconnected
185      */

186     protected void flushConnectInfosCache()
187     {
188         this.infos = new Vector JavaDoc();
189     }
190     
191     /**
192      * Get the connections informations on all known clients
193      *
194      * @return the info list
195      */

196     public Vector JavaDoc getAllConnectInfos()
197     {
198         return this.infos;
199     }
200     
201     /**
202      * Get a ConnectInfo from the server or from the local cache
203      * if it has already been fetched.
204      *
205      * @param username ther user or service name
206      * @return the connection informations
207      */

208     public ConnectInfo getConnectInfo(String JavaDoc username)
209     {
210         ConnectInfo ci_res = null;
211         boolean found = false;
212         
213         // search in the local cache
214
for(int i = 0; i < this.infos.size() && ! found; i++)
215         {
216             ci_res = (ConnectInfo)this.infos.elementAt(i);
217             
218             if(ci_res.getName().equals(username) ||
219                     ci_res.getName().equals(username))
220                 found = true;
221         }
222         
223         // not found, ask the server
224
if(! found)
225         {
226             try
227             {
228                 ObjectConnection oc = this.sendMessageTo(this.ci_server, "Server",
229                         "CONNECT_GET " + username);
230                 
231                 ci_res = (ConnectInfo)oc.read();
232                 oc.close();
233                 
234                 if(ci_res.isService() || ci_res.isServer())
235                     ci_res.setHostName(Client.getInstance().getConfig().getServerHost());
236                 
237                 this.infos.addElement(ci_res);
238             }
239             catch(Exception JavaDoc e)
240             {
241                 DialogBox.error(Translation.tr("communicatorUserInfoError"));
242                 ci_res = null;
243             }
244         }
245         
246         return ci_res;
247     }
248     
249     /**
250      * Send a message to the target with the correct parameters
251      * and following the protocol correctly.
252      *
253      * @param who how to connect to the target
254      * @param dest the target component
255      * @param data the content
256      * @return a Socket connected to the target
257      */

258     public ObjectConnection sendMessageTo(ConnectInfo who, String JavaDoc dest, Object JavaDoc data)
259     {
260         try
261         {
262             Socket sock = ClientSocketFactory.getSocket(who);
263             ObjectConnection oc = new ObjectConnection(sock);
264             
265             Message message = new Message(parent.getMyInfos(), who, dest, data);
266             Logging.getLogger().finer("Communicator::sendMessageTo() MESSAGE:" + message);
267             
268             oc.write(message);
269             String JavaDoc ack = oc.readString();
270             if(ack.equals("OK"))
271                 return oc;
272             
273             if(ack.startsWith("FAILED "))
274                 DialogBox.error(ack.substring(7));
275             else
276                 DialogBox.error(Translation.tr("communicatorConnectionError") + " : " + ack);
277             
278             oc.close();
279             
280             return null;
281         }
282         catch(Exception JavaDoc e)
283         {
284             e.printStackTrace();
285             DialogBox.error(Translation.tr("communicatorSendError") + " : " + who);
286             return null;
287         }
288     }
289     
290     /**
291      * Fetch the user list from the server
292      *
293      * @return the list
294      */

295     public Vector JavaDoc getUserList()
296     {
297         try {
298             ObjectConnection oc = this.sendMessageTo(this.ci_server, "Server", "CONNECT_LIST");
299             Vector JavaDoc userList = (Vector JavaDoc)oc.read();
300             oc.close();
301             
302             return userList;
303         } catch(Exception JavaDoc e) {
304             DialogBox.error(Translation.tr("communicatorUserListError"));
305         }
306         
307         return null;
308     }
309     
310     /**
311      * Get the real server connectinfo
312      * Used to see if SSL is enabled
313      */

314     private void getServerConnectInfo()
315     {
316         try {
317             ObjectConnection oc = this.sendMessageTo(this.ci_server, "Server", "GET_SERVER_INFO");
318             this.ci_server = (ConnectInfo)oc.read();
319             this.ci_server.setHostName(parent.getConfig().getServerHost());
320             oc.close();
321         } catch(Exception JavaDoc e) {
322             Logging.getLogger().warning("Unable to get server connect info : " + e);
323         }
324     }
325     
326     /**
327      * Is the server using SSL ?
328      *
329      * @return true if SSL is enabled on the server
330      */

331     public boolean useSSL()
332     {
333         return this.ci_server.useSSL();
334     }
335 }
336
Popular Tags