ServiceConnectionDatapublic class ServiceConnectionData extends BytePack Utility class that keeps service connection information, packs it
into byte array and restores form it. Has methods to check if a
service represented by an instance can accept a connection represented
by another one. |
Fields Summary |
---|
int | errorError type, like in BluetoothConnectionException ,
ff error occured while looking for connection, -1
if there is no error. Only makes sense in case of CONNECTION_DATA. | int | socketPortTCP socket port emulated service listens on. | byte[] | addressIP address or host name of service in case of CONNECTION_DATA,
Bluetooth address of client in case of CLIENT_DATA | int | portChannel ID or PSM. | int | receiveMTUReceiveMTU in server side terms. | int | transmitMTUTransmitMTU in server side terms. | int | protocolProtocol ID as in BluetoothUrl . | boolean | masterShows wither server is master. | boolean | encryptShows wither encrypted connection required. | boolean | authorizeShows wither authorization required by server. | boolean | authenticateShows wither authentication required by server. | private boolean | acceptingIdentifies if a service defined by this instance accepting currently.
This flag is used to overcome TCK inconsistency where client may
start connecting to Bluetooth TCK Agent while the latter is still not
ready after previous connection. | private static final int | ACCEPT_DELAYDelay duration for the case when acception is requested while it is
not known if corresponding service is accepting. | private static final int | MAX_ACCEPT_TRIALSMax amount of acception trials when it is not known if service
is accepting | static final int | SERVER_DATAPacked data type that implies that the following fields are included:
socketPort, port, receiveMTU, transmitMTU, receiveMTU, protocol,
master, encrypt, authorize, authenticate. | static final int | CONNECTION_DATAPacked data type that implies that the following fields are included:
socketPort, IP address, receiveMTU, transmitMTU. | static final int | CLIENT_DATAPacked data type that implies that the following fields are included:
transmitMTU, receiveMTU, Bluetooth address. | static final int | CONN_REQUEST_DATAPacked data type that implies that the following fields are included:
Bluetooth address, protocol, port, transmitMTU, receiveMTU, encrypt,
authenticate, master.
It is used CONNECT_TO_SERVICE request to the emulation server | private static final int | BTADDR_SIZESize of Bluetooth address in bytes representation. | static final int | CLIENT_DATA_SIZEByte array size when packed with CLIENT_DATA type. | static final int | SERVER_DATA_SIZEByte array size when packed with SERVER_DATA type. | private static final int | PROTOCOL_MASKBit mask to highlight protcol type. | private static final int | MASTER_MASKBit mask to highlight master flag. | private static final int | ENCRYPT_MASKBit mask to highlight encrypt flas. | private static final int | AUTHORIZE_MASKBit mask to highlight authorize flag. | private static final int | AUTHENTICATE_MASKBit mask to highlight authenticate flag. |
Constructors Summary |
---|
ServiceConnectionData()Constructs an instance which indicates that unknown error occured
while looking for connection.
this(BluetoothConnectionException.FAILED_NOINFO);
| ServiceConnectionData(int error)Constructs an instance which indicates that a connection error occured.
this.error = error;
| ServiceConnectionData(byte[] data, int type)Constructs an instance by given byte representation.
super(data);
switch (type) {
case CONN_REQUEST_DATA:
address = extractBytes(BTADDR_SIZE);
// the rest of CONN_REQUEST_DATA case is poceeded here as well
case SERVER_DATA:
if (type == SERVER_DATA) {
socketPort = extractInt();
}
port = extractInt();
receiveMTU = extractInt();
transmitMTU = extractInt();
int misc = extract();
protocol = misc & PROTOCOL_MASK;
master = (misc & MASTER_MASK) != 0;
encrypt = (misc & ENCRYPT_MASK) != 0;
authorize = (misc & AUTHORIZE_MASK) != 0;
authenticate = (misc & AUTHORIZE_MASK) != 0;
break;
case CONNECTION_DATA:
error = extractInt();
if (error == -1) {
socketPort = extractInt();
receiveMTU = extractInt();
transmitMTU = extractInt();
address = extractBytes(Const.IP_SIZE);
}
break;
case CLIENT_DATA:
receiveMTU = extractInt();
transmitMTU = extractInt();
address = extractBytes(BTADDR_SIZE);
break;
default:
throw new IllegalArgumentException();
}
release();
|
Methods Summary |
---|
void | accept(com.sun.midp.jsr82emul.ServiceConnectionData client)Checks if client connection with given parameters can be accepted,
if it can not, sets error code. If acception is possible but
requires fields alignment, makes it modifying connection data
passed as a parameter.
client.address = address;
client.socketPort = socketPort;
client.error = -1;
if (client.master && master) {
client.error = BluetoothConnectionException.UNACCEPTABLE_PARAMS;
} else if (client.protocol == BluetoothUrl.L2CAP) {
// If connection accepted successfully,
// (receiveMTU, transmitMTU) set here for the parameter
// become actual (receiveMTU, transmitMTU) for server and
// actual (transmitMTU, receiveMTU) for client.
if (client.transmitMTU > receiveMTU || transmitMTU > client.receiveMTU) {
client.error = BluetoothConnectionException.UNACCEPTABLE_PARAMS;
} else {
if (client.transmitMTU == -1) {
client.transmitMTU = receiveMTU;
}
if (transmitMTU != -1) {
client.receiveMTU = transmitMTU;
}
}
}
if (client.error == -1) {
synchronized (this) {
int trial = 0;
for (; trial < MAX_ACCEPT_TRIALS && !accepting; trial++) {
try {
Thread.sleep(ACCEPT_DELAY);
} catch (Throwable t) {}
}
if (trial == MAX_ACCEPT_TRIALS) {
client.error = BluetoothConnectionException.TIMEOUT;
} else {
// accepted successfully, now service is occupied by current client
accepting = false;
}
}
}
| void | setAccepting()Sets acception flag to true to identify that represented service
is currebtly accepting. A part of the way that overcomes TCK
inconsistency.
accepting = true;
| byte[] | toByteArray(int type)Retrieves bytes representation.
switch (type) {
case CONN_REQUEST_DATA:
reset(new byte[BTADDR_SIZE + SERVER_DATA_SIZE - 1]);
appendBytes(address);
// the rest of CONN_REQUEST_DATA is proceeded here as well
case SERVER_DATA:
if (type == SERVER_DATA) {
reset(new byte[SERVER_DATA_SIZE]);
appendInt(socketPort);
}
appendInt(port);
appendInt(receiveMTU);
appendInt(transmitMTU);
int misc = protocol;
misc |= master? MASTER_MASK : 0;
misc |= encrypt? ENCRYPT_MASK : 0;
misc |= authorize? AUTHORIZE_MASK : 0;
misc |= authenticate? AUTHORIZE_MASK : 0;
append((byte)misc);
break;
case CONNECTION_DATA:
if (error == -1) {
reset(new byte[4 * 4 + Const.IP_SIZE]);
appendInt(error);
appendInt(socketPort);
appendInt(receiveMTU);
appendInt(transmitMTU);
appendBytes(address);
} else {
reset(new byte[4]);
appendInt(error);
}
break;
case CLIENT_DATA:
reset(new byte[CLIENT_DATA_SIZE]);
appendInt(receiveMTU);
appendInt(transmitMTU);
appendBytes(address);
break;
default:
throw new IllegalArgumentException();
}
return release();
|
|