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

TestSipRefreshHelper

public class TestSipRefreshHelper extends com.sun.midp.i3test.TestCase implements SipClientConnectionListener, SipRefreshListener, SipServerConnectionListener
Tests for SipRefreshHelper class. sendRegister() example was taken from JSR180 spec.

Fields Summary
int
refreshID
Refresh identifier
int
refreshStatus
Refresh status
SipRefreshHelper
refHelper
Refresh helper
Constructors Summary
Methods Summary
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 The code was copied from TestSipOriginatingInvite

param
scnLocal Local SIP connection notifier

        SipServerConnection ssc = null;

        // System.out.println(">>> Request received!");

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

            assertEquals("State should be REQUEST_RECEIVED",
                    SipServerConnectionImpl.REQUEST_RECEIVED,
                    ((SipServerConnectionImpl)ssc).getState());

            if (ssc.getMethod().equals("REGISTER")) {
                /*
                 * RFC 3261, p. 66:
                 *
                 * The registrar returns a 200 (OK) response. The response MUST
                 * contain Contact header field values enumerating all current
                 * bindings. Each Contact value MUST feature an "expires"
                 * parameter indicating its expiration interval chosen by the
                 * registrar. The response SHOULD include a Date header field.
                 */

                int i, index;
                String[] contacts = ssc.getHeaders("Contact");
                assertNotNull("contact is null", contacts);
                assertTrue("Invalid Contact header length: " + contacts.length,
                    contacts.length > 0);

                String[] newContacts = new String[contacts.length];
                assertNotNull("newContact is null", newContacts);

                for (i = 0; i < contacts.length; i++) {
                    index = contacts[i].indexOf(';");

                    // System.out.println(">>> " + contacts[i]);

                    if (index == -1) {
                        newContacts[i] = contacts[i];
                    } else {
                        newContacts[i] = contacts[i].substring(0, index);
                    }

                    newContacts[i] += ";expires=1";
                }

                // write message body to respond back to client
                ssc.initResponse(200);

                assertEquals("State should be INITIALIZED",
                    SipServerConnectionImpl.INITIALIZED,
                    ((SipServerConnectionImpl)ssc).getState());

                // remove the default Contact header
                ssc.removeHeader("Contact");

                // DEBUG
                /*
                contacts = ssc.getHeaders("Contact");
                for (i = 0; i < contacts.length; i++) {
                    System.out.println(">>> " + contacts[i]);
                }
                */
                //

                ssc.send();
            }
        } catch (SipException sipex) {
            sipex.printStackTrace();
            fail("Unexpected SipException: " + sipex);
        } catch (Exception e) {
            e.printStackTrace();
            fail("Unexpected Exception: " + e);
        }
    
public voidnotifyResponse(SipClientConnection scc)
Notifier

param
scc client connection

        try {
            // retrieve the response received
            scc.receive(0);

            // System.out.println(">>> Response received: " +
            //     scc.getStatusCode());

            if (scc.getStatusCode() == 200) {
                // handle 200 OK response
            } else {
                // handle possible error responses
               fail("Response code: " + scc.getStatusCode());
            }
        } catch (Exception ex) {
            // handle Exceptions
            fail("Exception '" + ex + "' was thrown.");
        }
    
public voidrefreshEvent(int ID, int statusCode, java.lang.String reasonPhrase)
Refresh callback.

param
ID refresh ID
param
statusCode refresh status
param
reasonPhrase

        // System.out.println(">>> Refresh event, code " + statusCode);

        refreshStatus = statusCode;

        if (statusCode == 0) {
            // stopped refresh
            // fail("stopped refresh!");
        } else if (statusCode == 200) {
            // successful refresh
        } else {
            // failed request
            fail("failed request, status: " + statusCode);
        }
    
public voidrunTests()
Run the tests.

        declare("SipRefreshHelper test: sendRegister()");
        sendRegister();
    
public voidsendRegister()
Sends 'REGISTER' request.


            
       
        SipClientConnection scc = null;
        SipConnectionNotifier scn = null;

        try {
            // Open SIP server connection and listen to port 5060
            scn = (SipConnectionNotifier)Connector.open("sip:5060");
            scn.setListener(this);

            // Initialize connection to the registrar host.com
            scc = (SipClientConnection)Connector.open("sip:localhost");
            scc.setListener(this);

            // Initialize request and set From, To and Contact headers
            scc.initRequest("REGISTER", null);
            scc.setHeader("From", "sip:sippy.user@localhost");
            scc.setHeader("To", "sip:sippy.user@localhost");
            scc.setHeader("Contact",
                          "<sip:UserB@192.168.200.201>;expires=3600");
            scc.addHeader("Contact",
                          "<mailto:UserB@biloxi.com>;expires=4294967295");

            refreshID = scc.enableRefresh(this);
            assertTrue("Invalid refreshID!", refreshID != 0);

            // System.out.println(">>> id = " + refreshID);

            scc.send();

            refHelper = SipRefreshHelper.getInstance();
            assertNotNull("refHelper is null!", refHelper);

            // -----------------------------
            //  do something else for a while
            // ------------------------------
            synchronized (this) {
                try {
                    wait(1200);
                } catch (Exception e) {
                    System.out.println(">>> Exception!!! " + e);
                }
            }

            // System.out.println(">>> (1) status = " + refreshStatus);

            // check that refresh was successful
            assertEquals("Invalid refresh status (1)!", 200, refreshStatus);

            // update REGISTER, with new "mailto:" Contact and no content
            String c[] = { "<mailto:UserB@company.com>" };
            refHelper.update(refreshID, c, null, 0, 6000);

            // -----------------------------
            //  do something else for a while
            // ------------------------------
            synchronized (this) {
                try {
                    wait(1200);
                } catch (Exception e) {
                    System.out.println(">>> Exception!!! " + e);
                }
            }

            // System.out.println(">>> (2) status = " + refreshStatus);

            // check that refresh is still ok
            assertEquals("Invalid refresh status (2)!", 200, refreshStatus);

            // stop REGISTER refresh altogether
            refHelper.stop(refreshID);
        } catch (Exception ex) { // handle Exceptions
            ex.printStackTrace();
            fail("Exception '" + ex + "' was thrown.");
        }