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: package com.sun.midp.jsr82emul;
027:
028: import java.io.IOException;
029: import java.io.InputStream;
030: import java.io.OutputStream;
031: import javax.microedition.io.SocketConnection;
032: import com.sun.midp.main.Configuration;
033: import com.sun.midp.security.SecurityToken;
034: import com.sun.midp.security.ImplicitlyTrustedClass;
035: import com.sun.midp.jsr082.SecurityInitializer;
036:
037: /**
038: * Represents a client for JSR 82 emulation environment.
039: * It is not a part of JSR 82 implementation and is only used within
040: * JSR 82 emulation mode. The emulation mode allows running tests without
041: * real native Bluetooth libraries or hardware.
042: *
043: * In the emulation mode the client runs under J2ME VM control and connects
044: * thru a socket to a server which runs somewhere on the Internet under J2SE
045: * VM control.
046: *
047: * A client represents a Bluetooth local or remote device, connection
048: * or anything that requires Bluetooth ether communication. A server
049: * in turn represents Bluetooth ether.
050: */
051: public class EmulationClient {
052:
053: /**
054: * Inner class to request security token from SecurityInitializer.
055: * SecurityInitializer should be able to check this inner class name.
056: */
057: static private class SecurityTrusted implements
058: ImplicitlyTrustedClass {
059: };
060:
061: /** Internal security token that grants access to restricted API. */
062: protected static SecurityToken internalSecurityToken = SecurityInitializer
063: .requestToken(new SecurityTrusted());
064:
065: /** Keeps default port number for server connections. */
066: private final static int EMUL_PORT = Configuration.getIntProperty(
067: "com.sun.midp.jsr82emul.serverPort", 1234);
068:
069: /** IP address of EmulationServer. */
070: private static String serverIP;
071:
072: /**
073: * Keeps messenger that proveds utilities for communicating with server.
074: */
075: protected Messenger messenger = new Messenger();
076:
077: /** Keeps socket connection to the server. */
078: protected SocketConnection connection = null;
079: /** Keeps input stream from server. */
080: protected InputStream fromServer = null;
081: /** Keeps output stream to server. */
082: protected OutputStream toServer = null;
083:
084: /**
085: * Creates an instance.
086: */
087: protected EmulationClient() {
088: }
089:
090: /**
091: * Retrieves port of emulation server.
092: * @return EmulationServer IP address from JSR82_EMUL_IP environment
093: * variable, <code>null</code> if the variable is not defined.
094: */
095: private static native String getServerIP();
096:
097: /**
098: * Retrieves local IP address.
099: * @return <code>String</code> object containing local IP address,
100: * <code>null</code> if retrieving failed.
101: */
102: static native String getLocalIP();
103:
104: /**
105: * Retrieves emulation server IP address, launches server if needed.
106: * @return emulation server IP address.
107: */
108: private static synchronized String getServer() {
109: if (serverIP != null) {
110: return serverIP;
111: }
112:
113: serverIP = getServerIP();
114:
115: // Local IP of emulation server means that it is possibly
116: // expected that current application will launch the server.
117: if (serverIP == null || serverIP.length() == 0
118: || serverIP.equals("127.0.0.1")) {
119: EmulationServer.launch();
120: serverIP = "localhost";
121: } else if (serverIP.equals(getLocalIP())) {
122: EmulationServer.launch();
123: }
124:
125: return serverIP;
126: }
127:
128: /**
129: * Opens socket connection to current host at current port.
130: *
131: * @exception IOException if there is an open connection or
132: * en error ocurred while trying to connect.
133: */
134: protected void connect() throws IOException {
135: if (connection != null) {
136: throw new IOException("connection is already open");
137: }
138:
139: connection = (SocketConnection) new com.sun.midp.io.j2me.socket.Protocol()
140: .openPrim(internalSecurityToken, "//" + getServer()
141: + ":" + EMUL_PORT);
142:
143: fromServer = connection.openDataInputStream();
144: toServer = connection.openDataOutputStream();
145: }
146:
147: /**
148: * Closes current socket connection.
149: *
150: * @exception IOException if an error occured or ocurred.
151: */
152: protected void disconnect() throws IOException {
153: if (connection != null) {
154: messenger.send(toServer, Messenger.DONE, "");
155:
156: toServer.close();
157: fromServer.close();
158: connection.close();
159:
160: toServer = null;
161: fromServer = null;
162: connection = null;
163: }
164: }
165: }
|