Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package test.load.concurrency;
  
  import java.util.Timer;
  
  import javax.sip.Dialog;
 import  junit.framework.TestCase;

This class is a UAC template.

Author(s):
M. Ranganathan
 
 
 public class Shootist extends TestCase implements SipListener {
 
     static AddressFactory addressFactory;
 
     static MessageFactory messageFactory;
 
     static HeaderFactory headerFactory;
 
     static SipStack sipStack;
 
 
 
     static String transport;
 
     // Starts at -2000, a ramp-up period is required for performance testing.
     int byeCount = -2000;
 
     int ackCount;
 
     long start;
 
     static int MAXCONCURRENTINVITE = 200;
     static int NDIALOGS = 1000;
     static int NBPROVIDERS = 1;
 
 
     // Keeps track of successful dialog completion.
     private static Timer timer;
 
     static {
          = new Timer();
     }
 
     class TTask extends TimerTask {
         Dialog dialog;
 
         public TTask(Dialog dialog) {
             this. = dialog;
         }
 
         public void run() {
             if (.getState() != .) {
                 ..println("BYE not received for " + this.);
                 ..println("State " + this..getState());
                 ..println("dialogId " + this..getDialogId());
                 Appdata appData = (Appdata.getApplicationData();
 
                 ..println("ackCount " + appData.ackCount);
                 ((gov.nist.javax.sip.stack.SIPDialog).printDebugInfo();
                 System.exit(0);
             } else {
                this. = null;
            }
        }
    }
    class Appdata {
        protected TTask ttask;
        protected long startTime;
        protected long endTime;
        protected int ackCount;
        Appdata(Dialog dialog) {
             = new TTask(dialog);
            .schedule(, 20 * 1000 *  / 100);
             = System.currentTimeMillis();
        }
        public void cancelTimer() {
            this.. = null;
            this..cancel();
             = System.currentTimeMillis();
        }
    }
    class TestTimer extends TimerTask {
        private Shootist shootist;
        public TestTimer(Shootist shootist) {
            this. = shootist;
        }
        public void run() {
            assertTrue("Missed BYE " + .,
                    . >= );
            ProtocolObjects.destroy();
            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 requestReceivedEvent) {
        Request request = requestReceivedEvent.getRequest();
        ServerTransaction serverTransactionId = requestReceivedEvent
                .getServerTransaction();
        // 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 {
            if (serverTransactionId == null) {
                return;
            }
            Dialog dialog = serverTransactionId.getDialog();
            Response response = .createResponse(200, request);
            serverTransactionId.sendResponse(response);
            Appdata appdata = (Appdatadialog.getApplicationData();
            appdata.cancelTimer();
            int ndialogs = .decrementAndGet();
            // System.out.println(nbConcurrentInvite);
            if ( ndialogs >  ) ..println("Concurrent invites = " + ndialogs);
            synchronizedthis) {
                if ( ndialogs < /2 ) this.notify();
            }
            // Synchronization necessary for Multiprocessor machine
            // noted by Matt Porter.
            this.++;
            // System.out.println("bye count = " + byeCount);
            if ( == ) {
                long current = System.currentTimeMillis();
                float sec = (float) (current - ) / 1000f;
                ..println("Thrupt = " + (float) ( / sec));
            }
            // dialog.delete();
        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(0);
        }
    }
    public void processResponse(ResponseEvent responseReceivedEvent) {
        Response response = (ResponseresponseReceivedEvent.getResponse();
        Transaction tid = responseReceivedEvent.getClientTransaction();
        if (tid == null) {
            return;
        }
        try {
            if (response.getStatusCode() == .
                    && ((CSeqHeaderresponse.getHeader(.))
                            .getMethod().equals(.)) {
                // Request cancel = inviteTid.createCancel();
                // ClientTransaction ct =
                // sipProvider.getNewClientTransaction(cancel);
                // ct.sendRequest();
                Dialog dialog = tid.getDialog();
                if (dialog.getState() != .) {
                    CSeqHeader cseq = (CSeqHeaderresponse.getHeader(.);
                    Request ackRequest = dialog.createAck(cseq.getSeqNumber());
                    dialog.sendAck(ackRequest);
                    Appdata appData = (Appdatadialog.getApplicationData();
                    if (appData != null)
                        appData.ackCount++;
                }
            }
        } catch (Exception ex) {
            Dialog dialog = tid.getDialog();
            ..println("Dialog state is " + dialog.getState());
            ex.printStackTrace();
            System.exit(0);
        }
    }
    public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
        ..println("Transaction Time out");
        Request request = null;
        Transaction transaction = null;
        if (timeoutEvent.isServerTransaction()) {
            transaction = timeoutEvent.getServerTransaction();
            request = ((ServerTransactiontransaction).getRequest();
        } else {
            transaction = timeoutEvent.getClientTransaction();
            request = ((ClientTransactiontransaction).getRequest();
        }
        ..println("state = " + transaction.getState());
        ..println("dialog = " + transaction.getDialog());
        ..println("dialogState = "
                + transaction.getDialog().getState());
        ..println("Transaction Time out");
        ..println("Transaction " + transaction);
        ..println("request " + request);
        fail("Unexpected event: TimeoutEvent ");
    }
    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(fromName,
                    fromSipAddress);
            Address fromNameAddress = .createAddress(fromAddress);
            fromNameAddress.setDisplayName(fromDisplayName);
            String tag = new Integer((int) (Math.random() * 10000)).toString();
            FromHeader fromHeader = .createFromHeader(
                    fromNameAddresstag);
            // create To Header
            SipURI toAddress = 
                    .createSipURI(toUsertoSipAddress);
            Address toNameAddress = .createAddress(toAddress);
            toNameAddress.setDisplayName(toDisplayName);
            ToHeader toHeader = .createToHeader(toNameAddress,
                    null);
            // create Request URI
            SipURI requestURI = .createSipURI(toUser,
                    toSipAddress);
            // Create ViaHeaders
            ArrayList viaHeaders = new ArrayList();
            SipProvider sipProvider = getNextProvider();
            int port = sipProvider.getListeningPoint().getPort();
            ViaHeader viaHeader = .createViaHeader(sipProvider
                    .getListeningPoint().getIPAddress(), port,
                    null);
            // add via headers
            viaHeaders.add(viaHeader);
            // Create a new CallId header
            CallIdHeader callIdHeader = sipProvider.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,
                    .callIdHeadercSeqHeaderfromHeader,
                    toHeaderviaHeadersmaxForwards);
            // Create contact headers
            String host = sipProvider.getListeningPoint()
                    .getIPAddress();
            // Create the contact name address.
            SipURI contactURI = .createSipURI(fromNamehost);
            contactURI.setPort(port);
            contactURI.setTransportParam();
            Address contactAddress = .createAddress(contactURI);
            // Add the contact address.
            contactAddress.setDisplayName(fromName);
             = .createContactHeader(contactAddress);
            request.addHeader();
            // Add the extension header.
            Header extensionHeader = .createHeader("My-Header",
                    "my header value");
            request.addHeader(extensionHeader);
            Header callInfoHeader = .createHeader("Call-Info",
                    "<http://www.antd.nist.gov>");
            request.addHeader(callInfoHeader);
            SipURI myUri = .createSipURI(nullhost);
            myUri.setLrParam();
            myUri.setTransportParam();
            myUri.setPort(5070);
            Address address = .createAddress(nullmyUri);
            RouteHeader routeHeader = .createRouteHeader(address);
            request.setHeader(routeHeader);
            // Create the client transaction.
            ClientTransaction inviteTid = sipProvider
                    .getNewClientTransaction(request);
            Dialog dialog = inviteTid.getDialog();
            // Set a pointer to our application data
            Appdata appdata = new Appdata(dialog);
            dialog.setApplicationData(appdata);
            // send the request out.
            inviteTid.sendRequest();
            .incrementAndGet();
                // System.out.println(nbConcurrentInvite);
        } catch (Exception ex) {
            ..println(ex.getMessage());
            ex.printStackTrace();
            usage();
        }
    }
    int currentProvider = 0;
    private SipProvider getNextProvider() {
        synchronized (this) {
            ++;
            if ( >= ) {
                 = 0;
            }
            return [];
        }
    }
    public void createProvider(SipListener listenerthrows Exception {
         = new SipProvider[];
        for (int i = 0; i < i++) {
            ListeningPoint listeningPoint = .createListeningPoint(
                    "127.0.0.1", 15060 + i);
            [i] = .createSipProvider(listeningPoint);
            [i].addSipListener(listener);
        }
    }
    public void processIOException(IOExceptionEvent exceptionEvent) {
        ..println("IOException occured while retransmitting requests:"
                + exceptionEvent);
    }
    public void processTransactionTerminated(
            TransactionTerminatedEvent transactionTerminatedEvent) {
        // System.out.println("Transaction Terminated event: " +
        // transactionTerminatedEvent);
    }
    public void processDialogTerminated(
            DialogTerminatedEvent dialogTerminatedEvent) {
        // System.out.println("Dialog Terminated event: " +
        // dialogTerminatedEvent);
    }
    public Shootist() {
        TimerTask testTimer = new TestTimer(this);
        ..schedule(testTimer, 20000 *  / 100);
    }
    public static int getNdialogs() {
        return ;
    }
    public static void main(String args[]) throws Exception {
        ProtocolObjects.init("shootist"true);
         = Integer.parseInt(args[0]);
        final Shootist shootist = new Shootist();
        shootist.createProvider(shootist);
        shootist.start = System.currentTimeMillis();
        while (shootist.byeCount < ) {
                while (shootist.nbConcurrentInvite.intValue() >= ) {
                    ..println("Waiting for max invite count to go down!");
                    synchronized(shootist) {
                     try {
                        shootist.wait();
                     } catch (Exception ex) {
                     }
                    }
                }
            if (shootist.byeCount == 0) {
                shootist.start = System.currentTimeMillis();
            }
            if (.equalsIgnoreCase("udp")) {
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                }
            }
            shootist.sendInvite();
        }
    }
New to GrepCode? Check out our FAQ X