Search in sources :

Example 1 with ListeningPointExt

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();
    }
}
Also used : ContactHeader(javax.sip.header.ContactHeader) RouteHeader(javax.sip.header.RouteHeader) RecordRouteHeader(javax.sip.header.RecordRouteHeader) DialogState(javax.sip.DialogState) FromHeader(javax.sip.header.FromHeader) ClientTransaction(javax.sip.ClientTransaction) Request(javax.sip.message.Request) SipURI(javax.sip.address.SipURI) InvalidArgumentException(javax.sip.InvalidArgumentException) ParseException(java.text.ParseException) SipException(javax.sip.SipException) ListeningPointExt(gov.nist.javax.sip.ListeningPointExt) Random(java.util.Random) ViaHeader(javax.sip.header.ViaHeader) Dialog(javax.sip.Dialog) ServerTransaction(javax.sip.ServerTransaction)

Example 2 with ListeningPointExt

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);
    }
}
Also used : Response(javax.sip.message.Response) ContactHeader(javax.sip.header.ContactHeader) ListeningPointExt(gov.nist.javax.sip.ListeningPointExt) InvalidArgumentException(javax.sip.InvalidArgumentException) Request(javax.sip.message.Request) ListeningPoint(javax.sip.ListeningPoint) ParseException(java.text.ParseException) ServerTransaction(javax.sip.ServerTransaction) SipException(javax.sip.SipException)

Aggregations

ListeningPointExt (gov.nist.javax.sip.ListeningPointExt)2 ParseException (java.text.ParseException)2 InvalidArgumentException (javax.sip.InvalidArgumentException)2 ServerTransaction (javax.sip.ServerTransaction)2 SipException (javax.sip.SipException)2 ContactHeader (javax.sip.header.ContactHeader)2 Request (javax.sip.message.Request)2 Random (java.util.Random)1 ClientTransaction (javax.sip.ClientTransaction)1 Dialog (javax.sip.Dialog)1 DialogState (javax.sip.DialogState)1 ListeningPoint (javax.sip.ListeningPoint)1 SipURI (javax.sip.address.SipURI)1 FromHeader (javax.sip.header.FromHeader)1 RecordRouteHeader (javax.sip.header.RecordRouteHeader)1 RouteHeader (javax.sip.header.RouteHeader)1 ViaHeader (javax.sip.header.ViaHeader)1 Response (javax.sip.message.Response)1