001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package javax.microedition.sip;
028:
029: import com.sun.midp.i3test.TestCase;
030: import javax.microedition.io.Connector;
031: import java.io.InputStream;
032: import java.io.OutputStream;
033: import java.io.IOException;
034: import javax.microedition.sip.*;
035: import gov.nist.microedition.sip.SipServerConnectionImpl;
036:
037: /**
038: * Tests for end-to-end SIP client-server communication
039: *
040: */
041: public class TestRegisterTCP extends TestCase implements
042: SipClientConnectionListener, SipServerConnectionListener {
043:
044: /** Connection notifier. */
045: SipConnectionNotifier scn = null;
046: /** Connection notifier. */
047: SipConnectionNotifier scnAssociated = null;
048: /** Server connection. */
049: // SipServerConnection ssc = null;
050: /** Client connection. */
051: SipClientConnection scc = null;
052: /** Boolean flag to indicate that test was successful */
053: boolean done = false;
054:
055: /**
056: * Open the connection and start the thread to call the run
057: * method. It waits until the InputStream is open before returning.
058: */
059: void setup() {
060: try {
061: // Open SIP server connection and listen to port 9090
062: // scn = (SipConnectionNotifier) Connector.open("sip:9090");
063: scn = (SipConnectionNotifier) Connector
064: .open("sip:9090;transport=tcp");
065: // scnAssociated = (SipConnectionNotifier)
066: // Connector.open("sip:;transport=tcp");
067: } catch (Exception ex) {
068: assertNull("Exception during scn open", scn);
069: ex.printStackTrace();
070: }
071:
072: assertNotNull("scn is null", scn);
073:
074: try {
075: scn.setListener(this );
076:
077: // Open SIP client connection to the local SIP server
078: scc = (SipClientConnection) Connector
079: .open("sip:sippy.tester@localhost:9090;transport=tcp");
080:
081: scc.setListener(this );
082: } catch (Exception ex) {
083: assertNull("Exception during sc open", scc);
084: ex.printStackTrace();
085: cleanup();
086: }
087:
088: assertNotNull("sc is null", scc);
089:
090: }
091:
092: /**
093: * Send a SIP message from user agent client to user agent server
094: * SipConnectionNotifier queues the incoming requests if the
095: * instance of SipServerConnection is not available yet
096: *
097: */
098: public void testSendRegister() {
099:
100: try {
101: // scc.initRequest("REGISTER", scnAssociated);
102: scc.initRequest("REGISTER", null);
103: scc.send();
104:
105: } catch (IOException ioe) {
106: ioe.printStackTrace();
107: cleanup();
108: fail("IOException in testSendRegister");
109: }
110:
111: }
112:
113: /**
114: * Accept a new connection request and process the request from client.
115: * Send "OK" to client
116: *
117: * This method is declared in SipServerConnectionListener
118: *
119: * @param scnLocal Local SIP connection notifier
120: */
121: public void notifyRequest(SipConnectionNotifier scnLocal) {
122: try {
123: // block and wait for incoming request.
124: // SipServerConnection is establised and returned
125: // when new request is received.
126: SipServerConnection ssc = scnLocal.acceptAndOpen();
127: assertNotNull("ssc is null", ssc);
128:
129: if (ssc.getMethod().equals("REGISTER")) {
130: ssc.initResponse(200);
131: ssc.send();
132: }
133: ssc.close();
134: } catch (IOException ioe) {
135: assertNull("Unexpected IOException", ioe);
136: ioe.printStackTrace();
137: cleanup();
138: }
139: }
140:
141: /**
142: * Received the response from user agent server
143: *
144: * This method is declared in SipClientConnectionListener
145: *
146: * @param sccLocal Local SIP Client (UAC)
147: *
148: */
149: public void notifyResponse(SipClientConnection sccLocal) {
150: try {
151:
152: boolean ok = sccLocal.receive(0);
153:
154: if (ok) { // response received
155: if (sccLocal.getStatusCode() == 200) {
156: synchronized (this ) {
157: done = true;
158: notify();
159: }
160: }
161: }
162: } catch (Exception ex) {
163: // handle Exceptions
164: ex.printStackTrace();
165: }
166: }
167:
168: /**
169: * Close all connections.
170: *
171: */
172: void cleanup() {
173: // System.out.println("cleanup called");
174: try {
175: if (scc != null) {
176: scc.close();
177: }
178:
179: if (scn != null) {
180: scn.close();
181: }
182:
183: if (scnAssociated != null) {
184: scnAssociated.close();
185: }
186: } catch (IOException ioe) {
187: assertNull("Unexpected IOException during cleanup", ioe);
188: ioe.printStackTrace();
189: }
190: }
191:
192: /**
193: * Tests execute.
194: *
195: */
196: public void runTests() {
197: declare("TestRegisterTCP ");
198:
199: setup();
200: testSendRegister();
201:
202: synchronized (this ) {
203: while (!done) {
204: try {
205: wait();
206: } catch (InterruptedException e) {
207: System.out.println("Catch interrupt");
208: }
209: }
210: }
211:
212: cleanup();
213:
214: assertTrue("Failure in TestRegisterTCP", done);
215: }
216:
217: }
|