private void reverseRegister(
     RegisterSocketEvent e, int port, InetAddress localHost, boolean error) {
   try {
     e.setSource(this);
     e.setDir(Direction.invert(e.getDir()));
     e.port = port;
     e.localHost = localHost;
     e.error = error;
     e.init();
     e.go();
   } catch (AppiaEventException ex) {
     ex.printStackTrace();
   }
 }
 /**
  * Handles all received event
  *
  * @param ev
  */
 public void handle(Event ev) {
   if (ev instanceof TextEvent
       || ev instanceof DrawEvent
       || ev instanceof ImageEvent
       || ev instanceof ClearWhiteBoardEvent
       || ev instanceof MouseButtonEvent) handleInterestingEvent((GroupSendableEvent) ev);
   else {
     try {
       ev.go();
     } catch (AppiaEventException ex) {
       ex.printStackTrace();
     }
   }
 }
  private void handlePDUSize(MaxPDUSizeEvent e) {

    log.debug(":handlePDUSize ");

    try {
      e.pduSize = param_MAX_UDPMSG_SIZE - MAX_UdpSimple_HEADERS;

      e.setDir(Direction.invert(e.getDir()));
      e.setSource(this);
      e.init();
      e.go();
    } catch (AppiaEventException ex) {
      ex.printStackTrace();
      System.err.println("Unexpected exception when forwarding " + "MaxPDUSize event in UDPSimple");
    }
  }
  /**
   * Handles all events related to messenger features: TextEvent, DrawEvent, ImageEvent,
   * ClearWhiteBoardEvent, MouseButtonEvent
   *
   * <p>If an event direction is DOWN an hash is generated otherwise received and calculated hashes
   * are compared. <br>
   * Different hashes mean that an event is ignored
   *
   * @param ev
   */
  private void handleInterestingEvent(GroupSendableEvent ev) {

    Message message = ev.getMessage();
    byte[] data;
    Hash h = new Hash();
    //		String type;

    if (ev.getDir() == Direction.DOWN) {
      data = message.toByteArray();
      md.update(data);
      md.update(secret);
      byte[] my = md.digest();
      h.setHash(my);
      message.pushObject(h);
      md.reset();
      // out.println("MD calculated");
      ev.setObjectsMessage(message);
      try {
        ev.go();
      } catch (AppiaEventException e) {
        e.printStackTrace();
      }

    } else {
      // incoming hash
      h = (Hash) message.popObject();
      data = message.toByteArray();

      // calculates hash
      md.update(data);
      md.update(secret);

      if (MessageDigest.isEqual(md.digest(), h.getHash())) {
        // out.println("Valid");
        try {
          ev.go();
        } catch (AppiaEventException e) {
          e.printStackTrace();
        }
      }
      //			else{
      //				out.println("Error! Compromised message received!");
      //				out.println("Message ignored!");
      //			}
      md.reset();
    }
  }
  private void handleMulticastInit(MulticastInitEvent e) {

    log.debug(":handleAppiaMulticastInit");

    if (!multicastReaders.containsKey(e.ipMulticast)) {
      /*creates a multicast socket and binds it to a specific port on the local host machine*/
      try {
        MulticastSocket multicastSock =
            new MulticastSocket(((InetSocketAddress) e.ipMulticast).getPort());

        log.debug(":handleAppiaMulticastInit: Socket Multicast created. Address: " + e.ipMulticast);

        /*joins a multicast group*/
        multicastSock.joinGroup(((InetSocketAddress) e.ipMulticast).getAddress());

        // keeping the multicast address...
        ipMulticast =
            new InetSocketAddress(
                ((InetSocketAddress) e.ipMulticast).getAddress(),
                ((InetSocketAddress) e.ipMulticast).getPort());

        log.debug(":handleAppiaMulticastInit: Socket Multicast joined.");

        try {
          multicastSock.setSoTimeout(param_SOTIMEOUT);
        } catch (SocketException se) {
          System.err.println(
              "Unable to set SoTimeout value on UdpSimpleSession. Using default OS value.");
          se.printStackTrace();
        }

        /* The socket is binded. Launch reader and return the event.*/
        final UdpSimpleReader multicastReader =
            new UdpSimpleReader(this, multicastSock, ipMulticast, e.fullDuplex ? null : myAddress);
        final Thread thread =
            e.getChannel()
                .getThreadFactory()
                .newThread(multicastReader, "MulticastReaderThread [" + ipMulticast + "]");
        multicastReader.setParentThread(thread);
        thread.start();

        multicastReaders.put(ipMulticast, multicastReader);

        /*forwarding the event*/
        e.error = false;
      } catch (IOException ex) {
        ex.printStackTrace();
        System.err.println("Error creating/joining the multicast socket");
        e.error = true;
      }
    } else {
      log.debug(":handleAppiaMulticastInit: Requested multicast socket already existed.");
    }

    try {
      e.setDir(Direction.invert(e.getDir()));
      e.setSource(this);
      e.init();
      e.go();
      log.debug(":handleAppiaMulticastInit: Returning multicastInit with error code: " + e.error);
      log.debug(
          ":handleAppiaMulticastInit: Direction is "
              + (e.getDir() == Direction.DOWN ? "DOWN" : "UP"));
    } catch (AppiaEventException ex) {
      ex.printStackTrace();
    }
  }