Package com.sun.grizzly

Examples of com.sun.grizzly.ConnectorHandler


    private  ConnectorHandler createHandler(ProxyRequestHandler task)
    throws Exception {
        Endpoint ep = task.getEndpoint();
        SocketAddress remote = ep.getSocketAddress();
        CallbackHandler callbackHandler = null;
        ConnectorHandler connectorHandler =
                cacheableHandlerPool.getConnection(ep)
       
        if ((connectorHandler == null) ||
                (connectorHandler.getUnderlyingChannel() == null) ||
                (!connectorHandler.getUnderlyingChannel().isOpen())){
            connectorHandler =
                    controller.acquireConnectorHandler(Controller.Protocol.TCP);               
            callbackHandler =
                new DefaultCallBackHandler(connectorHandler, task);
            ((TCPConnectorHandler)connectorHandler).setConnectionTimeout(10*1000);
            try {
                if (ep.getBindAddress() == null){
                    connectorHandler.connect(remote, callbackHandler);
                } else {
                    connectorHandler.connect(remote,
                            new InetSocketAddress(ep.getBindAddress(), 0), callbackHandler);
                }
            } catch (Exception ex) {
                throw ex;
            }
        } else {
            callbackHandler = connectorHandler.getCallbackHandler();
            ((DefaultCallBackHandler)callbackHandler).
                    refresh(connectorHandler, task);
        }
  
        if (_logger.isLoggable(Level.FINEST)){
            _logger.log(Level.FINEST,
                    "clb.proxy.connectionmanager.handler_created",
                    connectorHandler.getUnderlyingChannel() + "Local :" +
                    ((SocketChannel) connectorHandler.getUnderlyingChannel()).socket()
                    .getLocalAddress() + "Remote :" +
                    ((SocketChannel) connectorHandler.getUnderlyingChannel()).socket()
                    .getRemoteSocketAddress() + " for " + task.getEndpoint());
        }
        return connectorHandler;
    }   
View Full Code Here


// version with logging...
    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});
View Full Code Here

     * @param key Selection key corresponding to the inbound channel.
     */
    public boolean doProxyHttp(ProxyRequestHandler task, ByteBuffer buff,
            SelectionKey clientkey) {
        boolean cachehandler = true;
        ConnectorHandler handler = null;
        long bytesRead = 0;
        long bytesWritten = 0;
        boolean moredata = false;
        if (_logger.isLoggable(Level.FINEST)){
            _logger.log(Level.FINEST, "clb.proxy.dotask.task",task);
        }
        if (task != null) {
            /**
             * taskInfo contains the buffer, length of the task
             * and the bytesWritten field, with this and the key for the
             * inbound channel we should be ready to read from
             * the client channel  and write bytes into server channel
             */
            connectionManager.registerServerEndpoint(clientkey, task);
            /*
             * We have the buff, start writing, if we want more we just
             * have to return to the selector and wait for data
             */
            bytesRead = buff.limit();
            buff.flip();
            try {
                handler = connectionManager.getHandler(task);
            } catch (Exception ex) {
                _logger.log(Level.SEVERE,"Failed : creating connection to backend", ex);
                connectionManager.cleanUpHandler(handler, task.getEndpoint());
                task.setError(true);
                return false;
            }
            task.setConnectorHandler(handler);
        } else {
            moredata = true;
            Exception writeEx = null;
            /**
             * If we dont have a task object it means that
             * we are receiving more data from an existing channel for
             * a known request that is being processed, for which
             * we had registered a ReadTask. Read the bytes from the channel and
             * send it.
             */
                   
            task = connectionManager.getServerEndpoint(clientkey);
            try {
                bytesRead = Reader.read(buff, (SocketChannel) clientkey.channel(),
                        ProxyConfig.getInstance().getReadTimeOutInt());
                if (_logger.isLoggable(Level.FINE)){
                    _logger.log(Level.FINE, "clb.proxy.bytes_read", "" +  bytesRead);
                }

            } catch (IOException ex) {
                _logger.log(Level.SEVERE,"clb.proxy.bytes_read_failed ",
                        ex.getMessage());
                writeEx = ex;               
            }
            if ((writeEx != null) || (bytesRead == -1)){                               
                /**
                 * Close the BE channel also because
                 * that must be waiting for data.
                 */
                connectionManager.cleanUpHandler(task.getConnectorHandler(),
                        task.getEndpoint());
                return false;
            }
        }
        if (task == null) {
            _logger.log(Level.SEVERE,"clb.proxy.doproxy_task_null");
            return false;
        }
        handler = task.getConnectorHandler()
        Exception exception = null;
        if (bytesRead <= 0) {
            /* Go back to Grizzly 1.0 selector thread and
             * register the read key, return true will ensure keepalive.
             */
            return cachehandler;
        } else {
            try {
                int retry = 0;
                int pos = buff.position();
                int limit = buff.limit();
                while (true) {                                      
                    try {
                        bytesWritten = handler.write(buff, true);
                        task.setConnectorHandler(handler);
                        break;
                    } catch (Exception ioe) {
                        if (moredata) {
                            throw new IOException("More data for a closed BE channel");
View Full Code Here

        public ConnectorHandler get(TargetTuple tt) {
            return streams.get(tt);
        }

        private void remove(TargetTuple tt) {
            ConnectorHandler handler = streams.remove(tt);

            if (handler != null) {
                try {
                    handler.close();
                } catch (Exception e) {
                    if (logger.isLoggable(Level.FINEST)) {
                        logger.log(Level.FINEST,
                                "sip.network.grizzly.handler.close.error",
                                new Object[]{handler});
View Full Code Here

                        throw new ConnectException("Connection refused to: " +
                                tt.getSocketAddress());
                    }
                }
            }
            ConnectorHandler handler = null;
            if (useCache) {
                handler = streams.get(tt);
                if ((handler != null) &&
                        (handler.getUnderlyingChannel() == null ||
                        !handler.getUnderlyingChannel().isOpen() ||
                        handler.getSelectorHandler() == null ||
                        handler.getSelectorHandler().getAsyncQueueWriter() ==
                        null)) {
                    // none of these warning should ever happen
                    /*
                     * The following checks are to ensure that we do not
                     * use stale connections after a listener has been
                     * disabled and then enabled.
                     */
                    if (handler.getUnderlyingChannel() == null) {
                        if (logger.isLoggable(Level.FINEST)) {
                            logger.log(Level.FINEST,
                                    "discarding a connector with a null channel " +
                                    "for " + tt.toString());
                        }
                    }
                    if (handler.getSelectorHandler() == null) {
                        if (logger.isLoggable(Level.FINEST)) {
                            logger.log(Level.FINEST,
                                    "discarding a connector with a null " +
                                    "selector handler for " + tt.toString());
                        }
                    } else if (handler.getSelectorHandler().getAsyncQueueWriter() ==
                            null) {
                        if (logger.isLoggable(Level.FINEST)) {

                            logger.log(Level.FINEST,
                                    "discarding a connector with a null async " +
                                    "writer for " + tt.toString());
                        }
                    }

                    try {
                        handler.close();
                    } catch (Exception e) {
                        if (logger.isLoggable(Level.FINE)) {
                            logger.log(Level.FINE,
                                    "sip.network.grizzly.handler.close.error",
                                    new Object[]{handler});
                        }
                    }

                    connectionManager.remove(tt); //Cleanup a broken connection

                    handler = null;
                }
            }

            if (handler == null) {
                handler = createConnectorHandler(tt, sipServletMessage);
            }

            // The connection failed, returning null.
            if (handler == null && useCache) {
                if (sipServletMessage.getRetryPolicy() ==
                        SipServletMessageImpl.RetryPolicy.RESOLVE_AND_RETRY) {
                    if (sipServletMessage.getMessageType() ==
                            SipServletMessageImpl.SipMessageType.SipResponse) {
                        try {
                            resolve((SipServletResponseImpl) sipServletMessage);
                            //try again
                            tt = getBindTuple(sipServletMessage);
                            handler = createConnectorHandler(tt, sipServletMessage);
                        } catch (Exception e) {
                            logSendFailed((SipServletResponseImpl) sipServletMessage,
                                    e);
                        }
                    }
                } else {
                //then we have already tried with the first createConnectorHandler(...) invocation
                }
            }

            //after any potential retry
            if (handler == null) {
                throw new ConnectException("Connection refused to: " +
                        tt.getSocketAddress());
            }

            boolean cacheBuffer = false;
            if (bb == null) {
                sipServletMessage.toBufferInit();

                bb = toBuffer(sipServletMessage);
                cacheBuffer = true;
            }

            if (!bb.hasRemaining()) {
                if (logger.isLoggable(Level.SEVERE)) {
                    logger.log(Level.SEVERE,
                            "sip.stack.network.invalid_bytebuffer");
                }

                return;
            }

            try {
                if (sipServletMessage.toBufferHasRemaining()) {
                    synchronized (handler.getUnderlyingChannel()) {
                        writeMessage(bb, handler, tt);
                        while (sipServletMessage.toBufferHasRemaining()) {
                            //This is a big message
                            bb = toBuffer(sipServletMessage);
                            writeMessage(bb, handler, tt);
                        }
                    }
                } else {
                    writeMessage(bb, handler, tt);
                }
            } catch (Exception ex) {
                if (logger.isLoggable(Level.SEVERE)) {
                    logger.log(Level.SEVERE, "sip.network.grizzly.write.failed",
                            new Object[]{tt, ex.getMessage()});
                }

                try {
                    handler.close();
                } catch (Exception e) {
                    if (logger.isLoggable(Level.FINEST)) {
                        logger.log(Level.FINEST,
                                "sip.network.grizzly.handler.close.error",
                                new Object[]{handler});
View Full Code Here

         * @throws java.io.IOException
         */
        private ConnectorHandler createConnectorHandler(TargetTuple tt,
                SipServletMessageImpl sipServletMessage)
                throws IOException {
            ConnectorHandler handler = null;

            int protocol = tt.getProtocol().ordinal();          
            synchronized (tt.key().intern()) {
                handler = streams.get(tt);
                       
                if (handler != null) {
                    return handler;
                }
                if (protocol == SipTransports.TCP) {
                    handler = createHandlerTCP(tt);
                } else if ((handler == null) &&
                        (protocol == SipTransports.TLS)) {
                    handler = createHandlerTLS(tt);
                } else if (handler == null) {
                    handler = createHandlerUDP(tt);
                }

                if (handler != null) {
                    SelectableChannel channel = handler.getUnderlyingChannel();

                    if ((channel != null) && channel.isOpen() && (handler.getSelectorHandler() != null) && (handler.getSelectorHandler().getAsyncQueueWriter() != null)) {
                        streams.putIfAbsent(tt, handler);
                    } else {
                      if (handler.getUnderlyingChannel() == null) {
                            if (logger.isLoggable(Level.FINEST)){
                                logger.log(
                                        Level.FINEST, "not returning a " +
                                        "connector with a null channel " +
                                        tt.toString());
                            }
                      }
                      if (handler.getSelectorHandler() == null) {
                            if (logger.isLoggable(Level.FINEST)){
                        logger.log(Level.FINEST,
                                        "not rerturning a connector with a null " +
                                        "selector handler " + tt.toString());
                            }
                      } else if (handler.getSelectorHandler().getAsyncQueueWriter() == null) {
                            if (logger.isLoggable(Level.FINEST)){
                        logger.log(Level.FINEST, "not returning a " +
                                        "connector with a null async " +
                                        "writer " + tt.toString());
                            }
View Full Code Here

        keepAliveIdleTimeOut = (long) (0.9 * time);
    }
   
   
    public ConnectorHandler getConnection()  {  
        ConnectorHandler connection  = null;
        try {           
            connection = idleConnections.poll(WAIT_TIME,
                    TimeUnit.MILLISECONDS);
            if (connection != null){
                if (shouldCheckKeepAlive() &&
                        !keepAliveCounter.isUsable(connection)){
                    try {
                        connection.close();
                    } catch (Exception e){
                        ;
                    }
                    removeConnection(connection);
                    connection = null;
View Full Code Here

TOP

Related Classes of com.sun.grizzly.ConnectorHandler

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.