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.SipClientConnection;
033: import java.io.OutputStream;
034: import java.io.IOException;
035: import javax.microedition.sip.SipException;
036:
037: /**
038: * Test Multiple SipConnectionNotifiers on single host
039: *
040: */
041: public class TestMultiNotifiers extends TestCase implements
042: SipServerConnectionListener {
043:
044: /** Connection notifier. */
045: SipConnectionNotifier scn1 = null;
046: /** Connection notifier. */
047: SipConnectionNotifier scn2 = null;
048: /** Client connection. */
049: SipClientConnection scc1 = null;
050: /** Client connection. */
051: SipClientConnection scc2 = null;
052: /** SIP request message from client to server */
053: String clientMsg = "Client : How are you?";
054:
055: /**
056: * Open SipConnectionNotifier(SCN1 and SCN2) on ports 6080 and 5081.
057: * Open two client connections connecting to SCN1 and SCN2
058: */
059: void setup() {
060: try {
061: // Open SipConnectionNotifier on port 6080
062: scn1 = (SipConnectionNotifier) Connector.open("sip:6080");
063: assertNotNull("scn1 is null", scn1);
064: scn1.setListener(this );
065: } catch (Exception ex) {
066: assertNull("Exception during scn1 open", scn1);
067: ex.printStackTrace();
068: }
069:
070: try {
071: // Open SipConnectionNotifier on port 6081
072: scn2 = (SipConnectionNotifier) Connector.open("sip:6081");
073: assertNotNull("scn2 is null", scn2);
074: scn2.setListener(this );
075: } catch (Exception ex) {
076: assertNull("Exception during scn2 open", scn2);
077: ex.printStackTrace();
078: }
079:
080: try {
081: // Open SIP client connection to the SCN ON PORT 6080
082: scc1 = (SipClientConnection) Connector
083: .open("sip:sippy.tester@localhost:6080");
084: assertNotNull("scc1 is null", scc1);
085: } catch (Exception ex) {
086: assertNull("Exception during scc1 open", scc1);
087: ex.printStackTrace();
088: cleanup();
089: }
090:
091: try {
092: // Open SIP client connection to the SCN ON PORT 6081
093: scc2 = (SipClientConnection) Connector
094: .open("sip:sippy.tester@localhost:5081");
095: assertNotNull("scc2 is null", scc2);
096: } catch (Exception ex) {
097: assertNull("Exception during scc2 open", scc2);
098: ex.printStackTrace();
099: cleanup();
100: }
101:
102: }
103:
104: /**
105: * Send a SIP INVITE message
106: * @param scc SIP client connection
107: * @param snc SIP Connection Notifier
108: *
109: */
110: public void testInviteMsg(SipClientConnection scc,
111: SipConnectionNotifier scn) {
112:
113: try {
114: scc.initRequest("INVITE", scn);
115: scc.setHeader("From", "sip:sippy.tester@localhost");
116: scc.setHeader("Subject", "Hello...");
117: scc.setHeader("Contact", "sip:user@"
118: + scn.getLocalAddress() + ":" + scn.getLocalPort());
119:
120: // write message body
121: scc.setHeader("Content-Type", "text/plain");
122: scc.setHeader("Content-Length", Integer.toString(clientMsg
123: .length()));
124: OutputStream os = scc.openContentOutputStream();
125: os.write(clientMsg.getBytes());
126: os.close(); // close stream and send the message
127:
128: // System.out.println("Inviting..." + scc.getHeader("To"));
129: } catch (IOException ioe) {
130: ioe.printStackTrace();
131: cleanup();
132: fail("IOException in TestMultiNotifiers");
133: }
134:
135: }
136:
137: /**
138: * Receive request from client
139: *
140: * This method is declared in SipServerConnectionListener
141: *
142: * @param scnLocal Local SIP connection notifier
143: */
144: public void notifyRequest(SipConnectionNotifier scnLocal) {
145: try {
146: System.out.println("Local SCN port = "
147: + scnLocal.getLocalPort());
148: } catch (IOException ioe) {
149: ioe.printStackTrace();
150: fail("IOException in TestMultiNotifiers");
151: }
152:
153: }
154:
155: /**
156: * Close all connections.
157: *
158: */
159: void cleanup() {
160: try {
161: if (scc1 != null) {
162: scc1.close();
163: }
164: if (scc2 != null) {
165: scc2.close();
166: }
167:
168: if (scn1 != null) {
169: scn1.close();
170: }
171: if (scn2 != null) {
172: scn2.close();
173: }
174: } catch (IOException ioe) {
175: assertNull("Unexpected IOException during cleanup", ioe);
176: ioe.printStackTrace();
177: }
178: }
179:
180: /**
181: * Tests execute.
182: *
183: */
184: public void runTests() {
185: declare("Test TestMultiNotifiers ");
186:
187: setup();
188:
189: testInviteMsg(scc1, scn1);
190: synchronized (this ) {
191: try {
192: wait(500);
193: } catch (InterruptedException e) {
194: System.out.println("Catch interrupt");
195: }
196: }
197:
198: testInviteMsg(scc2, scn2);
199: synchronized (this ) {
200: try {
201: wait(500);
202: } catch (InterruptedException e) {
203: System.out.println("Catch interrupt");
204: }
205: }
206:
207: cleanup();
208:
209: assertTrue("Failure in TestMultiNotifiers", true);
210: }
211:
212: }
|