Search in sources :

Example 1 with InvocationContext

use of org.mobicents.tools.sip.balancer.InvocationContext in project load-balancer by RestComm.

the class HttpRequestHandler method handleHttpRequest.

private void handleHttpRequest(ChannelHandlerContext ctx, final MessageEvent e) throws Exception {
    String currAddress = e.getRemoteAddress().toString();
    semaphore = semaphoreMap.get(currAddress);
    if (semaphore == null) {
        semaphore = new Semaphore(1);
        Semaphore tempSemaphore = semaphoreMap.putIfAbsent(currAddress, semaphore);
        if (tempSemaphore != null)
            semaphore = tempSemaphore;
    }
    try {
        semaphore.acquire();
    } catch (InterruptedException ex) {
    }
    if (!readingChunks && e.getMessage() instanceof HttpRequest) {
        request = (HttpRequest) e.getMessage();
        if (logger.isDebugEnabled()) {
            logger.debug("Request URI accessed: " + request.getUri() + " channel " + e.getChannel());
        }
        if (HttpChannelAssociations.urlRewriteFilter != null)
            HttpChannelAssociations.urlRewriteFilter.doFilter(request, e);
        AdvancedChannel currentAC = HttpChannelAssociations.channels.get(new AdvancedChannel(e.getChannel()));
        Channel associatedChannel = null;
        if (currentAC != null)
            associatedChannel = currentAC.getChannel();
        InvocationContext invocationContext = balancerRunner.getLatestInvocationContext();
        // SIPNode node = null;
        try {
            // TODO: If WebSocket request, choose a NODE that is able to
            // handle WebSocket requests (has a websocket connector)
            node = invocationContext.balancerAlgorithm.processHttpRequest(request);
        } catch (Exception ex) {
            StringWriter sw = new StringWriter();
            ex.printStackTrace(new PrintWriter(sw));
            logger.warn("Problem in balancer algorithm", ex);
            writeResponse(e, HttpResponseStatus.INTERNAL_SERVER_ERROR, "Load Balancer Error: Exception in the balancer algorithm:\n" + sw.toString());
            return;
        }
        if (node == null) {
            if (logger.isInfoEnabled()) {
                logger.info("Service unavailable. No server is available.");
            }
            writeResponse(e, HttpResponseStatus.SERVICE_UNAVAILABLE, IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("500.html")));
            return;
        }
        if (associatedChannel != null && associatedChannel.isConnected()) {
            semaphore.release();
            associatedChannel.write(request);
        } else {
            e.getChannel().getCloseFuture().addListener(new ChannelFutureListener() {

                public void operationComplete(ChannelFuture arg0) throws Exception {
                    closeChannelPair(arg0.getChannel());
                }
            });
            // Start the connection attempt.
            ChannelFuture future = null;
            Set<String> headers = request.getHeaderNames();
            if (headers.contains("Sec-WebSocket-Protocol")) {
                if (request.getHeader("Sec-WebSocket-Protocol").equalsIgnoreCase("sip")) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("New SIP over WebSocket request. WebSocket uri: " + request.getUri());
                        logger.debug("Dispatching WebSocket request to node: " + node.getIp() + " port: " + node.getProperties().get("wsPort"));
                    }
                    wsrequest = true;
                    wsVersion = request.getHeader(Names.SEC_WEBSOCKET_VERSION);
                    websocketServerPipelineFactory = new WebsocketModifyClientPipelineFactory();
                    future = HttpChannelAssociations.inboundBootstrap.connect(new InetSocketAddress(node.getIp(), Integer.parseInt(node.getProperties().get("wsPort"))));
                }
            } else {
                if (!isSecured) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Dispatching HTTP request to node: " + node.getIp() + " port: " + node.getProperties().get("httpPort"));
                    }
                    future = HttpChannelAssociations.inboundBootstrap.connect(new InetSocketAddress(node.getIp(), Integer.parseInt(node.getProperties().get("httpPort"))));
                } else {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Dispatching HTTPS request to node: " + node.getIp() + " port: " + node.getProperties().get("sslPort"));
                    }
                    future = HttpChannelAssociations.inboundSecureBootstrap.connect(new InetSocketAddress(node.getIp(), Integer.parseInt(node.getProperties().get("sslPort"))));
                }
            }
            future.addListener(new ChannelFutureListener() {

                public void operationComplete(ChannelFuture arg0) throws Exception {
                    Channel channel = arg0.getChannel();
                    if (pattern != null && pattern.matcher(request.getUri()).find()) {
                        logger.info("request : " + request.getUri() + " matches to pattern : " + pattern);
                        HttpChannelAssociations.channels.put(new AdvancedChannel(e.getChannel(), true), new AdvancedChannel(channel, true));
                        HttpChannelAssociations.channels.put(new AdvancedChannel(channel, true), new AdvancedChannel(e.getChannel(), true));
                    } else {
                        HttpChannelAssociations.channels.put(new AdvancedChannel(e.getChannel(), false), new AdvancedChannel(channel, false));
                        HttpChannelAssociations.channels.put(new AdvancedChannel(channel, false), new AdvancedChannel(e.getChannel(), false));
                    }
                    if (request.isChunked()) {
                        readingChunks = true;
                    }
                    semaphore.release();
                    channel.write(request);
                    if (wsrequest) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("This is a websocket request, changing the pipeline");
                        }
                        // Modify the Client Pipeline - Phase 1
                        ChannelPipeline p = channel.getPipeline();
                        websocketServerPipelineFactory.upgradeClientPipelineFactoryPhase1(p, wsVersion);
                    }
                    channel.getCloseFuture().addListener(new ChannelFutureListener() {

                        public void operationComplete(ChannelFuture arg0) throws Exception {
                            closeChannelPair(arg0.getChannel());
                        }
                    });
                }
            });
        }
    } else {
        HttpChunk chunk = (HttpChunk) e.getMessage();
        if (chunk.isLast()) {
            readingChunks = false;
        }
        semaphore.release();
        HttpChannelAssociations.channels.get(new AdvancedChannel(e.getChannel())).getChannel().write(chunk);
        if (logger.isDebugEnabled())
            logger.debug("Send chunked request from : " + e.getChannel().getLocalAddress() + " to : " + e.getChannel().getRemoteAddress() + " capacity : " + chunk.getContent().capacity());
    }
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) ChannelFuture(org.jboss.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) Channel(org.jboss.netty.channel.Channel) Semaphore(java.util.concurrent.Semaphore) ChannelFutureListener(org.jboss.netty.channel.ChannelFutureListener) URISyntaxException(java.net.URISyntaxException) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) StringWriter(java.io.StringWriter) InvocationContext(org.mobicents.tools.sip.balancer.InvocationContext) PrintWriter(java.io.PrintWriter) HttpChunk(org.jboss.netty.handler.codec.http.HttpChunk)

Example 2 with InvocationContext

use of org.mobicents.tools.sip.balancer.InvocationContext in project load-balancer by RestComm.

the class HttpResponseHandler method handleHttpResponse.

private void handleHttpResponse(ChannelHandlerContext ctx, final MessageEvent e) throws Exception {
    if (e.getMessage() instanceof HttpChunkTrailer) {
        HttpChunkTrailer chunk = (HttpChunkTrailer) e.getMessage();
        balancerRunner.balancerContext.httpBytesToClient.addAndGet(chunk.getContent().capacity());
        if (chunk.isLast())
            readingChunks = false;
        AdvancedChannel ac = HttpChannelAssociations.channels.get(new AdvancedChannel(e.getChannel()));
        Channel channel = null;
        if (ac != null)
            channel = ac.getChannel();
        if (channel != null) {
            if (logger.isDebugEnabled())
                logger.debug("Send chunked response from : " + e.getChannel().getRemoteAddress() + " to : " + channel.getRemoteAddress() + " capacity : " + chunk.getContent().capacity());
            channel.write(chunk);
        }
    } else if (!readingChunks || !(e.getMessage() instanceof DefaultHttpChunk)) {
        response = (HttpResponse) e.getMessage();
        int stsusCode = response.getStatus().getCode();
        if (stsusCode > 399 && stsusCode < 600) {
            AdvancedChannel ac = HttpChannelAssociations.channels.get(new AdvancedChannel(e.getChannel()));
            if (ac != null && ac.isCheckNeed()) {
                InetSocketAddress address = (InetSocketAddress) e.getChannel().getRemoteAddress();
                InvocationContext invocationContext = balancerRunner.getLatestInvocationContext();
                KeySip keySip = new KeySip(address.getHostString(), address.getPort(), false);
                Node currNode = invocationContext.sipNodeMap(false).get(keySip);
                if (currNode != null) {
                    currNode.setBad(true);
                    String instanseId = currNode.getProperties().get(Protocol.RESTCOMM_INSTANCE_ID);
                    if (instanseId != null)
                        invocationContext.httpNodeMap.get(new KeyHttp(currNode.getProperties().get(Protocol.RESTCOMM_INSTANCE_ID))).setBad(true);
                    logger.error("Error code [" + stsusCode + "] detected in HTTP response. From  node : " + currNode + ". This node will marked as bad.");
                    String currInstanceId = (String) currNode.getProperties().get("Restcomm-Instance-Id");
                    if (currInstanceId != null)
                        logger.warn("Node : " + invocationContext.httpNodeMap.remove(new KeyHttp(currInstanceId)) + " from httpNodeMap");
                    // invocationContext.badSipNodeMap(false).put(keySip, currNode);
                    invocationContext.balancerAlgorithm.nodeRemoved(currNode);
                }
            // TODO CHECK REQUEST AND REMOVE NODE
            }
        }
        updateStatistic(response);
        balancerRunner.balancerContext.httpBytesToClient.addAndGet(response.getContent().capacity());
        if (response.isChunked()) {
            readingChunks = true;
        }
        AdvancedChannel ac = HttpChannelAssociations.channels.get(new AdvancedChannel(e.getChannel()));
        Channel channel = null;
        if (ac != null)
            channel = ac.getChannel();
        if (channel != null) {
            if (logger.isDebugEnabled())
                logger.debug("Send response from : " + e.getChannel().getRemoteAddress() + " to : " + channel.getRemoteAddress() + " capacity : " + response.getContent().capacity());
            channel.write(response);
        }
        Set<String> headers = response.getHeaderNames();
        if (headers.contains("Sec-WebSocket-Protocol")) {
            if (response.getHeader("Sec-WebSocket-Protocol").equalsIgnoreCase("sip")) {
                if (logger.isDebugEnabled()) {
                    logger.debug("WebSocket response");
                }
                wsVersion = response.getHeader(Names.SEC_WEBSOCKET_VERSION);
                // Modify the Server pipeline
                ChannelPipeline p = channel.getPipeline();
                websocketModifyServerPipelineFactory = new WebsocketModifyServerPipelineFactory();
                websocketModifyServerPipelineFactory.upgradeServerPipelineFactory(p, wsVersion);
            }
        }
    } else {
        HttpChunk chunk = (HttpChunk) e.getMessage();
        balancerRunner.balancerContext.httpBytesToClient.addAndGet(chunk.getContent().capacity());
        if (chunk.isLast())
            readingChunks = false;
        AdvancedChannel ac = HttpChannelAssociations.channels.get(new AdvancedChannel(e.getChannel()));
        Channel channel = null;
        if (ac != null)
            channel = ac.getChannel();
        if (channel != null) {
            if (logger.isDebugEnabled())
                logger.debug("Send chunked response from : " + e.getChannel().getRemoteAddress() + " to : " + channel.getRemoteAddress() + " capacity : " + chunk.getContent().capacity());
            channel.write(chunk);
        }
    }
}
Also used : Set(java.util.Set) DefaultHttpChunk(org.jboss.netty.handler.codec.http.DefaultHttpChunk) InetSocketAddress(java.net.InetSocketAddress) Channel(org.jboss.netty.channel.Channel) Node(org.mobicents.tools.heartbeat.api.Node) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) HttpChunkTrailer(org.jboss.netty.handler.codec.http.HttpChunkTrailer) KeySip(org.mobicents.tools.sip.balancer.KeySip) InvocationContext(org.mobicents.tools.sip.balancer.InvocationContext) KeyHttp(org.mobicents.tools.sip.balancer.KeyHttp) DefaultHttpChunk(org.jboss.netty.handler.codec.http.DefaultHttpChunk) HttpChunk(org.jboss.netty.handler.codec.http.HttpChunk)

Aggregations

InetSocketAddress (java.net.InetSocketAddress)2 Channel (org.jboss.netty.channel.Channel)2 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)2 HttpChunk (org.jboss.netty.handler.codec.http.HttpChunk)2 InvocationContext (org.mobicents.tools.sip.balancer.InvocationContext)2 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 URISyntaxException (java.net.URISyntaxException)1 Set (java.util.Set)1 Semaphore (java.util.concurrent.Semaphore)1 ChannelFuture (org.jboss.netty.channel.ChannelFuture)1 ChannelFutureListener (org.jboss.netty.channel.ChannelFutureListener)1 DefaultHttpChunk (org.jboss.netty.handler.codec.http.DefaultHttpChunk)1 HttpChunkTrailer (org.jboss.netty.handler.codec.http.HttpChunkTrailer)1 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)1 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)1 Node (org.mobicents.tools.heartbeat.api.Node)1 KeyHttp (org.mobicents.tools.sip.balancer.KeyHttp)1 KeySip (org.mobicents.tools.sip.balancer.KeySip)1