コード例 #1
0
 /*
  * Constructor used by servers.
  */
 public SocketClientConnection(
     SocketServer parentServer, SocketPipe clientPipe, ThreadGroup threadGroup)
     throws CommunicationException {
   super(threadGroup, clientPipe.toString());
   this.clientPipe = clientPipe;
   this.parentServer = parentServer;
   serverConnection = true;
   connectedToServer = false;
   // Find out who we're connected to, only if we are a server.
   // Because, if we are a client, we must be connected to a server.
   ConnectMessage connectMsg = (ConnectMessage) clientPipe.read();
   if (connectMsg.getSourceType() == HostType.CLIENT) {
     clientDestination = connectMsg.getSourceDestination();
     notifyConnectionListeners(clientDestination);
   } else if (connectMsg.getSourceType() == HostType.SERVER) {
     connectedToServer = true;
   } else {
     throw new CommunicationException(
         "Incorrected HostType transmitted through the wire:"
             + connectMsg.getSourceType()
             + ". Communication Layer exception.");
   }
   // reply to the connect request with broker ID
   clientPipe.write(new ConnectReplyMessage(parentServer.getServerURI()));
 }
コード例 #2
0
 /**
  * The main thread execution method. Performs part of the connection protocol, and then loops,
  * listening for messages.
  */
 public void run() {
   try {
     SocketMessage socketMsg;
     // Receive until client closes connection, indicated by -1 return
     while ((socketMsg = clientPipe.read()) != null && !this.isInterrupted()) {
       /*
        * If the received message is a string, it must be the messageID of a message that
        * was just sent. If we are on the client side, inform the senderCallback
        * MessageSender of the new messageID.
        *
        * This will only ever occur when we are a client.
        */
       if (socketMsg.getMessageType() == SocketMessageType.PUB_SUB_REPLY) {
         senderCallBack.messageIDReceived(((PubSubReplyMessage) socketMsg).getMessageID());
         continue;
       }
       /*
        * Otherwise, we have received a Message. Handle it appropriately.
        */
       // Receive subsequent messages, after the initial ID String
       if (socketMsg.getMessageType() == SocketMessageType.PUB_SUB) {
         PubSubMessage pubSubSocketMsg = (PubSubMessage) socketMsg;
         Message pubSubMsg = pubSubSocketMsg.getMessage();
         notifyMessageListeners(pubSubMsg, pubSubSocketMsg.getHostType());
         // Send back the messageID if we are a server and connected to a client
         if (serverConnection
             && !connectedToServer
             && !(pubSubMsg instanceof PublicationMessage)) {
           clientPipe.write(new PubSubReplyMessage(pubSubMsg.getMessageID()));
         }
         pubSubSocketMsg.setTimeofReceiving(System.currentTimeMillis());
         System.out.println(
             "number of clients = "
                 + ++counter
                 + " and totalTime is "
                 + pubSubSocketMsg.getTotalTime()
                 + " time of sending : "
                 + pubSubSocketMsg.getTimeOfSending()
                 + " time of receiving :"
                 + pubSubSocketMsg.getTimeOfReceiving());
         System.out.println("======================================================");
       }
     }
     // Close the socket. We are done with this client!
     clientPipe.close();
   } catch (CommunicationException e) {
     if (clientDestination != null) {
       disconnect(clientDestination);
     }
   }
 }