Fields Summary |
---|
ServiceConnectionData | serviceDataOptions of service to connect to. |
private Opener | openerOpener object needed to perform open operation in a separate thread. |
private javax.microedition.io.SocketConnection | socketConnectionSocket connection that emulates JSR82 one. |
private Sender | senderProtocol dependent data sener. |
private Receiver | receiverProtocol dependent data receiver. |
public int | handleHandle that identifies this connection emulation. |
static final int | CONN_OPENRequest code for Open operation. |
static final int | CONN_CLOSERequest code for Close operation. |
static final int | CONN_INITRequest code for Init operation. |
static final int | CONN_SENDRequest code for Send operation. |
static final int | CONN_RECEIVERequest code for Receive operation. |
Methods Summary |
---|
public void | close()Close this connection emulation.
if (opener != null) {
opener.interrupt();
opener = null;
}
if (sender != null) {
sender.close();
sender = null;
}
if (receiver != null) {
receiver.close();
receiver = null;
}
if (socketConnection != null) {
try {
socketConnection.close();
} catch (IOException e) {
// ignoring
}
socketConnection = null;
}
ConnRegistry.getInstance().unregister(handle);
|
private static native int | getHandle(int protocol)Retrieves new handle for a newly created connection at server side.
|
private static native boolean | getOutData(int handle, byte[] buf)Retrieves bytes from output native buffer for sending.
|
public int | getReceiveMTU()Returns actually established ReceiveMTU.
return serviceData.receiveMTU;
|
public java.lang.String | getRemoteAddress()Returns Bluetooth address of other side of this connection.
return BluetoothUtils.getAddressString(serviceData.address);
|
public int | getTransmitMTU()Returns actually established TransmitMTU.
return serviceData.transmitMTU;
|
private static native void | notifyOpen(int handle, boolean success, int receiveMTU, int transmitMTU)Notifies porting layer on open operation completion.
|
static native void | notifyReceived(int handle, byte[] bytes, int len)Notifies porting layer on receive operation completion.
|
static native void | notifySent(int handle, int len)Notifies porting layer on send operation completion.
|
public int | open()Opens client-side connection. Requests connection with given client
connection string from emulation server
ServiceConnectionData connData;
connect();
messenger.sendBytes(toServer, Messenger.CONNECT_TO_SERVICE,
serviceData.toByteArray(
ServiceConnectionData.CONN_REQUEST_DATA));
messenger.receive(fromServer);
if (messenger.getCode() != Messenger.SERVICE_AT) {
throw new EmulationException(
"Unexpected emulation server response " +
messenger.getCode());
}
connData = new ServiceConnectionData(
messenger.getBytes(), ServiceConnectionData.CONNECTION_DATA);
// no need to communicate with emulation sever any more
// since a direct connection is open
disconnect();
if (connData.error != -1) {
throw new BluetoothConnectionException(connData.error);
}
// vice versa to server side properties
serviceData.receiveMTU = connData.transmitMTU;
serviceData.transmitMTU = connData.receiveMTU;
// applying bitwise operation to get unsigned values from bytes
socketConnection = (SocketConnection)
new com.sun.midp.io.j2me.socket.Protocol().openPrim(
internalSecurityToken, "//" +
(connData.address[0] & 0xff) + "." +
(connData.address[1] & 0xff) + "." +
(connData.address[2] & 0xff) + "." +
(connData.address[3] & 0xff) +
":" + connData.socketPort);
connData.address = DeviceEmul.getLocalDeviceEmul().address;
sender = Sender.create(socketConnection,
serviceData.protocol, handle);
// It is important to use blocking sender method here
sender.plainSend(connData.toByteArray(
ServiceConnectionData.CLIENT_DATA));
return handle;
|
public int | open(javax.microedition.io.SocketConnection socketConnection)Opens server-side connection, i.e. assigns this emulation with already
open server-side socket connection.
this.socketConnection = socketConnection;
receiver = Receiver.create(socketConnection,
serviceData.protocol, handle);
// Calling blocking method intentionally here
ServiceConnectionData connData = new ServiceConnectionData(
receiver.forceReceive(ServiceConnectionData.CLIENT_DATA_SIZE),
ServiceConnectionData.CLIENT_DATA);
serviceData.receiveMTU = connData.receiveMTU;
serviceData.transmitMTU = connData.transmitMTU;
serviceData.address = connData.address;
return handle;
|
public void | process(BytePack request)Processes request from porting layer to emulation.
switch (request.extract()) {
case CONN_OPEN:
Log.log("Processing CONN_OPEN " + handle);
serviceData.address = request.extractBytes(Const.BTADDR_SIZE);
serviceData.port = request.extractInt();
opener = new Opener();
opener.start();
break;
case CONN_CLOSE:
Log.log("Processing CONN_CLOSE " + handle);
close();
break;
case CONN_INIT:
Log.log("Processing CONN_INIT " + handle);
serviceData = new ServiceConnectionData(
request.extractBytes(ServiceConnectionData.SERVER_DATA_SIZE),
ServiceConnectionData.SERVER_DATA);
break;
case CONN_SEND:
Log.log("Processing CONN_SEND " + handle);
send(request.extractInt());
break;
case CONN_RECEIVE:
Log.log("Processing CONN_RECEIVE " + handle);
receive();
break;
default:
Log.log("Processing CONN_UNKNOWN " + handle);
throw new EmulationException("Unknown connection request");
}
|
private void | receive()Reads data from emulated connection.
if (receiver == null) {
receiver = Receiver.create(socketConnection,
serviceData.protocol, handle);
}
receiver.receive();
|
private void | send(int len)Sends data to the other side of the emulated connection.
byte[] buf = new byte[len];
if (getOutData(handle, buf)) {
if (sender == null) {
sender = Sender.create(socketConnection,
serviceData.protocol, handle);
}
sender.send(buf);
}
buf = null;
|