}
@WebMethod
public boolean connect(String bankName) {
// Verify if connection already exists
Connection currentConnection = null;
try {
currentConnection = ConnectionPool.getConnection(bankName);
} catch (Exception e1) {
e1.printStackTrace();
}
// Test the connection state : if already connected then return true
if (currentConnection != null && currentConnection.getState() == EnumState.connected) {
return true;
}
else if (currentConnection != null && currentConnection.getState() == EnumState.idle) {
// if idle then ask for a connection
ConnectionWS currentConnectionWS = new ConnectionWS();
OutputConnectionWS outputToSend;
try {
outputToSend = currentConnectionWS.messageParser("connect_" + bankName);
} catch (Exception e) {
e.printStackTrace();
return false;
}
FPDU fpduToSend = outputToSend.getFpdu();
// Send FPDU to the interserver
if (!sendFPDU(fpduToSend)) {
// If sendFPDU fail then return false
return false;
}
}
// Get the connection and wait for connected state
try {
currentConnection = ConnectionPool.getConnection(bankName);
} catch (Exception e1) {
return false;
}
// If null connection return false
if (currentConnection == null) {
return false;
}
// Wait for connected state
for (int i = 0; i < PesitImpl.timeOut; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Check state of currentConnection
if (currentConnection.getState() == EnumState.connected) {
return true;
}
else if (currentConnection.getState() == EnumState.connection_refused) {
break;
}
}
// If connection is still idle then the CONNECT message was lost
// Otherwise it means that the connection is in a particular state for a client and then
// - if transfer_in_progress return true
// - if wait_for_relconf then return false
// TODO : more cases
if (currentConnection.getState() == EnumState.idle) {
// Remove connection from connection pool
ConnectionPool.returnConnection(bankName, currentConnection);
return false;
}
else if (currentConnection.getState() == EnumState.waitfor_relconf) {
return false;
}
return false;
}