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 javax.microedition.sip.SipConnectionNotifier;
032: import javax.microedition.sip.SipServerConnection;
033: import javax.microedition.sip.SipClientConnection;
034: import java.io.InputStream;
035: import java.io.OutputStream;
036: import java.io.IOException;
037: import javax.microedition.sip.SipException;
038:
039: /**
040: * Tests for end-to-end SIP client-server communication
041: *
042: */
043: public class TestSipCommunication extends TestCase implements Runnable {
044:
045: /** Connection notifier. */
046: SipConnectionNotifier scn = null;
047: /** Server connection. */
048: SipServerConnection ssc = null;
049: /** Client connection. */
050: SipClientConnection sc = null;
051: /** Communication transport. */
052: String transport = "udp";
053:
054: /** Server flag. */
055: boolean serverEstablished = false;
056:
057: /** Thread that is blocked for acceptAndOpen. */
058: Thread t1;
059:
060: /**
061: * Open the connection and start the thread to call the run
062: * method. It waits until the InputStream is open before returning.
063: */
064: void setup() {
065: serverEstablished = false;
066: try {
067: // Open SIP server connection and listen to port 5060
068: scn = (SipConnectionNotifier) Connector
069: .open("sip:;transport=" + transport);
070: } catch (Exception ex) {
071: assertNull("Exception during scn open", scn);
072: ex.printStackTrace();
073: }
074: assertNotNull("scn is null", scn);
075:
076: try {
077: // Open SIP client connection to the local SIP server
078: sc = (SipClientConnection) Connector.open("sip:"
079: + scn.getLocalAddress() + ":" + scn.getLocalPort()
080: + ";transport=" + transport);
081: } catch (Exception ex) {
082: assertNull("Exception during sc open", sc);
083: ex.printStackTrace();
084: cleanup();
085: }
086:
087: assertNotNull("sc is null", sc);
088:
089: // Launch a thread which would be blocked for scn.acceptAndOpen()
090: t1 = new Thread(this );
091: t1.start();
092: }
093:
094: /**
095: * Accept a new connection request and process the request from client.
096: */
097: public void run() {
098: try {
099: // block and wait for incoming request.
100: // SipServerConnection is establised and returned
101: // when new request is received.
102: ssc = scn.acceptAndOpen();
103:
104: assertNotNull("ssc is null", ssc);
105: serverEstablished = true;
106: } catch (SipException sipex) {
107: assertNull("Unexpected SipException", sipex);
108: sipex.printStackTrace();
109: cleanup();
110: } catch (IOException ioe) {
111: assertNull("Unexpected IOException", ioe);
112: ioe.printStackTrace();
113: cleanup();
114: } finally {
115: synchronized (this ) {
116: notifyAll();
117: }
118: }
119: }
120:
121: /**
122: * Send request and receive a responce.
123: */
124: public void testSipCommunication() {
125: boolean result = false;
126: try {
127: result = sendMsgFromClient("Hello SIP-World");
128: } catch (SipException sipex) {
129: assertNull("Unexpected SipException", sipex);
130: sipex.printStackTrace();
131: } catch (IOException ioe) {
132: assertNull("Unexpected IOException", ioe);
133: ioe.printStackTrace();
134: }
135:
136: if (result) {
137: result = processClientRequest();
138: }
139:
140: if (result) {
141: result = receiveMsgFromServer();
142: }
143:
144: assertTrue("SIP client is not communicating to server", result);
145: }
146:
147: /**
148: * Send a SIP message from user agent client to user agent server
149: * SipConnectionNotifier queues the incoming requests if the
150: * instance of SipServerConnection is not available yet
151: *
152: * @param msg message to be sent
153: * @return true if sc is non-null and message is sent successfully
154: * false otherwise
155: */
156: boolean sendMsgFromClient(String msg) throws SipException,
157: IOException {
158: if (sc != null) {
159: sc.initRequest("MESSAGE", null);
160: sc.setHeader("From", "sip:sippy.tester@"
161: + scn.getLocalAddress());
162: sc.setHeader("Subject", "testing...");
163:
164: // write message body
165: sc.setHeader("Content-Type", "text/plain");
166: sc.setHeader("Content-Length", Integer.toString(msg
167: .length()));
168: OutputStream os = sc.openContentOutputStream();
169: os.write(msg.getBytes());
170: os.close(); // close stream and send the message
171:
172: return true;
173: }
174:
175: return false;
176: }
177:
178: /**
179: * Process the client request at server and receive the message
180: *
181: * @return true if the client request is received at server
182: * false if the request is not received
183: *
184: */
185: boolean processClientRequest() {
186: boolean requestProcessed = false;
187:
188: StringBuffer receivedMsgStr = new StringBuffer();
189:
190: // Wait for the request to receive at server
191: synchronized (this ) {
192: while (!serverEstablished) {
193: try {
194: wait(2000);
195: } catch (InterruptedException e) {
196: // Ignore interrupt
197: }
198: }
199: }
200:
201: if (serverEstablished) {
202: try {
203: // what was the SIP method
204: String method = ssc.getMethod();
205: if (method.equals("MESSAGE")) {
206: // read the content of the MESSAGE
207: String contentType = ssc.getHeader("Content-Type");
208: if ((contentType != null)
209: && contentType.equals("text/plain")) {
210: InputStream is = ssc.openContentInputStream();
211: int ch;
212: // read content
213: ch = is.read();
214: while ((ch = is.read()) != -1) {
215: receivedMsgStr.append(ch);
216: }
217: }
218: // initialize SIP 200 OK and send it back
219: ssc.initResponse(200);
220: ssc.send();
221: requestProcessed = true;
222: }
223: } catch (Exception ex) {
224: // handle Exceptions
225: ex.printStackTrace();
226: } finally {
227: }
228: }
229: return requestProcessed;
230: }
231:
232: /**
233: * Receive the message and extract status code at server
234: *
235: * @return true if the status code is OK
236: * false otherwise
237: *
238: */
239: boolean receiveMsgFromServer() {
240: boolean receivedOKResponse = false;
241: try {
242:
243: // wait maximum 15 seconds for response
244: boolean ok = sc.receive(15000);
245:
246: if (ok) { // response received
247: if (sc.getStatusCode() == 200) {
248: receivedOKResponse = true;
249: }
250: }
251: } catch (Exception ex) {
252: // handle Exceptions
253: ex.printStackTrace();
254: } finally {
255: }
256: return receivedOKResponse;
257: }
258:
259: /**
260: * Close all connections.
261: *
262: */
263: void cleanup() {
264: try {
265: if (sc != null) {
266: sc.close();
267: }
268:
269: if (ssc != null) {
270: ssc.close();
271: }
272:
273: if (scn != null) {
274: scn.close();
275: }
276: } catch (IOException ioe) {
277: assertNull("Unexpected IOException during cleanup", ioe);
278: ioe.printStackTrace();
279: }
280: }
281:
282: /**
283: * Tests execute.
284: *
285: */
286: public void runTests() {
287:
288: declare("Test TestSipCommunication on " + transport);
289: setup();
290: testSipCommunication();
291: cleanup();
292: assertTrue("OK", true); // temporary
293:
294: transport = "tcp";
295: declare("Test TestSipCommunication on " + transport);
296: setup();
297: testSipCommunication();
298: cleanup();
299: assertTrue("OK", true); // temporary
300:
301: }
302:
303: }
|