Package com.sun.grizzly

Examples of com.sun.grizzly.Context


    /*
     * Method invoked when the read event is selected for this channel.
     */
    public void onRead(IOEvent<Context> ioEvent) {
        Context ctx = ioEvent.attachment();
        ByteBuffer byteBuffer = null;
        WorkerThread workerThread = (WorkerThread) Thread.currentThread();
        byteBuffer = workerThread.getByteBuffer();
        SelectionKey febeSelectionKey = ctx.getSelectionKey();       
        if (!readFromFeBeChannel(ctx)) {
            return;
       
        /*
         * Paranoid check to fail early.
         */
        if (byteBuffer.position() == 0 || responseHandler == null
                || clientKey == null ) {
            ctx.getSelectorHandler().register(febeSelectionKey,
                    SelectionKey.OP_READ);
            return;
        }
        /* Parsed is an instance variable whose value has to be preserved
         * until the lifecycle of the callback handler.
         */
        if (!parsed) {
            while (!responseHandler.parse(byteBuffer)) {
                if (_logger.isLoggable(Level.FINE)) {
                    _logger.log(Level.FINE,
                            "clb.proxy.callback_algorithm_parse_false");
                }
                if (!readFromFeBeChannel(ctx)) {
                    return;
                }
            }
            parsed = true;
        } else {
            byteBuffer.flip();
        }
        SocketChannel clientChannel = null;
        if (clientKey != null){
            clientChannel = (SocketChannel)clientKey.channel();
        }
       
        if (_logger.isLoggable(Level.FINEST)) {
            _logger.log(Level.FINEST, "clb.proxy.callback.algorithm_finished",
                    clientChannel.socket().getRemoteSocketAddress());
            _logger.log(Level.FINEST, "clb.proxy.callback_clientkey",
                    clientKey.channel());
            _logger.log(Level.FINEST, "clb.proxy.callback.server_channel",
                    ctx.getSelectionKey().channel());
            _logger.log(Level.FINEST, "Byte Buffer " + byteBuffer);
            try {
                dumpBuffer(byteBuffer);
            } catch (Exception e) {
                //ignore
            }
        }
       
        long byteswritten = 0;
        int iPos = byteBuffer.position();
        int iLimit = byteBuffer.limit();
        int bytesToBeWritten = byteBuffer.remaining();       
        boolean writeSuccess = true;
        /*
         * If we have the bytes ready write it to the client
         */
       
        if ((clientChannel != null) && (clientChannel.isConnected())) {
            try {
                if (isSecure) {
                    if (_logger.isLoggable(Level.FINEST)) {
                        _logger.log(Level.FINEST,
                                "clb.proxy.callback.ssl_writer",
                                clientChannel + " " + byteBuffer + " " +
                                outputBB + " " + sslEngine);
                    }
                    byteswritten = SSLOutputWriter.flushChannel(clientChannel,
                            byteBuffer, outputBB, sslEngine);
                } else {
                    byteswritten = OutputWriter.flushChannel(clientChannel,
                            byteBuffer);
                }
            } catch (Exception ioe) {
                /*
                 * If write fails then close client and update the response
                 * handler. We have to drop the response because we cannot send
                 * it through another channel.
                 */
                writeSuccess = false;
                responseHandler.updateBytesWritten(bytesToBeWritten);
                byteBuffer.position(iPos);
                byteBuffer.limit(iLimit);               
            }
            if (responseHandler.isTransferEncoding()) {
                if (_logger.isLoggable(Level.FINE)) {
                    _logger.log(Level.FINE, "Transfer encoding Byte buffer " +
                            byteBuffer);
                }
                /**
                 * It is quite inefficient to search for termination in the whole
                 * bytebuffer, Needs to be modified
                 */
                responseHandler.updateEnd((ByteBuffer) byteBuffer.flip());
            } else {
                responseHandler.updateBytesWritten(byteswritten);
            }

        } else {
            _logger.log(Level.SEVERE,
                    "clb.proxy.callback_client_channel_closed");
            writeSuccess = false;
        }
        if (byteBuffer != null) {
            byteBuffer.clear();
        }               
        boolean register = false;
        if (outputBB != null) {
            outputBB.clear();
       
        if (responseHandler.hasRemaining()) {
            /*
             * If write to client failed and there is still more data
             * to be read from the backend we cannot re-use the febe channel
             * anymore.
             */
            if (!writeSuccess) {
                cleanFeBeHandler(ctx.getSelectorHandler(),
                        febeSelectionKey);
                connectionManager.removeClientEndpoint(clientKey);
                connectionManager.cancelClientKey(clientKey);
            } else {
                register = true;
            }          
            if (_logger.isLoggable(Level.FINEST)) {
                _logger.log(Level.FINEST,
                        "clb.proxy.callback.algorithm_more_data",
                        clientChannel.socket().getRemoteSocketAddress());
            }
        } else {                        
            /* keep the key registered because it can be closed by the
             * peer when in the cache, when this happens the read will
             * will be invoked and we can close the channel. We dont have to
             * keep it registered when keep alive is disabled because we
             * can  be sure that the channel will not be closed. 
             */           
            register = true;
            // refactor
            if (!writeSuccess){
                /*
                 * Close only the client because we can re-use febe channel
                 */
                connectionManager.removeClientEndpoint(clientKey);
                connectionManager.cancelClientKey(clientKey);
            }    
           
            if (!keepAlive) {
                if (_logger.isLoggable(Level.FINEST)) {
                    _logger.log(Level.FINEST,
                            "clb.proxy.callback.algorithm_cancelled_key",
                            clientChannel.socket().getRemoteSocketAddress());
                }
                connectionManager.cancelClientKey(clientKey);
            } else {
                if (_logger.isLoggable(Level.FINEST)) {
                    _logger.log(Level.FINEST,
                            "clb.proxy.callback.algorithm_registered_key",
                            clientChannel.socket().getRemoteSocketAddress());
                }
                connectionManager.registerClientKey(clientKey);               
            }
           
            release();
        }
         if (register) {
            ctx.getSelectorHandler().register(febeSelectionKey,
                    SelectionKey.OP_READ);
        }
       
    }
View Full Code Here


                throws IOException {
            Object attach = SelectionKeyAttachment.getAttachment(key);

            ByteBuffer buff = null;

            final Context context = pollContext(ctx, key,
                    Context.OpType.OP_READ);

            try {
                buff = acquireBuffer();

                socketAddress = ((DatagramChannel) key.channel()).receive(buff);

                if (socketAddress != null) {
                    context.setAttribute(ReadFilter.UDP_SOCKETADDRESS,
                            socketAddress);

                    context.setAttribute(GrizzlyNetworkManager.UDP_BUFFER, buff);

                    context.setPipeline(pipeline());
                } else {
                    ctx.getController().returnContext(context);

                    releaseBuffer(buff);

                    return false;
                }
            } catch (Exception e) {
                ctx.getController().returnContext(context);

                releaseBuffer(buff);

                /*
                 * Mostly this will be a port undreacahable exception
                 * and the key has to be cancelled
                 */
                // added this check to prevent FT failures
                if(staleConnectionsTimeout > 0 ) {
                    if (getRole() == Role.CLIENT) {
                        DatagramChannel channel = ((DatagramChannel) key.channel());                    TargetTuple tt = new TargetTuple(SipTransports.UDP_PROT,
                            channel.socket().getInetAddress().getHostAddress(),
                            channel.socket().getPort());
                        if (staleUDPConnections.get(tt) == null){
                            staleUDPConnections.put(tt, System.currentTimeMillis());
                        }
                    }
                }

                if (logger.isLoggable(Level.FINE)) {
                    logger.log(Level.FINE, "UDP read exception ", e.getMessage());
                }

                if (logger.isLoggable(Level.FINEST)) {
                    logger.log(Level.FINEST, "UDP read exception ", e);
                }

                return false;
            }

            if (asyncQueueReader.isAsyncQueueReaderEnabledFor(key)) {
                invokeAsyncQueueReader(context);
            } else if (attach instanceof CallbackHandler) {
                invokeCallbackHandler((CallbackHandler) attach, context);
            } else {
                try {
                    context.execute(ProtocolChainContextTask.poll());
                } catch (Exception r) {
                    ctx.getController().returnContext(context);

                    releaseBuffer(buff);

View Full Code Here

            }
        }

        public void onRead(IOEvent<Context> ioEvent) {
            try {
                Context ctx = ioEvent.attachment();

                ctx.setAttribute(IS_CLIENT_EXECUTION, true);

                SelectionKey key = ctx.getSelectionKey();

                if (!key.isValid()) {
                    connectionManager.remove(targetTuple);

                    return;
                }

                ctx.setAttribute("tt", targetTuple);

                // disable OP_READ on key before doing anything else
                if ((targetTuple.getProtocol() != SipTransports.UDP_PROT) ||
                        useDefaultUDPSelectorHandler) {
                    key.interestOps(key.interestOps() &
                            (~SelectionKey.OP_READ));
                }
                if (!key.channel().isOpen()) {
                    if (logger.isLoggable(Level.FINER)) {
                        logger.log(Level.FINER, "ChannelClosed " + key.channel());
                    }
                    return;
                }
                ctx.getProtocolChain().execute(ioEvent.attachment());
            } catch (Throwable e) {
                if (logger.isLoggable(Level.SEVERE)) {
                    logger.log(Level.SEVERE,
                            "sip.stack.network.connection_read_failed",
                            new Object[]{"tcp"});
View Full Code Here

        }

        @Override
        public void onRead(IOEvent<Context> ioEvent) {
            try {
                Context ctx = ioEvent.attachment();
                ctx.setAttribute(IS_CLIENT_EXECUTION, true);

                SelectionKey key = ctx.getSelectionKey();

                if (!key.isValid()) {
                    connectionManager.remove(targetTuple);

                    return;
                }

                ctx.setAttribute("tt", targetTuple);

                // disable OP_READ on key before doing anything else
                key.interestOps(key.interestOps() & (~SelectionKey.OP_READ));

                ctx.getProtocolChain().execute(ioEvent.attachment());
            } catch (Throwable e) {
                if (logger.isLoggable(Level.SEVERE)) {
                    logger.log(Level.SEVERE,
                            "sip.stack.network.connection_read_failed",
                            new Object[]{"tcp"});
View Full Code Here

TOP

Related Classes of com.sun.grizzly.Context

Copyright © 2018 www.massapicom. 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.