/**
   * Initializes the serversocket and the multiplexer.
   *
   * @param selectorCreator The selector creator that will be used for creating a selector for the
   *     multiplexer.
   */
  private void init(SelectorCreator selectorCreator) {
    try {
      ServerSocketChannel channel = ServerSocketChannel.open();
      ServerSocket serverSocket = channel.socket();

      // Set the preference to bandwidth > short connection time && latency (only large messages
      // will be send / received here).
      serverSocket.setPerformancePreferences(0, 0, 2);

      serverSocket.bind(null);
      Config.setUsingPort(serverSocket.getLocalPort());

      channel.configureBlocking(false);
      selector = selectorCreator.getSelector();
      channel.register(selector, SelectionKey.OP_ACCEPT);

      ToolDataHandlerFactory toolDataHandlerFactory =
          new ToolDataHandlerFactory(InterToolDataHandler.class);
      multiplexer =
          new MultiThreadedMultiplexer(
              selectorCreator, Multiplexer.SERVERMODE, toolDataHandlerFactory);
      multiplexer.setDaemon(true);
      multiplexer.start();
    } catch (IOException ioex) {
      Logger.getInstance()
          .log(
              "An IOException occured while initializing the MultiplexingClientServer.",
              Logger.ERROR,
              ioex);
    }
  }
示例#2
0
  // Server constructor
  public Messenger(int port) {
    try {
      // Open server
      serverSocket = new ServerSocket();
      serverSocket.setSoTimeout(SOCKET_CONNECT_TIMEOUT);
      serverSocket.setPerformancePreferences(0, 1, 0); // Prioritize latency
      serverSocket.bind(new InetSocketAddress(LOCALHOST, port));

      // TODO: If "address is in use" errors pops up on re-using the same port:
      /*serverSocket.setReuseAddress(true);
      serverSocket.bind(new InetSocketAddress("localhost", port));*/
    } catch (IOException ex) {
      throw new ProcessCommunicationException(ex);
    }
  }
  /**
   * This is the main loop of the whole server. It sets up the socket and then proceeds to loop
   * indefinitely, blocking while it waits for incoming connections.
   */
  public void run() {
    try {
      listenSocket = new ServerSocket();
      listenSocket.setReuseAddress(true);
      listenSocket.setPerformancePreferences(1, 2, 0);
      listenSocket.bind(
          new InetSocketAddress(
              Config.getConfig().getAddressToBind(), Config.getConfig().getPortNumber()));
    } catch (IOException e) {
      System.err.println("Exception while creating listening socket: " + e.toString());
      System.err.println("Server exiting.");
      System.exit(-1);
    }

    System.out.println("The socket is now listening.");
    Socket socket;
    PrintWriter writer;
    ConnectionHandler temp;
    int listSize;
    while (keepRunning) {
      try {
        socket = listenSocket.accept();
        socket.setSoTimeout(120000);
        synchronized (listLock) {
          listSize = connectionList.size();
        }
        if (Config.getConfig().getMaxConnections() != 0
            && listSize >= Config.getConfig().getMaxConnections()) {
          writer = new PrintWriter(socket.getOutputStream(), true);
          writer.println("Error: server has reached maximum number " + "of connections.");
          writer.close();
          socket.close();
        } else {
          temp = new ConnectionHandler(socket, this);
          synchronized (listLock) {
            connectionList.add(temp);
          }
          new Thread(temp).start();
        }
      } catch (IOException e) {
        System.err.println("Exception while accepting incoming " + "connection: ");
        System.err.println(e.toString());
      }
    }
  }
示例#4
0
    @Override
    public final void run() {
      try {
        final TcpPipe enclosing = TcpPipe.this;
        Log.log("startup reciever endpoint for %s", enclosing);

        ServerSocket server = new ServerSocket(0);
        server.setPerformancePreferences(SO_CONNTIME_PREF, SO_LATENCY_PREF, SO_BANDWIDTH_PREF);
        server.setReceiveBufferSize(SO_RCV_BUFF_SIZE);

        int soport = server.getLocalPort();

        Log.log("-- RCV endpoint server socket opened on port %d", soport);

        if (!port_ref.compareAndSet(INIT_PORT, soport))
          throw new IllegalStateException("portUpdater");

        Log.log("-- RCV endpoint port set to %d", port_ref.get());

        Log.log("-- RCV endpoint now accepting connection ..");
        latch.countDown();
        Socket socket = server.accept();
        socket.setKeepAlive(true);
        socket.setPerformancePreferences(SO_CONNTIME_PREF, SO_LATENCY_PREF, SO_BANDWIDTH_PREF);
        socket.setTcpNoDelay(true);
        socket.setSendBufferSize(SO_RCV_BUFF_SIZE);
        SocketAddress remsoaddr = socket.getRemoteSocketAddress();

        Log.log("-- RCV endpoint accepted connection from %s", remsoaddr);

        if (!rcvsocket_ref.compareAndSet(null, socket))
          throw new IllegalStateException("recvInUpdater");

      } catch (IOException e) {
        e.printStackTrace();
      } finally {
        Log.log("server endpoint established");
      }
    }
示例#5
0
  private static void initSocket(ServerSocket socket, Configuration conf) throws SocketException {
    final int[] performancePreferences = conf.performancePreferences();
    if (performancePreferences != null) {
      int n = performancePreferences.length;
      int connectionTime = 0;
      int latency = 0;
      int bandWidth = 0;
      if (n > 2) {
        connectionTime = performancePreferences[0];
        latency = performancePreferences[1];
        bandWidth = performancePreferences[2];
      } else if (n > 1) {
        connectionTime = performancePreferences[0];
        latency = performancePreferences[1];
      } else if (n > 0) connectionTime = performancePreferences[0];

      socket.setPerformancePreferences(connectionTime, latency, bandWidth);
    }

    if (conf.reuseAddr()) socket.setReuseAddress(true);

    final Integer recvBufSize = conf.recvBufSize();
    if (recvBufSize != null) socket.setReceiveBufferSize(recvBufSize);
  }
 public void test_setPerformancePreference_Int_Int_Int() throws Exception {
   ServerSocket theSocket = new ServerSocket();
   theSocket.setPerformancePreferences(1, 1, 1);
 }
示例#7
0
 @Signature({@Arg("connectTime"), @Arg("latency"), @Arg("bandWidth")})
 public Memory setPerformancePreferences(Environment env, Memory... args) throws SocketException {
   socket.setPerformancePreferences(args[0].toInteger(), args[1].toInteger(), args[2].toInteger());
   return Memory.NULL;
 }