use of gov.nist.javax.sip.ListeningPointExt in project load-balancer by RestComm.
the class BackToBackUserAgent method processRequest.
public void processRequest(RequestEvent requestEvent) {
try {
Request request = requestEvent.getRequest();
if (request.getMethod().equals(Request.INVITE)) {
ServerTransaction st = sp.getNewServerTransaction(requestEvent.getRequest());
incomingDialog = st.getDialog();
try {
replyToRequestEvent(requestEvent.getRequest(), st, Response.TRYING);
Request newRequest = (Request) request.clone();
newRequest.removeHeader(RouteHeader.NAME);
newRequest.removeHeader(RecordRouteHeader.NAME);
FromHeader fromHeader = (FromHeader) newRequest.getHeader(FromHeader.NAME);
fromHeader.setTag(Long.toString(Math.abs(new Random().nextLong())));
ViaHeader viaHeader = ((ListeningPointExt) sp.getListeningPoint(transport)).createViaHeader();
newRequest.setHeader(viaHeader);
ContactHeader contactHeader = ((ListeningPointExt) sp.getListeningPoint(transport)).createContactHeader();
newRequest.setHeader(contactHeader);
SipURI route = this.protocolObjects.addressFactory.createSipURI("lbint", lbAddress + ":" + lbPort);
route.setParameter("node_host", "127.0.0.1");
route.setParameter("node_port", "" + port);
route.setLrParam();
RouteHeader routeHeader = (RouteHeader) this.protocolObjects.headerFactory.createRouteHeader(this.protocolObjects.addressFactory.createAddress(route));
newRequest.setHeader(routeHeader);
clientTransaction = sp.getNewClientTransaction(newRequest);
outgoingDialog = clientTransaction.getDialog();
clientTransaction.setApplicationData(st);
clientTransaction.sendRequest();
} catch (Throwable e) {
e.printStackTrace();
replyToRequestEvent(request, st, Response.SERVICE_UNAVAILABLE);
}
} else if (request.getMethod().equals(Request.BYE)) {
ServerTransaction st = requestEvent.getServerTransaction();
replyToRequestEvent(requestEvent.getRequest(), st, Response.OK);
Dialog peerDialog = getPeerDialog(requestEvent.getDialog());
Request outgoingRequest = peerDialog.createRequest(requestEvent.getRequest().getMethod());
final ClientTransaction ct = sp.getNewClientTransaction(outgoingRequest);
peerDialog.sendRequest(ct);
} else if (request.getMethod().equals(Request.CANCEL)) {
try {
final Dialog peerDialog = outgoingDialog;
final DialogState peerDialogState = peerDialog.getState();
if (peerDialogState == null || peerDialogState == DialogState.EARLY) {
Request cancelRequest = clientTransaction.createCancel();
sp.sendRequest(cancelRequest);
} else {
clientTransaction = sp.getNewClientTransaction(peerDialog.createRequest(Request.BYE));
clientTransaction.sendRequest();
}
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
use of gov.nist.javax.sip.ListeningPointExt in project load-balancer by RestComm.
the class BackToBackUserAgent method forwardResponse.
private void forwardResponse(ResponseEvent receivedResponse) throws SipException {
try {
ServerTransaction serverTransaction = (ServerTransaction) receivedResponse.getClientTransaction().getApplicationData();
Request stRequest = serverTransaction.getRequest();
Response newResponse = this.messageFactory.createResponse(receivedResponse.getResponse().getStatusCode(), stRequest);
ListeningPoint peerListeningPoint = sp.getListeningPoint(transport);
ContactHeader peerContactHeader = ((ListeningPointExt) peerListeningPoint).createContactHeader();
newResponse.setHeader(peerContactHeader);
serverTransaction.sendResponse(newResponse);
} catch (InvalidArgumentException e) {
throw new SipException("invalid response", e);
} catch (ParseException e) {
throw new SipException("invalid response", e);
}
}
Aggregations