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 TestPrack extends TestCase implements
042: SipClientConnectionListener, SipServerConnectionListener {
043:
044: /** Connection notifier. */
045: SipConnectionNotifier scn = null;
046: /** Server connection. */
047: // SipServerConnection ssc = null;
048: /** Client connection. */
049: SipClientConnection scc = null;
050: /** Boolean flag to indicate that test was successful */
051: boolean done = false;
052: /** SIP request message from client to server */
053: String clientMsg = "Client : How are you?";
054: /** SIP response message from server to client */
055: String serverMsg = "Server : Fine";
056:
057: /**
058: * Open the connection and start the thread to call the run
059: * method. It waits until the InputStream is open before returning.
060: */
061: void setup() {
062: try {
063: // Open SIP server connection and listen to port 6789
064: scn = (SipConnectionNotifier) Connector.open("sip:6789");
065: } catch (Exception ex) {
066: assertNull("Exception during scn open", scn);
067: ex.printStackTrace();
068: }
069:
070: assertNotNull("scn is null", scn);
071:
072: try {
073: scn.setListener(this );
074:
075: // Open SIP client connection to the local SIP server
076: scc = (SipClientConnection) Connector
077: .open("sip:sippy.tester@localhost:6789");
078:
079: scc.setListener(this );
080: } catch (Exception ex) {
081: assertNull("Exception during sc open", scc);
082: ex.printStackTrace();
083: cleanup();
084: }
085:
086: assertNotNull("sc is null", scc);
087:
088: }
089:
090: /**
091: * Send a SIP message from user agent client to user agent server
092: * SipConnectionNotifier queues the incoming requests if the
093: * instance of SipServerConnection is not available yet
094: *
095: */
096: public void testPrack() {
097:
098: try {
099: scc.initRequest("INVITE", scn);
100: // scc.setHeader("From", "sip:sippy.tester@localhost");
101: scc.setHeader("From", "sip:alice@some.server");
102: scc.setHeader("To", "sip:bob@some.other.server");
103: scc.setHeader("Subject", "Hello...");
104: scc.setHeader("Contact", "sip:user@"
105: + scn.getLocalAddress() + ":" + scn.getLocalPort());
106:
107: // write message body
108: scc.setHeader("Content-Type", "text/plain");
109: scc.setHeader("Content-Length", Integer.toString(clientMsg
110: .length()));
111: scc.setHeader("Require", "event, path, 100rel");
112: OutputStream os = scc.openContentOutputStream();
113: os.write(clientMsg.getBytes());
114: os.close(); // close stream and send the message
115:
116: // System.out.println("Inviting..." + scc.getHeader("To"));
117: } catch (IOException ioe) {
118: ioe.printStackTrace();
119: cleanup();
120: fail("IOException in testPrack");
121: }
122:
123: }
124:
125: /**
126: * Accept a new connection request and process the request from client.
127: * Send "OK" to client
128: *
129: * This method is declared in SipServerConnectionListener
130: *
131: * @param scnLocal Local SIP connection notifier
132: */
133: public void notifyRequest(SipConnectionNotifier scnLocal) {
134: try {
135: // block and wait for incoming request.
136: // SipServerConnection is establised and returned
137: // when new request is received.
138: SipServerConnection ssc = scnLocal.acceptAndOpen();
139: assertNotNull("ssc is null", ssc);
140:
141: if (ssc.getMethod().equals("INVITE")) {
142: ssc.initResponse(180);
143: ssc.setHeader("RSeq", "1");
144: ssc.send();
145: } else if (ssc.getMethod().equals("PRACK")) {
146: ssc.initResponse(200);
147: ssc.send();
148: }
149: ssc.close();
150: } catch (SipException sipex) {
151: assertNull("Unexpected SipException", sipex);
152: sipex.printStackTrace();
153: cleanup();
154: } catch (IOException ioe) {
155: assertNull("Unexpected IOException", ioe);
156: ioe.printStackTrace();
157: cleanup();
158: }
159: }
160:
161: /**
162: * Received the response from user agent server
163: *
164: * This method is declared in SipClientConnectionListener
165: *
166: * @param sccLocal Local SIP Client (UAC)
167: *
168: */
169: public void notifyResponse(SipClientConnection sccLocal) {
170: try {
171:
172: boolean ok = sccLocal.receive(0);
173:
174: if (ok) { // response received
175:
176: if (sccLocal.getStatusCode() == 180) {
177: // scc.initPrack();
178: // scc.initRequest("PRACK", scn);
179: SipClientConnection sccPrack = sccLocal.getDialog()
180: .getNewClientConnection("PRACK");
181: // scc.setHeader("Content-Type", "text/plain");
182: String rSeq = sccLocal.getHeader("RSeq");
183: sccPrack.setHeader("RAck", rSeq + " "
184: + sccLocal.getHeader("CSeq"));
185: sccPrack.setListener(this );
186: sccPrack.send();
187: synchronized (this ) {
188: done = true;
189: notify();
190: }
191: //} else if (sccLocal.getStatusCode() == 200) {
192: // System.out.println("ACK received at server");
193: }
194: }
195: } catch (Exception ex) {
196: // handle Exceptions
197: ex.printStackTrace();
198: }
199: }
200:
201: /**
202: * Close all connections.
203: *
204: */
205: void cleanup() {
206: // System.out.println("cleanup called");
207: try {
208: if (scc != null) {
209: scc.close();
210: }
211:
212: if (scn != null) {
213: scn.close();
214: }
215: } catch (IOException ioe) {
216: assertNull("Unexpected IOException during cleanup", ioe);
217: ioe.printStackTrace();
218: }
219: }
220:
221: /**
222: * Tests execute.
223: *
224: */
225: public void runTests() {
226: declare("Test TestPrack ");
227:
228: setup();
229: testPrack();
230:
231: synchronized (this ) {
232: while (!done) {
233: try {
234: wait();
235: } catch (InterruptedException e) {
236: System.out.println("Catch interrupt");
237: }
238: }
239: }
240:
241: cleanup();
242:
243: assertTrue("Failure in TestPrack", done);
244: }
245:
246: }
|