Package com.sun.grizzly

Examples of com.sun.grizzly.SelectorHandler


         * logic below, so closing the server socket may not be necessary.
         * The VIP/CLB might use the presence of the socket to detect if
         * a listener/instance is alive. So let us close the server socket
         * depending on  a property.
         */
        SelectorHandler serverHandler = e.getValue().getLeft();

        if (closesocket) {
            try {
                Set<SelectionKey> keys = serverHandler.keys();

                for (SelectionKey key : keys) {
                    if ((key.interestOps() & SelectionKey.OP_ACCEPT) ==
                            SelectionKey.OP_ACCEPT) {
                        serverHandler.closeChannel(key.channel());
                    }
                }
            } catch (Exception ce) {
                logger.log(Level.SEVERE,
                        "sip.stack.network.listener.disable.close.error");
            }
        }

        /*
         * Modify the filter chain to add a filter which blocks
         * new SIP requests.
         */
        DefaultProtocolChain protocolChain = new DefaultProtocolChain();

        if (serverHandler.protocol() == Controller.Protocol.TLS) {
            SSLReadFilter sslfilter = getSSLReadFilter(sipBindingCtx);

            protocolChain.addFilter(sslfilter);

            protocolChain.addFilter(new MessageProcessorFilter(this,
                    requestTimeOut, true, _bbPool, tlsAsyncWriteCallbackHandler));
        } else {
            protocolChain.setContinuousExecution(false);

            ReadFilter readFilter = new SharedReadFilter();

            readFilter.setContinuousExecution(false);

            protocolChain.addFilter(readFilter);

            protocolChain.addFilter(new MessageProcessorFilter(this,
                    requestTimeOut, true, _bbPool,
                    tcpUdpAsyncWriteCallbackHandler));
        }

        ProtocolChainInstanceHandler instanceHandler =
                new SimpleProtocolChainInstanceHandler(protocolChain);

        serverHandler.setProtocolChainInstanceHandler(instanceHandler);

        /*
         * Wait until the diable timeout is over, this method will
         * block until disable timeout, Each listener will wait for this much
         * time. A periodic check for incomplete transactions will be more efficient
View Full Code Here


    private synchronized void clearCachedHandlers(SelectorHandler server, SelectorHandler client) {
      ConcurrentHashMap<TargetTuple, ConnectorHandler> cache = connectionManager.streams;
    Set<TargetTuple> tuples = cache.keySet();
         for (TargetTuple tt : tuples) {
            ConnectorHandler connectorHandler = cache.get(tt);
            SelectorHandler selHandler = connectorHandler.getSelectorHandler();
            if (selHandler == null) {
                if (logger.isLoggable(Level.FINEST)) {
                    logger.log(Level.FINEST,
                            "sip.network.grizzly.clear.handlers",
                            new Object[]{tt});
                }
                cache.remove(tt);
            } else if (selHandler.equals(client) || selHandler.equals(server)) {
                if (logger.isLoggable(Level.FINEST)) {
                    logger.log(Level.FINEST,
                            "sip.network.grizzly.clear.handlers",
                            new Object[]{tt});
                }
View Full Code Here

                ((WorkerThread) Thread.currentThread());
        ByteBuffer buffer = workerThread.getByteBuffer();
        SipParser _parser = SipParser.getInstance();
        final Protocol prot = ctx.getProtocol();
        final SelectionKey key = ctx.getSelectionKey();
        final SelectorHandler handler = ctx.getSelectorHandler();
      final ThreadAttachment currentTA = workerThread.getAttachment();

        TargetTuple remote = null;
        InetSocketAddress local = null;
        X509Certificate[] x509Certs = null;

        switch (prot) {
            case TCP:
                s = ((SocketChannel) key.channel()).socket();
                remoteAddress = (InetSocketAddress) s.getRemoteSocketAddress();
                remote = new TargetTuple(SipTransports.TCP_PROT, remoteAddress);
                local = (InetSocketAddress) s.getLocalSocketAddress();

                break;

            case UDP:
                if (!GrizzlyNetworkManager.useDefaultUDPSelectorHandler){
                    buffer = (ByteBuffer) ctx.removeAttribute
                            (GrizzlyNetworkManager.UDP_BUFFER);               
                } else {
                    ctx.setKeyRegistrationState(Context.KeyRegistrationState.NONE);               
                    handler.register(key, SelectionKey.OP_READ);
                }
                DatagramSocket d = ((DatagramChannel) key.channel()).socket();
                remoteAddress =
                        (InetSocketAddress) ctx.getAttribute(ReadFilter.UDP_SOCKETADDRESS);
                remote = new TargetTuple(SipTransports.UDP_PROT, remoteAddress);
                local = (InetSocketAddress) d.getLocalSocketAddress();
                break;

            case TLS:
                s = ((SocketChannel) key.channel()).socket();
                remoteAddress = (InetSocketAddress) s.getRemoteSocketAddress();
                remote = new TargetTuple(SipTransports.TLS_PROT, remoteAddress);
                local = (InetSocketAddress) s.getLocalSocketAddress();
                Object[] certs =
                        (Object[]) ctx.removeAttribute(GrizzlyNetworkManager.SIP_CERTS);

                if ((certs != null) && (certs.length > 0)) {
                    ArrayList<X509Certificate> al =
                            new ArrayList<X509Certificate>();

                    for (int i = 0; i < certs.length; i++) {
                        if (certs[i] instanceof X509Certificate) {
                            al.add((X509Certificate) certs[i]);
                        } else {
                            logger.log(Level.WARNING,
                                    "sip.network.grizzly.wrong.certs",
                                    new Object[]{certs[i].getClass()});
                        }
                    }

                    x509Certs = al.toArray(new X509Certificate[al.size()]);
                }

                break;
        }

        try {
            int initialSize = 0;
            buffer.flip();

            int remaining = buffer.remaining();
            while (((remaining > 0) && (initialSize != remaining)) ||
                    (parsedMessage == null)) {
                initialSize = remaining;
                if (_message == null) {
                    skipNewLines(buffer);
                }
                if (!buffer.hasRemaining()) {
                    return invokeNextFilter;
                }

                parsedMessage = _parser.parseMessage(_message, buffer, local,
                        remote, null);
                remaining = buffer.remaining();

                if ((parsedMessage != null) &&
                        parsedMessage.isMessageComplete() && remaining > 0) {
                    if (isRequestBlocked) {
                        processMessageBlockedState(parsedMessage);
                        continue;
                    }
                    final SipServletMessageImpl msg = parsedMessage;
                    parsedMessage = null;
                    _message = null;
            final InetSocketAddress _remoteAddress = remoteAddress; 
                    msg.setCertificate(x509Certs);
                    SipContainerThreadPool.getInstance().execute(new Callable() {

                        public Object call() throws Exception {
                            if (prot == Protocol.TLS) {
                                if (currentTA != null) {
                                    ((WorkerThread) Thread.currentThread()).
                                            setSSLEngine(currentTA.getSSLEngine());
                                    ((WorkerThread) Thread.currentThread()).
                                            updateAttachment(Mode.SSL_ENGINE);
                                } else {
                                    logger.log(Level.WARNING,
                                            "Thread attachment is null");
                                    return null;
                                }
                            }
                            processMessage(msg, key, handler, _remoteAddress,
                                    prot);
                            return null;
                        }
                    });
                    continue;
                }

                if ((parsedMessage == null) ||
                        !parsedMessage.isMessageComplete()) {
                    // UDP packet are *always* read using a single read, hence
                    // no need to try another read using a temporary Selector.
                    if (prot != Protocol.UDP) {
                        if (logger.isLoggable(Level.FINEST)) {
                            logger.log(Level.FINEST,
                                    "sip.network.grizzly.incomplete.tcp.request",
                                    new Object[]{buffer.position()});
                        }
                        initialSize = remaining;
                        if (buffer.hasRemaining()) {
                            buffer.compact();
                        } else {
                            buffer.clear();
                        }

                        // The thread might block when reading more bytes using
                        // a temporary Selector.
                        ByteBufferInputStream inputStream =
                                new ByteBufferInputStream();
                        inputStream.setSecure((prot == Protocol.TLS));
                        inputStream.setSelectionKey(ctx.getSelectionKey());
                        inputStream.setReadTimeout(requestTimeOut * 1000);
                        int nRead = inputStream.read(buffer);
                        if (nRead <= 0) {
                            logger.log(Level.SEVERE,
                                    "sip.network.grizzly.readtimeout",
                                    new Object[]{requestTimeOut, nRead, buffer});
                            if (logger.isLoggable(Level.FINE)) {
                                SocketChannel channel = (SocketChannel) ctx.getSelectionKey().
                                        channel();
                                logger.log(Level.FINE,
                                        "sip.network.grizzly.readtimeout.channel" +
                                        channel);
                            }
                            if (parsedMessage != null) {
                                logger.log(Level.SEVERE,
                                        "sip.network.grizzly.readtimeout.drop",
                                        new Object[]{parsedMessage.toString()});
                            }                            // Do not invoke the next ProtocolFilter, if any.
                            return false;
                        }
                        remaining = buffer.remaining();
                        _message = parsedMessage;
                    } else {
                        return true;
                    }
                }
            }
            if (parsedMessage != null) {
                if (isRequestBlocked) {
                    processMessageBlockedState(parsedMessage);
                    return false;
                }
                parsedMessage.setCertificate(x509Certs);
                if (prot == Protocol.UDP) {
                    processMessage(parsedMessage, key, handler, remoteAddress,
                            prot);
                } else {
                    if (!finalRequestOnContainerThreadPool) {
                        /**
                         * The key has to be registered back here for TCP, because
                         * the request will be processesd in the same worker thread
                         * and further requests cannot be read until this request
                         * is processed by the app.
                         */                    
                        ctx.setKeyRegistrationState(Context.KeyRegistrationState.NONE);
                        handler.register(key, SelectionKey.OP_READ);
                        processMessage(parsedMessage, key, handler,
                                remoteAddress, prot);
                    } else {
                        /**
                         * This is necessary if we have to ensure (mitigate
View Full Code Here

     * @return <tt>true</tt> if the previous ProtocolFilter postExecute method
     *         needs to be invoked.
     */
    public boolean postExecute(Context ctx) throws IOException
    {
        final SelectorHandler selectorHandler =
                ctx.getSelectorHandler();
        final SelectionKey key = ctx.getSelectionKey();
       
        if (!isError && endPoint.keepAlive())
        {
View Full Code Here

TOP

Related Classes of com.sun.grizzly.SelectorHandler

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.