FileDocCategorySizeDatePackage
SMSMessageConnection.javaAPI DocphoneME MR2 API (J2ME)14648Wed May 02 18:00:32 BST 2007com.sun.tck.wma.sms

SMSMessageConnection

public class SMSMessageConnection extends com.sun.tck.wma.PropLoader implements com.sun.tck.wma.MessageConnection
SMS message connection handler.

Fields Summary
protected String
host
Machine name - the parsed target address from the URL.
protected String
port
Port number from the URL connection string.
protected String
clientHost
Datagram host for sending/receiving.
protected int
portOut
Datagram transport for sending.
protected int
portIn
Datagram transport for receiving.
protected String
phoneNumber
Phone number of the message sender.
int
fragmentsize
Fragment size for large messages.
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 SMSMessageConnection()
Constructor for SMS message connection handling.


           
      

        /* 
         * Configurable parameters for low level transport.
         * e.g. sms://+5551234:54321 maps to datagram://129.148.70.80:123
         */

        clientHost = "localhost";
        portOut = 11100;
        portIn = 11101;
        phoneNumber = "+5551234";

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

        clientHost = getProp("localhost", "JSR_205_DATAGRAM_HOST",
            "connections.prop", "DatagramHost");
        
        portOut = getIntProp(11100, "JSR_205_SMS_OUT_PORT",
            "connections.prop", "SMSDatagramPortOut");

        portIn = getIntProp(11101, "JSR_205_SMS_PORT",
            "connections.prop", "SMSDatagramPortIn");

        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) {
            dgc.close();
            dgc = null;
            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("SMSMessageConnection: " + 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: sms://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: sms://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.

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
         * SMS messages to be sent is <code>sms://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);
            }
            port = name.substring(colon + 1);
        } else {
            if (name.length() > 2) {
                host = name.substring(2); 
            }
        }

        /* Open the inbound server datagram connection. */
        dgc =  new DatagramSocket(portIn);
        
        open = true;
        return this;
    
public synchronized com.sun.tck.wma.Messagereceive()
Receives the bytes that have been sent over the connection, constructs a Message object, and returns it.

If there are no Messages waiting on the connection, this method will block until a message is received, or the MessageConnection is closed.

return
a Message object
exception
IOException if an I/O error occurs.


        dgc.receive(mess);

        /* get message buffer and extract info */
        byte[] buf = mess.getData();
        SMSPacket packet = new SMSPacket(buf);
        int msgType = packet.getEncodingType();
        int smsPort = packet.getPort();
        long time = packet.getTimeStamp();
        String address = packet.getAddress();
        String phoneNumber = packet.getPhoneNumber();
        int msgLen = packet.getMessageLength();
        byte[] messg = packet.getMessage(msgLen);

        debug("SMS PACKET: encodingType = " + msgType);
        debug("SMS PACKET: port = " + smsPort);
        debug("SMS PACKET: time = " + time);
        debug("SMS PACKET: address = " + address);
        debug("SMS PACKET: Sender's phone number = " + phoneNumber);
        debug("SMS PACKET: message length = " + msgLen);
        debug("SMS PACKET: message:" + new String(messg));

        Message msg = null;
        if (msgType == MessageTransportConstants.GSM_TEXT ||
            msgType == MessageTransportConstants.GSM_UCS2) {

            TextMessage textmsg = (TextMessage) 
                newMessage(MessageConnection.TEXT_MESSAGE,
                           address);

            /* Always store text messages as UCS 2 bytes. */
            if (msgType == MessageTransportConstants.GSM_TEXT) {
                messg = TextEncoder.decode(messg);
            }
            ((TextObject)textmsg).setPayloadData(messg);
             msg = textmsg;
         } else {
             BinaryMessage binmsg = (BinaryMessage) 
                 newMessage(MessageConnection.BINARY_MESSAGE,
                            address);
             ((BinaryObject)binmsg).setPayloadData(messg);
             msg = binmsg;
         }
         ((MessageObject) msg).setTimeStamp(time);

        return msg; 
    
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.


        /** Saved timestamp for use with multiple segment records. */
        long sendtime = System.currentTimeMillis();

        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;
            }
        }


        mess = new DatagramPacket(buf,
                   MessageTransportConstants.DATAGRAM_PACKET_SIZE);
        mess.setAddress(InetAddress.getByName(clientHost));
        mess.setPort(portOut);

        SMSPacket smsPacket = new SMSPacket();
        smsPacket.setEncodingType(encodingType);
	if (port != null) {
            smsPacket.setPort(Integer.parseInt(port));
	} else {
            smsPacket.setPort(0);
	}
        smsPacket.setTimeStamp(sendtime);

        /*
         * Set target address.
         */
        smsPacket.setAddress(dmsg.getAddress());

        /*
         * Set sender's phone number
         */
        smsPacket.setPhoneNumber(phoneNumber);

        smsPacket.setMessageLength(buffer.length);
        smsPacket.setMessage(buffer);

        debug("SMS PACKET: clientHost = " + InetAddress.getByName(clientHost));
        debug("SMS PACKET: portOut = " + portOut);

        debug("SMS PACKET: encoding type = " + encodingType);
        debug("SMS PACKET: port = " + port);
        debug("SMS PACKET: timestamp = " + sendtime);
        debug("SMS PACKET: address = " + dmsg.getAddress());
        debug("SMS PACKET: sender's phone number = " + phoneNumber);
        debug("SMS PACKET: message length = " + buffer.length);
        debug("SMS PACKET: message:" + new String(buffer));
        byte[] buf = smsPacket.getData();

        mess.setData(buf, 0, buf.length);
        dgc.send(mess);