1 25 37 38 package org.datashare.client; 39 40 import org.datashare.objects.DataShareConnectionDescriptor; 41 import org.datashare.objects.ActivateConnectionObject; 42 import org.datashare.objects.DataShareObject; 43 import org.datashare.objects.ChannelDescription; 44 import org.datashare.objects.ChannelDescriptionArray; 45 import org.datashare.objects.RegistrationInfo; 46 import org.datashare.objects.RequestTree; 47 import org.datashare.objects.ServerInfo; 48 import org.datashare.objects.ClientSessionInfo; 49 import org.datashare.objects.CreateSessionRequest; 50 import org.datashare.objects.RegisterTreeListener; 51 import org.datashare.objects.TreeObject; 52 import org.datashare.objects.UpdateAvailableMsg; 53 import org.datashare.SessionUtilities; 54 55 import java.net.InetAddress ; 56 import java.util.Arrays ; 57 58 65 public class DataShareClient implements ClientDataReceiverInterface 66 { 67 private DataShareClientInterface rci; private String userName; private String password; public String sessionName; private String sessionDescription; 72 private String enterpriseName; public String clientUniqueName = null; private String publicInfo; private InetAddress serverInetAddress = null; private String clientClass; 77 78 private DataShareConnectionDescriptor commandStatusConnectionDescriptor = null; 80 private DataShareConnection commandStatusConnection = null; 81 private int serverCommandStatusPort = 42444; 83 private DataShareConnectionDescriptor dataConnectionDescriptor = null; 85 private DataShareConnection dataConnection = null; 86 private String channelName; 87 private String channelDescription; 88 private int channelType; 89 90 93 public DataShareClient() 94 {} 95 96 101 public void 102 initialize(DataShareClientInterface rci, 103 InetAddress serverInetAddress, 104 int serverCommandStatusPort, 105 String sessionName, 106 String sessionDescription, 107 String userName, 108 String password, 109 String publicInfo, 110 String enterpriseName, 111 String channelName, 112 String channelDescription, 113 int channelType 114 ) 115 { 116 this.rci = rci; 117 clientClass = rci.getClass().toString(); 118 if(clientClass.startsWith("class ")) 119 clientClass = clientClass.substring(5); this.serverInetAddress = serverInetAddress; 121 this.serverCommandStatusPort = serverCommandStatusPort; 122 this.sessionName = sessionName; 123 this.sessionDescription = sessionDescription; 124 this.userName = userName; 125 this.password = password; 126 this.publicInfo = publicInfo; 127 this.enterpriseName = enterpriseName; 128 this.channelName = channelName; 129 this.channelDescription = channelDescription; 130 this.channelType = channelType; 131 connectToServer(); 132 } 133 134 137 private void connectToServer() 138 { 139 commandStatusConnectionDescriptor = 140 new DataShareConnectionDescriptor("", 141 new ChannelDescription(), 142 serverInetAddress, 143 serverCommandStatusPort); 144 try{ 145 commandStatusConnection = new DataShareConnection(commandStatusConnectionDescriptor,this); 147 RegistrationInfo ri = 148 new RegistrationInfo(userName, 149 password, 150 enterpriseName, 151 clientClass, 152 RegistrationInfo.UNKNOWN, 153 publicInfo); 154 commandStatusConnection.sendToAll(ri); } 159 catch(Exception e) 160 { 161 System.out.println("Problems creating our commandStatus connection..."); 162 e.printStackTrace(); 163 rci.commandStatusConnectionError(true, e.getMessage()); 164 rci.dataChannelIsReady(false); 165 shutDown(); 166 } 167 } 168 169 170 173 public void 174 sendDataToAll(Object object) 175 { 176 if(dataConnection != null) 177 dataConnection.sendToAll(object); 178 else 179 System.out.println("Trying to send data before the data connection is ready"); 180 } 181 182 185 public void 186 sendDataToOthers(Object object) 187 { 188 if(dataConnection != null) 189 dataConnection.sendToOthers(object); 190 else 191 System.out.println("Trying to send data before the data connection is ready"); 192 } 193 194 197 public void 198 sendDataToClient(Object object, String destinationClientKey) 199 { 200 if(dataConnection != null) 201 dataConnection.sendToClient(object, destinationClientKey); 202 else 203 System.out.println("Trying to send data before the data connection is ready"); 204 } 205 206 208 private void 209 createDataChannel() 210 { 211 ChannelDescription cd = new ChannelDescription(channelName, 212 channelDescription, 213 ChannelDescription.TCP); 214 ChannelDescription cda1[] = {cd}; 215 ChannelDescriptionArray cda = new ChannelDescriptionArray(cda1); 216 CreateSessionRequest csr = 218 new CreateSessionRequest(sessionName, 219 sessionDescription, 220 false, true, cda, 223 true); commandStatusConnection.sendToAll(csr); } 226 227 228 229 232 public void 233 shutDown() 234 { 235 try{ 236 if(dataConnection != null) 237 { 238 rci.dataChannelIsReady(false); 239 dataConnection.closeAll(); 240 dataConnection = null; 241 } 242 } 243 catch(Exception e) {} 244 try{ 245 if(commandStatusConnection != null) 246 { 247 commandStatusConnection.closeAll(); 248 commandStatusConnection = null; 249 } 250 } 251 catch(Exception e) {} 252 253 } 254 255 259 public void 260 registerForUpdates(boolean allChanges) 261 { 262 commandStatusConnection.sendToAll(new RegisterTreeListener(allChanges)); } 264 265 269 public void 270 requestNewTreeNodes() 271 { 272 commandStatusConnection.sendToAll(new RequestTree()); 273 } 274 275 278 public void 279 dataReceived(DataShareObject dso) 280 { 281 try{ 282 Object object = SessionUtilities.retrieveObject(dso.objectBytes); 283 if(object instanceof ServerInfo) 284 { 285 ServerInfo serverInfo = (ServerInfo)object; 287 clientUniqueName = serverInfo.clientKey; this.createDataChannel(); 289 } 290 else if(object instanceof UpdateAvailableMsg) 291 { 292 UpdateAvailableMsg msg = (UpdateAvailableMsg)object; 293 rci.updateReceived(msg); 294 } 295 else if(object instanceof TreeObject) 296 { 297 TreeObject to = (TreeObject)object; 298 rci.treeReceived(to.allNodes); 299 } 300 else if(object instanceof ClientSessionInfo) 301 { System.out.println("Received a ClientSessionInfo object..."); 303 ClientSessionInfo ourNewSession = (ClientSessionInfo)object; 304 byte[] originalAddress = serverInetAddress.getAddress(); 309 for(int channelCount=0; channelCount<ourNewSession.dsConnDescriptor.length; channelCount++) 310 { 311 byte[] dataAddress = ourNewSession.dsConnDescriptor[channelCount].serverIP.getAddress(); 312 int channelProtocol = ourNewSession.dsConnDescriptor[channelCount].type; 313 if(channelProtocol != ChannelDescription.MULTICAST && !Arrays.equals(originalAddress, dataAddress)) 314 { 315 System.out.println("Server is using a different IP address now, suspect NAT is used..."); 316 ourNewSession.dsConnDescriptor[channelCount].serverIP = serverInetAddress; 319 } 320 } 321 joinSession(ourNewSession); 322 } 323 else 324 { 325 System.out.println("received unexpected object in command stream"); 326 } 327 } 328 catch(Exception e) 329 { 330 e.printStackTrace(); 331 } 332 } 333 334 338 private void 339 joinSession(ClientSessionInfo newSession) 340 { 341 try{ 342 dataConnectionDescriptor = newSession.dsConnDescriptor[0]; 344 dataConnection = new DataShareConnection(dataConnectionDescriptor, rci); 345 dataConnection.sendToAll(new ActivateConnectionObject(this.clientUniqueName),true); 347 rci.dataChannelIsReady(true); 348 } 350 catch(Exception e) 351 { 352 e.printStackTrace(); 353 rci.dataChannelIsReady(false); 354 } 355 } 356 357 358 362 public void 363 connectionLost(DataShareConnection dsc) 364 { 365 rci.commandStatusConnectionError(true, "Lost commandStatus connection to server"); 366 } 367 368 } 369 370 371 | Popular Tags |