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

TestSipCommunication

public class TestSipCommunication extends com.sun.midp.i3test.TestCase implements Runnable
Tests for end-to-end SIP client-server communication

Fields Summary
javax.microedition.sip.SipConnectionNotifier
scn
Connection notifier.
javax.microedition.sip.SipServerConnection
ssc
Server connection.
javax.microedition.sip.SipClientConnection
sc
Client connection.
String
transport
Communication transport.
boolean
serverEstablished
Server flag.
Thread
t1
Thread that is blocked for acceptAndOpen.
Constructors Summary
Methods Summary
voidcleanup()
Close all connections.

        try {
            if (sc != null) {
                sc.close();
            }

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

            if (scn != null) {
                scn.close();
            }
        } catch (IOException ioe) {
            assertNull("Unexpected IOException during cleanup", ioe);
            ioe.printStackTrace();
        } 
    
booleanprocessClientRequest()
Process the client request at server and receive the message

return
true if the client request is received at server false if the request is not received

        boolean requestProcessed = false;

        StringBuffer receivedMsgStr = new StringBuffer();

        // Wait for the request to receive at server
        synchronized (this) {
            while (!serverEstablished) {
                try {
                    wait(2000);
                } catch (InterruptedException e) {
                    // Ignore interrupt
                }
            } 
        }

        if (serverEstablished) {
            try {
                // what was the SIP method
                String method = ssc.getMethod();
                if (method.equals("MESSAGE")) {
                    // read the content of the MESSAGE
                    String contentType = ssc.getHeader("Content-Type");
                    if ((contentType != null) && 
                        contentType.equals("text/plain")) {
                        InputStream is = ssc.openContentInputStream();
                        int ch;
                        // read content
                        ch = is.read();
                        while ((ch = is.read()) != -1) {
                            receivedMsgStr.append(ch);
                        }
                    }
                    // initialize SIP 200 OK and send it back
                    ssc.initResponse(200);
                    ssc.send();
                    requestProcessed = true;
                }
            } catch (Exception ex) {
                // handle Exceptions
                ex.printStackTrace();
            } finally {
            }
        }
        return requestProcessed;
    
booleanreceiveMsgFromServer()
Receive the message and extract status code at server

return
true if the status code is OK false otherwise

        boolean receivedOKResponse = false;
        try {

           // wait maximum 15 seconds for response
           boolean ok = sc.receive(15000);

           if (ok) {  // response received
               if (sc.getStatusCode() == 200) {
                    receivedOKResponse = true;
               }
           }
        } catch (Exception ex) {
            // handle Exceptions
            ex.printStackTrace();
        } finally {
        } 
        return receivedOKResponse;
    
public voidrun()
Accept a new connection request and process the request from client.

        try {
            // block and wait for incoming request.
            // SipServerConnection is establised and returned
            // when new request is received.
            ssc = scn.acceptAndOpen();

            assertNotNull("ssc is null", ssc);
            serverEstablished = true;
        } catch (SipException sipex) {
            assertNull("Unexpected SipException", sipex);
            sipex.printStackTrace();
            cleanup();
        } catch (IOException ioe) {
            assertNull("Unexpected IOException", ioe);
            ioe.printStackTrace();
            cleanup();
        } finally {
            synchronized (this) {
                notifyAll();
            }
        }
    
public voidrunTests()
Tests execute.


        declare("Test TestSipCommunication on " + transport);
        setup();
        testSipCommunication();        
        cleanup();
        assertTrue("OK", true); // temporary

        transport = "tcp";
        declare("Test TestSipCommunication on " + transport);
        setup();
        testSipCommunication();        
        cleanup();
        assertTrue("OK", true); // temporary

    
booleansendMsgFromClient(java.lang.String msg)
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

param
msg message to be sent
return
true if sc is non-null and message is sent successfully false otherwise

        if (sc != null) {
	    sc.initRequest("MESSAGE", null);
            sc.setHeader("From", "sip:sippy.tester@"+scn.getLocalAddress());
            sc.setHeader("Subject", "testing...");

            // write message body
            sc.setHeader("Content-Type", "text/plain");
            sc.setHeader("Content-Length", Integer.toString(msg.length()));
            OutputStream os = sc.openContentOutputStream();
            os.write(msg.getBytes());
            os.close(); // close stream and send the message
        
            return true;
        }

        return false;
    
voidsetup()
Open the connection and start the thread to call the run method. It waits until the InputStream is open before returning.


                               
      
        serverEstablished = false;
        try {
            // Open SIP server connection and listen to port 5060
            scn = (SipConnectionNotifier) 
                Connector.open("sip:;transport="+transport);
        } catch (Exception ex) {
            assertNull("Exception during scn open", scn);
            ex.printStackTrace();
        }
        assertNotNull("scn is null", scn);

        try {
            // Open SIP client connection to the local SIP server
            sc = (SipClientConnection) Connector.open("sip:" +
                                                      scn.getLocalAddress() +
                                                      ":" +
                                                      scn.getLocalPort() +
                                                      ";transport=" +
                                                      transport);
        } catch (Exception ex) {
            assertNull("Exception during sc open", sc);
            ex.printStackTrace();
            cleanup();
        }

        assertNotNull("sc is null", sc);

        // Launch a thread which would be blocked for scn.acceptAndOpen()
        t1 = new Thread(this);
        t1.start();
    
public voidtestSipCommunication()
Send request and receive a responce.

        boolean result = false;
        try {
            result = sendMsgFromClient("Hello SIP-World");
        } catch (SipException sipex) {
            assertNull("Unexpected SipException", sipex);
            sipex.printStackTrace();
        } catch (IOException ioe) {
            assertNull("Unexpected IOException", ioe);
            ioe.printStackTrace();
        } 

        if (result) {
            result = processClientRequest();
        } 

        if (result) {
            result = receiveMsgFromServer();
        } 

        assertTrue("SIP client is not communicating to server", result);