Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package test.unit.gov.nist.javax.sip.stack.timeoutontermineted;
  
  import java.util.Random;
  import java.util.Timer;
  
  import javax.sip.Dialog;
 
 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):
Bartosz Baranowski
 
 
 public class Shootme implements SipListener {
 
     private static final String myAddress = "127.0.0.1";
 
 
     private SipProvider sipProvider;
 
     private final int myPort;
 
     private static String unexpectedException = "Unexpected exception ";
 
     private static Logger logger = Logger.getLogger(Shootme.class);
 
     private boolean inviteSeen;
 
     private final SipStack sipStack;
 
     private static AddressFactory addressFactory;
 
     private static MessageFactory messageFactory;
 
     private static HeaderFactory headerFactory;
 
     private static final String transport = "udp";
 
     private static Timer timer = new Timer();
     private boolean seen_txTermseen_txTimeoutseen_dte;
 
     private ServerTransaction inviteTid;
 
     private Dialog inviteDialog;
 
 
     private final int delay;
 
     class MyTimerTask extends TimerTask {
         RequestEvent requestEvent;
         String toTag;
         ServerTransaction serverTx;
 
         public MyTimerTask(RequestEvent requestEventServerTransaction txString toTag) {
             .info("MyTimerTask ");
             this. = requestEvent;
             this. = toTag;
             this. = tx;
 
         }
 
         @Override
         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);
        }
    }
    public void processResponse(ResponseEvent responseEvent) {
    }



    
Process the invite request.
    public void processInvite(RequestEvent requestEventServerTransaction 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();
             = st;
            if (st == null) {
                .info("null server tx -- getting a new one");
                st = sipProvider.getNewServerTransaction(request);
            }
            .info("getNewServerTransaction : " + st);
            // Create the 100 Trying response.
            Response response = .createResponse(.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);
            Thread.sleep(timeToSleep);
            st.sendResponse(response);
            ContactHeader contactHeader = .createContactHeader(address);
            response.addHeader(contactHeader);
            String toTag = new Integer(new Random().nextInt()).toString();
            Dialog dialog = st.getDialog();
             = dialog;
            .terminateOnBye(true);
            dialog.setApplicationData(st);
            this. = true;
            .schedule(new MyTimerTask(requestEventsttoTag), this.);
        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(0);
        }
    }
    private void sendInviteOK(RequestEvent requestEventServerTransaction inviteTidString toTag) {
        try {
            .info("sendInviteOK: " + inviteTid);
            if (inviteTid.getState() == .) {
                .info("shootme: Dialog state before OK: " + inviteTid.getDialog().getState());
                ..println("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();
                ((ToHeaderokResponse.getHeader(.)).setTag(toTag);
                Address address = .createAddress("Shootme <sip:" +  + ":" + myPort + ">");
                ContactHeader contactHeader = .createContactHeader(address);
                okResponse.addHeader(contactHeader);
                inviteTid.sendResponse(okResponse);
                .info("shootme: Dialog state after OK: " + inviteTid.getDialog().getState());
                TestCase.assertEquals(.inviteTid.getDialog().getState());
            } else {
                .info("semdInviteOK: inviteTid = " + inviteTid + " state = " + inviteTid.getState());
                ..println("sentInviteOK: inviteTid = " + inviteTid + " state = " + inviteTid.getState());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    public void processIOException(IOExceptionEvent exceptionEvent) {
        .error("IOException happened for " + exceptionEvent.getHost() + " port = " + exceptionEvent.getPort());
        TestCase.fail("Unexpected exception");
    }
    public void processTransactionTerminated(TransactionTerminatedEvent transactionTerminatedEvent) {
        .info("[s]Transaction terminated event recieved: "+transactionTerminatedEvent.getServerTransaction());
        ..println("[s]Transaction terminated event recieved: "+transactionTerminatedEvent.getServerTransaction());
        //if (transactionTerminatedEvent.getClientTransaction() == inviteTid)
             = true;
    }
    public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
        .info("[s]Transaction timedout event recieved: "+timeoutEvent.getServerTransaction());
        ..println("[s]Transaction timedout event recieved: "+timeoutEvent.getServerTransaction());
        //if (timeoutEvent.getClientTransaction() == inviteTid)
             = true;
    }
    public void processDialogTerminated(DialogTerminatedEvent dialogTerminatedEvent) {
        .info("[s]Dialog terminated event recieved: "+dialogTerminatedEvent.getDialog());
        ..println("[s]Dialog terminated event recieved: "+dialogTerminatedEvent.getDialog());
        //if (dialogTerminatedEvent.getDialog() == inviteDialog)
             = true;
    }
    public SipProvider createProvider() {
        try {
             = .createSipProvider(lp);
            .info("provider " + );
            .info("sipStack = " + );
            return ;
        } catch (Exception ex) {
            .error(ex);
            TestCase.fail();
            return null;
        }
    }
    public Shootme(int myPortint delay ) {
        this. = myPort;
        this. = delay;
        SipObjects sipObjects = new SipObjects(myPort"shootme""on");
         = sipObjects.addressFactory;
         = sipObjects.messageFactory;
         = sipObjects.headerFactory;
        this. = sipObjects.sipStack;
    }
    public void checkState() {
        TestCase.assertTrue("INVTE must be observed",);
        TestCase.assertTrue("INVITE transaction should temrinate.");
        TestCase.assertFalse("INVITE transaction should not timeout.");
        TestCase.assertTrue("INVITE dialog should die.");
    }
    public void stop() {
        this..stop();
    }
New to GrepCode? Check out our FAQ X