1 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 ; 29 import java.util.Vector ; 30 31 32 36 public class Communicator 37 { 38 private ConnectInfo ci_server; 39 private ConnectInfo ci_proxy; 40 private Vector infos; 41 private Client parent; 42 private static Communicator instance; 43 44 49 public static Communicator getInstance() 50 { 51 return instance; 52 } 53 54 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(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 (); 78 Communicator.instance = this; 79 } 80 81 86 protected int updatePlugins() 87 { 88 int nupdated = 0; 89 Vector list = new Vector (); 90 91 try 92 { 93 ObjectConnection oc = this.sendMessageTo(this.ci_server, "Server", "PLUGIN_LIST"); 94 95 list = (Vector )oc.read(); 96 parent.getConnectBox().setProgressMax(list.size()); 97 98 oc.close(); 99 } 100 catch(Exception e) 101 { 102 DialogBox.error(Translation.tr("communicatorPluginUpdateError")); 103 } 104 105 for(int i = 0; i<list.size(); i++) 106 { 107 StringTokenizer stk = new StringTokenizer ((String )list.elementAt(i), " "); 108 String plugin = stk.nextToken(); 109 String version = stk.nextToken(); 110 111 parent.getConnectBox().setProgressValue(i, plugin + " v" + version); 112 113 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 136 protected void downloadPlugin(String name) 137 { 138 String 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 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 165 protected void setProxyInfo() throws IOException 166 { 167 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 186 protected void flushConnectInfosCache() 187 { 188 this.infos = new Vector (); 189 } 190 191 196 public Vector getAllConnectInfos() 197 { 198 return this.infos; 199 } 200 201 208 public ConnectInfo getConnectInfo(String username) 209 { 210 ConnectInfo ci_res = null; 211 boolean found = false; 212 213 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 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 e) 240 { 241 DialogBox.error(Translation.tr("communicatorUserInfoError")); 242 ci_res = null; 243 } 244 } 245 246 return ci_res; 247 } 248 249 258 public ObjectConnection sendMessageTo(ConnectInfo who, String dest, Object 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 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 e) 283 { 284 e.printStackTrace(); 285 DialogBox.error(Translation.tr("communicatorSendError") + " : " + who); 286 return null; 287 } 288 } 289 290 295 public Vector getUserList() 296 { 297 try { 298 ObjectConnection oc = this.sendMessageTo(this.ci_server, "Server", "CONNECT_LIST"); 299 Vector userList = (Vector )oc.read(); 300 oc.close(); 301 302 return userList; 303 } catch(Exception e) { 304 DialogBox.error(Translation.tr("communicatorUserListError")); 305 } 306 307 return null; 308 } 309 310 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 e) { 322 Logging.getLogger().warning("Unable to get server connect info : " + e); 323 } 324 } 325 326 331 public boolean useSSL() 332 { 333 return this.ci_server.useSSL(); 334 } 335 } 336 | Popular Tags |