use of org.mobicents.tools.sip.balancer.KeySip in project load-balancer by RestComm.
the class HttpRequestHandler method addRegex.
private boolean addRegex(MessageEvent e) {
String regex = getUrlParameters(((HttpRequest) e.getMessage()).getUri()).get("regex");
String ip = getUrlParameters(((HttpRequest) e.getMessage()).getUri()).get("ip");
String port = getUrlParameters(((HttpRequest) e.getMessage()).getUri()).get("port");
boolean isIpV6 = false;
if (ip != null)
isIpV6 = LbUtils.isValidInet6Address(ip);
logger.info("regex : " + regex + " IP :" + ip);
if (regex != null && ip != null && port != null) {
KeySip keySip = new KeySip(ip, Integer.parseInt(port), isIpV6);
if (balancerRunner.balancerContext.regexMap == null) {
balancerRunner.balancerContext.regexMap = new ConcurrentHashMap<String, KeySip>();
balancerRunner.balancerContext.regexMap.put(regex, keySip);
return true;
} else {
balancerRunner.balancerContext.regexMap.put(regex, keySip);
return true;
}
} else if (regex != null) {
KeySip keySip = balancerRunner.balancerContext.regexMap.remove(regex);
if (keySip != null) {
logger.info("regex removed from map : " + regex);
return true;
} else {
logger.info("regex did not remove from map : " + regex);
return false;
}
} else
return false;
}
use of org.mobicents.tools.sip.balancer.KeySip 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);
}
}
}
Aggregations