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: }
|