Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package test.load.multidialog;
  
  import javax.sip.*;
  import javax.sip.header.*;
  import java.util.*;

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 AddressFactory addressFactory;
 
     private static MessageFactory messageFactory;
 
     private static HeaderFactory headerFactory;
 
     private static SipStack sipStack;
 
     private static String myAddress = "127.0.0.1";
 
     private static int myPort = 5070;
 
     int numInvite = 0;
 
     private int terminatedCount;
 
     private int createdCount;
 
     private HashSet dialogIds;
 
     private HashMap transactionIDs;
 
     class TTask extends TimerTask {
 
         RequestEvent requestEvent;
 
         ServerTransaction st;
 
         public TTask(RequestEvent requestEventServerTransaction st) {
             this. = requestEvent;
             this. = st;
         }
 
         public void run() {
             Request request = .getRequest();
             try {
                 //System.out.println("shootme: got an Invite sending OK");
                 Response response = .createResponse(180, request);
                 ToHeader toHeader = (ToHeaderresponse
                         .getHeader(.);
                 Address address = .createAddress("Shootme <sip:"
                         +  + ":" +  + ">");
                 ContactHeader contactHeader = 
                         .createContactHeader(address);
                 response.addHeader(contactHeader);
 
                 //System.out.println("got a server tranasaction " + st);
                 Dialog dialog = .getDialog();
                 /*
                  * if (dialog != null) { System.out.println("Dialog " + dialog);
                  * System.out.println("Dialog state " + dialog.getState()); }
                  */
                 .sendResponse(response); // send 180(RING)
                 response = .createResponse(200, request);
                 toHeader = (ToHeaderresponse.getHeader(.);
                 String toTag = new Integer( (int) (Math.random() * 1000) ).toString();
                 toHeader.setTag(toTag); // Application is supposed to set.
                 response.addHeader(contactHeader);
 
                 .sendResponse(response);// send 200(OK)
 
             } catch (Exception ex) {
                 ex.printStackTrace();
                 System.exit(0);
             }
 
         }
 
     }
 
     protected static final String usageString = "java "
             + "examples.shootist.Shootist \n"
             + ">>>> is your class path set to the root?";
 
     private static void usage() {
         ..println();
         System.exit(0);
 
     }
 
     public void processRequest(RequestEvent requestEvent) {
         Request request = requestEvent.getRequest();
        ServerTransaction serverTransactionId = requestEvent
                .getServerTransaction();
             /*
             * System.out.println("\n\nRequest " + request.getMethod() + "
             * received at " + sipStack.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);
        }
    }

    
Process the ACK request. Send the bye and complete the call flow.
    public void processAck(RequestEvent requestEvent,
            ServerTransaction serverTransaction) {
        SipProvider sipProvider = (SipProviderrequestEvent.getSource();
        try {
            // System.out.println("*** shootme: got an ACK "
            // + requestEvent.getRequest());
            if (serverTransaction == null) {
                .
                        .println("null server transaction -- ignoring the ACK!");
                return;
            }
            Dialog dialog = serverTransaction.getDialog();
            this. ++;
            ..println("Dialog Created = " + dialog.getDialogId() + " createdCount " + this. +
                    " Dialog State = " + dialog.getState() );
            if ( this..contains(dialog.getDialogId())) {
                ..println("OOPS ! I already saw " + dialog.getDialogId());
            } else {
                this..add(dialog.getDialogId());
            }
            Request byeRequest = dialog.createRequest(.);
            ClientTransaction tr = sipProvider
                    .getNewClientTransaction(byeRequest);
            // System.out.println("shootme: got an ACK -- sending bye! ");
            dialog.sendRequest(tr);
        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(0);
        }
    }

    
Process the invite request.
    public void processInvite(RequestEvent requestEvent,
            ServerTransaction serverTransaction) {
        try {
            //System.out.println("ProcessInvite");
            Request request = requestEvent.getRequest();
            SipProvider sipProvider = (SipProviderrequestEvent.getSource();
            // Note you need to create the Server Transaction
            // before the listener returns but you can delay sending the response
            ServerTransaction st = sipProvider.getNewServerTransaction(request);
            if ( .containsKey(st.getBranchId())) {
                ..println("OOOPS -- seen this guy before!! This must be a late guy " + st.getBranchId()
                        + " st = " + .get(st.getBranchId()));
                return;
            } else {
                .putst.getBranchId(),st);
            }
            TTask ttask = new TTask(requestEventst);
            int ttime;
            if (( % 4) == 0)
                ttime = 5000;
            else if (( % 4) == 1)
                ttime = 1000;
            else
                ttime = 300;
            ++;
            new Timer().schedule(ttaskttime);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    
Process the bye request.
    public void processBye(RequestEvent requestEvent,
            ServerTransaction serverTransactionId) {
        Request request = requestEvent.getRequest();
        try {
            // System.out.println("shootme: got a bye sending OK.");
            Response response = .createResponse(200, request,
                    nullnull);
            serverTransactionId.sendResponse(response);
            // System.out.println("Dialog State is "
            // + serverTransactionId.getDialog().getState());
        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(0);
        }
    }
    public void processResponse(ResponseEvent responseReceivedEvent) {
        // System.out.println("Got a response");
        Response response = (ResponseresponseReceivedEvent.getResponse();
        Transaction tid = responseReceivedEvent.getClientTransaction();
        // System.out.println("Response received with client transaction id "
        // + tid + ":\n" + response);
        try {
            if (response.getStatusCode() == .
                    && ((CSeqHeaderresponse.getHeader(.))
                            .getMethod().equals(.)) {
                Dialog dialog = tid.getDialog();
                Request request = tid.getRequest();
                dialog.sendAck(request);
            }
        } 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();
        }
        /*
         * System.out.println("state = " + transaction.getState());
         * System.out.println("dialog = " + transaction.getDialog());
         * System.out.println("dialogState = " +
         * transaction.getDialog().getState());
         * System.out.println("Transaction Time out" + transaction.getBranchId());
         */
    }
    public void init() {
        this. = new HashSet();
        this. = new HashMap();
        SipFactory sipFactory = null;
         = null;
        sipFactory = SipFactory.getInstance();
        sipFactory.setPathName("gov.nist");
        Properties properties = new Properties();
        properties.setProperty("javax.sip.STACK_NAME""shootme");
        // You need 16 for logging traces. 32 for debug + traces.
        // Your code will limp at 32 but it is best for debugging.
        properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL""32");
        properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",
                "shootmedebuglog.txt");
        properties.setProperty("gov.nist.javax.sip.SERVER_LOG",
                "shootmelog.txt");
        // Guard against starvation.
        properties.setProperty("gov.nist.javax.sip.READ_TIMEOUT""1000");
        // properties.setProperty("gov.nist.javax.sip.MAX_MESSAGE_SIZE",
        // "4096");
        properties.setProperty("gov.nist.javax.sip.CACHE_SERVER_CONNECTIONS",
                "false");
        try {
            // Create SipStack object
             = sipFactory.createSipStack(properties);
            ..println("sipStack = " + );
        } catch (PeerUnavailableException e) {
            // could not find
            // gov.nist.jain.protocol.ip.sip.SipStackImpl
            // in the classpath
            e.printStackTrace();
            ..println(e.getMessage());
            if (e.getCause() != null)
                e.getCause().printStackTrace();
            System.exit(0);
        }
        try {
             = sipFactory.createHeaderFactory();
             = sipFactory.createAddressFactory();
             = sipFactory.createMessageFactory();
            ListeningPoint lp = .createListeningPoint(, 5070,
                    "udp");
            ListeningPoint lp1 = .createListeningPoint(, 5070,
                    "tcp");
            Shootme listener = this;
            SipProvider sipProvider = .createSipProvider(lp);
            ..println("udp provider " + sipProvider);
            sipProvider.addSipListener(listener);
            sipProvider = .createSipProvider(lp1);
            ..println("tcp provider " + sipProvider);
            sipProvider.addSipListener(listener);
        } catch (Exception ex) {
            ..println(ex.getMessage());
            ex.printStackTrace();
            usage();
        }
    }
    public static void main(String args[]) {
        /* pass dynamic parameters in *.bat file(command line) */
        if (args.length >= 1)
             = args[0];
        if (args.length >= 2)
             = Integer.parseInt(args[1]);
        ..println("\n***Address=<" +  + ">, Port=<" + 
                + ">.");
        new Shootme().init();
    }
    public void processIOException(IOExceptionEvent exceptionEvent) {
        ..println("IOException event");
    }
    public void processTransactionTerminated(
            TransactionTerminatedEvent transactionTerminatedEvent) {
        //System.out.println("TransactionTerminatedEvent");
    }
    public void processDialogTerminated(
            DialogTerminatedEvent dialogTerminatedEvent) {
        Dialog dialog = dialogTerminatedEvent.getDialog();
        this.++;
        ..println("Dialog Terminated Event "  + dialog.getDialogId() + " terminatedCount = " + );
        if ( ! this..contains(dialog.getDialogId())) {
            ..println("Saw a terminated event for an unknown dialog id");
        } else {
            this..remove(dialog.getDialogId());
        }
    }
New to GrepCode? Check out our FAQ X