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));
}
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());
}
}
}
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();
}
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;
}
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();
}
}
}
}
}
Aggregations