it.remove();
}
} catch (Exception e) {
- EMLogger.getLogger().warning(e.getMessage());
+ EMLogger.getLogger().warning("Selector error : " + e.getMessage());
}
}
}
private void checkConnection(SelectionKey key) {
SocketChannel sc = (SocketChannel)key.channel();
VMSocket vms = (VMSocket)key.attachment();
+ boolean isReady = false;
+
if (sc.isConnected()) {
- // Register to selector with read_event and send message to ECS
- if (addToRegisterQueue(vms, SelectionKey.OP_READ)) {
- vms.sendDeviceReq();
- } else {
- //EMLogger.getLogger().warning("Connect to ECS(port:" + vms.getEcs_port() + ") Fail.");
- vms.disconnect();
+ isReady = addToRegisterQueue(vms, SelectionKey.OP_READ);
+
+ } else {
+ try {
+ if (sc.finishConnect()) {
+ // Connect success
+ isReady = addToRegisterQueue(vms, SelectionKey.OP_READ);
+ } else {
+ // Connect pending.
+ EMLogger.getLogger().info("Connection not completed yet : " + vms.getEcs_port());
+ return;
+ }
+ } catch (IOException e) { // Connection failed.
+ isReady = false;
}
+ }
+
+ if (isReady) {
+ EMLogger.getLogger().info("Connection success : " + vms.getEcs_port());
+ vms.sendDeviceReq();
+
} else {
- // Close socket
+ // EMLogger.getLogger().info("Connection fail : " + vms.getEcs_port());
vms.disconnect();
- EMLogger.getLogger().info("Connect to ECS(port:" + vms.getEcs_port() + ") Fail.");
}
}
}
public boolean connect() {
+ // Add socket to selector and try connect.
+ // Connection process is completed by selector event.
try {
socket = SocketChannel.open();
socket.configureBlocking(false);
- if (!socket.connect(new InetSocketAddress("localhost", ecs_port))) {
- if (!socket.finishConnect()) {
- // Connection not complete yet. So postpone checking of this connection later
- // by registering this socket to selector.
- if (CheckingRunningEmulator.addToRegisterQueue(this, SelectionKey.OP_CONNECT)) {
- return true;
- } else {
- EMLogger.getLogger().warning("Connect to ECS(port:" + ecs_port +") Fail.");
- disconnect();
- }
- }
+
+ if (CheckingRunningEmulator.addToRegisterQueue(this, SelectionKey.OP_CONNECT)) {
+ socket.connect(new InetSocketAddress("localhost", ecs_port));
+
+ } else {
+ EMLogger.getLogger().warning("Add to selector(connect event) fail :" + ecs_port);
+ disconnect();
}
- EMLogger.getLogger().info("Connected to ECS(port:" + ecs_port + ")");
+
} catch (IOException e) {
- //EMLogger.getLogger().info("Connect to ECS(port:" + ecs_port +") Fail." + e.getMessage());
+ // EMLogger.getLogger().info("Connect fail.(" + e.getMessage() + ") - " + ecs_port);
disconnect();
return false;
}
- if (socket != null) {
- // success to connection
- if (CheckingRunningEmulator.addToRegisterQueue(this, SelectionKey.OP_READ)) {
- sendDeviceReq();
- } else {
- EMLogger.getLogger().warning("Connect to ECS(port:" + ecs_port +") Fail.");
- disconnect();
- return false;
- }
- }
return true;
}