Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package test.tck.msgflow.callflows.prack;
  
  
  import javax.sip.Dialog;
 
 
 import  test.tck.TestHarness;
 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 static SipProvider sipProvider;
 
     private static AddressFactory addressFactory;
 
     private static MessageFactory messageFactory;
 
     private static HeaderFactory headerFactory;
 
     private static SipStack sipStack;
 
     private ContactHeader contactHeader;
 
     private ClientTransaction inviteTid;
 
     private Dialog dialog;
 
     private String transport;
 
     private boolean prackTriggerReceived;
     private boolean prackConfirmed;
 
     public static final int myPort = 5070;
 
     private static Logger logger = Logger.getLogger("test.tck");
 
     private String toUser = "LittleGuy";
 
     public Shootist(ProtocolObjects protObjects) {
          = protObjects.addressFactory;
          = protObjects.messageFactory;
          = protObjects.headerFactory;
          = protObjects.sipStack;
          = protObjects.transport;
     }
 
     public SipProvider createProvider() throws Exception {
         ListeningPoint lp = .createListeningPoint("127.0.0.1",
                 );
 
          = .createSipProvider(lp);
         .info( + " SIP provider " + );
 
         return ;
     }
 
 
     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);
    }
    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) {
            TestHarness.fail(ex.getMessage());
            System.exit(0);
        }
    }
    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 (cseq.getMethod() == .) {
             = true;
        }
        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());
        SipProvider provider = (SipProviderresponseReceivedEvent.getSource();
         = tid.getDialog();
        try {
            if (response.getStatusCode() == .) {
                if (cseq.getMethod().equals(.)) {
                    Request ackRequest = .createAck(((CSeqHeaderresponse.getHeader(.)).getSeqNumber());
                    .info("Sending ACK");
                    .sendAck(ackRequest);
                }
            } else if ( response.getStatusCode() == Shootme.PRACK_CODE) {
                 = true;
                RequireHeader requireHeader = (RequireHeaderresponse.getHeader(.);
                if ( requireHeader.getOptionTag().equalsIgnoreCase("100rel")) {
                    Dialog dialog = tid.getDialog();
                    Request prackRequest = dialog.createPrack(response);
                    // create Request URI
                    SipURI requestURI = .createSipURI(,
                            "127.0.0.1:" + Shootme.myPort);
                    prackRequest.setRequestURI(requestURI);
                    ClientTransaction ct = provider.getNewClientTransaction(prackRequest);
                    dialog.sendRequest(ct);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            TestHarness.fail("Unexpected exception " + ex.getMessage());
        }
    }
    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 toDisplayName = "The Little Blister";
            // 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(toSipAddress);
            Address toNameAddress = .createAddress(toAddress);
            toNameAddress.setDisplayName(toDisplayName);
            ToHeader toHeader = .createToHeader(toNameAddress,
                    null);
            // create Request URI
            SipURI requestURI = .createSipURI(,
                    "127.0.0.1:" + Shootme.myPort);
            // Create ViaHeaders
            ArrayList viaHeaders = new ArrayList();
            ViaHeader viaHeader = .createViaHeader("127.0.0.1",
                    .getListeningPoint().getPort(),
                    null);
            // add via headers
            viaHeaders.add(viaHeader);
            // 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 = .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 = "127.0.0.1";
            SipURI contactUrl = .createSipURI(fromNamehost);
            ListeningPoint lp = .getListeningPoint();
            contactUrl.setPort(lp.getPort());
            // Create the contact name address.
            SipURI contactURI = .createSipURI(fromNamehost);
            contactURI.setPort(.getListeningPoint()
                    .getPort());
            Address contactAddress = .createAddress(contactURI);
            // Add the contact address.
            contactAddress.setDisplayName(fromName);
             = .createContactHeader(contactAddress);
            request.addHeader();
            /*
             * When the UAC creates a new request, it can insist on reliable
             * delivery of provisional responses for that request. To do that,
             * it inserts a Require header field with the option tag 100rel into
             * the request.
             */
            RequireHeader requireHeader = 
                    .createRequireHeader("100rel");
            request.addHeader(requireHeader);
            // Create the client transaction.
             = .getNewClientTransaction(request);
            this. = .getDialog();
            // send the request out.
            .sendRequest();
        } catch (Exception ex) {
            TestHarness.fail("sendInvite failed because of " + ex.getMessage());
        }
    }
    public void processIOException(IOExceptionEvent exceptionEvent) {
        .info("IOException happened for "
                + exceptionEvent.getHost() + " port = "
                + exceptionEvent.getPort());
    }
    public void processTransactionTerminated(
            TransactionTerminatedEvent transactionTerminatedEvent) {
        .info("Transaction terminated event recieved");
    }
    public void processDialogTerminated(
            DialogTerminatedEvent dialogTerminatedEvent) {
        .info("dialogTerminatedEvent");
    }
    public void checkState() {
        TestHarness.assertTrue( this. );
        TestHarness.assertTrue(  );
    }
New to GrepCode? Check out our FAQ X