Examples of SocketChannel


Examples of java.nio.channels.SocketChannel

    worker.onConnect(socketChannel);
  }

  private void read(SelectionKey key) {
    SocketChannel socketChannel = (SocketChannel) key.channel();

    // Clear out our read buffer so it's ready for new data
    this.readBuffer.clear();

    // Attempt to read off the channel
    int numRead;
    try {
      numRead = socketChannel.read(this.readBuffer);
    } catch (IOException e) {
      // The remote forcibly closed the connection, cancel
      // the selection key and close the channel.
      logger.debug("Remote closed connnection", e);
      key.cancel();
View Full Code Here

Examples of java.nio.channels.SocketChannel

    // Hand the data off to our worker thread
    this.worker.onData(this, socketChannel, this.readBuffer.array(), numRead);
  }

  private void write(SelectionKey key) {
    SocketChannel socketChannel = (SocketChannel) key.channel();

    synchronized (this.pendingData) {
      List<ByteBuffer> queue = this.pendingData.get(socketChannel);

      try {
        // Write until there's not more data ...
        while (!queue.isEmpty()) {
          ByteBuffer buf = queue.get(0);
          socketChannel.write(buf);

          if (buf.remaining() > 0) {
            // ... or the socket's buffer fills up
            break;
          }
View Full Code Here

Examples of java.nio.channels.SocketChannel

   * @throws IOException in case of an I/O error
   * @throws InvalidVersionException if the version of Marauroa used to create
   *         the dump and the one used to parse it are incompatible
   */
  public void dump(InputStream is, boolean dumpRawData) throws IOException, InvalidVersionException {
    SocketChannel channel = new FakeSocketChannel(InetAddress.getByName("localhost"), 32123);

    while (true) {
      //read a packet from the opened file
      byte[] data = new byte[100];
      int cnt = is.read(data);
View Full Code Here

Examples of java.nio.channels.SocketChannel

    /**
     * Gets a channel to communicate with the server.
     * @return a socket channel.
     */
    private SocketChannel getChannel() {
        SocketChannel channel = null;

        // open
        try {
            channel = SocketChannel.open();
        } catch (IOException e) {
            cleanChannel(channel);
            throw new IllegalStateException("Cannot open a channel", e);
        }

        // Connect
        try {
            channel.connect(this.socketAddress);
        } catch (IOException e) {
            cleanChannel(channel);
            throw new IllegalStateException("Cannot connect the channel", e);
        }

View Full Code Here

Examples of java.nio.channels.SocketChannel

        Class<?> clazz = null;

        try {
            return super.findClass(name);
        } catch (ClassNotFoundException cnfe) {
            SocketChannel channel = null;
            try {
                long tStart = System.currentTimeMillis();
                // Get channel
                channel = getChannel();
                ByteBuffer answerBuffer = sendRequest(new ClassRequest(name), channel);
View Full Code Here

Examples of java.nio.channels.SocketChannel

     * Ask and return the remote PROVIDER_URL in order to connect with RMI.
     * @return a string with the PROVIDER_URL value.
     */
    public String getProviderURL() {
        String providerURL = null;
        SocketChannel channel = null;
        try {
            long tStart = System.currentTimeMillis();
            // Get channel
            channel = getChannel();
            ByteBuffer answerBuffer = sendRequest(new ProviderURLRequest(), channel);
View Full Code Here

Examples of java.nio.channels.SocketChannel

        if (name.startsWith("META-INF")) {
            return null;
        }

        SocketChannel channel = null;
        try {
            long tStart = System.currentTimeMillis();

            // Get channel
            channel = getChannel();
View Full Code Here

Examples of java.nio.channels.SocketChannel

     * Handle a new client that is being connected.
     * @throws IOException if cannot accept the client
     */
    private void handleAccept() throws IOException {
        // new incoming connection
        SocketChannel client = this.server.accept();

        // Non blocking client
        client.configureBlocking(false);

        // Register client (with an empty channel attachment)
        client.register(this.selector, SelectionKey.OP_READ, new ChannelAttachment());
    }
View Full Code Here

Examples of java.nio.channels.SocketChannel

     * @param selectionKey the selected key.
     * @throws IOException if cannot read from the channel.
     */
    private void handleRead(final SelectionKey selectionKey) throws IOException {
        // Get the client channel that has data to read
        SocketChannel client = (SocketChannel) selectionKey.channel();

        // current bytecode read
        ChannelAttachment channAttachment = (ChannelAttachment) selectionKey.attachment();
        ByteBuffer channBuffer = channAttachment.getByteBuffer();

        // Read again
        int bytesread = client.read(channBuffer);
        if (bytesread == -1) {
            // close (as the client has been disconnected)
            selectionKey.cancel();
            client.close();
        }

        // Client send data, analyze data

        // Got header ?
        if (channBuffer.position() >= Message.HEADER_SIZE) {

            // Yes, got header
            // Check if it is a protocol that we manage
            byte version = channBuffer.get(0);
            if (version != PROTOCOL_VERSION) {
                selectionKey.cancel();
                client.close();
                throw new IllegalStateException("Invalid protocol version : waiting '" + PROTOCOL_VERSION + "', got '" + version
                        + "'.");
            }

            // Get operation asked by client
            byte opCode = channBuffer.get(1);

            // Length
            int length = channBuffer.getInt(2);
            if (length < 0) {
                selectionKey.cancel();
                client.close();
                throw new IllegalStateException("Invalid length for client '" + length + "'.");
            }

            if (length > MAX_LENGTH_INCOMING_MSG) {
                selectionKey.cancel();
                client.close();
                throw new IllegalStateException("Length too big, max length = '" + MAX_LENGTH_INCOMING_MSG + "', current = '"
                        + length + "'.");
            }

            // Correct header and correct length ?
            if (channBuffer.position() >= Message.HEADER_SIZE + length) {
                // set the limit (specified in the length), else we have a
                // default buffer limit
                channBuffer.limit(Message.HEADER_SIZE + length);

                // duplicate this buffer
                ByteBuffer dataBuffer = channBuffer.duplicate();

                // skip header (already analyzed)
                dataBuffer.position(Message.HEADER_SIZE);

                // Switch on operations :
                try {
                    switch (opCode) {
                    case ProtocolConstants.CLASS_REQUEST:
                        handleReadClassRequest(selectionKey, dataBuffer);
                        break;
                    case ProtocolConstants.RESOURCE_REQUEST:
                        handleReadResourceRequest(selectionKey, dataBuffer);
                        break;
                    case ProtocolConstants.PROVIDER_URL_REQUEST:
                        handleReadProviderURLRequest(selectionKey, dataBuffer);
                        break;
                    default:
                        // nothing to do
                    }
                } catch (Exception e) {
                    // clean
                    selectionKey.cancel();
                    client.close();
                    throw new IllegalStateException("Cannot handle request with opCode '" + opCode + "'.", e);
                }
            }
        }

View Full Code Here

Examples of java.nio.channels.SocketChannel

     * Handle all write operations on channels.
     * @param selectionKey the selected key.
     * @throws IOException if cannot write to the channel.
     */
    private void handleWrite(final SelectionKey selectionKey) throws IOException {
        SocketChannel channel = (SocketChannel) selectionKey.channel();

        // Write the data that was attached on the selection key
        ByteBuffer buffer = (ByteBuffer) selectionKey.attachment();
        if (buffer.hasRemaining()) {
            channel.write(buffer);
        } else {
            // finished to write, close
            channel.close();
        }
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.