/**
   * The main thread execution method. Performs part of the connection protocol, and then loops,
   * listening for messages.
   */
  public void run() {
    MessageDestination connectedDest = null;
    try {
      // If we are a server open oos and ois and send the server's ID
      if (serverConnection) {
        clientPipe.write(new ConnectReplyMessage(parentServer.getServerURI()));
      }

      // 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.
      if (serverConnection) {
        ConnectMessage connectMsg = (ConnectMessage) clientPipe.read();
        if (connectMsg.getSourceType() == HostType.CLIENT) {
          connectedToServer = false;
          connectedDest = connectMsg.getSourceDestination();
        } else if (connectMsg.getSourceType() == HostType.SERVER) {
          connectedToServer = true;
        } else {
          System.out.println(
              "Incorrected HostType transmitted through the wire:"
                  + connectMsg.getSourceType()
                  + ". Communication Layer exception.");
        }
      }

      // Alert ConnectionListener of connection only if we are on a server instance AND we are
      // connected to a client
      if (serverConnection && !connectedToServer) {
        notifyConnectionListeners(connectedDest);
      }

      SocketMessage msg;
      // Receive until client closes connection, indicated by -1 return
      while ((msg = clientPipe.read()) != null) {
        /*
         * Otherwise, we have received a Message. Handle it appropriately.
         */
        // Receive subsequent messages, after the initial ID String
        if (msg.getMessageType() == SocketMessageType.PUB_SUB) {
          PubSubMessage pubSubSocketMsg = (PubSubMessage) msg;
          Message pubSubMsg = pubSubSocketMsg.getMessage();
          notifyMessageListeners(pubSubMsg, pubSubSocketMsg.getHostType());
        } else {
          // TODO: handle error condition
        }
      }
      clientPipe.close(); // Close the socket. We are done with this
      // client!
    } catch (CommunicationException e) {
      if (serverConnection && !connectedToServer) {
        disconnect(connectedDest);
      }
    }
  }
 /**
  * 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);
     }
   }
 }