Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package test.tck.msgflow.callflows.forkedinvite;
  
  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 Shootme   implements SipListener {
 
 
 
 
     private static final String myAddress = "127.0.0.1";
 
     private Hashtable serverTxTable = new Hashtable();
 
     private SipProvider sipProvider;
 
     private int myPort ;
 
     private static String unexpectedException = "Unexpected exception ";
 
     private static Logger logger = Logger.getLogger(Shootme.class);
 
     private ProtocolObjects protocolObjects;
 
 
     private boolean inviteSeen;
 
 
     private boolean byeSeen;
 
     private boolean ackSeen;
 
     private boolean actAsNonRFC3261UAS;

    
Causes this UAS to act as a non-RFC3261 UAS, i.e. does not set a to-tag
 
     public void setNonRFC3261boolean b ) {
         this. = b;
     }
 
     class MyTimerTask extends TimerTask {
         RequestEvent  requestEvent;
         // String toTag;
         ServerTransaction serverTx;
 
         public MyTimerTask(RequestEvent requestEvent,ServerTransaction tx) {
             .info("MyTimerTask ");
             this. = requestEvent;
             // this.toTag = toTag;
             this. = tx;
 
         }
 
         public void run() {
             sendInviteOK(,);
         }
 
     }
 
 
 
     public void processRequest(RequestEvent requestEvent) {
         Request request = requestEvent.getRequest();
         ServerTransaction serverTransactionId = requestEvent
                 .getServerTransaction();
 
         .info("\n\nRequest " + request.getMethod()
                 + " received at " + ..getStackName()
                 + " with server transaction id " + serverTransactionId);
 
         if (request.getMethod().equals(.)) {
             processInvite(requestEventserverTransactionId);
         } else if (request.getMethod().equals(.)) {
             processAck(requestEventserverTransactionId);
         } else if (request.getMethod().equals(.)) {
             processBye(requestEventserverTransactionId);
        } else if (request.getMethod().equals(.)) {
            processCancel(requestEventserverTransactionId);
        }
    }
    public void processResponse(ResponseEvent responseEvent) {
    }

    
Process the ACK request. Send the bye and complete the call flow.
    public void processAck(RequestEvent requestEvent,
            ServerTransaction serverTransaction) {
        .info("shootme: got an ACK! ");
        .info("Dialog = " + requestEvent.getDialog());
        if ( requestEvent.getDialog() != null ) {
            /* Could be late arriving ACK */
            .info("Dialog State = " + requestEvent.getDialog().getState());
        }
        this. = true;
    }

    
Process the invite request.
    public void processInvite(RequestEvent requestEvent,
            ServerTransaction serverTransaction) {
        SipProvider sipProvider = (SipProviderrequestEvent.getSource();
        Request request = requestEvent.getRequest();
        try {
            .info("shootme: got an Invite sending Trying");
            // logger.info("shootme: " + request);
            ServerTransaction st = requestEvent.getServerTransaction();
            if (st == null) {
                .info("null server tx -- getting a new one");
                st = sipProvider.getNewServerTransaction(request);
            }
            .info("getNewServerTransaction : " + st);
            String txId = ((ViaHeader)request.getHeader(.)).getBranch();
            this..put(txIdst);
            // Create the 100 Trying response.
                    request);
                ListeningPoint lp = sipProvider.getListeningPoint(.);
            int myPort = lp.getPort();
            Address address = ..createAddress("Shootme <sip:"
                    +  + ":" + myPort + ">");
            // Add a random sleep to stagger the two OK's for the benifit of implementations
            // that may not be too good about handling re-entrancy.
            int timeToSleep = (int) ( Math.random() * 1000);
         
            st.sendResponse(response);
            Response ringingResponse = ..createResponse(.,
                    request);
            ContactHeader contactHeader = ..createContactHeader(address);
            response.addHeader(contactHeader);
            ToHeader toHeader = (ToHeaderringingResponse.getHeader(.);
            String toTag =  ? null : new Integer((int) (Math.random() * 10000)).toString();
            if (!toHeader.setTag(toTag); // Application is supposed to set.
            ringingResponse.addHeader(contactHeader);
            st.sendResponse(ringingResponse);
            Dialog dialog =  st.getDialog();
            dialog.setApplicationData(st);
            this. = true;
            new Timer().schedule(new MyTimerTask(requestEvent,st/*,toTag*/), timeToSleep);
        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(0);
        }
    }
    private  void sendInviteOK(RequestEvent requestEventServerTransaction inviteTid) {
        try {
            .info("sendInviteOK: " + inviteTid);
            if (inviteTid.getState() != .) {
                .info("shootme: Dialog state before OK: "
                        + inviteTid.getDialog().getState());
                SipProvider sipProvider = (SipProviderrequestEvent.getSource();
                Request request = requestEvent.getRequest();
                Response okResponse = ..createResponse(.,
                        request);
                    ListeningPoint lp = sipProvider.getListeningPoint(.);
                int myPort = lp.getPort();
                Address address = ..createAddress("Shootme <sip:"
                        +  + ":" + myPort + ">");
                ContactHeader contactHeader = .
                        .createContactHeader(address);
                okResponse.addHeader(contactHeader);
                inviteTid.sendResponse(okResponse);
                //logger.info("shootme: Dialog state after OK: "
                //      + inviteTid.getDialog().getState());
                // TestHarness.assertEquals( DialogState.CONFIRMED , inviteTid.getDialog().getState() );
            } else {
                .info("semdInviteOK: inviteTid = " + inviteTid + " state = " + inviteTid.getState());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    
Process the bye request.
    public void processBye(RequestEvent requestEvent,
            ServerTransaction serverTransactionId) {
        Request request = requestEvent.getRequest();
        try {
            .info("shootme:  got a bye sending OK.");
            .info("shootme:  dialog = " + requestEvent.getDialog());
            .info("shootme:  dialogState = " + requestEvent.getDialog().getState());
            Response response = ..createResponse(200, request);
            if ( serverTransactionId != null) {
                serverTransactionId.sendResponse(response);
            }
            .info("shootme:  dialogState = " + requestEvent.getDialog().getState());
            this. = true;
        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(0);
        }
    }
    public void processCancel(RequestEvent requestEvent,
            ServerTransaction serverTransactionId) {
        Request request = requestEvent.getRequest();
        SipProvider sipProvider = (SipProvider)requestEvent.getSource();
        try {
            .info("shootme:  got a cancel. " );
            // Because this is not an In-dialog request, you will get a null server Tx id here.
            if (serverTransactionId == null) {
                serverTransactionId = sipProvider.getNewServerTransaction(request);
            }
            Response response = ..createResponse(200, request);
            serverTransactionId.sendResponse(response);
            String serverTxId = ((ViaHeader)response.getHeader(.)).getBranch();
            ServerTransaction serverTx = (ServerTransactionthis..get(serverTxId);
            if ( serverTx != null && (serverTx.getState().equals(.) ||
                    serverTx.getState().equals(.))) {
                Request originalRequest = serverTx.getRequest();
                Response resp = ..createResponse(.,originalRequest);
                serverTx.sendResponse(resp);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(0);
        }
    }
    public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
        Transaction transaction;
        if (timeoutEvent.isServerTransaction()) {
            transaction = timeoutEvent.getServerTransaction();
        } else {
            transaction = timeoutEvent.getClientTransaction();
        }
        .info("state = " + transaction.getState());
        .info("dialog = " + transaction.getDialog());
        .info("dialogState = "
                + transaction.getDialog().getState());
        .info("Transaction Time out");
    }
    public SipProvider createProvider() {
        try {
                    .);
             = ..createSipProvider(lp);
            .info("provider " + );
            .info("sipStack = " + .);
            return ;
        } catch (Exception ex) {
            .error(ex);
            TestHarness.fail();
            return null;
        }
    }
    public Shootmeint myPortProtocolObjects protocolObjects ) {
        this. = myPort;
        this. = protocolObjects;
    }
    public void processIOException(IOExceptionEvent exceptionEvent) {
        .info("IOException");
    }
    public void processTransactionTerminated(
            TransactionTerminatedEvent transactionTerminatedEvent) {
        .info("Transaction terminated event recieved");
    }
    public void processDialogTerminated(
            DialogTerminatedEvent dialogTerminatedEvent) {
        .info("Dialog terminated event recieved");
    }
    public void checkState() {
        TestHarness.assertTrue("Should see invite");
        TestHarness.assertTrue("Should see either an ACK or a BYE, or both", || );
    }
New to GrepCode? Check out our FAQ X