Fields Summary |
---|
private static boolean | launchedShows if emulation server is already running. |
private static EmulationServer | instanceThe only instance of EmulationServer . |
private static com.sun.midp.security.SecurityToken | internalSecurityTokenThis class has a different security domain than the MIDlet suite. |
private static com.sun.midp.io.j2me.serversocket.Socket | serverSocketServer socket that accepts TCP clients connections. |
private static final int | SOCKET_PORTPort to open server socket at. |
private static final int | INQUIRY_DELAYDuration of a delay that is applied prior to start inquiry in
order to let all emulated devices update their device classes. |
private Hashtable | servicesKeeps services being advertised in the ether. |
private Hashtable | devicesKeeps devices that present in the ether. |
private static final DeviceKey[] | defaultAddrDefault Bluetooth address wrapped by DeviceKey objects. |
static final int | ADDR_COUNTAmount of default addresses. |
int | nextAddrValue used to generate unique Bluetooth addresses. |
Methods Summary |
---|
public ServiceConnectionData | connectToService(ServiceConnectionData client)Retrieves emulation (TCP) connection URL for connecting to the
service represented by given JSR 82 client connection string.
ServiceKey key = new ServiceKey(client.address,
client.protocol, client.port);
ServiceConnectionData service =
(ServiceConnectionData) services.get(key);
// Log.log("SERVER: requested connection to service " + key);
if (service == null) {
// indicates error
client.error = BluetoothConnectionException.FAILED_NOINFO;
// Log.log("SERVER: service not found " + key);
} else {
// either accepts or sets error value
service.accept(client);
}
return client;
|
public static com.sun.midp.jsr82emul.EmulationServer | getInstance()Retrns the only instance if the server.
launch();
return instance;
|
public static synchronized void | launch()Launches the server if not yet running.
if (launched) {
return;
}
try {
serverSocket = new Socket();
serverSocket.open(SOCKET_PORT, internalSecurityToken);
instance = new EmulationServer();
Thread thread = new Thread(instance);
thread.start();
} catch (IOException e) {
// server is already running within another isolate or under
// another VM control - nothing to do
} finally {
launched = true;
}
|
public static void | main(java.lang.String[] args)Launches the server in a standalone manner.
launch();
|
public synchronized byte[] | registerDevice(DeviceState deviceState)Registers new device in the ether.
DeviceKey key = null;
int i;
for (i = 0; i < ADDR_COUNT; i++) {
if (!devices.containsKey(defaultAddr[i])) {
key = defaultAddr[i];
break;
}
}
if (i == ADDR_COUNT) {
byte[] btaddr = new byte[Const.BTADDR_SIZE];
for (i = 0; i < 4; i++) {
btaddr[i] = (byte) (nextAddr >> (8 * i));
}
key = new DeviceKey(btaddr);
nextAddr = nextAddr % (Integer.MAX_VALUE - 1) + 1;
}
devices.put(key, deviceState);
return key.getAddrBytes();
|
public com.sun.midp.jsr82emul.ServiceKey | registerService(ServiceConnectionData service, byte[] btaddr)Registers a service in Bluetooth ether, i.e. starts advertising it.
ServiceKey key = new ServiceKey(
btaddr, service.protocol, service.port);
Log.log("SERVER: registreing service " + key);
ServiceConnectionData registered =
(ServiceConnectionData) services.get(key);
if (registered != null) {
registered.setAccepting();
} else {
services.put(key, service);
}
return key;
|
public void | run()The Runnable interface implementation.
while (true) {
try {
new ClientHandler(
(SocketConnection)serverSocket.acceptAndOpen());
} catch (Throwable e) {
// ignoring
}
}
|
public InquiryResults | runInquiry(int discoverable, byte[] btaddr)Performs inquiry (devices discovery).
try {
// Syncronizing classes of devices for the scenario like this:
// - make an action that should update class of device1;
// - start inquiry on device2 expecting updated class of device1.
// In this case class of device1 changes immediately locally but
// it takes time to update it on the emulation server and affect all
// inquiry operations. Waiting here to let class of device1 to become
// up-to-date.
Thread.sleep(INQUIRY_DELAY);
} catch (InterruptedException e) {}
InquiryResults res = new InquiryResults();
Enumeration addresses = devices.keys();
Enumeration states = devices.elements();
while (addresses.hasMoreElements()) {
DeviceKey addr = (DeviceKey) addresses.nextElement();
DeviceState state = (DeviceState) states.nextElement();
if (state.getDiscoverable() == discoverable &&
!addr.equals(btaddr)) {
res.add(addr.getAddrBytes(), state.getCoD());
}
}
return res;
|
public synchronized void | unregisterDevice(byte[] btaddr)Unregisters device from the emulated ether.
devices.remove(new DeviceKey(btaddr));
|
public void | unregisterService(com.sun.midp.jsr82emul.ServiceKey key)Stops advertising the service represented by given record.
Log.log("SERVER: unregistreing service " + key);
services.remove(key);
|