Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package test.unit.gov.nist.javax.sip.stack.dialog.b2bua;
  
  
  import java.util.HashSet;
  import java.util.Timer;
 
 
 import  junit.framework.TestCase;
 
 
 import  test.tck.msgflow.callflows.ProtocolObjects;


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 implements SipListener {
 
     private ContactHeader contactHeader;
 
     private ClientTransaction inviteTid;
 
 
     private SipProvider sipProvider;
 
     private String host = "127.0.0.1";
 
     private int port;
 
     private String peerHost = "127.0.0.1";
 
     private int peerPort;
 
     private ListeningPoint listeningPoint;
 
     private static String unexpectedException = "Unexpected exception ";
 
     private static Logger logger = Logger.getLogger(Shootist.class);
 
 
 
     private Dialog originalDialog;
 
     private HashSet<DialogforkedDialogs = new HashSet<Dialog>();
 
     private Dialog ackedDialog;
 
     private SipStack sipStack;
 
     private HashSet<DialogcanceledDialog = new HashSet<Dialog>();
 
     private boolean byeResponseSeen;
 
     private int counter;
 
     private static HeaderFactory headerFactory;
 
     private static MessageFactory messageFactory;
    private static AddressFactory addressFactory;
    private static final String transport = "udp";
    static boolean callerSendsBye  = true;
    private boolean byeSent;
    private Timer timer = new Timer();
    private boolean inviteOkSeen;
    class SendBye extends TimerTask {
        private Dialog dialog;
        public SendBye(Dialog dialog ) {
            this. = dialog;
        }
        @Override
        public void run() {
           try {
               TestCase.assertEquals ("Dialog state must be confirmed",
                        .,.getState());
               Request byeRequest = .createRequest(.);
               ClientTransaction ctx = .getNewClientTransaction(byeRequest);
               .sendRequest(ctx);
           } catch (Exception ex) {
               TestCase.fail("Unexpected exception");
           }
        }
    }
    public Shootist(int myPortint proxyPortthrows Exception {
        this. = myPort;
        ProtocolObjects sipObjects = new ProtocolObjects("shootist","gov.nist","udp",true,falsefalse);
         = sipObjects.addressFactory;
         = sipObjects.messageFactory;
         = sipObjects.headerFactory;
        this. = sipObjects.sipStack;
        this. = proxyPort;
        this.createSipProvider();
        this..addSipListener(this);
    }
    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.
        if (request.getMethod().equals(.))
            processBye(requestserverTransactionId);
        else
            TestCase.fail("Unexpected request ! : " + request);
    }
    public void processBye(Request request,
            ServerTransaction serverTransactionId) {
        try {
            .info("shootist:  got a bye .");
            if (serverTransactionId == null) {
                .info("shootist:  null TID.");
                return;
            }
            Dialog dialog = serverTransactionId.getDialog();
            .info("Dialog State = " + dialog.getState());
            Response response = .createResponse(
                    200, request);
            serverTransactionId.sendResponse(response);
            .info("shootist:  Sending OK.");
            .info("Dialog State = " + dialog.getState());
        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(0);
        }
    }
    public synchronized 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);
        .info("Response = " + response + " class=" + response.getClass() );
        Dialog dialog = responseReceivedEvent.getDialog();
        TestCase.assertNotNull( dialog );
        if (tid != null)
            .info("transaction state is " + tid.getState());
        else
            .info("transaction = " + tid);
        .info("Dialog = " + dialog);
        .info("Dialog state is " + dialog.getState());
        try {
            if (response.getStatusCode() == .) {
                if (cseq.getMethod().equals(.)) {
                    this. = true;
                    TestCase.assertEquals( .dialog.getState() );
                    Request ackRequest = dialog.createAck(cseq
                            .getSeqNumber());
                    TestCase.assertNotNull( ackRequest.getHeader. ) );
                    if ( dialog == this. ) {
                        dialog.sendAck(ackRequest);
                        return;
                    }
                    // Proxy will fork. I will accept the first dialog.
                    this..add(dialog);
                    if ( responseReceivedEvent.getClientTransaction() != null ) {
                        .info("Sending ACK");
                        dialog.sendAck(ackRequest);
                        TestCase.assertTrue(
                                "Dialog state should be CONFIRMED"dialog
                                        .getState() == .);
                        TestCase.assertTrue(this. == null ||
                                this. == dialog);
                        this. = dialog;
                        if (  ) {
                            .schedulenew SendBye(), 1000  );
                        }
                    } else {
                        this..add(dialog);
                        // Send ACK to quench re-transmission
                        .sendRequest(ackRequest);
                        // Kill the second dialog by sending a bye.
                        SipProvider sipProvider = (SipProviderresponseReceivedEvent
                                .getSource();
                        Request byeRequest = dialog.createRequest(.);
                        ClientTransaction ct = sipProvider
                                .getNewClientTransaction(byeRequest);
                        dialog.sendRequest(ct);
                    }
                } else if ( cseq.getMethod().equals(.)) {
                    if ( dialog == this.) {
                        this. = true;
                    }
                } else {
                    .info("Response method = " + cseq.getMethod());
                }
            } else if ( response.getStatusCode() == . ) {
                //TestHarness.assertEquals( DialogState.EARLY, dialog.getState() );
            }
        } catch (Throwable ex) {
            ex.printStackTrace();
            // System.exit(0);
        }
    }
    public SipProvider createSipProvider() {
        try {
             = .createListeningPoint(
                    "udp");
            .info("listening point = " +  + " port = " + );
            .info("listening point = " + );
             = 
                    .createSipProvider();
            return ;
        } catch (Exception ex) {
            .error(ex);
            TestCase.fail();
            return null;
        }
    }
    public void checkState() {
       
       TestCase.assertTrue("Should see BYE response for ACKED Dialog",this.);
       TestCase.assertTrue("InviteOK seen"this.);
    }
    public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
        .info("Transaction Time out");
    }
    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();
            // JvB: Make sure that the implementation matches the messagefactory
            callIdHeader = 
                    .createCallIdHeader(callIdHeader.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.callIdHeadercSeqHeader,
                    fromHeadertoHeaderviaHeadersmaxForwards);
            // Create contact headers
            SipURI contactUrl = .createSipURI(
                    fromName);
            contactUrl.setPort(.getPort());
            // Create the contact name address.
            SipURI contactURI = .createSipURI(
                    fromName);
            contactURI.setPort(.getListeningPoint(
                    ).getPort());
            contactURI.setTransportParam();
            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);
            Dialog dialog = .getDialog();
            TestCase.assertTrue("Initial dialog state should be null",
                    dialog.getState() == null);
            // send the request out.
            .sendRequest();
            this. = dialog;
        } catch (Exception ex) {
            .error(ex);
            TestCase.fail();
        }
    }
    public void processIOException(IOExceptionEvent exceptionEvent) {
        .error("IOException happened for " + exceptionEvent.getHost()
                + " port = " + exceptionEvent.getPort());
        TestCase.fail("Unexpected exception");
    }
    public void processTransactionTerminated(
            TransactionTerminatedEvent transactionTerminatedEvent) {
        .info("Transaction terminated event recieved");
    }
    public void processDialogTerminated(
            DialogTerminatedEvent dialogTerminatedEvent) {
        TestCase.assertTrue("DTE dialog must be one of those we canceled",
                this..contains((Dialog)dialogTerminatedEvent.getDialog() ));
    }
    public void stop() {
      this..stop();
    }
New to GrepCode? Check out our FAQ X