FileDocCategorySizeDatePackage
ClientSessionImpl.javaAPI DocphoneME MR2 API (J2ME)8791Wed May 02 18:00:32 BST 2007com.sun.kvem.jsr082.obex

ClientSessionImpl

public class ClientSessionImpl extends ObexPacketStream implements javax.obex.ClientSession
The class implements client side of session.

Fields Summary
private boolean
busy
private Object
lockObject
private int
owner
javax.obex.Operation
operation
Current active operation. When not null, all method is ClientSession is blocked.
private long
connId
Constructors Summary
public ClientSessionImpl(ObexTransport transport)


         
        super(transport);

        // owner field of all created HeaderSets
        owner = HeaderSetImpl.OWNER_CLIENT;
        isClient = true;
    
Methods Summary
public javax.obex.HeaderSetconnect(javax.obex.HeaderSet headers)

        lockCheckHeaders(headers);
        try {
            if (isConnected) {
                throw new IOException("already connected");
            }
            byte[] head = {
                (byte)OPCODE_CONNECT,
                0, 0, // length will be here
                0x10, // obex protocol version 1.0
                0x00, // flags, all zero for this version of OBEX
                // maximum client supported packet length
                (byte) (OBEX_MAXIMUM_PACKET_LENGTH / 0x100),
                (byte) (OBEX_MAXIMUM_PACKET_LENGTH % 0x100),
            };

            sendPacket(head, -1, (HeaderSetImpl) headers, true);
            recvPacket();
            if (packetLength < 7 || buffer[3] != 0x10) {
		// IMPL_NOTE: It is not decided what the implementation should do
		// if the OBEX version does not match. For example, Windows
		// implementation uses version 1.2, Linux implementation
		// uses version 1.1. JSR-82 should probably work with both.
                // throw new IOException("unsupported server obex version");
            }
            HeaderSetImpl recvHeaders = new HeaderSetImpl(owner);
            parsePacketHeaders(recvHeaders, 7);

            if (shouldSendAuthResponse()) {
                // server ignores challenge before authenticating client
                authFailed = false;
                sendPacket(head, -1, (HeaderSetImpl) headers, true);
                recvPacket();
                if (packetLength < 7 || buffer[3] != 0x10) {
                    // IMPL_NOTE: See the comment above.
		    // throw new IOException("unsupported server obex version");
                }
                recvHeaders = new HeaderSetImpl(owner);
                parsePacketHeaders(recvHeaders, 7);
            }

            maxSendLength = decodeLength16(5);

            if (maxSendLength > OBEX_MAXIMUM_PACKET_LENGTH) {
                maxSendLength = OBEX_MAXIMUM_PACKET_LENGTH;
            }

            if (packetType == ResponseCodes.OBEX_HTTP_OK) {
                if (authFailed) {
                    throw new IOException("server is not authenticated");
                }
                isConnected = true;
            }
            return recvHeaders;
        } finally {
            unlock();
        }
    
public javax.obex.HeaderSetcreateHeaderSet()

        return new HeaderSetImpl(HeaderSetImpl.OWNER_CLIENT_USER);
    
public javax.obex.HeaderSetdelete(javax.obex.HeaderSet headers)

        lockCheckHeaders(headers);
        try {
            if (!isConnected) {
                throw new IOException("not connected");
            }
            Operation op = new ClientOperation(this,
                    (HeaderSetImpl)headers, false);

            op.getResponseCode(); // indicates end of operation
            HeaderSet recvHeaders = op.getReceivedHeaders();
            op.close();
            return recvHeaders;
        } finally {
            unlock();
        }
    
public javax.obex.HeaderSetdisconnect(javax.obex.HeaderSet headers)

        lockCheckHeaders(headers);
        try {
            if (!isConnected) {
                throw new IOException("not connected");
            }
            sendPacket(PACKET_DISCONNECT, connId,
                    (HeaderSetImpl) headers, true);
            recvPacket();
            HeaderSetImpl recvHeaders = new HeaderSetImpl(owner);
            parsePacketHeaders(recvHeaders, 3);
            if (packetType == ResponseCodes.OBEX_HTTP_OK) {
                isConnected = false;
            }
            return recvHeaders;
        } finally {
            unlock();
        }
    
public javax.obex.Operationget(javax.obex.HeaderSet headers)

        lockCheckHeaders(headers);
        try {
            if (!isConnected) {
                throw new IOException("not connected");
            }
            new ClientOperation(this, (HeaderSetImpl)headers, true);
            return operation;
        } finally {
            unlock();
        }
    
public longgetConnectionID()

        return connId;
    
voidheaderTooLarge()

        throw new IOException("header too large");
    
private voidlockCheckHeaders(javax.obex.HeaderSet headers)

        if (isClosed()) {
            throw new IOException("session closed");
        }

        if (operation != null) {
            throw new IOException("already in operation");
        }

        if (headers != null) {
            if (!(headers instanceof HeaderSetImpl)
                    || ((HeaderSetImpl)headers).owner == owner) {
                throw new IllegalArgumentException("wrong headerset class");
            }
        }
        synchronized (lockObject) {
            if (busy) {
                throw new IOException("already in operation");
            }
            busy = true;
        }
    
voidonAuthenticationFailure(byte[] username)

        if (operation != null) {
            operation.abort();
        }
        throw new IOException("server is not authenticated");
    
voidonMissingAuthResponse()

        if (packetType != ResponseCodes.OBEX_HTTP_UNAUTHORIZED) {
            if (operation != null) {
                operation.abort();
            }
            throw new IOException("no auth response from server");
        }
    
public javax.obex.Operationput(javax.obex.HeaderSet headers)

        lockCheckHeaders(headers);
        try {
            if (!isConnected) {
                throw new IOException("not connected");
            }
            new ClientOperation(this,
                    (HeaderSetImpl) headers, false);
            return operation;
        } finally {
            unlock();
        }
    
public voidsetConnectionID(long id)

        if (id < 0L || id > 0xFFFFFFFFL) {
            throw new IllegalArgumentException("invalid id");
        }
        connId = id;
    
public javax.obex.HeaderSetsetPath(javax.obex.HeaderSet headers, boolean backup, boolean create)

        lockCheckHeaders(headers);
        try {
            if (!isConnected) {
                throw new IOException("not connected");
            }
            byte[] head = {
                (byte)OPCODE_SETPATH,
                0, 0, // length will be here
                (byte)((backup ? 1 : 0) + (create ? 0 : 2)), // flags
                0x00, // constants
            };

            sendPacket(head, connId, (HeaderSetImpl) headers, true);
            recvPacket();
            HeaderSetImpl recvHeaders = new HeaderSetImpl(owner);
            parsePacketHeaders(recvHeaders, 3);
            return recvHeaders;
        } finally {
            unlock();
        }
    
private voidunlock()

        synchronized (lockObject) {
            busy = false;
        }