Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package test.tck.msgflow.callflows.refer;
  
  
  import javax.sip.Dialog;
 
 
This example shows an out-of-dialog REFER scenario: referer sends REFER to referee, with Refer-To set to Shootme referee sends INVITE to Shootme, and NOTIFYs to referer about call progress

Author(s):
Jeroen van Bemmel
Ivelin Ivanov
 
 
 public class Referrer implements SipListener {
 
     private SipProvider sipProvider;
 
     private AddressFactory addressFactory;
 
     private MessageFactory messageFactory;
 
     private HeaderFactory headerFactory;
 
     private SipStack sipStack;
 
     private ContactHeader contactHeader;
 
     private String transport;
 
     public static final int myPort = 5080;
 
     public int count;//< Number of NOTIFYs
 
     private static Logger logger = Logger.getLogger(Referrer.class);
 
     static {
         try {
             .setLevel(.);
             .addAppender(new FileAppender(new SimpleLayout(),
                     "logs/refereroutputlog.txt"));
         } catch (Exception ex) {
             throw new RuntimeException(ex);
         }
     }
 
     private ClientTransaction subscribeTid;
 
     private ListeningPoint listeningPoint;
 
     public Referrer(ProtocolObjects protObjects) {
          = protObjects.addressFactory;
          = protObjects.messageFactory;
          = protObjects.headerFactory;
          = protObjects.sipStack;
          = protObjects.transport;
     }
 
     public void processRequest(RequestEvent requestReceivedEvent) {
         Request request = requestReceivedEvent.getRequest();
         ServerTransaction serverTransactionId = requestReceivedEvent
                .getServerTransaction();
        String viaBranch = ((ViaHeader)(request.getHeaders(.).next())).getParameter("branch");
        .info("\n\nRequest " + request.getMethod() + " received at "
                + .getStackName() + " with server transaction id "
                + serverTransactionId +
                " branch ID = " + viaBranch);
        //logger.info( request );
        if (request.getMethod().equals(.)) {
            processNotify(requestReceivedEventserverTransactionId);
        } else if ( request.getMethod().equals(.)) {
            processInviterequestReceivedEvent );
        } else if ( request.getMethod().equals(.)) {
            processAckrequestReceivedEvent );
        } else if ( request.getMethod().equals(.)) {
            processByerequestReceivedEvent );
        } else {
            TestHarness.fail"Unexpected request type:" + request.getMethod() );
        }
    }
    private void processNotify(RequestEvent requestEvent,
            ServerTransaction serverTransactionId) {
        SipProvider provider = (SipProviderrequestEvent.getSource();
        Request notify = requestEvent.getRequest();
        if ( notify.getMethod().equals("NOTIFY") ) try {
            .info("referer:  got a NOTIFY count  " + ++this. + ":\n" + notify );
            if (serverTransactionId == null) {
                .info("referer:  null TID.");
                serverTransactionId = provider.getNewServerTransaction(notify);
            }
            Dialog dialog = serverTransactionId.getDialog();
            .info("Dialog = " + dialog);
            TestHarness.assertTrue("Dialog should not be null"dialog != null);
            .info("Dialog State = " + dialog.getState());
            Response response = .createResponse(200, notify);
            // SHOULD add a Contact
            ContactHeader contact = (ContactHeader.clone();
            ((SipURI)contact.getAddress().getURI()).setParameter"id""sub" );
            response.addHeadercontact );
            .info("Transaction State = " + serverTransactionId.getState());
            serverTransactionId.sendResponse(response);
            .info("Dialog State = " + dialog.getState());
            SubscriptionStateHeader subscriptionState = (SubscriptionStateHeadernotify
                    .getHeader(.);
            // Subscription is terminated?
            String state = subscriptionState.getState();
            if (state.equalsIgnoreCase(.)) {
                dialog.delete();
            } else {
                .info("Referer: state now " + state);
            }
        } catch (Exception ex) {
            TestHarness.fail("Failed processing notify, because of " + ex);
        } else {
            TestHarness.fail"Unexpected request type" );
        }
    }
    private void processInviteRequestEvent re )
    {
        SipProvider provider = (SipProviderre.getSource();
        ServerTransaction st = re.getServerTransaction();
        try {
            if (st==nullst = provider.getNewServerTransactionre.getRequest() );
            Response r = .createResponse( 100 , re.getRequest());
            st.sendResponser );
            r = .createResponse( 180 , re.getRequest());
            r.addHeader( (ContactHeader.clone() );
            ((ToHeaderr.getHeader("To")).setTag"inv_res" );
            st.sendResponser );
            Thread.sleep( 500 );
            r = .createResponse( 200, re.getRequest() );
            r.addHeader( (ContactHeader.clone() );
            ((ToHeaderr.getHeader("To")).setTag"inv_res" );
            st.sendResponser );
        } catch (Throwable t) {
            t.printStackTrace();
            TestHarness.fail"Throwable:" + t.getLocalizedMessage() );
        }
    }
    private void processAckRequestEvent re )
    {
        // ignore it, Referee sends BYE right after
    }
    private void processByeRequestEvent re )
    {
        try {
            re.getServerTransaction().sendResponse(
                    .createResponse(200, re.getRequest()));
        } catch (Throwable t) {
            t.printStackTrace();
            TestHarness.fail"Throwable:" + t.getLocalizedMessage() );
        }
    }
    public void processResponse(ResponseEvent responseReceivedEvent) {
        Response response = (ResponseresponseReceivedEvent.getResponse();
        Transaction tid = responseReceivedEvent.getClientTransaction();
        .info("Got a response:" + response.getStatusCode()
                + ':' + response.getHeader. ) );
        .info("Response received with client transaction id " + tid
                + ": " + response.getStatusCode()  );
        if (tid == null) {
            .info("Stray response -- dropping ");
            return;
        }
        .info("transaction state is " + tid.getState());
        .info("Dialog = " + tid.getDialog());
        if ( tid.getDialog () != null )
            .info("Dialog State is " + tid.getDialog().getState());
    }
    public SipProvider createProvider() throws Exception {
        this. = .createListeningPoint("127.0.0.1",
                );
        return ;
    }
    public void sendRefer() {
        try {
            String fromName = "BigGuy";
            String fromSipAddress = "here.com";
            String fromDisplayName = "The Master Blaster";
            String toSipAddress = "127.0.0.1";
            String toUser = "referee";
            String toDisplayName = "Referee";
            // create >From Header
            SipURI fromAddress = .createSipURI(fromName,
                    fromSipAddress);
            Address fromNameAddress = .createAddress(fromAddress);
            fromNameAddress.setDisplayName(fromDisplayName);
            FromHeader fromHeader = .createFromHeader(
                    fromNameAddress"12345");
            // create To Header
            SipURI toAddress = 
                    .createSipURI(toUsertoSipAddress);
            Address toNameAddress = .createAddress(toAddress);
            toNameAddress.setDisplayName(toDisplayName);
            ToHeader toHeader = .createToHeader(toNameAddress,
                    null);
            // create Request URI
            SipURI requestURI = .createSipURI(toUser,
                    toSipAddress);
            requestURI.setPort. );// referee
            requestURI.setTransportParam();
            // Create ViaHeaders
            ArrayList viaHeaders = new ArrayList();
            int port = .getListeningPoint().getPort();
            ViaHeader viaHeader = .createViaHeader("127.0.0.1",
                    portnull);
            // add via headers
            viaHeaders.add(viaHeader);
            // Create a new CallId header
            CallIdHeader callIdHeader = .getNewCallId();
            // JvB: Make sure that the implementation matches the messagefactory
            callIdHeader = .createCallIdHeadercallIdHeader.getCallId() );
            // Create a new Cseq header
            CSeqHeader cSeqHeader = .createCSeqHeader(1L,
                    .);
            // Create a new MaxForwardsHeader
            MaxForwardsHeader maxForwards = 
                    .createMaxForwardsHeader(70);
            // Create the request.
            Request request = .createRequest(requestURI,
                    .callIdHeadercSeqHeaderfromHeader,
                    toHeaderviaHeadersmaxForwards);
            // Create contact headers
            String host = .getIPAddress();
            SipURI contactUrl = .createSipURI(fromNamehost);
            contactUrl.setPort(.getPort());
            // Create the contact name address.
            SipURI contactURI = .createSipURI(fromNamehost);
            contactURI.setTransportParam();
            contactURI.setPort(.getListeningPoint().getPort());
            Address contactAddress = .createAddress(contactURI);
            // Add the contact address.
            contactAddress.setDisplayName(fromName);
             = .createContactHeader(contactAddress);
            request.addHeader();
            // Create the client transaction.
             = .getNewClientTransaction(request);
            // REFER has an implicit "refer" event
            // Create an event header for the subscription.
            // EventHeader eventHeader = headerFactory.createEventHeader("foo");
            // eventHeader.setEventId("foo");
            // request.addHeader(eventHeader);
            // Make the INVITE come back to this listener!
            ReferToHeader referTo = .createReferToHeader(
                    .createAddress"<sip:127.0.0.1:" +  + ";transport=" +  + ">" )
            );
            request.addHeaderreferTo );
            .info("Refer Dialog = " + .getDialog());
            // send the request out.
            .sendRequest();
        } catch (Throwable ex) {
            TestHarness.fail("Referrer failed sending Subscribe request, because of " + ex);
        }
    }
    public void processIOException(IOExceptionEvent exceptionEvent) {
        .info("io exception event received");
        TestHarness.fail("IOException unexpected");
    }
    public void processTransactionTerminated(
            TransactionTerminatedEvent tte) {
        .info("transaction terminated:" + tte );
    }
    public void processDialogTerminated(
            DialogTerminatedEvent dialogTerminatedEvent) {
        .info("dialog terminated event received");
    }
    public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
        .info("Transaction Time out");
    }
New to GrepCode? Check out our FAQ X