FileDocCategorySizeDatePackage
TestSipDialogServer.javaAPI DocphoneME MR2 API (J2ME)7548Wed May 02 18:00:40 BST 2007gov.nist.microedition.sip

TestSipDialogServer

public class TestSipDialogServer extends com.sun.midp.i3test.TestCase
Tests for SipDialog(server side) class.

Fields Summary
SipDialog
dialog
SIP dialog
SipClientConnection
scc
Client connection
SipConnectionNotifier
scn
Connection Notifier
Constructors Summary
Methods Summary
public voidTestInvite()
Test SipDialog for INVITE

        try {
            scn = (SipConnectionNotifier) Connector.open("sip:5060");
            scc = (SipClientConnection)
            Connector.open("sip:alice@localhost:5060");
            scc.initRequest("INVITE", scn);
            
            scc.setHeader("From", "sip:sippy.tester@localhost");
            scc.setHeader("Subject", "Hello...");
            scc.setHeader("Contact", "sip:user@"+scn.getLocalAddress() +
                    ":" + scn.getLocalPort());
            
            // write message body
            scc.setHeader("Content-Type", "text/plain");
            String clientMsg = "Hello, how are you?";
            scc.setHeader("Content-Length",
                    Integer.toString(clientMsg.length()));
            OutputStream os = scc.openContentOutputStream();
            os.write(clientMsg.getBytes());
            os.close(); // close stream and send the message
            
            notifyInviteRequest(scn);
            boolean resp = scc.receive(10000); // wait 10 secs for response
            if (resp) {
                if (scc.getStatusCode() == 200) {
                    // System.out.println("Received OK for INVITE");
                }
            }
            scn.close();
            scc.close();
        } catch (Exception ex) {
            // handle Exceptions
            System.out.println("Exception received");
            ex.printStackTrace();
        }
        assertTrue("Test1 done", true);
    
public voidTestSubscribe()
Test SipDialog for SUBSCRIBE

        try {
            scn = (SipConnectionNotifier) Connector.open("sip:5060");
            scc = (SipClientConnection)
            Connector.open("sip:alice@localhost:5060");
            scc.initRequest("SUBSCRIBE", scn);
            scc.setHeader("From", "sip:UserA@host.com");
            scc.setHeader("Accept", "application/pidf+xml");
            scc.setHeader("Event", "presence");
            scc.setHeader("Expires", "950");
            String contact = new String("sip:user@"+scn.getLocalAddress() + 
                    ":"+scn.getLocalPort());
            scc.setHeader("Contact", contact);
            scc.send();
            notifySubscribeRequest(scn);
            boolean resp = scc.receive(10000); // wait 10 secs for response
            if (resp) {
                if (scc.getStatusCode() == 200) {
                    // System.out.println("Received OK for subscribe");
                    /*
                    dialog = scc.getDialog();
                    // initialize new SipClientConnection
                    scc = dialog.getNewClientConnection("SUBSCRIBE");
                    // read dialog Call-ID
                    callID = scc.getHeader("Call-ID");
                    // read remote tag
                    SipHeader sh = new SipHeader("To", scc.getHeader("To"));
                    remoteTag = sh.getParameter("tag");
                    // unSUBSCRIBE
                    scc.setHeader("Expires", "0");
                    scc.send();
                    */
                }
            } else {
                // didn't receive any response in given time
            }
            scn.close();
            scc.close();
        } catch (Exception ex) {
            // handle Exceptions
            System.out.println("Exception received");
            ex.printStackTrace();
        }
        assertTrue("Test1 done", true);
    
public voidnotifyInviteRequest(SipConnectionNotifier scn)
Process INVITE request at UAS

param
scn SipConnectionNotifier at UAS

        try {
            SipServerConnection ssc;
            // retrieve the request received
            ssc = scn.acceptAndOpen();
            if (ssc.getMethod().equals("INVITE")) {
                SipDialog dialog = ssc.getDialog();
                assertTrue("Dialog is not null", dialog == null);
                ssc.initResponse(100);
                ssc.send();
                dialog = ssc.getDialog();
                assertTrue("Dialog is null after sending 100 TRYING", 
                            dialog != null);
                assertTrue("Dialog is not in EARLY state",
                        dialog.getState() == SipDialog.EARLY);
                /* 
                ssc.initResponse(200);
                ssc.send();
                assertTrue("Dialog is null after sending 200 OK", 
                            dialog != null);
                assertTrue("Dialog is not in CONFIRMED state",
                        dialog.getState() == SipDialog.CONFIRMED);
                */
            }
        } catch (Exception ex) {
            // handle Exceptions
        }
    
public voidnotifySubscribeRequest(SipConnectionNotifier scn)
Process SUBSCRIBE request at UAS

param
scn SipConnectionNotifier at UAS

        try {
            SipServerConnection ssc;
            // retrieve the request received
            ssc = scn.acceptAndOpen();
            if (ssc.getMethod().equals("SUBSCRIBE")) {
                SipDialog dialog = ssc.getDialog();
                assertTrue("Dialog is not null", dialog == null);
                ssc.initResponse(200);
                ssc.send();
                dialog = ssc.getDialog();
                assertTrue("Dialog is null after sending 200 OK", 
                            dialog != null);
                assertTrue("Dialog is not in CONFIRMED state",
                        dialog.getState() == SipDialog.CONFIRMED);
            }
        } catch (Exception ex) {
            // handle Exceptions
        }
    
public voidrunTests()
Tests execute

        declare("INVITE request");
        TestInvite();
        declare("SUBSCRIBE request");
        TestSubscribe();