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 SystemServiceRequestProtocolAMS {
033: final static int SERVICE_REQUEST_STATUS_OK = 0;
034: final static int SERVICE_REQUEST_STATUS_ERROR = 1;
035:
036: private final static int INVALID_STATE = -1;
037: private final static int WAIT_FOR_BEGIN_SESSION_STATE = 1;
038: private final static int WAIT_FOR_SERVICE_ID_STATE = 2;
039: private final static int SEND_SERVICE_REQUEST_STATUS_STATE = 3;
040: private final static int SEND_SERVICE_TO_CLIENT_LINK_STATE = 4;
041: private final static int SEND_CLIENT_TO_SERVICE_LINK_STATE = 5;
042: private final static int WAIT_FOR_LINKS_RECEIVED_ACK_STATE = 6;
043: private final static int WAIT_FOR_END_SESSION_STATE = 7;
044: private final static int END_SESSION_STATE = 8;
045: private final static int END_STATE = 9;
046:
047: private SystemServiceRequestListener requestListener;
048:
049: private int state = INVALID_STATE;
050:
051: SystemServiceRequestProtocolAMS(
052: SystemServiceRequestListener requestListener) {
053:
054: /**
055: * Argument sanity check
056: */
057: if (requestListener == null) {
058: throw new NullPointerException();
059: }
060:
061: this .requestListener = requestListener;
062: }
063:
064: SystemServiceConnectionLinks handleServiceRequest(
065: SystemServiceConnectionLinks sendReceiveLinks)
066: throws ClosedLinkException, InterruptedIOException,
067: IOException {
068:
069: Link sendLink = sendReceiveLinks.getSendLink();
070: Link receiveLink = sendReceiveLinks.getReceiveLink();
071:
072: /**
073: * Arguments sanity checks
074: */
075: if (sendLink == null || receiveLink == null) {
076: throw new NullPointerException();
077: }
078:
079: if (!sendLink.isOpen() || !receiveLink.isOpen()) {
080: throw new IllegalStateException();
081: }
082:
083: SystemServiceConnectionLinks connectionLinks = null;
084:
085: state = WAIT_FOR_BEGIN_SESSION_STATE;
086: try {
087: connectionLinks = doHandleServiceRequest(sendLink,
088: receiveLink);
089: } finally {
090: state = INVALID_STATE;
091: }
092:
093: return connectionLinks;
094: }
095:
096: private SystemServiceConnectionLinks doHandleServiceRequest(
097: Link sendLink, Link receiveLink)
098: throws ClosedLinkException, InterruptedIOException,
099: IOException {
100:
101: String serviceID = "";
102: SystemServiceConnectionLinks connectionLinks = null;
103:
104: if (state == INVALID_STATE) {
105: throw new IllegalStateException();
106: }
107:
108: while (state != END_STATE) {
109: switch (state) {
110: case WAIT_FOR_BEGIN_SESSION_STATE: {
111: // wait for session begin request
112: LinkMessage msg = receiveLink.receive();
113: String str = msg.extractString();
114:
115: // check request validity
116: if (!str
117: .equals(SystemServiceRequestProtocolClient.START_SESSION_STR)) {
118: throw new IllegalStateException();
119: }
120:
121: // advance to next state
122: state = WAIT_FOR_SERVICE_ID_STATE;
123: break;
124: }
125:
126: case WAIT_FOR_SERVICE_ID_STATE: {
127: // wait for service id
128: LinkMessage msg = receiveLink.receive();
129: serviceID = msg.extractString();
130:
131: // advance to next state
132: state = SEND_SERVICE_REQUEST_STATUS_STATE;
133: break;
134: }
135:
136: case SEND_SERVICE_REQUEST_STATUS_STATE: {
137: // try to obtain connection to service
138: int status = SERVICE_REQUEST_STATUS_OK;
139: try {
140: connectionLinks = null;
141: connectionLinks = requestListener
142: .onServiceRequest(serviceID);
143: } finally {
144: if (connectionLinks == null) {
145: status = SERVICE_REQUEST_STATUS_ERROR;
146: }
147:
148: // send status
149: ByteArrayOutputStream bos = new ByteArrayOutputStream();
150: DataOutputStream os = new DataOutputStream(bos);
151: os.writeInt(status);
152: byte[] data = bos.toByteArray();
153:
154: LinkMessage msg = LinkMessage.newDataMessage(data);
155: sendLink.send(msg);
156:
157: // advance to next state
158: if (status == SERVICE_REQUEST_STATUS_OK) {
159: state = SEND_SERVICE_TO_CLIENT_LINK_STATE;
160: } else {
161: state = END_STATE;
162: }
163: }
164:
165: break;
166: }
167:
168: case SEND_SERVICE_TO_CLIENT_LINK_STATE: {
169: // send service to client link
170: Link link = connectionLinks.getSendLink();
171: LinkMessage msg = LinkMessage.newLinkMessage(link);
172: sendLink.send(msg);
173:
174: // advance to next state
175: state = SEND_CLIENT_TO_SERVICE_LINK_STATE;
176: break;
177: }
178:
179: case SEND_CLIENT_TO_SERVICE_LINK_STATE: {
180: // send client to service link
181: Link link = connectionLinks.getReceiveLink();
182: LinkMessage msg = LinkMessage.newLinkMessage(link);
183: sendLink.send(msg);
184:
185: // advance to next state
186: state = WAIT_FOR_LINKS_RECEIVED_ACK_STATE;
187: break;
188: }
189:
190: case WAIT_FOR_LINKS_RECEIVED_ACK_STATE: {
191: // wait for links recieved ack
192: LinkMessage msg = receiveLink.receive();
193: String str = msg.extractString();
194:
195: // check request validity
196: if (!str
197: .equals(SystemServiceRequestProtocolClient.LINKS_RECEIVED_ACK_STR)) {
198: throw new IllegalStateException();
199: }
200:
201: // notify listener about connection passed to client
202: if (connectionLinks != null) {
203: requestListener
204: .onLinksPassedToClient(connectionLinks);
205: }
206:
207: // advance to next state
208: state = WAIT_FOR_END_SESSION_STATE;
209: break;
210: }
211:
212: case WAIT_FOR_END_SESSION_STATE: {
213: // wait for session end request
214: LinkMessage msg = receiveLink.receive();
215: String str = msg.extractString();
216:
217: // check request validity
218: if (!str
219: .equals(SystemServiceRequestProtocolClient.END_SESSION_STR)) {
220: throw new IllegalStateException();
221: }
222:
223: // advance to next state
224: state = END_SESSION_STATE;
225: break;
226: }
227:
228: case END_SESSION_STATE: {
229: // advance to next state
230: state = END_STATE;
231: break;
232: }
233: }
234: }
235:
236: state = INVALID_STATE;
237:
238: return connectionLinks;
239: }
240: }
|