FileDocCategorySizeDatePackage
CBSMessageConnection.javaAPI DocphoneME MR2 API (J2ME)12186Wed May 02 18:00:32 BST 2007com.sun.tck.wma.cbs

CBSMessageConnection

public class CBSMessageConnection extends com.sun.tck.wma.PropLoader implements com.sun.tck.wma.MessageConnection
CBS message connection handler.

Fields Summary
protected String
host
Machine name - the parsed target address from the URL.
protected String
msgID
Message ID from the URL connection string.
protected String
clientHost
Datagram host for sending CBS messages.
protected int
portOut
Datagram transport for sending.
protected String
phoneNumber
Phone number of the message sender.
DatagramSocket
dgc
Datagram server connection.
byte[]
buf
Datagram buffer.
DatagramPacket
mess
Datagram envelope for sending or receiving messages.
protected boolean
open
Open flag indicates when the connection is closed. When a connection is closed, subsequent operations throw an exception.
Constructors Summary
public CBSMessageConnection()
Construct a new CBS message connection handler.



                
      

        /* 
         * Configurable parameters for low level transport.
         * e.g. cbs://:11100 maps to datagram://129.148.70.80:123
         */

        clientHost = "localhost";
        portOut = 22200;
        phoneNumber = "+5551234";

        /* 
         * Check for overrides in the "connections.prop"
         * configuration file.
         */

        
        clientHost = getProp("localhost", "JSR_205_DATAGRAM_HOST",
            "connections.prop", "DatagramHost");

        portOut = getIntProp(22200, "JSR_205_CBS_OUT_PORT",
            "connections.prop", "CBSDatagramPortOut");

        phoneNumber = getProp("+5551234", "JSR_205_PHONE_NUMBER",
            "connections.prop", "PhoneNumber");

    
Methods Summary
public voidclose()
Closes the connection. Reset the connection is open flag so methods can be checked to throws an appropriate exception for operations on a closed connection.

exception
IOException if an I/O error occurs.


        if (open) {
            open = false;
        }
    
private voiddebug(java.lang.String text)
Show a debug message.

param
text The text to be displayed after a mandatory prefix.

        System.out.println("CBSMessageConnection: " + text);
    
public com.sun.tck.wma.MessagenewMessage(java.lang.String type)
Constructs a new message object of a text or binary type. When the TEXT_MESSAGE constant is passed in, the created object implements the TextMessage interface. When the BINARY_MESSAGE constant is passed in, the created object implements the BinaryMessage interface.

If this method is called in a sending mode, a new Message object is requested from the connection. For example:

Message msg = conn.newMessage(TEXT_MESSAGE);

The newly created Message does not have the destination address set. It must be set by the application before the message is sent.

If it is called in receiving mode, the Message object does have its address set. The application can act on the object to extract the address and message data.

param
type either TEXT_MESSAGE or BINARY_MESSAGE.
return
a new message

        return newMessage(type, null);
    
public com.sun.tck.wma.MessagenewMessage(java.lang.String type, java.lang.String addr)
Constructs a new message object of a text or binary type and specifies a destination address. When the TEXT_MESSAGE constant is passed in, the created object implements the TextMessage interface. When the BINARY_MESSAGE constant is passed in, the created object implements the BinaryMessage interface.

The destination address addr has the following format: cbs://phone_number:port.

param
type either TEXT_MESSAGE or BINARY_MESSAGE.
param
addr the destination address of the message.
return
a new Message object.

        /* Return the appropriate type of sub message. */
        if (type == MessageConnection.TEXT_MESSAGE) {
            return  new TextObject(addr); 
        } else if (type == MessageConnection.BINARY_MESSAGE) {
            return  new BinaryObject(addr);
        }
        return null; /* message type not supported */
    
public com.sun.tck.wma.MessageConnectionopenPrim(java.lang.String name)
Opens a connection. This method is called from Connector.open() method to obtain the destination address given in the name parameter.

The format for the name string for this method is: cbs://phone_number:port where the phone_number: is optional. If the phone_number parameter is present, the connection is being opened in "client" mode. This means that messages can be sent. If the parameter is absent, the connection is being opened in "server" mode. This means that messages can be sent and received.

The connection that is opened is to a low-level transport mechanism which can be any of the following:

  • A datagram Short Message Peer to Peer (SMPP) to a service center.
  • A comm connection to a phone device with AT-commands.
  • a native CBS stack.

param
name The target of the connection.
return
This connection.
throws
IOException if the connection is closed or unavailable.


        /*
         * If <code>host == null</code>, then we are a server endpoint at
         * the supplied <code>port</code>.
         *
         * If <code>host != null</code> we are a client endpoint at a port
         * decided by the system and the default address for
         * CBS messages to be sent is <code>cbs://host:port</code> .
         */

        if (name.charAt(0) != '/" || name.charAt(1) != '/") {
            throw new IllegalArgumentException(
                           "Missing protocol separator.");
        }
        
        int colon = name.indexOf(':");
        if (colon > 0) {
            if (colon != 2) {
                host = name.substring(2, colon);
            }
            msgID = name.substring(colon + 1);
        } else {
            if (name.length() > 2) {
                host = name.substring(2); 
            }
        }

        open = true;
        return this;
    
public synchronized com.sun.tck.wma.Messagereceive()
Not supported. Normally, this would receive bytes that have been sent over the connection.

exception
IOException Always thrown to indicate that this features is not supported.
return
Message This object is never returned.

        throw new IOException("Server-Side receive not supported.");
    
public voidsend(com.sun.tck.wma.Message dmsg)
Sends a message over the connection. This method extracts the data payload from the Message object so that it can be sent as a datagram.

param
dmsg a Message object.
exception
ConnectionNotFoundException if the address is invalid or if no address is found in the message.
exception
IOException if an I/O error occurs.


        byte[] buffer = null;
        String type = null;
        if (dmsg instanceof TextMessage) {
            type = MessageConnection.TEXT_MESSAGE;
            buffer = ((TextObject)dmsg).getPayloadData();
        } else if (dmsg instanceof BinaryMessage) {
            type = MessageConnection.BINARY_MESSAGE;
            buffer = ((BinaryObject)dmsg).getPayloadData();
        }

        /*
         * For text messages choose between UCS-2 or GSM 7-bit
         * encoding.
         */
        int encodingType = MessageTransportConstants.GSM_BINARY;
        if (type.equals(MessageConnection.TEXT_MESSAGE)) {
            byte[] gsm7bytes = TextEncoder.encode(buffer);
            if (gsm7bytes != null) {
                encodingType = MessageTransportConstants.GSM_TEXT;
                buffer = gsm7bytes;
            } else {
                encodingType = MessageTransportConstants.GSM_UCS2;
            }
        }


        /* Datagram envelope for sending messages. */
        mess = new DatagramPacket(buf,
                   MessageTransportConstants.DATAGRAM_PACKET_SIZE);

        /* Set the address and port to which the datagram is being sent. */
        mess.setAddress(InetAddress.getByName(clientHost));
        mess.setPort(portOut);

        /* Create the special CBS packet. */
        CBSPacket cbsPacket = new CBSPacket();
        cbsPacket.setEncodingType(encodingType);
	if (msgID != null) {
            cbsPacket.setMessageID(Integer.parseInt(msgID));
	} else {
            cbsPacket.setMessageID(0);
	}
        cbsPacket.setMessageLength(buffer.length);
        cbsPacket.setMessage(buffer);

        debug("CBS PACKET: encoding type = " + encodingType);
        debug("CBS PACKET: msgID = " + msgID);
        debug("CBS PACKET: address = " + dmsg.getAddress());
        debug("CBS PACKET: message length = " + buffer.length);
        debug("CBS PACKET: message:" + new String(buffer));

        /* Make the CBS packet the payload for the datagram, and send it. */
        byte[] buf = cbsPacket.getData();
        mess.setData(buf, 0, buf.length);

        /* Open the outbound datagram connection. */
        dgc = new DatagramSocket();
        dgc.send(mess);
        dgc.close();
        dgc = null;