FileDocCategorySizeDatePackage
ServiceConnectionData.javaAPI DocphoneME MR2 API (J2ME)11843Wed May 02 18:00:30 BST 2007com.sun.midp.jsr82emul

ServiceConnectionData

public class ServiceConnectionData extends BytePack
Utility class that keeps service connection information, packs it into byte array and restores form it. Has methods to check if a service represented by an instance can accept a connection represented by another one.

Fields Summary
int
error
Error type, like in BluetoothConnectionException, ff error occured while looking for connection, -1 if there is no error. Only makes sense in case of CONNECTION_DATA.
int
socketPort
TCP socket port emulated service listens on.
byte[]
address
IP address or host name of service in case of CONNECTION_DATA, Bluetooth address of client in case of CLIENT_DATA
int
port
Channel ID or PSM.
int
receiveMTU
ReceiveMTU in server side terms.
int
transmitMTU
TransmitMTU in server side terms.
int
protocol
Protocol ID as in BluetoothUrl.
boolean
master
Shows wither server is master.
boolean
encrypt
Shows wither encrypted connection required.
boolean
authorize
Shows wither authorization required by server.
boolean
authenticate
Shows wither authentication required by server.
private boolean
accepting
Identifies if a service defined by this instance accepting currently. This flag is used to overcome TCK inconsistency where client may start connecting to Bluetooth TCK Agent while the latter is still not ready after previous connection.
private static final int
ACCEPT_DELAY
Delay duration for the case when acception is requested while it is not known if corresponding service is accepting.
private static final int
MAX_ACCEPT_TRIALS
Max amount of acception trials when it is not known if service is accepting
static final int
SERVER_DATA
Packed data type that implies that the following fields are included: socketPort, port, receiveMTU, transmitMTU, receiveMTU, protocol, master, encrypt, authorize, authenticate.
static final int
CONNECTION_DATA
Packed data type that implies that the following fields are included: socketPort, IP address, receiveMTU, transmitMTU.
static final int
CLIENT_DATA
Packed data type that implies that the following fields are included: transmitMTU, receiveMTU, Bluetooth address.
static final int
CONN_REQUEST_DATA
Packed data type that implies that the following fields are included: Bluetooth address, protocol, port, transmitMTU, receiveMTU, encrypt, authenticate, master. It is used CONNECT_TO_SERVICE request to the emulation server
private static final int
BTADDR_SIZE
Size of Bluetooth address in bytes representation.
static final int
CLIENT_DATA_SIZE
Byte array size when packed with CLIENT_DATA type.
static final int
SERVER_DATA_SIZE
Byte array size when packed with SERVER_DATA type.
private static final int
PROTOCOL_MASK
Bit mask to highlight protcol type.
private static final int
MASTER_MASK
Bit mask to highlight master flag.
private static final int
ENCRYPT_MASK
Bit mask to highlight encrypt flas.
private static final int
AUTHORIZE_MASK
Bit mask to highlight authorize flag.
private static final int
AUTHENTICATE_MASK
Bit mask to highlight authenticate flag.
Constructors Summary
ServiceConnectionData()
Constructs an instance which indicates that unknown error occured while looking for connection.

    
                       
     
        this(BluetoothConnectionException.FAILED_NOINFO);
    
ServiceConnectionData(int error)
Constructs an instance which indicates that a connection error occured.

param
error error code as in BluetoothConnectionException

        this.error = error;
    
ServiceConnectionData(byte[] data, int type)
Constructs an instance by given byte representation.

param
data byte representation
param
type type of packed data, must be one of SERVER_DATA, CONNECTION_DATA, CLIENT_DATA, CONN_REQUEST_DATA

        super(data);        

        switch (type) {        
        case CONN_REQUEST_DATA:
            address = extractBytes(BTADDR_SIZE);
        
        // the rest of CONN_REQUEST_DATA case is poceeded here as well
        case SERVER_DATA:
            if (type == SERVER_DATA) {
                socketPort = extractInt();
            }
            
            port = extractInt();
            receiveMTU = extractInt();
            transmitMTU = extractInt();
        
            int misc = extract();
            protocol = misc & PROTOCOL_MASK;
            master = (misc & MASTER_MASK) != 0;
            encrypt = (misc & ENCRYPT_MASK) != 0;
            authorize = (misc & AUTHORIZE_MASK) != 0;
            authenticate = (misc & AUTHORIZE_MASK) != 0;
            break;
            
        case CONNECTION_DATA:
            error = extractInt();
            if (error == -1) {
                socketPort = extractInt();
                receiveMTU = extractInt();
                transmitMTU = extractInt();
                address = extractBytes(Const.IP_SIZE);
            }
            break;
        
        case CLIENT_DATA:
            receiveMTU = extractInt();
            transmitMTU = extractInt();
            address = extractBytes(BTADDR_SIZE);
            break;
            
        default:
            throw new IllegalArgumentException();
        }
        
        release();
    
Methods Summary
voidaccept(com.sun.midp.jsr82emul.ServiceConnectionData client)
Checks if client connection with given parameters can be accepted, if it can not, sets error code. If acception is possible but requires fields alignment, makes it modifying connection data passed as a parameter.

param
client parameters of client connection request derived from client url, the parameters can be modified by this method if connection can be accepted.
return
error code as in BluetoothConnectionException, if connection can not be accepted, -1 if it can

        client.address = address;
        client.socketPort = socketPort;
        client.error = -1;
        
        if (client.master && master) {
            client.error = BluetoothConnectionException.UNACCEPTABLE_PARAMS;
        
        } else if (client.protocol == BluetoothUrl.L2CAP) {
            // If connection accepted successfully,
            // (receiveMTU, transmitMTU) set here for the parameter 
            // become actual (receiveMTU, transmitMTU) for server and
            // actual (transmitMTU, receiveMTU) for client.
            
            if (client.transmitMTU > receiveMTU || transmitMTU > client.receiveMTU) {
                client.error = BluetoothConnectionException.UNACCEPTABLE_PARAMS;
            } else {
                if (client.transmitMTU == -1) {
                    client.transmitMTU = receiveMTU;
                }
                if (transmitMTU != -1) {
                    client.receiveMTU = transmitMTU;
                }
            }
        }
        
        if (client.error == -1) {
            synchronized (this) {
                int trial = 0;
                for (; trial < MAX_ACCEPT_TRIALS && !accepting; trial++) {
                    try {
                        Thread.sleep(ACCEPT_DELAY);
                    } catch (Throwable t) {}
                }
                
                if (trial == MAX_ACCEPT_TRIALS) {
                    client.error = BluetoothConnectionException.TIMEOUT;
                } else {
                    // accepted successfully, now service is occupied by current client
                    accepting = false;
                }
            }
        }
    
voidsetAccepting()
Sets acception flag to true to identify that represented service is currebtly accepting. A part of the way that overcomes TCK inconsistency.

        accepting = true;
    
byte[]toByteArray(int type)
Retrieves bytes representation.

param
type type of packed data, that defines which fields are to be packed, must be one of SERVER_DATA, CONNECTION_DATA, CLIENT_DATA, CONN_REQUEST_DATA
return
byte array that keeps packed properties

        switch (type) {
        case CONN_REQUEST_DATA:
            reset(new byte[BTADDR_SIZE + SERVER_DATA_SIZE - 1]);
            appendBytes(address);
        
        // the rest of CONN_REQUEST_DATA is proceeded here as well
        case SERVER_DATA:
            if (type == SERVER_DATA) {
                reset(new byte[SERVER_DATA_SIZE]);
                appendInt(socketPort);
            }
            
            appendInt(port);
            appendInt(receiveMTU);
            appendInt(transmitMTU);
        
            int misc = protocol;
            misc |= master? MASTER_MASK : 0;
            misc |= encrypt? ENCRYPT_MASK : 0;
            misc |= authorize? AUTHORIZE_MASK : 0;
            misc |= authenticate? AUTHORIZE_MASK : 0;
            append((byte)misc);
            break;
        
        case CONNECTION_DATA:
            if (error == -1) {
                reset(new byte[4 * 4 + Const.IP_SIZE]);
                appendInt(error);
                appendInt(socketPort);
                appendInt(receiveMTU);
                appendInt(transmitMTU);
                appendBytes(address);        
            
            } else {
                reset(new byte[4]);
                appendInt(error);
            }
            break;
            
        case CLIENT_DATA:
            reset(new byte[CLIENT_DATA_SIZE]);
            appendInt(receiveMTU);
            appendInt(transmitMTU);
            appendBytes(address);
            break;
            
        default:
            throw new IllegalArgumentException();
        }
        
        return release();