Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package test.unit.gov.nist.javax.sip.stack;
  
  
  import java.util.Random;
  
 
 import  junit.framework.TestCase;
 
 public class NoAutoDialogTest extends TestCase {
 
     protected boolean testFail = false;
 
     protected StringBuffer errorBuffer = new StringBuffer();
 
     protected int myPort;
 
     protected int remotePort = 5060;
 
     protected String testProtocol = "udp";
 
     protected Address remoteAddress;
 
     protected SipURI requestUri;
 
     protected MessageFactory messageFactory = null;
 
     protected HeaderFactory headerFactory = null;
 
     protected AddressFactory addressFactory = null;
 
     protected static String host = null;
 
     protected Client client;
 
     protected Server server;
 
     public final int SERVER_PORT = 5600;
 
     public final int CLIENT_PORT = 6500;
 
     public NoAutoDialogTest() {
 
     }
 
     protected void doFail(String message) {
          = true;
         .append(message + "\n");
 
     }
 
     public boolean isFailed() {
         return ;
     }
 
     public String generateFromTag() {
         return new Integer(Math.abs(new Random().nextInt())).toString();
     }
 
     @Override
     protected void setUp() throws Exception {
 
         this. = new Client();
         this. = new Server();
         Thread.sleep(500);
     }
 
     @Override
     protected void tearDown() throws Exception {
 
        this..stop();
        this..stop();
 
    }
    protected ViaHeader getLocalVia(SipProvider _providerthrows ParseException,
            InvalidArgumentException {
                .getIPAddress(), _provider.getListeningPoint().getPort(), _provider
                .getListeningPoint().getTransport(), null);
    }
    public String doMessage(Throwable t) {
        StringBuffer sb = new StringBuffer();
        int tick = 0;
        Throwable e = t;
        do {
            StackTraceElement[] trace = e.getStackTrace();
            if (tick++ == 0)
                sb.append(e.getClass().getCanonicalName() + ":" + e.getLocalizedMessage() + "\n");
            else
                sb.append("Caused by: " + e.getClass().getCanonicalName() + ":"
                        + e.getLocalizedMessage() + "\n");
            for (StackTraceElement ste : trace)
                sb.append("\t" + ste + "\n");
            e = e.getCause();
        } while (e != null);
        return sb.toString();
    }
    public class Server implements SipListener {
        protected SipStack sipStack;
        protected SipFactory sipFactory = null;
        protected SipProvider provider = null;
        private boolean i_receivedInvite;
        private boolean i_sent180;
        private boolean i_receivedCancel;
        private boolean i_sent200Cancel;
        private ServerTransaction inviteStx;
        private Request inviteRequest;
        private boolean i_inviteTxTerm;
        public Server() {
            try {
                final Properties defaultProperties = new Properties();
                 = "127.0.0.1";
                defaultProperties.setProperty("javax.sip.AUTOMATIC_DIALOG_SUPPORT""off");
                defaultProperties.setProperty("javax.sip.STACK_NAME""server");
                defaultProperties.setProperty("gov.nist.javax.sip.TRACE_LEVEL""DEBUG");
                defaultProperties.setProperty("gov.nist.javax.sip.DEBUG_LOG""server_debug_NoAutoDialogTest.txt");
                defaultProperties.setProperty("gov.nist.javax.sip.SERVER_LOG""server_log_NoAutoDialogTest.txt");
                defaultProperties.setProperty("gov.nist.javax.sip.READ_TIMEOUT""1000");
                defaultProperties.setProperty("gov.nist.javax.sip.CACHE_SERVER_CONNECTIONS",
                        "false");
                this. = SipFactory.getInstance();
                this..setPathName("gov.nist");
                this. = this..createSipStack(defaultProperties);
                this..start();
                ListeningPoint lp = this..createListeningPoint();
                this. = this..createSipProvider(lp);
                 = this..createHeaderFactory();
                 = this..createMessageFactory();
                 = this..createAddressFactory();
                this..addSipListener(this);
            } catch (Exception e) {
                e.printStackTrace();
                fail("unexpected exception ");
            }
        }
        public void stop() {
            this..stop();
        }
        public void processDialogTerminated(DialogTerminatedEvent dte) {
            fail("Unexpected Dialog Terminated Event");
        }
        public void processIOException(IOExceptionEvent arg0) {
        }
        public void processRequest(RequestEvent requestEvent) {
            ..println("PROCESS REQUEST ON SERVER");
            Request request = requestEvent.getRequest();
            SipProvider provider = (SipProviderrequestEvent.getSource();
            if (request.getMethod().equals(.)) {
                try {
                    ..println("Received invite");
                    this. = true;
                    this. = provider
                            .getNewServerTransaction(request);
                    this. = request;
                    Response response = .createResponse(100, request);
                    .sendResponse(response);
                    response = .createResponse(180, request);
                    ((ToHeaderresponse.getHeader(.)).setTag("asdgaeyvewacyta"
                            + Math.random());
                    .sendResponse(response);
                    ..println("Sent 180:\n" + response);
                     = true;
                } catch (Exception e) {
                    e.printStackTrace();
                    fail("Unexpected exception");
                }
            } else if (request.getMethod().equals(.)) {
                ..println("Received CANCEL");
                try {
                     = true;
                    Response response = .createResponse(200, requestEvent
                            .getRequest());
                    requestEvent.getServerTransaction().sendResponse(response);
                     = true;
                    Response inviteResponse =
                        .createResponse(.,);
                    .sendResponse(inviteResponse);
                } catch (Exception e) {
                    e.printStackTrace();
                    fail("Unexpected exception ");
                }
            }
        }
        public void processResponse(ResponseEvent arg0) {
        }
        public void processTimeout(TimeoutEvent arg0) {
        }
        public void processTransactionTerminated(TransactionTerminatedEvent tte) {
            ClientTransaction ctx = tte.getClientTransaction();
            ServerTransaction stx = tte.getServerTransaction();
            if (ctx != null) {
                String method = ctx.getRequest().getMethod();
                if (method.equals(.)) {
                     = true;
                    ..println("Invite term TERM");
                }
            } else {
                String method = stx.getRequest().getMethod();
                if (method.equals(.)) {
                     = true;
                    ..println("Invite term TERM");
                }
            }
        }
    }
    public class Client implements SipListener {
        private SipFactory sipFactory;
        private SipStack sipStack;
        private SipProvider provider;
        private boolean o_sentInviteo_received180o_sentCancelo_receiver200Cancel,
                o_inviteTxTermo_dialogTerinated;
        public Client() {
            try {
                final Properties defaultProperties = new Properties();
                 = "127.0.0.1";
                defaultProperties.setProperty("javax.sip.AUTOMATIC_DIALOG_SUPPORT""off");
                defaultProperties.setProperty("javax.sip.STACK_NAME""client");
                defaultProperties.setProperty("gov.nist.javax.sip.TRACE_LEVEL""DEBUG");
                defaultProperties.setProperty("gov.nist.javax.sip.DEBUG_LOG""client_debug_NoAutoDialogTest.txt");
                defaultProperties.setProperty("gov.nist.javax.sip.SERVER_LOG""client_log_NoAutoDialogTest.txt");
                defaultProperties.setProperty("gov.nist.javax.sip.READ_TIMEOUT""1000");
                defaultProperties.setProperty("gov.nist.javax.sip.CACHE_SERVER_CONNECTIONS",
                        "false");
                this. = SipFactory.getInstance();
                this..setPathName("gov.nist");
                this. = this..createSipStack(defaultProperties);
                this..start();
                ListeningPoint lp = this..createListeningPoint();
                this. = this..createSipProvider(lp);
                 = this..createHeaderFactory();
                 = this..createMessageFactory();
                 = this..createAddressFactory();
                this..addSipListener(this);
            } catch (Exception e) {
                e.printStackTrace();
                fail("unexpected exception ");
            }
        }
        public void checkState() {
            if (! || ! || ! || !
                    || ! || !) {
                fail("FAILED o_sentInvite[" +  + "] o_received180[" + 
                        + "] o_sentCancel[" +  + "] o_receiver200Cancel["
                        +  + "] o_inviteTxTerm[" + 
                        + "] o_dialogTerinated[" +  + "]  ");
            }
        }
        public void stop() {
            this..stop();
        }
        public void processDialogTerminated(DialogTerminatedEvent arg0) {
            ..println("Dialog term TERM");
             = true;
        }
        public void processIOException(IOExceptionEvent ioexception) {
            fail("Unexpected IO Exception");
        }
        public void processRequest(RequestEvent arg0) {
        }
        public void processResponse(ResponseEvent responseEvent) {
            Response response = responseEvent.getResponse();
            int code = response.getStatusCode();
            if (code == 180) {
                try {
                     = true;
                    Request cancel = responseEvent.getClientTransaction().createCancel();
                    ClientTransaction cancelTX = .getNewClientTransaction(cancel);
                    cancelTX.sendRequest();
                    ..println("Send CANCEL:\n" + cancel);
                     = true;
                } catch (SipException e) {
                    e.printStackTrace();
                    doFail(doMessage(e));
                }
            } else if (code == 200) {
                ..println("Receive Cancel200");
                 = true;
            }
        }
        public void processTimeout(TimeoutEvent arg0) {
        }
        public void processTransactionTerminated(TransactionTerminatedEvent arg0) {
            ClientTransaction ctx = arg0.getClientTransaction();
            String method = ctx.getRequest().getMethod();
            if (method.equals(.)) {
                ..println("Invite TERM");
                 = true;
            }
        }
        protected void sendLocalInvite() throws Exception {
            Request inviteRequest = .createRequest(null);
            String _host = this..getListeningPoint().getIPAddress();
            SipURI requestUri = .createSipURI(null_host);
            requestUri.setPort();
            inviteRequest.setMethod(.);
            inviteRequest.setRequestURI(requestUri);
            SipURI _remoteUri = .createSipURI(null"there.com");
            Address _remoteAddress = .createAddress(_remoteUri);
            inviteRequest.addHeader(.getNewCallId());
            inviteRequest.addHeader(.createCSeqHeader((long) 1, .));
            SipURI _localUri = .createSipURI(null"here.com");
            Address localAddress = .createAddress(_localUri);
            inviteRequest.addHeader(.createFromHeader(localAddress,
                    generateFromTag()));
            inviteRequest.addHeader(.createToHeader(_remoteAddressnull));
            SipURI contactUri = .createSipURI(null_host);
            contactUri.setPort();
            inviteRequest.addHeader(.createContactHeader(.createAddress(contactUri)));
            inviteRequest.addHeader(getLocalVia());
            // create and add the Route Header
            Header h = .createRouteHeader(_remoteAddress);
            inviteRequest.setMethod(.);
            inviteRequest.addHeader(.createMaxForwardsHeader(5));
            ClientTransaction inviteTransaction = this.
                    .getNewClientTransaction(inviteRequest);
            .getNewDialog(inviteTransaction);
            inviteTransaction.sendRequest();
            ..println("Sent INVITE:\n" + inviteRequest);
             = true;
        }
    }
    public void testSendInvite() {
        try {
            this..sendLocalInvite();
            Thread.sleep(50000);
        } catch (Exception ex) {
            ex.printStackTrace();
            fail("Unexpected exception ");
        }
        this..checkState();
    }
New to GrepCode? Check out our FAQ X