Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package examples.cancel;
  
  import javax.sip.*;
  import javax.sip.header.*;
  
 
 import java.util.*;
 
 import  junit.framework.TestCase;

This class is a UAC template. Shootist is the guy that shoots and shootme is the guy that gets shot.

Author(s):
M. Ranganathan
 
 
 public class Shootist extends TestCase implements SipListener {
 
     private SipProvider sipProvider;
 
     private ContactHeader contactHeader;
 
     private ListeningPoint listeningPoint;
 
     private static String transport = "udp";
 
     private static String host = "127.0.0.1";
 
     private static int port = 5060;
 
     private static String peerHost = "127.0.0.1";
 
     private static int peerPort = 5070;
 
     private ClientTransaction inviteTid;
 
     private static String unexpectedException = "Unexpected Exception ";
 
     private Dialog dialog;
 
     public static boolean sendDelayedCancel = false;
 
     private boolean cancelSent;
 
     private boolean cancelOKReceived;
 
     private boolean byeSent;
 
     private boolean byeOkReceived;
 
     private boolean requestTerminated;
 
     private static Logger logger = Logger.getLogger(Shootist.class);
 
 
 
 
 
     public void processRequest(RequestEvent requestReceivedEvent) {
         Request request = requestReceivedEvent.getRequest();
         ServerTransaction serverTransactionId = requestReceivedEvent
                 .getServerTransaction();
 
         .info("\n\nRequest " + request.getMethod()
                 + " received at " + ..getStackName()
                 + " with server transaction id " + serverTransactionId);
 
         // We are the UAC so the only request we get is the BYE.
         SipProvider provider = (SipProviderrequestReceivedEvent.getSource();
         if (request.getMethod().equals(.))
             processBye(providerrequestserverTransactionId);
 
     }
 
     public void processBye(SipProvider providerRequest request,
             ServerTransaction serverTransactionId) {
         try {
             .info("shootist:  got a bye .");
             /*
              * if (serverTransactionId == null) { serverTransactionId =
              * provider.getNewServerTransaction(request); }
              */
 
             Response response = ..createResponse(
                     200, request);
             provider.sendResponse(response);
             // serverTransactionId.sendResponse(response);
             .info("shootist:  Sending OK.");
             if (serverTransactionId != null) {
                 // NULL can happen if the bye arrives late.
                 Dialog dialog = serverTransactionId.getDialog();
                 .info("Dialog State = " + dialog.getState());
             }
        } catch (Exception ex) {
            .error(,ex);
            fail();
        }
    }
    public void processResponse(ResponseEvent responseReceivedEvent) {
        .info("Got a response");
        Response response = (ResponseresponseReceivedEvent.getResponse();
        ClientTransaction tid = responseReceivedEvent.getClientTransaction();
        CSeqHeader cseq = (CSeqHeaderresponse.getHeader(.);
        .info("Response received : Status Code = "
                + response.getStatusCode() + " " + cseq);
        if (tid == null) {
            .info("Stray response -- dropping ");
            return;
        }
        .info("transaction state is " + tid.getState());
        .info("Dialog = " + tid.getDialog());
        .info("Dialog State is " + tid.getDialog().getState());
        try {
            if (response.getStatusCode() == .) {
                if (cseq.getMethod().equals(.)) {
                    if (!fail("Should not see OK for the Invite");
                    // Got the OK for the invite. If Send Cancel was delayed.
                    Request ackRequest = .createAckcseq.getSeqNumber() );
                    .info("Sending ACK");
                    .sendAck(ackRequest);
                    Request byeRequest = .createRequest(.);
                    .sendRequest(.getNewClientTransaction(byeRequest));
                    this. = true;
                } else if (cseq.getMethod().equals(.)) {
                    this. = true;
                    if (.getState() == .) {
                        // oops cancel went in too late. Need to hang up the
                        // dialog.
                        .info("Sending BYE -- cancel went in too late !!");
                        Request byeRequest = .createRequest(.);
                        ClientTransaction ct = 
                                .getNewClientTransaction(byeRequest);
                        .sendRequest(ct);
                        assertTrue(.getState().equals(.));
                    }
                } else if (cseq.getMethod().equals(.)) {
                    this. = true;
                }
            } else if (response.getStatusCode() == .) {
                // Cancel the invite.
                if (!) {
                    if (! )
                        sendCancel();
                }
            }  else if (response.getStatusCode() == .) {
                assertTrue(cseq.getMethod().equals(.));
                this. = true;
            }
        } catch (Exception ex) {
            .error(ex);
            fail();
        }
    }
    public void checkState() {
        if (  )
            assertTrue &&   ) ;
        else
            assertTrue( &&  && ) ;
    }
    public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
        .info("Transaction Time out");
        fail("unexpected timeout");
    }
    private void sendCancel() {
        try {
            .info("Sending cancel");
            Request cancelRequest = .createCancel();
            ClientTransaction cancelTid = 
                    .getNewClientTransaction(cancelRequest);
            cancelTid.sendRequest();
             = true;
        } catch (Exception ex) {
            ex.printStackTrace();
            .error(ex);
            fail();
        }
    }
    public SipProvider createSipProvider() {
        try {
                    );
             = .
                    .createSipProvider();
            return ;
        } catch (Exception ex) {
            .error(ex);
            fail();
            return null;
        }
    }
    public void sendInvite() {
        try {
            String fromName = "BigGuy";
            String fromSipAddress = "here.com";
            String fromDisplayName = "The Master Blaster";
            String toSipAddress = "there.com";
            String toUser = "LittleGuy";
            String toDisplayName = "The Little Blister";
            // create >From Header
            SipURI fromAddress = ..createSipURI(
                    fromNamefromSipAddress);
            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(
                    toNameAddressnull);
            // create Request URI
            String peerHostPort =  + ":" + ;
            SipURI requestURI = ..createSipURI(
                    toUserpeerHostPort);
            // Create ViaHeaders
            ArrayList viaHeaders = new ArrayList();
            ViaHeader viaHeader = .
                    .createViaHeader(.getListeningPoint(
                            ).getPort(), null);
            // add via headers
            viaHeaders.add(viaHeader);
            SipURI sipuri = ..createSipURI(null,
                    );
            sipuri.setPort();
            sipuri.setLrParam();
            RouteHeader routeHeader = .
                    .createRouteHeader(.
                            .createAddress(sipuri));
            // Create ContentTypeHeader
            ContentTypeHeader contentTypeHeader = .
                    .createContentTypeHeader("application""sdp");
            // Create a new CallId header
            CallIdHeader callIdHeader = .getNewCallId();
            // Create a new Cseq header
            CSeqHeader cSeqHeader = .
                    .createCSeqHeader(1L, .);
            // Create a new MaxForwardsHeader
            MaxForwardsHeader maxForwards = .
                    .createMaxForwardsHeader(70);
            // Create the request.
            Request request = ..createRequest(
                    requestURI.callIdHeadercSeqHeader,
                    fromHeadertoHeaderviaHeadersmaxForwards);
            // Create contact headers
            SipURI contactUrl = ..createSipURI(
                    fromName);
            contactUrl.setPort(.getPort());
            // Create the contact name address.
            SipURI contactURI = ..createSipURI(
                    fromName);
            contactURI.setPort(.getListeningPoint("udp").getPort());
            Address contactAddress = .
                    .createAddress(contactURI);
            // Add the contact address.
            contactAddress.setDisplayName(fromName);
             = .
                    .createContactHeader(contactAddress);
            request.addHeader();
            // Dont use the Outbound Proxy. Use Lr instead.
            request.setHeader(routeHeader);
            // Add the extension header.
            Header extensionHeader = .
                    .createHeader("My-Header""my header value");
            request.addHeader(extensionHeader);
            String sdpData = "v=0\r\n"
                    + "o=4855 13760799956958020 13760799956958020"
                    + " IN IP4  129.6.55.78\r\n" + "s=mysession session\r\n"
                    + "p=+46 8 52018010\r\n" + "c=IN IP4  129.6.55.78\r\n"
                    + "t=0 0\r\n" + "m=audio 6022 RTP/AVP 0 4 18\r\n"
                    + "a=rtpmap:0 PCMU/8000\r\n" + "a=rtpmap:4 G723/8000\r\n"
                    + "a=rtpmap:18 G729A/8000\r\n" + "a=ptime:20\r\n";
            byte[] contents = sdpData.getBytes();
            request.setContent(contentscontentTypeHeader);
            extensionHeader = ..createHeader(
                    "My-Other-Header""my new header value ");
            request.addHeader(extensionHeader);
            Header callInfoHeader = ..createHeader(
                    "Call-Info""<http://www.antd.nist.gov>");
            request.addHeader(callInfoHeader);
            // Create the client transaction.
             = .getNewClientTransaction(request);
            // send the request out.
            .sendRequest();
             = .getDialog();
        } catch (Exception ex) {
            .error(ex);
            fail();
        }
    }
    public static void main(String args[]) throws Exception {
        .addAppender(new ConsoleAppender(new SimpleLayout()));
        ProtocolObjects.init("shootist");
        Shootist shootist = new Shootist();
        shootist.createSipProvider();
        shootist.sipProvider.addSipListener(shootist);
        shootist.sendInvite();
    }
    public void processIOException(IOExceptionEvent exceptionEvent) {
        .info("Got an IO Exception");
        fail("unexpected event");
    }
    public void processTransactionTerminated(
            TransactionTerminatedEvent transactionTerminatedEvent) {
        .info("Got a transaction terminated event");
    }
    public void processDialogTerminated(
            DialogTerminatedEvent dialogTerminatedEvent) {
        .info("Got a dialog terminated event");
        this.checkState();
    }
New to GrepCode? Check out our FAQ X