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

TestOriginatingInvite

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

Fields Summary
SipConnectionNotifier
scn
Connection notifier.
SipClientConnection
scc
Client connection.
boolean
done
Boolean flag to indicate that test was successful
String
clientMsg
SIP request message from client to server
String
serverMsg
SIP response message from server to client
Constructors Summary
Methods Summary
voidcleanup()
Close all connections.

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

            /*
            if (ssc != null) {
                ssc.close();
            }

            assertEquals("State should be TERMINATED",
                SipServerConnectionImpl.TERMINATED, 
                ((SipServerConnectionImpl)ssc).getState());
            */

            if (scn != null) {
                scn.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);

            assertEquals("State should be REQUEST_RECEIVED",
                    SipServerConnectionImpl.REQUEST_RECEIVED, 
                    ((SipServerConnectionImpl)ssc).getState());
            // System.out.println("Request received : " + ssc.getMethod());

            if (ssc.getMethod().equals("INVITE")) {
                ssc.initResponse(200);

                assertEquals("State should be INITIALIZED",
                    SipServerConnectionImpl.INITIALIZED, 
                    ((SipServerConnectionImpl)ssc).getState());
                
                // write message body to respond back to client
                ssc.setHeader("Content-Type", "text/plain");
                ssc.setHeader("Content-Length", 
                        Integer.toString(serverMsg.length()));
                OutputStream os = ssc.openContentOutputStream();
                assertEquals("State should be STREAM_OPEN",
                    SipServerConnectionImpl.STREAM_OPEN, 
                    ((SipServerConnectionImpl)ssc).getState());
                
                os.write(serverMsg.getBytes());
                os.close(); // close stream and send the message
                
                assertEquals("State should be COMPLETED",
                    SipServerConnectionImpl.COMPLETED, 
                    ((SipServerConnectionImpl)ssc).getState());
                
                // ssc.send();
            } else if (ssc.getMethod().equals("ACK")) {
                // System.out.println("ACK received at server");
                SipDialog dialog = ssc.getDialog();
                SipClientConnection scm = dialog.getNewClientConnection("MESSAGE");
                scm.send();
                // scc.setHeader("Content-Type", "text/plain");
           } else if (ssc.getMethod().equals("MESSAGE")) {
                // System.out.println("MESSAGE received at client");
                SipDialog dialog = ssc.getDialog();
                assertNotNull("dialog is null", dialog);
                synchronized (this) {
                    done = true;
                    notify();
                }
            }
            ssc.close();
        } catch (SipException sipex) {
            assertNull("Unexpected SipException", sipex);
            sipex.printStackTrace();
            cleanup();
        } 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) {
                    String contentType = sccLocal.getHeader("Content-Type");
                    String strLength = sccLocal.getHeader("Content-Length");
                    strLength.trim();
                    int contentLength = Integer.parseInt(strLength);
                    
                    assertEquals(
                            "Server response message content length incorrect", 
                            serverMsg.length(), contentLength);
                    
                    byte[] readBytes = new byte[contentLength];

                    if ((contentType != null) && 
                        contentType.equals("text/plain")) {
                        InputStream is = sccLocal.openContentInputStream();
                        is.read(readBytes, 0, contentLength);
                        String receivedMsg = new String(readBytes);
                        assertEquals(
                          "Received response message content length incorrect", 
                            receivedMsg.length(), contentLength);
                    }

                    scc.initAck();
                    scc.setHeader("Content-Type", "text/plain");
                    scc.send();
                }
           }
        } catch (Exception ex) {
            // handle Exceptions
            ex.printStackTrace();
        } 
    
public voidrunTests()
Tests execute.

        declare("Test TestOriginatingINVITE ");

        setup();
        testOriginatingInvite();        

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

        cleanup();
    
        assertTrue("Failure in TestOriginatingInvite", 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 5090
            scn = (SipConnectionNotifier) Connector.open("sip:5090");
        } 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:5090");
        
            scc.setListener(this);
        } catch (Exception ex) {
            assertNull("Exception during sc open", scc);
            ex.printStackTrace();
            cleanup();
        }

        assertNotNull("sc is null", scc);

    
public voidtestOriginatingInvite()
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("INVITE", scn);
            //scc.setHeader("From", "sip:sippy.tester@localhost");
            scc.setHeader("From", "sip:alice@some.server");
            scc.setHeader("To", "sip:bob@some.other.server");
            scc.setHeader("Subject", "Hello...");
            scc.setHeader("Contact", "sip:user@"+scn.getLocalAddress() + 
                    ":" + scn.getLocalPort()); 

            // write message body
            scc.setHeader("Content-Type", "text/plain");
            scc.setHeader("Content-Length", 
                    Integer.toString(clientMsg.length()));
            OutputStream os = scc.openContentOutputStream();
            os.write(clientMsg.getBytes());
            os.close(); // close stream and send the message
       
            // System.out.println("Inviting..." + scc.getHeader("To"));
        } catch (IOException ioe) {
            ioe.printStackTrace();
            cleanup();
            fail("IOException in testOriginatingInvite");
        }