/**
   * Handle the event ClientConnectionCloseNotificationEvent
   *
   * @param event
   */
  public void handleClientConnectionCloseNotificationEvent(
      ClientConnectionCloseNotificationEvent event) {

    try {

      if (!wsCommunicationsCloudClientManager
          .getCommunicationsCloudClientConnection(networkServiceType)
          .isRegister()) {

        this.register = Boolean.FALSE;

        if (communicationNetworkServiceConnectionManager != null) {
          communicationNetworkServiceConnectionManager.closeAllConnection();
          communicationNetworkServiceConnectionManager.stop();
        }

        communicationSupervisorPendingMessagesAgent.removeAllConnectionWaitingForResponse();

        onClientConnectionClose();
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  /**
   * Handle the event FailureComponentConnectionRequestNotificationEvent
   *
   * @param event
   */
  public void handleFailureComponentConnectionRequest(
      FailureComponentConnectionRequestNotificationEvent event) {

    try {

      System.out.println("Executing handleFailureComponentConnectionRequest ");
      communicationNetworkServiceConnectionManager.removeRequestedConnection(
          event.getRemoteParticipant().getIdentityPublicKey());
      communicationSupervisorPendingMessagesAgent.removeConnectionWaitingForResponse(
          event.getRemoteParticipant().getIdentityPublicKey());
      checkFailedSendMessage(event.getRemoteParticipant().getIdentityPublicKey());
      onFailureComponentConnectionRequest(event.getRemoteParticipant());

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  /**
   * Handle the event CompleteComponentConnectionRequestNotificationEvent
   *
   * @param event
   */
  public void handleCompleteComponentConnectionRequestNotificationEvent(
      CompleteComponentConnectionRequestNotificationEvent event) {

    try {

      /*
       * Tell the manager to handler the new connection established
       */
      communicationNetworkServiceConnectionManager
          .handleEstablishedRequestedNetworkServiceConnection(event.getRemoteComponent());
      communicationSupervisorPendingMessagesAgent.removeConnectionWaitingForResponse(
          event.getRemoteComponent().getIdentityPublicKey());

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  /**
   * Handle the event VPNConnectionLooseNotificationEvent
   *
   * @param event
   */
  public void handleVPNConnectionLooseNotificationEvent(VPNConnectionLooseNotificationEvent event) {

    try {

      if (event.getNetworkServiceApplicant()
          == getNetworkServiceProfile().getNetworkServiceType()) {

        String remotePublicKey = event.getRemoteParticipant().getIdentityPublicKey();
        if (communicationNetworkServiceConnectionManager != null) {
          communicationNetworkServiceConnectionManager.closeConnection(remotePublicKey);
        }

        communicationSupervisorPendingMessagesAgent.removeConnectionWaitingForResponse(
            remotePublicKey);

        reprocessMessages(event.getRemoteParticipant().getIdentityPublicKey());
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  /**
   * Handle the event ClientConnectionLooseNotificationEvent
   *
   * @param event
   */
  public void handleClientConnectionLooseNotificationEvent(
      ClientConnectionLooseNotificationEvent event) {

    try {

      if (!wsCommunicationsCloudClientManager
          .getCommunicationsCloudClientConnection(networkServiceType)
          .isRegister()) {

        if (communicationNetworkServiceConnectionManager != null) {
          communicationNetworkServiceConnectionManager.stop();
        }

        this.register = Boolean.FALSE;

        reprocessMessages();

        onClientConnectionLoose();
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  /** Method tha send a new Message */
  public void sendNewMessage(
      PlatformComponentProfile sender, PlatformComponentProfile destination, String messageContent)
      throws CantSendMessageException {

    try {

      /*
       * ask for a previous connection
       */
      CommunicationNetworkServiceLocal communicationNetworkServiceLocal =
          communicationNetworkServiceConnectionManager.getNetworkServiceLocalInstance(
              destination.getIdentityPublicKey());

      if (communicationNetworkServiceLocal != null) {
        System.out.println(
            "*** 12345 case 7:send msg in NS P2P layer active connection"
                + new Timestamp(System.currentTimeMillis()));
        // Send the message
        communicationNetworkServiceLocal.sendMessage(
            sender.getIdentityPublicKey(),
            sender.getPlatformComponentType(),
            sender.getNetworkServiceType(),
            messageContent);

      } else {
        System.out.println(
            "*** 12345 case 6:send msg in NS P2P layer not active connection"
                + new Timestamp(System.currentTimeMillis()));
        /*
         * Created the message
         */
        FermatMessage fermatMessage =
            FermatMessageCommunicationFactory.constructFermatMessage(
                sender.getIdentityPublicKey(), // Sender
                sender.getPlatformComponentType(), // Sender Type
                sender.getNetworkServiceType(), // Sender NS Type
                destination.getIdentityPublicKey(), // Receiver
                destination.getPlatformComponentType(), // Receiver Type
                destination.getNetworkServiceType(), // Receiver NS Type
                messageContent, // Message Content
                FermatMessageContentType.TEXT // Type
                );

        /*
         * Configure the correct status
         */
        ((FermatMessageCommunication) fermatMessage)
            .setFermatMessagesStatus(FermatMessagesStatus.PENDING_TO_SEND);

        /*
         * Save to the data base table
         */
        communicationNetworkServiceConnectionManager.getOutgoingMessageDao().create(fermatMessage);

        /*
         * Ask the client to connect
         */
        communicationNetworkServiceConnectionManager.connectTo(
            sender, getNetworkServiceProfile(), destination);
      }

    } catch (Exception e) {

      System.out.println("Error sending message: " + e.getMessage());
      throw new CantSendMessageException(CantSendMessageException.DEFAULT_MESSAGE, e);
    }
  }