Search in sources :

Example 31 with Node

use of org.mobicents.tools.heartbeat.api.Node in project load-balancer by RestComm.

the class RingingFailoverTest method testViaHeaderRewrite.

@Test
public void testViaHeaderRewrite() throws Exception, ParseException {
    CallIDAffinityBalancerAlgorithm algorithm = new CallIDAffinityBalancerAlgorithm();
    Response response = SipFactory.getInstance().createMessageFactory().createResponse(ringing);
    String node = "1.2.3.4";
    Integer port = 1234;
    Node adNode = new Node(node, node);
    adNode.getProperties().put("udpPort", "" + port);
    algorithm.balancerContext = new BalancerContext();
    algorithm.balancerContext.algorithmClassName = CallIDAffinityBalancerAlgorithm.class.getName();
    algorithm.balancerContext.smppToNodeAlgorithmClassName = SmppToNodeRoundRobinAlgorithm.class.getName();
    algorithm.balancerContext.smppToProviderAlgorithmClassName = SmppToProviderRoundRobinAlgorithm.class.getName();
    algorithm.setConfiguration(new LoadBalancerConfiguration());
    InvocationContext ctx = new InvocationContext("0", algorithm.balancerContext);
    ctx.sipNodeMap(false).put(new KeySip(adNode, false), adNode);
    algorithm.invocationContext = ctx;
    algorithm.processExternalResponse(response, false);
    algorithm.stop();
    assertTrue(response.toString().contains(node + ":" + port));
}
Also used : Response(javax.sip.message.Response) SmppToNodeRoundRobinAlgorithm(org.mobicents.tools.smpp.multiplexer.SmppToNodeRoundRobinAlgorithm) Node(org.mobicents.tools.heartbeat.api.Node) LoadBalancerConfiguration(org.mobicents.tools.configuration.LoadBalancerConfiguration) CallIDAffinityBalancerAlgorithm(org.mobicents.tools.sip.balancer.CallIDAffinityBalancerAlgorithm) BalancerContext(org.mobicents.tools.sip.balancer.BalancerContext) SmppToProviderRoundRobinAlgorithm(org.mobicents.tools.smpp.multiplexer.SmppToProviderRoundRobinAlgorithm) Test(org.junit.Test)

Example 32 with Node

use of org.mobicents.tools.heartbeat.api.Node in project load-balancer by RestComm.

the class UserSpace method remoteServersUpdated.

public void remoteServersUpdated() {
    List<Node> usedNodes = new ArrayList<Node>();
    for (Entry<Long, MClientConnectionImpl> entryConnections : connectionsToServers.entrySet()) usedNodes.add(entryConnections.getValue().getNode());
    List<Node> updatedNodes = new ArrayList<Node>(ctx.smppNodeMap.values());
    ListDiff<Node> diff = ListUtil.diff(usedNodes, updatedNodes);
    List<Node> addedNodes = diff.getAdded();
    List<Node> removedNodes = diff.getRemoved();
    for (Node node : addedNodes) {
        connectionsToServers.put(serverSessionID, new MClientConnectionImpl(serverSessionID, this, monitorExecutor, balancerRunner, node, isSslConnection));
        monitorExecutor.execute(new MBinderRunnable(connectionsToServers.get(serverSessionID), systemId, password, systemType));
        serverSessionID++;
    }
    for (Node node : removedNodes) {
        for (Entry<Long, MClientConnectionImpl> entry : connectionsToServers.entrySet()) {
            if (node.equals(entry.getValue().getNode()))
                connectionsToServers.remove(entry.getKey());
        }
    }
}
Also used : Node(org.mobicents.tools.heartbeat.api.Node) ArrayList(java.util.ArrayList)

Example 33 with Node

use of org.mobicents.tools.heartbeat.api.Node in project load-balancer by RestComm.

the class HttpServer method start.

public void start() {
    executor = Executors.newCachedThreadPool();
    nioServerSocketChannelFactory = new NioServerSocketChannelFactory(executor, executor);
    serverBootstrap = new ServerBootstrap(nioServerSocketChannelFactory);
    serverBootstrap.setPipelineFactory(new TestHttpServerPipelineFactory(true, requestCount, requests, chunkedresponse, badSever));
    serverChannel = serverBootstrap.bind(new InetSocketAddress("127.0.0.1", httpPort));
    serverSecureBootstrap = new ServerBootstrap(nioServerSocketChannelFactory);
    serverSecureBootstrap.setPipelineFactory(new TestHttpServerPipelineFactory(false, requestCount, requests, chunkedresponse, badSever));
    serverSecureChannel = serverSecureBootstrap.bind(new InetSocketAddress("127.0.0.1", sslPort));
    // ping
    node = new Node("HttpServer", "127.0.0.1");
    node.getProperties().put("version", "0");
    node.getProperties().put("httpPort", "" + httpPort);
    node.getProperties().put("udpPort", "" + udpPort);
    node.getProperties().put("sslPort", "" + sslPort);
    node.getProperties().put(Protocol.SESSION_ID, "" + System.currentTimeMillis());
    node.getProperties().put(Protocol.HEARTBEAT_PORT, "" + heartbeatPort);
    if (instanceId != null)
        node.getProperties().put("Restcomm-Instance-Id", instanceId);
    clientController = new ClientController(this, lbAddress, lbPort, node, 5000, heartbeatPeriod, executor);
    clientController.startClient();
}
Also used : NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) InetSocketAddress(java.net.InetSocketAddress) Node(org.mobicents.tools.heartbeat.api.Node) ClientController(org.mobicents.tools.heartbeat.impl.ClientController) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap)

Example 34 with Node

use of org.mobicents.tools.heartbeat.api.Node in project load-balancer by RestComm.

the class UserBasedAlgorithm method nextAvailableNode.

protected synchronized Node nextAvailableNode(Boolean isIpV6) {
    if (invocationContext.sipNodeMap(isIpV6).size() == 0)
        return null;
    Iterator<Entry<KeySip, Node>> currIt = null;
    if (isIpV6)
        currIt = ipv6It;
    else
        currIt = ipv4It;
    if (currIt == null) {
        currIt = invocationContext.sipNodeMap(isIpV6).entrySet().iterator();
        if (isIpV6)
            ipv6It = currIt;
        else
            ipv4It = currIt;
    }
    Entry<KeySip, Node> pair = null;
    int count = invocationContext.sipNodeMap(isIpV6).size();
    while (count > 0) {
        while (currIt.hasNext() && count > 0) {
            pair = currIt.next();
            if (invocationContext.sipNodeMap(isIpV6).containsKey(pair.getKey()) && !invocationContext.sipNodeMap(isIpV6).get(pair.getKey()).isGracefulShutdown() && !invocationContext.sipNodeMap(isIpV6).get(pair.getKey()).isBad()) {
                return pair.getValue();
            } else {
                count--;
            }
        }
        if (!currIt.hasNext())
            currIt = invocationContext.sipNodeMap(isIpV6).entrySet().iterator();
        if (isIpV6)
            ipv6It = currIt;
        else
            ipv4It = currIt;
    }
    return null;
}
Also used : Entry(java.util.Map.Entry) Node(org.mobicents.tools.heartbeat.api.Node) ListeningPoint(javax.sip.ListeningPoint)

Example 35 with Node

use of org.mobicents.tools.heartbeat.api.Node in project load-balancer by RestComm.

the class UserBasedAlgorithm method processExternalResponse.

@Override
public void processExternalResponse(Response response, Boolean isIpV6) {
    Via via = (Via) response.getHeader(Via.NAME);
    String transport = via.getTransport().toLowerCase();
    String host = via.getHost();
    Integer port = via.getPort();
    boolean found = false;
    if (invocationContext.sipNodeMap(isIpV6).containsKey(new KeySip(host, port, isIpV6)))
        found = true;
    if (logger.isDebugEnabled()) {
        logger.debug("external response node found ? " + found);
    }
    if (!found) {
        String headerKey = extractHeaderKey(response);
        Node node = userToMap.get(headerKey);
        // if(node == null || !invocationContext.nodes.contains(node)) {
        if (node == null || !invocationContext.sipNodeMap(isIpV6).containsValue(node)) {
            node = selectNewNode(node, headerKey, isIpV6);
            String transportProperty = transport + "Port";
            port = Integer.parseInt(node.getProperties().get(transportProperty));
            if (port == null)
                throw new RuntimeException("No transport found for node " + node + " " + transportProperty);
            if (logger.isDebugEnabled()) {
                logger.debug("changing via " + via + "setting new values " + node.getIp() + ":" + port);
            }
            try {
                via.setHost(node.getIp());
                via.setPort(port);
            } catch (Exception e) {
                throw new RuntimeException("Error setting new values " + node.getIp() + ":" + port + " on via " + via, e);
            }
            // need to reset the rport for reliable transports
            if (!ListeningPoint.UDP.equalsIgnoreCase(transport)) {
                via.setRPort();
            }
        } else {
            String transportProperty = transport + "Port";
            port = Integer.parseInt(node.getProperties().get(transportProperty));
            if (via.getHost().equalsIgnoreCase(node.getIp()) || via.getPort() != port) {
                if (logger.isDebugEnabled()) {
                    logger.debug("changing retransmission via " + via + "setting new values " + node.getIp() + ":" + port);
                }
                try {
                    via.setHost(node.getIp());
                    via.setPort(port);
                    via.removeParameter("rport");
                    via.removeParameter("received");
                } catch (Exception e) {
                    throw new RuntimeException("Error setting new values " + node.getIp() + ":" + port + " on via " + via, e);
                }
                // need to reset the rport for reliable transports
                if (!ListeningPoint.UDP.equalsIgnoreCase(transport)) {
                    via.setRPort();
                }
            }
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Node(org.mobicents.tools.heartbeat.api.Node) Via(gov.nist.javax.sip.header.Via)

Aggregations

Node (org.mobicents.tools.heartbeat.api.Node)70 ListeningPoint (javax.sip.ListeningPoint)19 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 Via (gov.nist.javax.sip.header.Via)7 HashMap (java.util.HashMap)7 SIPHeader (gov.nist.javax.sip.header.SIPHeader)6 ParseException (java.text.ParseException)6 ArrayList (java.util.ArrayList)6 ViaHeader (javax.sip.header.ViaHeader)6 RouteHeader (javax.sip.header.RouteHeader)5 SIPResponse (gov.nist.javax.sip.message.SIPResponse)4 UnknownHostException (java.net.UnknownHostException)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 InvalidArgumentException (javax.sip.InvalidArgumentException)4 SipException (javax.sip.SipException)4 SipURI (javax.sip.address.SipURI)4 RecordRouteHeader (javax.sip.header.RecordRouteHeader)4 ToHeader (javax.sip.header.ToHeader)4 Response (javax.sip.message.Response)4 Test (org.junit.Test)4