Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package test.unit.gov.nist.javax.sip.stack;
  
  
  
  import javax.sip.Dialog;
 
 
 import  test.tck.msgflow.callflows.ProtocolObjects;
 import  test.tck.msgflow.callflows.ScenarioHarness;
 
 public class SelfroutingTest extends ScenarioHarness {
 
     protected Shootist shootist;
 
     private static Logger logger = Logger.getLogger("test.tck");
 
     static {
         if (!.isAttached(console))
             .addAppender(console);
     }
 
     class Shootist  implements SipListener {
 
         private SipProvider provider;
 
 
         private ContactHeader contactHeader;
 
         private ListeningPoint listeningPoint;
 
         // To run on two machines change these to suit.
         public static final String myAddress = "127.0.0.1";
 
         private static final int myPort = 5060;
 
         protected ClientTransaction inviteTid;
 
 
 
 
         private ProtocolObjects protocolObjects;
 
         private Dialog dialog;
 
 
 
 
 
         public Shootist(ProtocolObjects protocolObjects) {
             super();
             this. = protocolObjects;
 
         }
 
 
 
         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);
 
             if (request.getMethod().equals(.))
                 processBye(requestserverTransactionId);
             else if (request.getMethod().equals(.))
                 processAck(requestserverTransactionId);
            else if (request.getMethod().equals(.))
                processInvite(requestserverTransactionId);
        }
        public void processBye(Request request,
                ServerTransaction serverTransactionId) {
            try {
                .info("shootist:  got a bye .");
                if (serverTransactionId == null) {
                    .info("shootist:  null TID.");
                    return;
                }
                Response response = ..createResponse(
                        200, request);
                serverTransactionId.sendResponse(response);
            } catch (Exception ex) {
                .error("unexpected exception",ex);
                fail("unexpected exception");
            }
        }
        
        public void processInvite(Request request,
                ServerTransaction serverTransactionId) {
            try {
                Response response = ..createResponse(
                        200, request);
                .sendResponse(response);
            } catch (Exception ex) {
                .error("unexpected exception",ex);
                fail("unexpected exception");
            }
        }
        
        public void processAck(Request request,
                ServerTransaction serverTransactionId) {
            try {
                .info("shootist:  got ACK .");
                if (serverTransactionId == null) {
                    .info("shootist:  null TID.");
                    return;
                }
                Request bye = .createRequest(.);
                ClientTransaction ctx = .getNewClientTransaction(bye);
                ctx.sendRequest();
            } catch (Exception ex) {
                .error("unexpected exception",ex);
                fail("unexpected exception");
            }
        }
        
        public boolean okToInviteReceived;
        public void processResponse(ResponseEvent responseReceivedEvent) {
            .info("Got a response");
            Response response = (ResponseresponseReceivedEvent.getResponse();
            Transaction tid = responseReceivedEvent.getClientTransaction();
            .info("Response received with client transaction id " + tid
                    + ":\n" + response.getStatusCode());
            if (tid != null) {
				.info("Dialog = " + responseReceivedEvent.getDialog());
				.info("Dialog State is "
responseReceivedEvent.getDialog().getState());
			}
            SipProvider provider = (SipProviderresponseReceivedEvent.getSource();
            try {
            	if (response.getStatusCode() == .) {
            		if(((CSeqHeaderresponse.getHeader(.))
            				.getMethod().equals(.)) {
            			 = true;
            		}
            	}
            } catch (Exception ex) {
                .error(ex);
                ex.printStackTrace();
                fail("unexpected exception");
            }
        }
        public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
            .info("Transaction Time out");
            .info("TimeoutEvent " + timeoutEvent.getTimeout());
        }
        public SipProvider createSipProvider() {
            try {
                 = ..createListeningPoint(
                        .);
                 = .
                        .createSipProvider();
                return ;
            } catch (Exception ex) {
                .error(ex);
                fail("unable to create provider");
                return null;
            }
        }
        public void sendInvite() {
            try {
                // Note that a provider has multiple listening points.
                // all the listening points must have the same IP address
                // and port but differ in their transport parameters.
                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(
                        fromNamefromSipAddress);
                Address fromNameAddress = .
                        .createAddress(fromAddress);
                fromNameAddress.setDisplayName(fromDisplayName);
                FromHeader fromHeader = .
                        .createFromHeader(fromNameAddressnew Integer((int) (Math
                                .random() * .)).toString());
                // create To Header
                SipURI toAddress = ..createSipURI(
                        toUsertoSipAddress);
                Address toNameAddress = .
                        .createAddress(toAddress);
                toNameAddress.setDisplayName(toDisplayName);
                ToHeader toHeader = ..createToHeader(
                        toNameAddressnull);
                // create Request URI addressed to me
                SipURI requestURI = ..createSipURI(
                        toUser + ":" + );
                // Create ViaHeaders
                ArrayList viaHeaders = new ArrayList();
                int port = .getListeningPoint(.)
                        .getPort();
                ViaHeader viaHeader = .
                        .createViaHeader(port,
                                .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 = ..createCallIdHeader( callIdHeader.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.callIdHeadercSeqHeader,
                        fromHeadertoHeaderviaHeadersmaxForwards);
                // Create contact headers
                // Create the contact name address.
                SipURI contactURI = ..createSipURI(
                        fromName);
                contactURI.setPort(.getListeningPoint(
                        .).getPort());
                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);
                String sdpData = "v=0\r\n"
                        + "o=4855 13760799956958020 13760799956958020"
                        + " IN IP4  129.6.55.78\r\n" + "s=mysession session\r\n"
                        + "p=+46 8 52018010\r\n" + "c=IN IP4  129.6.55.78\r\n"
                        + "t=0 0\r\n" + "m=audio 6022 RTP/AVP 0 4 18\r\n"
                        + "a=rtpmap:0 PCMU/8000\r\n" + "a=rtpmap:4 G723/8000\r\n"
                        + "a=rtpmap:18 G729A/8000\r\n" + "a=ptime:20\r\n";
                request.setContent(sdpDatacontentTypeHeader);
                // The following is the preferred method to route requests
                // to the peer. Create a route header and set the "lr"
                // parameter for the router header.
                Address address = .
                        .createAddress("<sip:" +  + ":" + 
                                + ">");
                // SipUri sipUri = (SipUri) address.getURI();
                // sipUri.setPort(PEER_PORT);
                RouteHeader routeHeader = .
                        .createRouteHeader(address);
                ((SipURI)address.getURI()).setLrParam();
                request.addHeader(routeHeader);
                extensionHeader = ..createHeader(
                        "My-Other-Header""my new header value ");
                request.addHeader(extensionHeader);
                Header callInfoHeader = ..createHeader(
                        "Call-Info""<http://www.antd.nist.gov>");
                request.addHeader(callInfoHeader);
                // Create the client transaction.
                this. = .getNewClientTransaction(request);
                this. = this..getDialog();
                // Note that the response may have arrived right away so
                // we cannot check after the message is sent.
                assertTrue(this..getState() == null);
                // send the request out.
                this..sendRequest();
            } catch (Exception ex) {
                .error("Unexpected exception"ex);
                fail("unexpected exception");
            }
        }
        /*
         * (non-Javadoc)
         *
         * @see javax.sip.SipListener#processIOException(javax.sip.IOExceptionEvent)
         */
        public void processIOException(IOExceptionEvent exceptionEvent) {
            .error("IO Exception!");
            fail("Unexpected exception");
        }
        /*
         * (non-Javadoc)
         *
         * @see javax.sip.SipListener#processTransactionTerminated(javax.sip.TransactionTerminatedEvent)
         */
        public void processTransactionTerminated(
                TransactionTerminatedEvent transactionTerminatedEvent) {
            .info("Transaction Terminated Event!");
        }
        /*
         * (non-Javadoc)
         *
         * @see javax.sip.SipListener#processDialogTerminated(javax.sip.DialogTerminatedEvent)
         */
        public void processDialogTerminated(
                DialogTerminatedEvent dialogTerminatedEvent) {
            .info("Dialog Terminated Event!");
        }
    }
    public SelfroutingTest()
    {
        super("Selfrouting"true);
    }
    public void setUp() {
    }
    public void testSelfroutingUDP() {
        try {
            this. = "udp";
            super.setUp();
             = new Shootist(getRiProtocolObjects());
            SipProvider shootistProvider = .createSipProvider();
            shootistProvider.addSipListener();
            SipStackImpl ss = (SipStackImplshootistProvider.getSipStack();
            getRiProtocolObjects().start();
            if (getTiProtocolObjects() != getRiProtocolObjects())
                getTiProtocolObjects().start();
        } catch (Exception ex) {
            ex.printStackTrace();
            fail("unexpected exception ");
        }
    	new Thread() {
    		@Override
    		public void run() {
    			super.run();
    		}
    	}.start();
        
        try {
			Thread.sleep(4000);
catch (InterruptedException e) {
		}
    }
    
    public void testSelfroutingTCP() {
        try {
            this. = "tcp";
            super.setUp();
             = new Shootist(getRiProtocolObjects());
            SipProvider shootistProvider = .createSipProvider();
            shootistProvider.addSipListener();
            getRiProtocolObjects().start();
            if (getTiProtocolObjects() != getRiProtocolObjects())
                getTiProtocolObjects().start();
        } catch (Exception ex) {
            ex.printStackTrace();
            fail("unexpected exception ");
        }
    	new Thread() {
    		@Override
    		public void run() {
    			super.run();
    		}
    	}.start();
        
        try {
			Thread.sleep(4000);
catch (InterruptedException e) {
		}
    }
    public void tearDown() {
        try {
            Thread.sleep(1000);
            getTiProtocolObjects().destroy();
            if (getTiProtocolObjects() != getRiProtocolObjects())
                getRiProtocolObjects().destroy();
            Thread.sleep(1000);
            this..clear();
            logTestCompleted();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
New to GrepCode? Check out our FAQ X