Link sendLink = sendReceiveLinks.getSendLink();
Link receiveLink = sendReceiveLinks.getReceiveLink();
Link c2sLink = null;
Link s2cLink = null;
if (state == INVALID_STATE) {
throw new IllegalStateException();
}
while (state != END_STATE) {
switch (state) {
case BEGIN_SESSION_STATE: {
// start session
sendLink.send(startSessionMsg);
// advance to next state
state = SEND_SERVICE_ID_STATE;
break;
}
case SEND_SERVICE_ID_STATE: {
// send service ID
LinkMessage msg = LinkMessage.newStringMessage(serviceID);
sendLink.send(msg);
// advance to next state
state = WAIT_FOR_STATUS_STATE;
break;
}
case WAIT_FOR_STATUS_STATE: {
// wait for status reply
LinkMessage msg = receiveLink.receive();
// extract status
byte[] data = msg.extractData();
ByteArrayInputStream bis = new ByteArrayInputStream(data);
DataInputStream is = new DataInputStream(bis);
int status = is.readInt();
// advance to next state
if (status == SystemServiceRequestProtocolAMS.SERVICE_REQUEST_STATUS_OK) {
state = WAIT_FOR_SERVICE_TO_CLIENT_LINK_STATE;
} else {
state = END_STATE;
}
break;
}
case WAIT_FOR_SERVICE_TO_CLIENT_LINK_STATE: {
// wait for link
LinkMessage msg = receiveLink.receive();
s2cLink = msg.extractLink();
// check link state
if (!s2cLink.isOpen()) {
throw new IllegalStateException();
}
// advance to next state
state = WAIT_FOR_CLIENT_TO_SERVICE_LINK_STATE;
break;
}
case WAIT_FOR_CLIENT_TO_SERVICE_LINK_STATE: {
// wait for link
LinkMessage msg = receiveLink.receive();
c2sLink = msg.extractLink();
// check link state
if (!c2sLink.isOpen()) {
throw new IllegalStateException();
}
// advance to next state
state = SEND_LINKS_RECEIVED_ACK_STATE;
break;
}
case SEND_LINKS_RECEIVED_ACK_STATE: {
// acknowledge links reception
sendLink.send(linksReceivedMsg);
// advance to next state
state = END_SESSION_STATE;
break;
}
case END_SESSION_STATE: {
// close session
sendLink.send(endSessionMsg);
if (c2sLink != null && s2cLink != null) {
connectionLinks = new SystemServiceConnectionLinks(
c2sLink, s2cLink);
}
// advance to next state
state = END_STATE;
break;
}
}
}
state = INVALID_STATE;