Source Code Cross Referenced for SystemServiceRequestProtocolClient.java in  » 6.0-JDK-Modules » j2me » com » sun » midp » services » 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 » com.sun.midp.services 
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 com.sun.midp.services;
028:
029:        import com.sun.midp.links.*;
030:        import java.io.*;
031:
032:        final class SystemServiceRequestProtocolClient {
033:            final static String START_SESSION_STR = "Starting service request";
034:            final static String END_SESSION_STR = "Finishing service request";
035:            final static String LINKS_RECEIVED_ACK_STR = "Links received";
036:
037:            private final static int INVALID_STATE = -1;
038:            private final static int BEGIN_SESSION_STATE = 1;
039:            private final static int SEND_SERVICE_ID_STATE = 2;
040:            private final static int WAIT_FOR_STATUS_STATE = 3;
041:            private final static int WAIT_FOR_SERVICE_TO_CLIENT_LINK_STATE = 4;
042:            private final static int WAIT_FOR_CLIENT_TO_SERVICE_LINK_STATE = 5;
043:            private final static int SEND_LINKS_RECEIVED_ACK_STATE = 6;
044:            private final static int END_SESSION_STATE = 7;
045:            private final static int END_STATE = 8;
046:
047:            private LinkMessage startSessionMsg = null;
048:            private LinkMessage endSessionMsg = null;
049:            private LinkMessage linksReceivedMsg = null;
050:
051:            private SystemServiceConnectionLinks sendReceiveLinks = null;
052:            private SystemServiceConnectionLinks connectionLinks = null;
053:
054:            private int state = INVALID_STATE;
055:
056:            SystemServiceRequestProtocolClient(
057:                    SystemServiceConnectionLinks sendReceiveLinks) {
058:
059:                Link sendLink = sendReceiveLinks.getSendLink();
060:                Link receiveLink = sendReceiveLinks.getReceiveLink();
061:
062:                /**
063:                 * Arguments sanity checks
064:                 */
065:                if (sendLink == null || receiveLink == null) {
066:                    throw new NullPointerException();
067:                }
068:
069:                if (!sendLink.isOpen() || !receiveLink.isOpen()) {
070:                    throw new IllegalStateException();
071:                }
072:
073:                this .sendReceiveLinks = sendReceiveLinks;
074:
075:                this .startSessionMsg = LinkMessage
076:                        .newStringMessage(START_SESSION_STR);
077:                this .endSessionMsg = LinkMessage
078:                        .newStringMessage(END_SESSION_STR);
079:                this .linksReceivedMsg = LinkMessage
080:                        .newStringMessage(LINKS_RECEIVED_ACK_STR);
081:            }
082:
083:            void requestService(String serviceID) throws ClosedLinkException,
084:                    InterruptedIOException, IOException {
085:
086:                connectionLinks = null;
087:                state = BEGIN_SESSION_STATE;
088:
089:                try {
090:                    doRequestService(serviceID);
091:                } finally {
092:                    state = INVALID_STATE;
093:                }
094:            }
095:
096:            SystemServiceConnectionLinks getSystemServiceConnectionLinks() {
097:                return connectionLinks;
098:            }
099:
100:            private void doRequestService(String serviceID)
101:                    throws ClosedLinkException, InterruptedIOException,
102:                    IOException {
103:
104:                Link sendLink = sendReceiveLinks.getSendLink();
105:                Link receiveLink = sendReceiveLinks.getReceiveLink();
106:
107:                Link c2sLink = null;
108:                Link s2cLink = null;
109:
110:                if (state == INVALID_STATE) {
111:                    throw new IllegalStateException();
112:                }
113:
114:                while (state != END_STATE) {
115:                    switch (state) {
116:                    case BEGIN_SESSION_STATE: {
117:                        // start session
118:                        sendLink.send(startSessionMsg);
119:
120:                        // advance to next state
121:                        state = SEND_SERVICE_ID_STATE;
122:                        break;
123:                    }
124:
125:                    case SEND_SERVICE_ID_STATE: {
126:                        // send service ID
127:                        LinkMessage msg = LinkMessage
128:                                .newStringMessage(serviceID);
129:                        sendLink.send(msg);
130:
131:                        // advance to next state
132:                        state = WAIT_FOR_STATUS_STATE;
133:                        break;
134:                    }
135:
136:                    case WAIT_FOR_STATUS_STATE: {
137:                        // wait for status reply
138:                        LinkMessage msg = receiveLink.receive();
139:
140:                        // extract status 
141:                        byte[] data = msg.extractData();
142:                        ByteArrayInputStream bis = new ByteArrayInputStream(
143:                                data);
144:                        DataInputStream is = new DataInputStream(bis);
145:                        int status = is.readInt();
146:
147:                        // advance to next state
148:                        if (status == SystemServiceRequestProtocolAMS.SERVICE_REQUEST_STATUS_OK) {
149:                            state = WAIT_FOR_SERVICE_TO_CLIENT_LINK_STATE;
150:                        } else {
151:                            state = END_STATE;
152:                        }
153:
154:                        break;
155:                    }
156:
157:                    case WAIT_FOR_SERVICE_TO_CLIENT_LINK_STATE: {
158:                        // wait for link
159:                        LinkMessage msg = receiveLink.receive();
160:                        s2cLink = msg.extractLink();
161:
162:                        // check link state
163:                        if (!s2cLink.isOpen()) {
164:                            throw new IllegalStateException();
165:                        }
166:
167:                        // advance to next state
168:                        state = WAIT_FOR_CLIENT_TO_SERVICE_LINK_STATE;
169:                        break;
170:                    }
171:
172:                    case WAIT_FOR_CLIENT_TO_SERVICE_LINK_STATE: {
173:                        // wait for link
174:                        LinkMessage msg = receiveLink.receive();
175:                        c2sLink = msg.extractLink();
176:
177:                        // check link state
178:                        if (!c2sLink.isOpen()) {
179:                            throw new IllegalStateException();
180:                        }
181:
182:                        // advance to next state
183:                        state = SEND_LINKS_RECEIVED_ACK_STATE;
184:                        break;
185:                    }
186:
187:                    case SEND_LINKS_RECEIVED_ACK_STATE: {
188:                        // acknowledge links reception
189:                        sendLink.send(linksReceivedMsg);
190:
191:                        // advance to next state
192:                        state = END_SESSION_STATE;
193:                        break;
194:                    }
195:
196:                    case END_SESSION_STATE: {
197:                        // close session
198:                        sendLink.send(endSessionMsg);
199:
200:                        if (c2sLink != null && s2cLink != null) {
201:                            connectionLinks = new SystemServiceConnectionLinks(
202:                                    c2sLink, s2cLink);
203:                        }
204:
205:                        // advance to next state
206:                        state = END_STATE;
207:                        break;
208:                    }
209:                    }
210:                }
211:
212:                state = INVALID_STATE;
213:            }
214:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.