FileDocCategorySizeDatePackage
SystemServiceRequestProtocolClient.javaAPI DocphoneME MR2 API (J2ME)7383Wed May 02 18:00:10 BST 2007com.sun.midp.services

SystemServiceRequestProtocolClient

public final class SystemServiceRequestProtocolClient extends Object

Fields Summary
static final String
START_SESSION_STR
static final String
END_SESSION_STR
static final String
LINKS_RECEIVED_ACK_STR
private static final int
INVALID_STATE
private static final int
BEGIN_SESSION_STATE
private static final int
SEND_SERVICE_ID_STATE
private static final int
WAIT_FOR_STATUS_STATE
private static final int
WAIT_FOR_SERVICE_TO_CLIENT_LINK_STATE
private static final int
WAIT_FOR_CLIENT_TO_SERVICE_LINK_STATE
private static final int
SEND_LINKS_RECEIVED_ACK_STATE
private static final int
END_SESSION_STATE
private static final int
END_STATE
private LinkMessage
startSessionMsg
private LinkMessage
endSessionMsg
private LinkMessage
linksReceivedMsg
private SystemServiceConnectionLinks
sendReceiveLinks
private SystemServiceConnectionLinks
connectionLinks
private int
state
Constructors Summary
SystemServiceRequestProtocolClient(SystemServiceConnectionLinks sendReceiveLinks)


     
             

        Link sendLink = sendReceiveLinks.getSendLink();
        Link receiveLink = sendReceiveLinks.getReceiveLink();

        /**
         * Arguments sanity checks
         */
        if (sendLink == null || receiveLink == null) {
            throw new NullPointerException();
        }

        if (!sendLink.isOpen() || !receiveLink.isOpen()) {
            throw new IllegalStateException();
        }

        this.sendReceiveLinks = sendReceiveLinks;

        this.startSessionMsg = LinkMessage.newStringMessage(START_SESSION_STR);
        this.endSessionMsg = LinkMessage.newStringMessage(END_SESSION_STR);
        this.linksReceivedMsg = LinkMessage.newStringMessage(
                LINKS_RECEIVED_ACK_STR);
    
Methods Summary
private voiddoRequestService(java.lang.String serviceID)


        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;
    
SystemServiceConnectionLinksgetSystemServiceConnectionLinks()

        return connectionLinks;
    
voidrequestService(java.lang.String serviceID)


        connectionLinks = null;
        state = BEGIN_SESSION_STATE;

        try {
            doRequestService(serviceID);
        } finally {
            state = INVALID_STATE; 
        }