use of gov.nist.javax.sip.header.SIPHeader in project load-balancer by RestComm.
the class WorstCaseUdpTestAffinityAlgorithm method processAssignedExternalRequest.
public Node processAssignedExternalRequest(Request request, Node assignedNode) {
Boolean isIpV6 = LbUtils.isValidInet6Address(assignedNode.getIp());
// if((y++)%2==0) if(request.getHeader("CSeq").toString().contains("1")) return assignedNode;
String callId = ((SIPHeader) request.getHeader(headerName)).getValue();
CSeqHeader cs = (CSeqHeader) request.getHeader(CSeqHeader.NAME);
long cseq = cs.getSeqNumber();
if (callIdMap.get(callId) != null) {
assignedNode = callIdMap.get(callId);
}
ViaHeader via = (ViaHeader) request.getHeader(Via.NAME);
String transport = via.getTransport().toLowerCase();
RouteHeader route = (RouteHeader) request.getHeader(RouteHeader.NAME);
SipURI uri = null;
if (route != null) {
uri = (SipURI) route.getAddress().getURI();
} else {
uri = (SipURI) request.getRequestURI();
}
try {
Node node;
if (!request.getMethod().equalsIgnoreCase("ACK")) {
// Gvag: new transaction should go to a new node
// getNodeA(callId+cseq);
Node newNode = nextAvailableNode(isIpV6);
if (newNode == null) {
// for(Node currNode:invocationContext.nodes) {
for (Node currNode : invocationContext.sipNodeMap(isIpV6).values()) {
if (!currNode.equals(assignedNode)) {
newNode = currNode;
}
}
}
node = newNode;
} else
node = assignedNode;
uri.setHost(node.getIp());
if (balancerContext.internalTransport != null) {
transport = balancerContext.internalTransport.toLowerCase();
} else if (balancerContext.terminateTLSTraffic)
if (transport.equalsIgnoreCase(ListeningPoint.TLS))
transport = ListeningPoint.TCP.toLowerCase();
else if (transport.equalsIgnoreCase(ListeningPointExt.WSS))
transport = ListeningPointExt.WS.toLowerCase();
Integer port = Integer.parseInt(node.getProperties().get(transport + "Port"));
uri.setPort(port);
callIdMap.put(callId, node);
setNodeA(callId + cseq, node);
if (request.getRequestURI().isSipURI()) {
SipURI ruri = (SipURI) request.getRequestURI();
String rurihostid = ruri.getHost() + ruri.getPort();
String originalhostid = assignedNode.getIp() + assignedNode.getProperties().get(transport + "Port");
if (rurihostid.equals(originalhostid)) {
ruri.setPort(port);
ruri.setHost(node.getIp());
}
}
return node;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return assignedNode;
}
use of gov.nist.javax.sip.header.SIPHeader in project load-balancer by RestComm.
the class WorstCaseUdpTestAffinityAlgorithm method processExternalRequest.
public Node processExternalRequest(Request request, Boolean isIpV6) {
String callId = ((SIPHeader) request.getHeader(headerName)).getValue();
Node node;
CSeqHeader cs = (CSeqHeader) request.getHeader(CSeqHeader.NAME);
long cseq = cs.getSeqNumber();
node = callIdMap.get(callId);
callIdTimestamps.put(callId, System.currentTimeMillis());
if (node == null) {
//
node = nextAvailableNode(isIpV6);
if (node == null)
return null;
callIdMap.put(callId, node);
if (logger.isDebugEnabled()) {
logger.debug("No node found in the affinity map. It is null. We select new node: " + node);
}
} else {
// if(!invocationContext.nodes.contains(node)) { // If the assigned node is now dead
if (!invocationContext.sipNodeMap(isIpV6).containsValue(node)) {
// If the assigned node is now dead
node = selectNewNode(node, callId);
} else {
// .. and we just leave it like that
if (logger.isDebugEnabled()) {
logger.debug("The assigned node in the affinity map is still alive: " + node);
}
if (!request.getMethod().equals("ACK")) {
// for(Node n:invocationContext.nodes) {
for (Node n : invocationContext.sipNodeMap(isIpV6).values()) {
if (!n.equals(node))
node = n;
break;
}
}
}
}
setNodeA(callId + cseq, node);
callIdMap.put(callId, node);
// }
return node;
}
use of gov.nist.javax.sip.header.SIPHeader in project load-balancer by RestComm.
the class CallIDAffinityBalancerAlgorithm method processExternalResponse.
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 callId = ((SIPHeader) response.getHeader(headerName)).getValue();
Node node = callIdMap.get(callId);
// if(node == null || !invocationContext.nodes.contains(node)) {
if (node == null || !invocationContext.sipNodeMap(isIpV6).containsValue(node)) {
node = selectNewNode(node, callId, 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