示例#1
0
  private void handleRegisterSocket(RegisterSocketEvent e) {

    log.debug(":handleRegisterSocket");

    /* if the socket is already binded then something is
     * wrong. Keep existing information.
     */
    if (sock != null) {
      reverseRegister(e, myAddress.getPort(), myAddress.getAddress(), true);
      return;
    }

    // Checks if address given is a local address
    if ((e.localHost != null) && !HostUtils.isLocalAddress(e.localHost)) {
      reverseRegister(e, e.port, null, true);
      return;
    }

    if (newSock(e.port, e.localHost, e.getChannel().getThreadFactory())) {
      reverseRegister(e, myAddress.getPort(), myAddress.getAddress(), false);
    } else {
      reverseRegister(e, e.port, null, true);
    }
  }
示例#2
0
  private boolean newSock(int port, InetAddress addr, ThreadFactory threadFactory) {
    if (addr == null) {
      if (param_LOCAL_ADDRESS == null) addr = HostUtils.getLocalAddress();
      else addr = param_LOCAL_ADDRESS;
    }

    if (port == RegisterSocketEvent.FIRST_AVAILABLE) {
      /*first available port*/
      try {
        sock = new DatagramSocket(0, addr);
      } catch (SocketException ex) {
        ex.printStackTrace();
        return false;
      }
    } else if (port == RegisterSocketEvent.RANDOMLY_AVAILABLE) {
      /*chooses a random port*/
      Random random = new Random();

      boolean sucess = false;

      /*verifies if the random port is a valid one*/
      while (!sucess) {
        port = Math.abs(random.nextInt() % Short.MAX_VALUE);
        /* Open Socket with any port*/
        try {
          sock = new DatagramSocket(port, addr);
          sucess = true;
        } catch (IllegalArgumentException ex) {
        } catch (SocketException se) {
        }
      }

    } else {
        /*Regular RegisterSocketEvent */

      /* Open the specified socket (if possible) */
      try {
        sock = new DatagramSocket(port, addr);
      }
      /* Socket exception. Possibly the socket is already bound.
      Return the event up to notify that the command could not
      be issued.
      */
      catch (SocketException se) {
        return false;
      } catch (IllegalArgumentException ex) {
        return false;
      }
    }

    // Determine local address
    myAddress = new InetSocketAddress(sock.getLocalAddress(), sock.getLocalPort());

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

    /* The socket is binded. Launch reader*/
    // FIXME
    sockReader = new UdpSimpleReader(this, sock, myAddress);
    Thread t = threadFactory.newThread(sockReader, "UdpSimpleReader [" + myAddress + "]");
    sockReader.setParentThread(t);
    t.start();

    return true;
  }