Source Code Cross Referenced for TestSipCommunication.java in  » 6.0-JDK-Modules » j2me » javax » microedition » sip » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » j2me » javax.microedition.sip 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.