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

TestMultiNotifiers

public class TestMultiNotifiers extends com.sun.midp.i3test.TestCase implements SipServerConnectionListener
Test Multiple SipConnectionNotifiers on single host

Fields Summary
javax.microedition.sip.SipConnectionNotifier
scn1
Connection notifier.
javax.microedition.sip.SipConnectionNotifier
scn2
Connection notifier.
javax.microedition.sip.SipClientConnection
scc1
Client connection.
javax.microedition.sip.SipClientConnection
scc2
Client connection.
String
clientMsg
SIP request message from client to server
Constructors Summary
Methods Summary
voidcleanup()
Close all connections.

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

            if (scn1 != null) {
                scn1.close();
            }
            if (scn2 != null) {
                scn2.close();
            }
        } catch (IOException ioe) {
            assertNull("Unexpected IOException during cleanup", ioe);
            ioe.printStackTrace();
        } 
    
public voidnotifyRequest(javax.microedition.sip.SipConnectionNotifier scnLocal)
Receive request from client This method is declared in SipServerConnectionListener

param
scnLocal Local SIP connection notifier

        try {
            System.out.println("Local SCN port = " + scnLocal.getLocalPort());
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail("IOException in TestMultiNotifiers");
        } 

    
public voidrunTests()
Tests execute.

        declare("Test TestMultiNotifiers ");

        setup();

        testInviteMsg(scc1, scn1);        
        synchronized (this) {
            try {
                wait(500);
            } catch (InterruptedException e) {
                System.out.println("Catch interrupt");
            }
        }

        testInviteMsg(scc2, scn2);        
        synchronized (this) {
            try {
                wait(500);
            } catch (InterruptedException e) {
                System.out.println("Catch interrupt");
            }
        }

        cleanup();
    
        assertTrue("Failure in TestMultiNotifiers", true);
    
voidsetup()
Open SipConnectionNotifier(SCN1 and SCN2) on ports 6080 and 5081. Open two client connections connecting to SCN1 and SCN2


                            
      
        try {
            // Open SipConnectionNotifier on port 6080
            scn1 = (SipConnectionNotifier) Connector.open("sip:6080");
            assertNotNull("scn1 is null", scn1);
            scn1.setListener(this);
        } catch (Exception ex) {
            assertNull("Exception during scn1 open", scn1);
            ex.printStackTrace();
        }
          
        try { 
            // Open SipConnectionNotifier on port 6081
            scn2 = (SipConnectionNotifier) Connector.open("sip:6081");
            assertNotNull("scn2 is null", scn2);
            scn2.setListener(this);
        } catch (Exception ex) {
            assertNull("Exception during scn2 open", scn2);
            ex.printStackTrace();
        }

        try {
            // Open SIP client connection to the SCN ON PORT 6080
            scc1 = (SipClientConnection) 
                Connector.open("sip:sippy.tester@localhost:6080");
            assertNotNull("scc1 is null", scc1);
        } catch (Exception ex) {
            assertNull("Exception during scc1 open", scc1);
            ex.printStackTrace();
            cleanup();
        }
        
        try {
            // Open SIP client connection to the SCN ON PORT 6081
            scc2 = (SipClientConnection) 
                Connector.open("sip:sippy.tester@localhost:5081");
            assertNotNull("scc2 is null", scc2);
        } catch (Exception ex) {
            assertNull("Exception during scc2 open", scc2);
            ex.printStackTrace();
            cleanup();
        }

    
public voidtestInviteMsg(javax.microedition.sip.SipClientConnection scc, javax.microedition.sip.SipConnectionNotifier scn)
Send a SIP INVITE message

param
scc SIP client connection
param
snc SIP Connection Notifier

        
        try {
            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");
            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 TestMultiNotifiers");
        }