Search in sources :

Example 46 with SipURI

use of javax.sip.address.SipURI in project XobotOS by xamarin.

the class SIPClientTransaction method createAck.

/*
     * (non-Javadoc)
     * 
     * @see javax.sip.ClientTransaction#createAck()
     */
public Request createAck() throws SipException {
    SIPRequest originalRequest = this.getOriginalRequest();
    if (originalRequest == null)
        throw new SipException("bad state " + getState());
    if (getMethod().equalsIgnoreCase(Request.ACK)) {
        throw new SipException("Cannot ACK an ACK!");
    } else if (lastResponse == null) {
        throw new SipException("bad Transaction state");
    } else if (lastResponse.getStatusCode() < 200) {
        if (sipStack.isLoggingEnabled()) {
            sipStack.getStackLogger().logDebug("lastResponse = " + lastResponse);
        }
        throw new SipException("Cannot ACK a provisional response!");
    }
    SIPRequest ackRequest = originalRequest.createAckRequest((To) lastResponse.getTo());
    // Pull the record route headers from the last reesponse.
    RecordRouteList recordRouteList = lastResponse.getRecordRouteHeaders();
    if (recordRouteList == null) {
        // request URI.
        if (lastResponse.getContactHeaders() != null && lastResponse.getStatusCode() / 100 != 3) {
            Contact contact = (Contact) lastResponse.getContactHeaders().getFirst();
            javax.sip.address.URI uri = (javax.sip.address.URI) contact.getAddress().getURI().clone();
            ackRequest.setRequestURI(uri);
        }
        return ackRequest;
    }
    ackRequest.removeHeader(RouteHeader.NAME);
    RouteList routeList = new RouteList();
    // start at the end of the list and walk backwards
    ListIterator<RecordRoute> li = recordRouteList.listIterator(recordRouteList.size());
    while (li.hasPrevious()) {
        RecordRoute rr = (RecordRoute) li.previous();
        Route route = new Route();
        route.setAddress((AddressImpl) ((AddressImpl) rr.getAddress()).clone());
        route.setParameters((NameValueList) rr.getParameters().clone());
        routeList.add(route);
    }
    Contact contact = null;
    if (lastResponse.getContactHeaders() != null) {
        contact = (Contact) lastResponse.getContactHeaders().getFirst();
    }
    if (!((SipURI) ((Route) routeList.getFirst()).getAddress().getURI()).hasLrParam()) {
        // Contact may not yet be there (bug reported by Andreas B).
        Route route = null;
        if (contact != null) {
            route = new Route();
            route.setAddress((AddressImpl) ((AddressImpl) (contact.getAddress())).clone());
        }
        Route firstRoute = (Route) routeList.getFirst();
        routeList.removeFirst();
        javax.sip.address.URI uri = firstRoute.getAddress().getURI();
        ackRequest.setRequestURI(uri);
        if (route != null)
            routeList.add(route);
        ackRequest.addHeader(routeList);
    } else {
        if (contact != null) {
            javax.sip.address.URI uri = (javax.sip.address.URI) contact.getAddress().getURI().clone();
            ackRequest.setRequestURI(uri);
            ackRequest.addHeader(routeList);
        }
    }
    return ackRequest;
}
Also used : RecordRouteList(gov.nist.javax.sip.header.RecordRouteList) RouteList(gov.nist.javax.sip.header.RouteList) SIPRequest(gov.nist.javax.sip.message.SIPRequest) SipURI(javax.sip.address.SipURI) Contact(gov.nist.javax.sip.header.Contact) RecordRoute(gov.nist.javax.sip.header.RecordRoute) RecordRouteList(gov.nist.javax.sip.header.RecordRouteList) AddressImpl(gov.nist.javax.sip.address.AddressImpl) SipException(javax.sip.SipException) RecordRoute(gov.nist.javax.sip.header.RecordRoute) Route(gov.nist.javax.sip.header.Route)

Example 47 with SipURI

use of javax.sip.address.SipURI in project XobotOS by xamarin.

the class SipHelper method createRequest.

private Request createRequest(String requestType, SipProfile caller, SipProfile callee, String tag) throws ParseException, SipException {
    FromHeader fromHeader = createFromHeader(caller, tag);
    ToHeader toHeader = createToHeader(callee);
    SipURI requestURI = callee.getUri();
    List<ViaHeader> viaHeaders = createViaHeaders();
    CallIdHeader callIdHeader = createCallIdHeader();
    CSeqHeader cSeqHeader = createCSeqHeader(requestType);
    MaxForwardsHeader maxForwards = createMaxForwardsHeader();
    Request request = mMessageFactory.createRequest(requestURI, requestType, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwards);
    request.addHeader(createContactHeader(caller));
    return request;
}
Also used : CSeqHeader(javax.sip.header.CSeqHeader) MaxForwardsHeader(javax.sip.header.MaxForwardsHeader) ViaHeader(javax.sip.header.ViaHeader) FromHeader(javax.sip.header.FromHeader) ToHeader(javax.sip.header.ToHeader) Request(javax.sip.message.Request) CallIdHeader(javax.sip.header.CallIdHeader) SipURI(javax.sip.address.SipURI)

Example 48 with SipURI

use of javax.sip.address.SipURI in project load-balancer by RestComm.

the class PureConsistentHashNodeDeathTest method testOKRingingLandOnDifferentNode.

@Test
public void testOKRingingLandOnDifferentNode() throws Exception {
    EventListener failureEventListener = new EventListener() {

        @Override
        public void uasAfterResponse(int statusCode, AppServer source) {
        }

        @Override
        public void uasAfterRequestReceived(String method, AppServer source) {
        // TODO Auto-generated method stub
        }

        @Override
        public void uacAfterRequestSent(String method, AppServer source) {
        // TODO Auto-generated method stub
        }

        @Override
        public void uacAfterResponse(int statusCode, AppServer source) {
            if (statusCode == 180) {
                ringingAppServer = source;
                // source.sendCleanShutdownToBalancers();
                source.stop();
                Helper.sleep(1000);
            } else {
                okAppServer = source;
            }
        }
    };
    for (AppServer as : servers) as.setEventListener(failureEventListener);
    shootist.callerSendsBye = true;
    String fromName = "sender";
    String fromHost = "sip-servlets.com";
    SipURI fromAddress = servers[0].protocolObjects.addressFactory.createSipURI(fromName, fromHost);
    String toUser = "replaces";
    String toHost = "sip-servlets.com";
    SipURI toAddress = servers[0].protocolObjects.addressFactory.createSipURI(toUser, toHost);
    SipURI ruri = servers[0].protocolObjects.addressFactory.createSipURI("usera", "127.0.0.1:5033");
    ruri.setLrParam();
    SipURI route = servers[0].protocolObjects.addressFactory.createSipURI("lbint", "127.0.0.1:5065");
    route.setParameter("node_host", "127.0.0.1");
    route.setParameter("node_port", "4060");
    route.setLrParam();
    shootist.start();
    // servers[0].sipListener.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
    servers[0].sipListener.sendSipRequest("INVITE", fromAddress, toAddress, null, route, false, null, null, ruri);
    Thread.sleep(20000);
    assertTrue(shootist.inviteRequest.getHeader(RecordRouteHeader.NAME).toString().contains("node_host"));
    assertNotSame(ringingAppServer, okAppServer);
    assertNotNull(ringingAppServer);
    assertNotNull(okAppServer);
}
Also used : AppServer(org.mobicents.tools.sip.balancer.AppServer) EventListener(org.mobicents.tools.sip.balancer.EventListener) SipURI(javax.sip.address.SipURI) Test(org.junit.Test)

Example 49 with SipURI

use of javax.sip.address.SipURI in project load-balancer by RestComm.

the class WorstCaseAffinityTest method testOKRingingLandOnSameNode.

@Test
public void testOKRingingLandOnSameNode() throws Exception {
    EventListener failureEventListener = new EventListener() {

        @Override
        public void uasAfterResponse(int statusCode, AppServer source) {
        }

        @Override
        public void uasAfterRequestReceived(String method, AppServer source) {
        // TODO Auto-generated method stub
        }

        @Override
        public void uacAfterRequestSent(String method, AppServer source) {
        // TODO Auto-generated method stub
        }

        @Override
        public void uacAfterResponse(int statusCode, AppServer source) {
            if (statusCode == 180) {
                ringingAppServer = source;
            } else {
                okAppServer = source;
            }
        }
    };
    for (AppServer as : servers) as.setEventListener(failureEventListener);
    shootist.callerSendsBye = true;
    String fromName = "sender";
    String fromHost = "sip-servlets.com";
    SipURI fromAddress = servers[0].protocolObjects.addressFactory.createSipURI(fromName, fromHost);
    String toUser = "replaces";
    String toHost = "sip-servlets.com";
    SipURI toAddress = servers[0].protocolObjects.addressFactory.createSipURI(toUser, toHost);
    SipURI ruri = servers[0].protocolObjects.addressFactory.createSipURI("usera", "127.0.0.1:5033");
    ruri.setLrParam();
    SipURI route = servers[0].protocolObjects.addressFactory.createSipURI("lbint", "127.0.0.1:5065");
    route.setParameter("node_host", "127.0.0.1");
    route.setParameter("node_port", "4060");
    route.setTransportParam(ListeningPoint.UDP);
    route.setLrParam();
    shootist.start();
    // servers[0].sipListener.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
    servers[0].sipListener.sendSipRequest("INVITE", fromAddress, toAddress, null, route, false, null, null, ruri);
    Thread.sleep(16000);
    assertTrue(shootist.inviteRequest.getHeader(RecordRouteHeader.NAME).toString().contains("node_host"));
    assertSame(ringingAppServer, okAppServer);
    assertNotNull(ringingAppServer);
    assertNotNull(okAppServer);
}
Also used : AppServer(org.mobicents.tools.sip.balancer.AppServer) EventListener(org.mobicents.tools.sip.balancer.EventListener) SipURI(javax.sip.address.SipURI) Test(org.junit.Test)

Example 50 with SipURI

use of javax.sip.address.SipURI 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)

Aggregations

SipURI (javax.sip.address.SipURI)70 ParseException (java.text.ParseException)27 Request (javax.sip.message.Request)24 Address (javax.sip.address.Address)22 InvalidArgumentException (javax.sip.InvalidArgumentException)20 ViaHeader (javax.sip.header.ViaHeader)20 SipException (javax.sip.SipException)19 ToHeader (javax.sip.header.ToHeader)18 ContactHeader (javax.sip.header.ContactHeader)17 FromHeader (javax.sip.header.FromHeader)17 Test (org.junit.Test)17 RouteHeader (javax.sip.header.RouteHeader)16 AppServer (org.mobicents.tools.sip.balancer.AppServer)16 EventListener (org.mobicents.tools.sip.balancer.EventListener)16 CSeqHeader (javax.sip.header.CSeqHeader)13 CallIdHeader (javax.sip.header.CallIdHeader)13 Header (javax.sip.header.Header)13 MaxForwardsHeader (javax.sip.header.MaxForwardsHeader)13 RecordRouteHeader (javax.sip.header.RecordRouteHeader)13 URI (javax.sip.address.URI)11