*/
@WebMethod
public OutputConnectionWS FPDUReception (FPDU fpdu)
throws ConnectionException, WrongFPDUException {
Connection connection;
FPDU fpduResponse = null;
FPDUParameter parameter;
String remoteBank = fpdu.getParameter().getSender();
Date date;
Boolean respBool = false;
// First we log the received FPDU
PeSITLog.logRemote(fpdu);
switch (fpdu.getType()) {
case CONNECT:
System.out.println(remoteBank +
" asked for connection");
connection = ConnectionPool.getConnection(
remoteBank);
// If a bank is already using the service
if (connection == null) {
System.out.println("CONNECTION WS Error: "
+ ConnectionPool.getLocalIdentity() +
" already asked for a connection");
// Raise exception
throw new ConnectionException("CONNECTION WS " +
"Error: "
+ ConnectionPool.getLocalIdentity() +
" already asked for a connection");
} else if (connection.getState() != EnumState.idle) {
System.out.println("Error: "
+ remoteBank +
" cannot ask for connection as a connection" +
" already exists");
throw new ConnectionException("CONNECTION WS " +
"Error: "
+ remoteBank +
" cannot ask for connection as a connection" +
" already exists");
}
connection.setState(EnumState.connected);
// Set connection ids
connection.setRemoteId(
fpdu.getParameter().getLocalConnectionId());
// If the new connection is open, send aconnect
parameter = new FPDUParameter("Local",
fpdu.getParameter().getSender());
date = new Date();
parameter.setDate(date);
parameter.setLocalConnectionId(connection.getIdConnection());
parameter.setRemoteConnectionId(connection.getRemoteId());
fpduResponse = new FPDU(EnumFPDU.ACONNECT, parameter);
respBool = true;
break;
case ACONNECT:
connection = ConnectionPool.getOutConnections()
.get(remoteBank);
if (connection == null ||
connection.getState() != EnumState.waitfor_acconnect) {
System.out.println("CONNECTION WS Error: " +
"Cannot ack connect ( " +
"did not send connect in the first place)");
// Raise exception
throw new WrongFPDUException("CONNECTION WS Error: " +
"Cannot ack connect ( " +
"did not receive connect in the first place)");
}
connection.setState(EnumState.connected);
// A transfer is now ready to start
break;
case RCONNECT:
connection = ConnectionPool.getOutConnections()
.get(remoteBank);
if (connection == null ||
connection.getState() != EnumState.waitfor_acconnect) {
System.out.println("CONNECTION WS Error: " +
"Cannot refuse connect ( " +
"did not receive connect in the first place)");
// Raise exception
throw new WrongFPDUException("CONNECTION WS Error: " +
"Cannot refuse connect ( " +
"did not receive connect in the first place)");
}
connection.setState(EnumState.connection_refused);
// A transfer is now ready to start
break;
case RELEASE:
// Here the remote bank wants to close the connection
// We have to check if the remote bank is the actual seeker
// of the connection , otherwise it cannot ask for a release
connection = ConnectionPool.getOutConnections()
.get(remoteBank);
if (connection == null ||
connection.getState() != EnumState.connected) {
System.out.println("CONNECTION WS Error: " +
"Cannot release connection (connection " +
"was not open in the first place)");
// Raise exception
throw new WrongFPDUException("CONNECTION WS Error: " +
"Cannot release connection (connection " +
"was not open in the first place)");
// TODO: handle lost of release packets
}
connection.setState(EnumState.idle);
// Release connection
ConnectionPool.returnConnection(remoteBank, connection);
// Create FPDU relconf
parameter = new FPDUParameter("Local", remoteBank);
date = new Date();
parameter.setDate(date);
parameter.setLocalConnectionId(connection.getIdConnection());
parameter.setRemoteConnectionId(connection.getRemoteId());
fpduResponse = new FPDU(EnumFPDU.RELCONF, parameter);
respBool = true;
break;
case RELCONF:
// The connection is closed
connection = ConnectionPool.getOutConnections()