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

SystemServiceRequestProtocolAMS

public final class SystemServiceRequestProtocolAMS extends Object

Fields Summary
static final int
SERVICE_REQUEST_STATUS_OK
static final int
SERVICE_REQUEST_STATUS_ERROR
private static final int
INVALID_STATE
private static final int
WAIT_FOR_BEGIN_SESSION_STATE
private static final int
WAIT_FOR_SERVICE_ID_STATE
private static final int
SEND_SERVICE_REQUEST_STATUS_STATE
private static final int
SEND_SERVICE_TO_CLIENT_LINK_STATE
private static final int
SEND_CLIENT_TO_SERVICE_LINK_STATE
private static final int
WAIT_FOR_LINKS_RECEIVED_ACK_STATE
private static final int
WAIT_FOR_END_SESSION_STATE
private static final int
END_SESSION_STATE
private static final int
END_STATE
private SystemServiceRequestListener
requestListener
private int
state
Constructors Summary
SystemServiceRequestProtocolAMS(SystemServiceRequestListener requestListener)

 

    
              

        /**
         * Argument sanity check
         */
        if (requestListener == null) {
            throw new NullPointerException();
        }

        this.requestListener = requestListener;
    
Methods Summary
private SystemServiceConnectionLinksdoHandleServiceRequest(Link sendLink, Link receiveLink)


        String serviceID = "";
        SystemServiceConnectionLinks connectionLinks = null;

        if (state == INVALID_STATE) {
            throw new IllegalStateException();
        }

        while (state != END_STATE) {
            switch (state) {
                case WAIT_FOR_BEGIN_SESSION_STATE: {
                    // wait for session begin request 
                    LinkMessage msg = receiveLink.receive();
                    String str = msg.extractString();

                    // check request validity
                    if (!str.equals(
                      SystemServiceRequestProtocolClient.START_SESSION_STR)) {
                        throw new IllegalStateException();
                    }

                    // advance to next state
                    state = WAIT_FOR_SERVICE_ID_STATE;
                    break;
                }

                case WAIT_FOR_SERVICE_ID_STATE: {
                    // wait for service id
                    LinkMessage msg = receiveLink.receive();
                    serviceID = msg.extractString();

                    // advance to next state
                    state = SEND_SERVICE_REQUEST_STATUS_STATE; 
                    break;
                }

                case SEND_SERVICE_REQUEST_STATUS_STATE: {
                    // try to obtain connection to service
                    int status = SERVICE_REQUEST_STATUS_OK;
                    try {
                        connectionLinks = null;
                        connectionLinks = 
                            requestListener.onServiceRequest(serviceID);
                    } finally {
                        if (connectionLinks == null) {
                            status = SERVICE_REQUEST_STATUS_ERROR;
                        }

                        // send status
                        ByteArrayOutputStream bos = 
                            new ByteArrayOutputStream();
                        DataOutputStream os = new DataOutputStream(bos);
                        os.writeInt(status);
                        byte[] data = bos.toByteArray();

                        LinkMessage msg = LinkMessage.newDataMessage(data);
                        sendLink.send(msg);

                        // advance to next state
                        if (status == SERVICE_REQUEST_STATUS_OK) {
                            state = SEND_SERVICE_TO_CLIENT_LINK_STATE;
                        } else {
                            state = END_STATE;
                        }
                    }

                    break;
                }

                case SEND_SERVICE_TO_CLIENT_LINK_STATE: {
                    // send service to client link
                    Link link = connectionLinks.getSendLink();
                    LinkMessage msg = LinkMessage.newLinkMessage(link);
                    sendLink.send(msg);

                    // advance to next state
                    state = SEND_CLIENT_TO_SERVICE_LINK_STATE; 
                    break;
                }

                case SEND_CLIENT_TO_SERVICE_LINK_STATE: {
                    // send client to service link
                    Link link = connectionLinks.getReceiveLink();
                    LinkMessage msg = LinkMessage.newLinkMessage(link);
                    sendLink.send(msg);

                    // advance to next state
                    state = WAIT_FOR_LINKS_RECEIVED_ACK_STATE;
                    break;
                }

                case WAIT_FOR_LINKS_RECEIVED_ACK_STATE: {
                    // wait for links recieved ack
                    LinkMessage msg = receiveLink.receive();
                    String str = msg.extractString();

                    // check request validity
                    if (!str.equals(SystemServiceRequestProtocolClient.
                                LINKS_RECEIVED_ACK_STR)) {
                        throw new IllegalStateException();
                    }

                    // notify listener about connection passed to client
                    if (connectionLinks != null) {
                        requestListener.onLinksPassedToClient(connectionLinks);
                    }
                                                           
                    // advance to next state
                    state = WAIT_FOR_END_SESSION_STATE;
                    break;
                }

                case WAIT_FOR_END_SESSION_STATE: {
                    // wait for session end request
                    LinkMessage msg = receiveLink.receive();
                    String str = msg.extractString();

                    // check request validity
                    if (!str.equals(
                      SystemServiceRequestProtocolClient.END_SESSION_STR)) {
                        throw new IllegalStateException();
                    }

                    // advance to next state
                    state = END_SESSION_STATE;
                    break;
                }

                case END_SESSION_STATE: {
                    // advance to next state
                    state = END_STATE;
                    break;
                }
            }
        }

        state = INVALID_STATE;

        return connectionLinks;
    
SystemServiceConnectionLinkshandleServiceRequest(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();
        }

        SystemServiceConnectionLinks connectionLinks = null;

        state = WAIT_FOR_BEGIN_SESSION_STATE;
        try {
            connectionLinks = doHandleServiceRequest(sendLink, receiveLink);
        } finally {
            state = INVALID_STATE;
        }

        return connectionLinks;