Example #1
0
 /**
  * Send a packet to the multicast group. Messages have the form (name): msg
  *
  * @param message message
  */
 public void sendToGroup(final String message) {
   try {
     if (LOG.isInfoEnabled()) {
       LOG.info(BUNDLE_MARKER, "Sending packet to group: " + message);
     }
     final DatagramPacket sendPacket = toDatagramPacket(message, groupAddress, GROUP_PORT);
     groupSocket.send(sendPacket);
     if (LOG.isInfoEnabled()) {
       LOG.info(BUNDLE_MARKER, "Packet sent to group: " + message);
     }
   } catch (IOException ioe) {
     LOG.error(BUNDLE_MARKER, "Failed to send packet: " + ioe.toString(), ioe);
   }
 }
Example #2
0
  /**
   * Checks if the LOCAL_PORT is availible. If not try another one.
   *
   * @param startport Start port, try first to open.
   * @return a DatagramSocket for the Registry.
   */
  private static DatagramSocket getAvailableSocket(final int startport) throws SocketException {

    SocketException lastex = null;
    for (int port = startport; port < LOCAL_PORT + MAX_PORT_RANGE; port++) {

      try {
        DatagramSocket ds = new DatagramSocket(port);
        LOG.info("LOCAL_PORT=" + port);
        return ds;
      } catch (SocketException e) {
        lastex = e;
        LOG.info("LOCAL_PORT " + port + " already in use - trying " + (port + 1));
      }
    }
    throw lastex;
  }
Example #3
0
 /** Stops all listeners. */
 public void stop() {
   if (LOG.isInfoEnabled()) {
     LOG.info(BUNDLE_MARKER, "Shutting down node " + this.nodeId);
   }
   // not necessary - we remove the node on the directoty level
   // sendToGroup(BYE + " " + nodeId);
   setRunning(false);
 }
Example #4
0
 /**
  * Repeatedly receive a packet from the group, process it. No messages are sent from here.
  * sendPacket().
  */
 public void receiveGroupMessages() {
   if (LOG.isDebugEnabled()) {
     LOG.debug("Starting group receive loop for " + nodeId);
   }
   while (this.isRunning()) {
     try {
       final byte[] data = new byte[BUFFER_SIZE]; // set up an empty packet
       final DatagramPacket packet = new DatagramPacket(data, data.length);
       if (LOG.isDebugEnabled()) {
         LOG.debug("Waiting for group message on " + groupSocket);
       }
       groupSocket.receive(packet); // wait for a packet
       final String msg = toString(packet);
       if (LOG.isDebugEnabled()) {
         LOG.debug(
             "Received group msg "
                 + msg
                 + " from "
                 + packet.getAddress()
                 + ":"
                 + packet.getPort()
                 + ".");
       }
       processGroupMessage(msg);
     } catch (SocketTimeoutException e) {
       if (LOG.isInfoEnabled()) {
         LOG.info(BUNDLE_MARKER, "GroupSocket receive timed out.");
       }
     } catch (IOException e) {
       LOG.error(BUNDLE_MARKER, e.toString(), e);
     } catch (Throwable t) {
       LOG.error(BUNDLE_MARKER, t.toString(), t);
     }
   }
   if (LOG.isDebugEnabled()) {
     LOG.debug("Group receive loop for " + nodeId + " was stopped.");
   }
 }
Example #5
0
 /**
  * Processes a message.
  *
  * @param message message
  * @throws IOException if something goes wrong
  */
 private void processGroupMessage(final String message) throws IOException {
   try {
     final MultiCastMessage multiCastMessage = new MultiCastMessage(message);
     if (multiCastMessage.getType() == MultiCastMessage.Type.HI
         && multiCastMessage.getNodeId() != RegistryImpl.getInstance().getNodeId()) {
       final InetAddress address = multiCastMessage.getSenderAddress();
       final int port = multiCastMessage.getSenderPort();
       // we don't use datagramPacket.getAddress(), because of
       // http://bugs.sun.com/view_bug.do?bug_id=4717228
       // -hendrik
       sendPrivateMessage(
           new MultiCastMessage(nodeId, MultiCastMessage.Type.HI, getSender()), address, port);
     }
     if (LOG.isInfoEnabled()) {
       LOG.info(BUNDLE_MARKER, "(NODE) processing node message: " + message);
     }
     if (multiCastMessage.isValid()) {
       multiCastMessage.setDirection(Direction.IN);
       fireMessage(multiCastMessage);
     }
   } catch (RuntimeException e) {
     LOG.error(BUNDLE_MARKER, "Failed to process group message: " + e.toString(), e);
   }
 }