FileDocCategorySizeDatePackage
TestRegisterTCP.javaAPI DocphoneME MR2 API (J2ME)6384Wed May 02 18:00:40 BST 2007javax.microedition.sip

TestRegisterTCP

public class TestRegisterTCP extends com.sun.midp.i3test.TestCase implements SipClientConnectionListener, SipServerConnectionListener
Tests for end-to-end SIP client-server communication

Fields Summary
SipConnectionNotifier
scn
Connection notifier.
SipConnectionNotifier
scnAssociated
Connection notifier.
SipClientConnection
scc
Client connection.
boolean
done
Boolean flag to indicate that test was successful
Constructors Summary
Methods Summary
voidcleanup()
Close all connections.

        // System.out.println("cleanup called");
        try {
            if (scc != null) {
                scc.close();
            }

            if (scn != null) {
                scn.close();
            }

            if (scnAssociated != null) {
                scnAssociated.close();
            }
        } catch (IOException ioe) {
            assertNull("Unexpected IOException during cleanup", ioe);
            ioe.printStackTrace();
        } 
    
public voidnotifyRequest(SipConnectionNotifier scnLocal)
Accept a new connection request and process the request from client. Send "OK" to client This method is declared in SipServerConnectionListener

param
scnLocal Local SIP connection notifier

        try {
            // block and wait for incoming request.
            // SipServerConnection is establised and returned
            // when new request is received.
            SipServerConnection ssc = scnLocal.acceptAndOpen();
            assertNotNull("ssc is null", ssc);

            if (ssc.getMethod().equals("REGISTER")) {
                ssc.initResponse(200);
                ssc.send();
            }
            ssc.close();
        } catch (IOException ioe) {
            assertNull("Unexpected IOException", ioe);
            ioe.printStackTrace();
            cleanup();
        }
    
public voidnotifyResponse(SipClientConnection sccLocal)
Received the response from user agent server This method is declared in SipClientConnectionListener

param
sccLocal Local SIP Client (UAC)

        try {

           boolean ok = sccLocal.receive(0);

           if (ok) {  // response received
               if (sccLocal.getStatusCode() == 200) {
                    synchronized (this) {
                        done = true;
                        notify();
                    }
                }
           }
        } catch (Exception ex) {
            // handle Exceptions
            ex.printStackTrace();
        } 
    
public voidrunTests()
Tests execute.

        declare("TestRegisterTCP ");

        setup();
        testSendRegister();        

        synchronized (this) {
            while (!done) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    System.out.println("Catch interrupt");
                }
            }
        }

        cleanup();
    
        assertTrue("Failure in TestRegisterTCP", done);
    
voidsetup()
Open the connection and start the thread to call the run method. It waits until the InputStream is open before returning.


                               
      
        try {
            // Open SIP server connection and listen to port 9090
            // scn = (SipConnectionNotifier) Connector.open("sip:9090");
            scn = (SipConnectionNotifier) Connector.open(
                "sip:9090;transport=tcp");
            // scnAssociated = (SipConnectionNotifier) 
            //      Connector.open("sip:;transport=tcp");
        } catch (Exception ex) {
            assertNull("Exception during scn open", scn);
            ex.printStackTrace();
        }

        assertNotNull("scn is null", scn);

        try {
            scn.setListener(this);
        
            // Open SIP client connection to the local SIP server
            scc = (SipClientConnection) 
                Connector.open("sip:sippy.tester@localhost:9090;transport=tcp");
        
            scc.setListener(this);
        } catch (Exception ex) {
            assertNull("Exception during sc open", scc);
            ex.printStackTrace();
            cleanup();
        }

        assertNotNull("sc is null", scc);

    
public voidtestSendRegister()
Send a SIP message from user agent client to user agent server SipConnectionNotifier queues the incoming requests if the instance of SipServerConnection is not available yet

        
        try {
            // scc.initRequest("REGISTER", scnAssociated);
            scc.initRequest("REGISTER", null);
            scc.send();

        } catch (IOException ioe) {
            ioe.printStackTrace();
            cleanup();
            fail("IOException in testSendRegister");
        }