コード例 #1
0
ファイル: ChannelListener.java プロジェクト: Ianwww/nifi
 public ChannelListener(
     final int threadPoolSize,
     final StreamConsumerFactory consumerFactory,
     final BufferPool bufferPool,
     int timeout,
     TimeUnit unit,
     final boolean readSingleDatagram)
     throws IOException {
   this.executor =
       Executors.newScheduledThreadPool(
           threadPoolSize + 1); // need to allow for long running ChannelDispatcher thread
   this.serverSocketSelector = Selector.open();
   this.socketChannelSelector = Selector.open();
   this.bufferPool = bufferPool;
   this.initialBufferPoolSize = bufferPool.size();
   channelDispatcher =
       new ChannelDispatcher(
           serverSocketSelector,
           socketChannelSelector,
           executor,
           consumerFactory,
           bufferPool,
           timeout,
           unit,
           readSingleDatagram);
   executor.schedule(channelDispatcher, 50, TimeUnit.MILLISECONDS);
 }
コード例 #2
0
  public static void main(String[] args) throws IOException {
    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
    serverSocketChannel.socket().bind(new InetSocketAddress(PORT));
    System.out.println("Server listening at port : " + PORT);
    serverSocketChannel.configureBlocking(false);
    Selector selector = Selector.open();
    serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

    while (true) {
      int n = selector.select();
      if (n == 0) {
        continue;
      }
      Iterator<SelectionKey> it = selector.selectedKeys().iterator();
      while (it.hasNext()) {
        SelectionKey key = it.next();
        if (key.isAcceptable()) {
          ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
          SocketChannel sc = ssc.accept();
          sc.configureBlocking(false);
          sc.register(selector, SelectionKey.OP_READ);
          System.out.println("accept client : " + sc);
          acceptClient(sc);
        }
        if (key.isReadable()) {
          readData(key);
        }
        it.remove();
      }
    }
  }
コード例 #3
0
ファイル: Signaler.java プロジェクト: 465060874/jeromq
  public Signaler() {
    //  Create the socketpair for signaling.
    Pipe pipe;

    try {
      pipe = Pipe.open();
    } catch (IOException e) {
      throw new ZError.IOException(e);
    }
    r = pipe.source();
    w = pipe.sink();

    //  Set both fds to non-blocking mode.
    try {
      Utils.unblockSocket(w);
      Utils.unblockSocket(r);
    } catch (IOException e) {
      throw new ZError.IOException(e);
    }

    try {
      selector = Selector.open();
      r.register(selector, SelectionKey.OP_READ);
    } catch (IOException e) {
      throw new ZError.IOException(e);
    }
  }
コード例 #4
0
 public void serve(int port) throws IOException {
   ServerSocketChannel serverChannel = ServerSocketChannel.open();
   serverChannel.configureBlocking(false);
   ServerSocket ss = serverChannel.socket();
   InetSocketAddress address = new InetSocketAddress(port);
   // Binds the server to the selected port
   ss.bind(address);
   // Opens the Selector for handling channels
   Selector selector = Selector.open();
   // Registers the ServerSocket with the Selector to accept connections
   serverChannel.register(selector, SelectionKey.OP_ACCEPT);
   final ByteBuffer msg = ByteBuffer.wrap("Hi!\r\n".getBytes());
   while (true) {
     try {
       // Waits for new events to process; blocks until the next incoming event
       selector.select();
     } catch (IOException e) {
       e.printStackTrace();
       break;
     }
     // Obtains all SelectionKey instances that received events
     Set<SelectionKey> readyKeys = selector.selectedKeys();
     Iterator<SelectionKey> iterator = readyKeys.iterator();
     while (iterator.hasNext()) {
       SelectionKey key = iterator.next();
       iterator.remove();
       try {
         // Checks if the event is a new connection ready to be accepted
         if (key.isAcceptable()) {
           ServerSocketChannel server = (ServerSocketChannel) key.channel();
           SocketChannel client = server.accept();
           client.configureBlocking(false);
           // Accepts client and registers it with the selector
           client.register(
               selector, SelectionKey.OP_WRITE | SelectionKey.OP_READ, msg.duplicate());
           System.out.println("Accepted connection from " + client);
         }
         // Checks if the socket is ready for writing data
         if (key.isWritable()) {
           SocketChannel client = (SocketChannel) key.channel();
           ByteBuffer buffer = (ByteBuffer) key.attachment();
           while (buffer.hasRemaining()) {
             // Writes data to the connected client
             if (client.write(buffer) == 0) {
               break;
             }
           }
           // Closes the connection
           client.close();
         }
       } catch (IOException e) {
         key.cancel();
         try {
           key.channel().close();
         } catch (IOException e1) {
         }
       }
     }
   }
 }
コード例 #5
0
ファイル: NIOServer.java プロジェクト: OAbern/misc-demo
  NIOServer(int bindLocalPort) {
    this.bindLocalPort = bindLocalPort;
    try {
      // 初始化serverSocketChannel 并且绑定本地端口
      serverSocketChannel = ServerSocketChannel.open();
      serverSocketChannel.bind(new InetSocketAddress(bindLocalPort));

      //            socketChannel = SocketChannel.open();
      //            socketChannel.bind(new InetSocketAddress());

      // 初始化selector、byteBuffer
      selector = Selector.open();
      byteBuffer = ByteBuffer.allocate(BUFFER_SIZE);

      // 配置通道为非阻塞(必须) 并且将通道注册到selector,监听注册事件
      serverSocketChannel.configureBlocking(false);
      serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
      System.out.println(
          "[** NIOServer **]: Init NIOServer success! The serverSocketChannel has opened in port "
              + bindLocalPort
              + ", registered with a seletcor and interested on SelectionKey.OP_ACCEPT!");
      INIT_FLAG = true;
    } catch (IOException e) {
      INIT_FLAG = false;
      System.out.println("[** NIOServer **]: Init NIOServer failed!");
      e.printStackTrace();
    }
  }
コード例 #6
0
ファイル: NIOUtils.java プロジェクト: developerSid/NeoIO
 public static Selector selectorOpen() throws NetSelectorException {
   try {
     return Selector.open();
   } catch (IOException e) {
     throw new NetSelectorException(e);
   }
 }
コード例 #7
0
  @Override
  public void run() {
    try {
      System.out.println("[Server Opened]");
      selector = Selector.open();
      ssc.register(selector, SelectionKey.OP_ACCEPT);
      while (true) {
        int canalsPreparats = selector.select();
        if (canalsPreparats == 0) {
          continue;
        }
        Set<SelectionKey> clausSeleccionades = selector.selectedKeys();
        Iterator<SelectionKey> iterador = clausSeleccionades.iterator();
        while (iterador.hasNext()) {
          SelectionKey clau = iterador.next();

          // CONCURRENCIA claus
          // Les claus son un recurs compartit, ja que quan finalitza
          // el joc, el métode update() les cancela. Per aquest motiu
          // quan en fem us, synchronitzem el objecte, i comprovem
          // que siguin vàlides
          synchronized (this) {
            if (clau.isValid() && clau.isAcceptable()) {
              ferAccept(clau);
            } else if (clau.isValid() && clau.isReadable()) {
              rebre(clau);
            }
          }
          iterador.remove();
        }
      }
    } catch (IOException ex) {
    }
  }
コード例 #8
0
ファイル: NIOClient.java プロジェクト: Bongwe/testjava
 /**
  * ���һ��Socketͨ�������Ը�ͨ����һЩ��ʼ���Ĺ���
  *
  * @param ip ���ӵķ�������ip
  * @param port ���ӵķ������Ķ˿ں�
  * @throws IOException
  */
 public void initClient(String ip, int port) {
   // ���һ��Socketͨ��
   SocketChannel channel = null;
   try {
     channel = SocketChannel.open();
     // ����ͨ��Ϊ������
     channel.configureBlocking(false);
     // ���һ��ͨ��������
     this.selector = Selector.open();
     // �ͻ������ӷ�����,��ʵ����ִ�в�û��ʵ�����ӣ���Ҫ��listen���������е�
     // ��channel.finishConnect();�����������
     channel.connect(new InetSocketAddress(ip, port));
     // ��ͨ���������͸�ͨ���󶨣���Ϊ��ͨ��ע��SelectionKey.OP_CONNECT�¼���
     channel.register(selector, SelectionKey.OP_CONNECT);
   } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     if (channel != null) {
       try {
         channel.close();
       } catch (IOException e1) {
         // TODO Auto-generated catch block
         e1.printStackTrace();
       }
     }
   }
 }
コード例 #9
0
 public RemoteSlaveConnection(int numSlaves, String host, int port) throws IOException {
   this.host = host;
   this.port = port > 0 && port < 65536 ? port : DEFAULT_PORT;
   slaves = new SocketChannel[numSlaves];
   communicationSelector = Selector.open();
   startServerSocket();
 }
コード例 #10
0
  public static void main(String[] argv) throws Exception {
    Pipe[] pipes = new Pipe[PIPES_COUNT];
    Pipe pipe = Pipe.open();
    Pipe.SinkChannel sink = pipe.sink();
    Pipe.SourceChannel source = pipe.source();
    Selector sel = Selector.open();
    source.configureBlocking(false);
    source.register(sel, SelectionKey.OP_READ);

    for (int i = 0; i < PIPES_COUNT; i++) {
      pipes[i] = Pipe.open();
      Pipe.SourceChannel sc = pipes[i].source();
      sc.configureBlocking(false);
      sc.register(sel, SelectionKey.OP_READ);
      Pipe.SinkChannel sc2 = pipes[i].sink();
      sc2.configureBlocking(false);
      sc2.register(sel, SelectionKey.OP_WRITE);
    }

    for (int i = 0; i < LOOPS; i++) {
      sink.write(ByteBuffer.allocate(BUF_SIZE));
      int x = sel.selectNow();
      sel.selectedKeys().clear();
      source.read(ByteBuffer.allocate(BUF_SIZE));
    }

    for (int i = 0; i < PIPES_COUNT; i++) {
      pipes[i].sink().close();
      pipes[i].source().close();
    }
    pipe.sink().close();
    pipe.source().close();
    sel.close();
  }
コード例 #11
0
  @Override
  public void run() {
    try {
      selector = Selector.open();

      while (true) {
        processSelectionQueue();

        int nKeys = selector.select(selectWaitTime); // blocking
        if (nKeys == 0) {
          continue;
        } else {
          logger.trace(String.format("Selector %d, keys num: %d", selectorNum, nKeys));
        }

        Set<SelectionKey> keys = selector.selectedKeys();
        Iterator<SelectionKey> iter = keys.iterator();

        while (iter.hasNext()) {
          SelectionKey key = iter.next();
          iter.remove();

          logger.trace("Key operations: " + key.readyOps());

          if (key.isWritable()) {
            doWrite(key);
          }
        }
      }
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
コード例 #12
0
  public static Selector openSelector() throws IOException {
    Selector result = null;
    // 在linux平台,尽量启用epoll实现
    if (isLinuxPlatform()) {
      try {
        final Class<?> providerClazz = Class.forName("sun.nio.ch.EPollSelectorProvider");
        if (providerClazz != null) {
          try {
            final Method method = providerClazz.getMethod("provider");
            if (method != null) {
              final SelectorProvider selectorProvider = (SelectorProvider) method.invoke(null);
              if (selectorProvider != null) {
                result = selectorProvider.openSelector();
              }
            }
          } catch (final Exception e) {
            // ignore
          }
        }
      } catch (final Exception e) {
        // ignore
      }
    }

    if (result == null) {
      result = Selector.open();
    }

    return result;
  }
コード例 #13
0
  // =============================================================
  //				LifeCycle
  // =============================================================
  @Override
  public boolean init() {
    try {
      selector = Selector.open();
      serverSocketChannel = ServerSocketChannel.open();
      serverSocketChannel.configureBlocking(false);
      serverSocketChannel
          .socket()
          .bind(
              hostName == null
                  ? new InetSocketAddress(port)
                  : new InetSocketAddress(hostName, port));
      serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

      // 后台读写线程
      // 这里只能有一个线程, socket读写非线程安全
      // readerExecutor = Executors.newFixedThreadPool(1, new
      // WaveriderThreadFactory(NET_WORK_READER, null, true));
      // writerExecutor = Executors.newFixedThreadPool(1, new
      // WaveriderThreadFactory(NET_WORK_WRITER, null, true));

      // 网络监听线程
      netWorkServerThread = new Thread(this, NET_WORK_SERVER_THREAD_NAME);
      netWorkServerThread.setDaemon(true);
    } catch (IOException e) {
      logger.error("Init DefaultNetworkServer failed: ", e);
      throw new RuntimeException(e);
    }

    return true;
  }
コード例 #14
0
  @Test
  public void testReceiveIntoExistingBuffer() throws Exception {
    Selector selector = mock(Selector.class);
    when(selector.select(SteamSocket.timeout)).thenReturn(1);
    mockStatic(Selector.class);
    when(Selector.open()).thenReturn(selector);
    when(this.channel.register(selector, SelectionKey.OP_READ)).thenReturn(null);

    this.socket.buffer = ByteBuffer.allocate(10);

    final SteamSocket socket = this.socket;
    when(this.channel.read(any(ByteBuffer.class)))
        .thenAnswer(
            new Answer<Integer>() {
              public Integer answer(InvocationOnMock invocationOnMock) throws Throwable {
                socket.buffer.put("test".getBytes());
                return 4;
              }
            });

    assertEquals(4, this.socket.receivePacket(4));

    ByteBuffer buffer = this.socket.buffer;
    assertEquals(0, buffer.position());
    assertEquals(4, buffer.capacity());
    assertEquals("test", new String(buffer.array()));
  }
コード例 #15
0
ファイル: ServerThread.java プロジェクト: spring/SpringLS
  public boolean startServer() {

    context.starting();

    Configuration configuration = getContext().getService(Configuration.class);
    int port = configuration.getInt(ServerConfiguration.PORT);

    try {
      context.getServer().setCharset("ISO-8859-1");

      // open a non-blocking server socket channel
      sSockChan = ServerSocketChannel.open();
      sSockChan.configureBlocking(false);

      // bind to localhost on designated port
      // ***InetAddress addr = InetAddress.getLocalHost();
      // ***sSockChan.socket().bind(new InetSocketAddress(addr, port));
      sSockChan.socket().bind(new InetSocketAddress(port));

      // get a selector for multiplexing the client channels
      readSelector = Selector.open();

    } catch (IOException ex) {
      LOG.error("Could not listen on port: " + port, ex);
      return false;
    }

    LOG.info("Listening for connections on TCP port {} ...", port);

    context.started();

    return true;
  }
コード例 #16
0
 public static synchronized NonBlockSocketServer getInstance() throws IOException {
   if (instance == null) {
     roller = Selector.open();
     instance = new NonBlockSocketServer();
   }
   return instance;
 }
コード例 #17
0
 public static void main(String[] args) throws IOException, InterruptedException {
   String nic = args.length > 0 ? args[0] : "0.0.0.0";
   int port = args.length > 1 ? Integer.parseInt(args[1]) : 12345;
   System.out.println("Listening on interface : " + nic + ":" + port);
   final ByteBuffer buffy = ByteBuffer.allocateDirect(PAGE_SIZE).order(ByteOrder.nativeOrder());
   final ServerSocketChannel serverSocket = ServerSocketChannel.open();
   serverSocket.socket().bind(new InetSocketAddress(nic, port));
   SocketChannel accepted = null;
   try {
     accepted = serverSocket.accept();
     accepted.socket().setTcpNoDelay(true);
     accepted.configureBlocking(false);
     serverSocket.close();
     Selector selector = Selector.open();
     accepted.register(selector, SelectionKey.OP_READ);
     while (!Thread.interrupted()) {
       if (pong(buffy, accepted, selector)) return;
     }
   } finally {
     if (accepted != null) {
       try {
         accepted.close();
       } catch (IOException ignored) {
       }
     }
   }
 }
コード例 #18
0
 /**
  * Construct a memcached connection.
  *
  * @param bufSize the size of the buffer used for reading from the server
  * @param f the factory that will provide an operation queue
  * @param a the addresses of the servers to connect to
  * @throws IOException if a connection attempt fails early
  */
 public MemcachedConnection(int bufSize, ConnectionFactory f, List<InetSocketAddress> a)
     throws IOException {
   reconnectQueue = new TreeMap<Long, MemcachedNode>();
   addedQueue = new ConcurrentLinkedQueue<MemcachedNode>();
   selector = Selector.open();
   List<MemcachedNode> connections = new ArrayList<MemcachedNode>(a.size());
   for (SocketAddress sa : a) {
     SocketChannel ch = SocketChannel.open();
     ch.configureBlocking(false);
     MemcachedNode qa = f.createMemcachedNode(sa, ch, bufSize);
     int ops = 0;
     if (ch.connect(sa)) {
       getLogger().info("Connected to %s immediately", qa);
       qa.connected();
       assert ch.isConnected();
     } else {
       getLogger().info("Added %s to connect queue", qa);
       ops = SelectionKey.OP_CONNECT;
     }
     qa.setSk(ch.register(selector, ops, qa));
     assert ch.isConnected() || qa.getSk().interestOps() == SelectionKey.OP_CONNECT
         : "Not connected, and not wanting to connect";
     connections.add(qa);
   }
   locator = f.createLocator(connections);
 }
コード例 #19
0
  /**
   * Start the server running - accepting connections, receiving messages. If the server is already
   * running, it will not be started again. This method is designed to be called in its own thread
   * and will not return until the server is stopped.
   *
   * @throws RuntimeException if the server fails
   */
  public void run() {
    // ensure that the server is not started twice
    if (!state.compareAndSet(State.STOPPED, State.RUNNING)) {
      started(true);
      return;
    }

    Selector selector = null;
    ServerSocketChannel server = null;
    try {
      selector = Selector.open();
      server = ServerSocketChannel.open();
      server.socket().bind(new InetSocketAddress(port));
      server.configureBlocking(false);
      server.register(selector, SelectionKey.OP_ACCEPT);
      started(false);
      while (state.get() == State.RUNNING) {
        selector.select(100); // check every 100ms whether the server has been requested to stop
        for (Iterator<SelectionKey> it = selector.selectedKeys().iterator(); it.hasNext(); ) {
          SelectionKey key = it.next();
          try {
            // remove key from the ready list
            it.remove();
            if (key.isConnectable()) {
              ((SocketChannel) key.channel()).finishConnect();
            }
            if (key.isAcceptable()) {
              // accept connection
              SocketChannel client = server.accept();
              client.configureBlocking(false);
              client.socket().setTcpNoDelay(true);
              // channel is registered for further events such as read or write
              SelectionKey acceptKey = client.register(selector, SelectionKey.OP_READ);
              connection(acceptKey);
            }
            if (key.isReadable()) {
              for (ByteBuffer message : readIncomingMessage(key)) {
                messageReceived(message, key);
              }
            }
          } catch (IOException ioe) {
            resetKey(key);
            disconnected(key);
          }
        }
      }
    } catch (Throwable e) {
      throw new RuntimeException("Server failure: " + e.getMessage());
    } finally {
      try {
        selector.close();
        server.socket().close();
        server.close();
        state.set(State.STOPPED);
        stopped();
      } catch (Exception e) {
        // do nothing - server failed
      }
    }
  }
コード例 #20
0
  /**
   * Starts a new Thread and connects to server
   *
   * @throws IOException
   */
  public Thread connect() throws IOException {
    this.running = true;
    this.readyState = WEBSOCKET_STATE_CONNECTING;
    // open socket
    socketChannel = SocketChannel.open();
    socketChannel.configureBlocking(false);
    // set address
    socketChannel.connect(new InetSocketAddress(uri.getHost(), port));
    // start a thread to make connection

    // More info:
    // http://groups.google.com/group/android-developers/browse_thread/thread/45a8b53e9bf60d82
    // http://stackoverflow.com/questions/2879455/android-2-2-and-bad-address-family-on-socket-connect
    System.setProperty("java.net.preferIPv4Stack", "true");
    System.setProperty("java.net.preferIPv6Addresses", "false");

    selector = Selector.open();
    socketChannel.register(selector, SelectionKey.OP_CONNECT);
    Log.v("websocket", "Starting a new thread to manage data reading/writing");

    Thread th = new Thread(this);
    th.start();
    // return thread object for explicit closing, if needed
    return th;
  }
コード例 #21
0
ファイル: EventLoop.java プロジェクト: pdex/xio2
 EventLoop() {
   try {
     selector = Selector.open();
   } catch (Exception e) {
     throw new RuntimeException(e);
   }
 }
コード例 #22
0
  @Override
  public void establish() throws IOException {
    discoverySelector = Selector.open();
    serverSocketChannel.register(discoverySelector, SelectionKey.OP_ACCEPT);
    int slaveCount = 0;
    while (slaveCount < slaves.length) {
      log.info("Awaiting registration from " + (slaves.length - slaveCount) + " slaves.");
      discoverySelector.select();
      Set<SelectionKey> keySet = discoverySelector.selectedKeys();
      Iterator<SelectionKey> it = keySet.iterator();
      while (it.hasNext()) {
        SelectionKey selectionKey = it.next();
        it.remove();
        if (!selectionKey.isValid()) {
          continue;
        }
        ServerSocketChannel srvSocketChannel = (ServerSocketChannel) selectionKey.channel();
        SocketChannel socketChannel = srvSocketChannel.accept();

        int slaveIndex = readInt(socketChannel);
        if (slaveIndex < 0) {
          for (int i = 0; i < slaves.length; ++i) {
            if (slaves[i] == null) {
              slaveIndex = i;
              break;
            }
          }
        } else if (slaveIndex >= slaves.length) {
          throw new IllegalArgumentException(
              "Slave requests invalid slaveIndex "
                  + slaveIndex
                  + " (expected "
                  + slaves.length
                  + " slaves)");

        } else if (slaves[slaveIndex] != null) {
          throw new IllegalArgumentException(
              "Slave requests slaveIndex "
                  + slaveIndex
                  + " but this was already assigned to "
                  + slaves[slaveIndex].getRemoteAddress());
        }
        writeInt(socketChannel, slaveIndex);
        writeInt(socketChannel, slaves.length);
        slaves[slaveIndex] = socketChannel;
        slaveCount++;
        slave2Index.put(socketChannel, slaveIndex);
        this.readBufferMap.put(socketChannel, ByteBuffer.allocate(DEFAULT_READ_BUFF_CAPACITY));
        socketChannel.configureBlocking(false);
        log.trace(
            "Added new slave connection "
                + slaveIndex
                + " from: "
                + socketChannel.socket().getInetAddress());
      }
    }
    mcastBuffer = ByteBuffer.allocate(DEFAULT_WRITE_BUFF_CAPACITY);
    log.info("Connection established from " + slaveCount + " slaves.");
  }
コード例 #23
0
  public boolean addServer(GearmanJobServerConnection conn)
      throws IllegalArgumentException, IllegalStateException {

    if (conn == null) {
      throw new IllegalArgumentException("Connection can not be null");
    }
    // this is a sub-optimal way to look for dups, but addJobServer
    // ops should be infrequent enough that this should be a big penalty
    for (GearmanJobServerSession sess : sessionMap.values()) {
      if (sess.getConnection().equals(conn)) {
        return true;
      }
    }

    GearmanJobServerSession session = new GearmanJobServerSession(conn);
    if (ioAvailable == null) {
      try {
        ioAvailable = Selector.open();
      } catch (IOException ioe) {
        LOG.warn("Failed to connect to job server " + conn + ".", ioe);
        return false;
      }
    }
    try {
      session.initSession(ioAvailable, this);
    } catch (IOException ioe) {
      LOG.warn("Failed to initialize session with job server " + conn + ".", ioe);
      return false;
    }
    SelectionKey key = session.getSelectionKey();
    if (key == null) {
      String msg =
          "Session "
              + session
              + " has a null "
              + "selection key. Server will not be added to worker.";
      LOG.warn(msg);
      throw new IllegalStateException(msg);
    }
    sessionMap.put(key, session);

    GearmanPacket p =
        new GearmanPacketImpl(
            GearmanPacketMagic.REQ, GearmanPacketType.SET_CLIENT_ID, ByteUtils.toUTF8Bytes(id));
    session.submitTask(new GearmanTask(p));

    for (FunctionDefinition def : functionMap.values()) {
      p = generateCanDoPacket(def);
      session.submitTask(new GearmanTask(p)); // NOPMD
    }

    p = new GearmanPacketImpl(GearmanPacketMagic.REQ, getGrabJobPacketType(), new byte[0]);
    GearmanTask gsr = new GearmanTask(new GrabJobEventHandler(session), p);
    taskMap.put(session, gsr);
    session.submitTask(gsr);

    LOG.debug("Added server " + conn + " to worker " + this);
    return true;
  }
コード例 #24
0
 public PingClient() throws IOException {
   selector = Selector.open();
   Connector connector = new Connector();
   Printer printer = new Printer();
   connector.start();
   printer.start();
   receiveTarget();
 }
コード例 #25
0
 private static Selector createSelector() throws IOException {
   ServerSocketChannel server = ServerSocketChannel.open(); // 打开ServerSocketChannel
   Selector selector = Selector.open(); // 创建一个选择器
   server.socket().bind(new InetSocketAddress(DEFAULT_PORT)); // Channel中绑定一个端口
   server.configureBlocking(false); // 绑定为非阻塞模式
   server.register(selector, SelectionKey.OP_ACCEPT); // 选择器注册在Channel上,  注册接收事件(因为服务器端首先是接收请求)
   return selector;
 }
コード例 #26
0
ファイル: JFQ_Server.java プロジェクト: neoyin/early-birds
 private void initServer() throws IOException {
   Selector selector = Selector.open();
   ServerSocketChannel ssc = ServerSocketChannel.open();
   ssc.configureBlocking(false);
   ssc.socket().bind(new InetSocketAddress(9999));
   ssc.register(selector, SelectionKey.OP_ACCEPT);
   readService.execute(new ReceiveServerThread(selector, queues, BUFFER_SIZE));
 }
コード例 #27
0
  @Test
  public void testHostCandidateHarvesting() throws IOException {
    // given
    FoundationsRegistry foundationsRegistry = new LiteFoundationsRegistry();
    HostCandidateHarvester harvester = new HostCandidateHarvester(foundationsRegistry);
    IceMediaStream mediaStream = new IceMediaStream("audio", true);

    Selector selector = Selector.open();

    // when
    try {
      PortManager portManager = new PortManager();
      portManager.setHighestPort(62000);
      portManager.setLowestPort(61000);
      harvester.harvest(portManager, mediaStream, selector);
    } catch (NoCandidatesGatheredException e) {
      fail();
    } catch (HarvestException e) {
      fail();
    }

    // then
    List<LocalCandidateWrapper> rtpCandidates = mediaStream.getRtpComponent().getLocalCandidates();
    List<LocalCandidateWrapper> rtcpCandidates =
        mediaStream.getRtcpComponent().getLocalCandidates();

    assertTrue(rtpCandidates.size() > 0);
    assertTrue(rtcpCandidates.size() > 0);
    // Evaluate RTP Candidates
    for (LocalCandidateWrapper candidateWrapper : rtpCandidates) {
      DatagramChannel udpChannel = candidateWrapper.getChannel();
      assertFalse(udpChannel.isBlocking());
      assertFalse(udpChannel.isConnected());
      assertTrue(udpChannel.isOpen());

      IceCandidate candidate = candidateWrapper.getCandidate();
      assertEquals(candidate, candidate.getBase());
      assertEquals(
          new InetSocketAddress(candidate.getAddress(), candidate.getPort()),
          udpChannel.getLocalAddress());
      assertNotNull(udpChannel.keyFor(selector));
    }
    // Evaluate RTCP candidates
    for (LocalCandidateWrapper candidateWrapper : rtcpCandidates) {
      DatagramChannel udpChannel = candidateWrapper.getChannel();
      assertFalse(udpChannel.isBlocking());
      assertFalse(udpChannel.isConnected());
      assertTrue(udpChannel.isOpen());

      IceCandidate candidate = candidateWrapper.getCandidate();
      assertEquals(candidate, candidate.getBase());
      assertEquals(
          new InetSocketAddress(candidate.getAddress(), candidate.getPort()),
          udpChannel.getLocalAddress());
      assertNotNull(udpChannel.keyFor(selector));
    }
  }
コード例 #28
0
  protected String sendAndReceive(byte[] payload) throws IOException {
    ByteBuffer sbuf = ByteBuffer.wrap(payload);
    ByteBuffer rbuf = ByteBuffer.allocateDirect(256);
    CharsetDecoder rbufDecoder = Charset.forName("UTF-8").newDecoder();
    StringBuilder response = new StringBuilder();

    int ops = SelectionKey.OP_WRITE | SelectionKey.OP_READ;
    if (this.channel.isConnectionPending()) {
      ops = ops | SelectionKey.OP_CONNECT;
    }

    Selector selector = Selector.open();
    try {
      this.channel.register(selector, ops);
      while (true) {
        if (0 < selector.select(Client.POLLS_INTERVAL * 1000)) {
          Iterator keys = selector.selectedKeys().iterator();
          while (keys.hasNext()) {
            SelectionKey key = (SelectionKey) keys.next();
            SocketChannel ch = (SocketChannel) key.channel();
            if (key.isConnectable()) {
              // Just connected
              ch.finishConnect();
            }
            if (key.isReadable() && !sbuf.hasRemaining()) {
              // Receiving the response
              while (0 < ch.read(rbuf)) {
                rbuf.flip();
                response.append(rbufDecoder.decode(rbuf).toString());
              }
              if (2 <= response.length()
                  && response
                      .substring(response.length() - 2, response.length())
                      .equals(SocketClient.TERMINATOR)) {
                response.setLength(response.length() - 2);
                return response.toString();
              } else if (0 == response.length()) {
                throw new IOException("Connection lost");
              }
            }
            if (key.isWritable() && sbuf.hasRemaining()) {
              // Sending the request
              while (0 < ch.write(sbuf) && sbuf.hasRemaining()) {
                //
              }
            }
            keys.remove();
          }
        }
      }
    } catch (java.lang.Exception e) {
      throw new IOException("API communication failed: " + e.toString());
    } finally {
      selector.close();
    }
  }
コード例 #29
0
  private void connect() throws IOException, ClosedChannelException {

    channel = DatagramChannel.open();
    channel.configureBlocking(false);
    channel.socket().bind(new InetSocketAddress(data_port));
    channel.connect(new InetSocketAddress(drone_addr, data_port));

    selector = Selector.open();
    channel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
  }
コード例 #30
0
 private void initializeServer(InetSocketAddress listenAddress) {
   try {
     this.serverChannel = ServerSocketChannel.open();
     this.serverChannel.configureBlocking(false);
     this.serverKey = serverChannel.register(selector = Selector.open(), SelectionKey.OP_ACCEPT);
     this.serverChannel.bind(listenAddress);
   } catch (IOException e) {
     LOG.severe("Couldn't initialize server");
   }
 }