FileDocCategorySizeDatePackage
UDPMessageProcessor.javaAPI DocphoneME MR2 API (J2ME)9287Wed May 02 18:00:42 BST 2007gov.nist.siplite.stack

UDPMessageProcessor

public class UDPMessageProcessor extends MessageProcessor implements Runnable
Sit in a loop and handle incoming udp datagram messages. For each Datagram packet, a new UDPMessageChannel is created. (Each UDP message is processed in its own thread).

Fields Summary
private boolean
running
Flag indicating the mesage processor is already running.
protected static final int
MAX_DATAGRAM_SIZE
Max datagram size.
private SIPMessageStack
sipStack
Our stack (that created us).
private DatagramConnection
dc
Current datagram connection handle.
int
port
Inbound message port number.
private Thread
thread
Message processor thread.
Constructors Summary
protected UDPMessageProcessor(SIPMessageStack sipStack, int port)
Constructor with initial stack and port number.

param
sipStack pointer to the stack.
param
port for incoming messages


                           
      
                                    
        this.sipStack = sipStack;
        this.port = port;
    
Methods Summary
public MessageChannelcreateMessageChannel(HostPort targetHostPort)
Create and return new TCPMessageChannel for the given host/port.

param
targetHostPort target host and port number
return
the new message channel

        return new UDPMessageChannel(targetHostPort.getHost().getHostname(),
                                 targetHostPort.getPort(), sipStack, this);
    
public MessageChannelcreateMessageChannel(java.lang.String host, int port)
Create and return new TCPMessageChannel for the given host/port.

param
host target host
param
port target port
return
the new message channel

        return new UDPMessageChannel(host, port, sipStack, this);
    
public intgetPort()
Gets the port from which the UDPMessageProcessor is reading messages

return
Port from which the udp message processor is reading messages.

        return this.port;
    
public SIPMessageStackgetSIPStack()
Gets the SIP Stack.

return
the sip stack.

        return sipStack;
    
public SIPMessageStackgetSipStack()
Returns the stack.

return
my sip stack.

        return sipStack;
    
public java.lang.StringgetTransport()
Returns the transport string.

return
the transport string

        return "udp";
    
public booleanisSecure()
UDP is not a secure protocol.

return
always false

        return false;
    
public voidrun()
Thread main routine.

        try {
            // infinite loop will be broken by stop() function
            while (true) {
                Datagram datagram = dc.newDatagram(MAX_DATAGRAM_SIZE);

                if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
                    Logging.report(Logging.INFORMATION, LogChannels.LC_JSR180,
                        "SYSTEM, UDPMessageProcessor, run(), listening!");
                }

                dc.receive(datagram);
                
                if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
                    Logging.report(Logging.INFORMATION,
                                   LogChannels.LC_JSR180,
                                   "SYSTEM, UDPMessageProcessor, run(), " +
                                   "packet revceived!");
                }

                // Create synchronous message handler
                // for this message (UDPMessageChannel.run() will be used 
                // inside UDPMessageChannel constructor)
                // IMPL_NOTE: execute UDPMessageChannel.run directly
                //       or create a new thread
                UDPMessageChannel udpMessageChannel =
                    new UDPMessageChannel(datagram, sipStack, this);
            }
        } catch (IOException ex) {
            // intentionally ignored.
            // can't create new datagram or
            // recieve exception
        }
    
public synchronized voidstart()
Starts our processor thread.

        /*
         * Creating datagram connection.
         */
        // Create a new datagram socket.
        /*
         * Original NIST method for opening the datagram connection
         * has been replaced by direct calls to instantiate the protocol
         * handler, in order to pass the security token for use of lower
         * level transport connection.
         * Original NIST sequence is :
         *
         * dc =
         *       (DatagramConnection)Connector.open("datagram://:"+
         *       sipStack.getPort("udp"));
         *
         */

        if (dc != null) {
            // the mesage processor is already running
            return;
        }

        com.sun.midp.io.j2me.datagram.Protocol conn =
        new com.sun.midp.io.j2me.datagram.Protocol();

        try {
            // It's incorrect to use sipStack.getPort() to get the port
            // for listening, because if multiple ports are enabled for
            // the same transport then this function returns the first one.
            // So the member 'port' is used.
            dc =  (DatagramConnection)conn.openPrim(
                               sipStack.getSecurityToken(), "//:" +
                               port, Connector.READ_WRITE, true);
            // timeouts are ignored

            if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
                Logging.report(Logging.INFORMATION, LogChannels.LC_JSR180,
                           "UDPMessageProcessor, start(), datagram server" +
                           "listener:  datagram://:" + port);
                Logging.report(Logging.INFORMATION, LogChannels.LC_JSR180,
                           "UDPMessageProcessor, start(), datagram size:" +
                           MAX_DATAGRAM_SIZE);
            }
        } catch (IOException ioe) {
            if (Logging.REPORT_LEVEL <= Logging.ERROR) {
                Logging.report(Logging.ERROR, LogChannels.LC_JSR180,
                           "UDPMessageProcessor, start(), can't" +
                           "create DatagramConnection: " + ioe.getMessage());
            }
        }

        /*
         * Start a thread to receive data on datagramconnection, dc
         */
        if (dc != null) {
            thread = new Thread(this);
            thread.start();
        }
    
public synchronized voidstop()
Stops the message processor.

        try {
            if (dc != null) {
                if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
                    Logging.report(Logging.INFORMATION, LogChannels.LC_JSR180,
                                           "UDPMessageProcessor, stop(), " +
                                           "The stack is going to stop!");
                }
                // interrupt run() infinite loop 
                dc.close();
                if (thread != null && thread.isAlive()) {
                    try {
                        thread.join();
                    } catch (InterruptedException exc) {
                        // intentionally ignored
                    }
                    thread = null;
                }
                // enable start of the processor 
                dc = null;
                if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
                    Logging.report(Logging.INFORMATION, LogChannels.LC_JSR180,
                        "UDPMessageProcessor, stop(), The stack is stopped");
                }
            }
        } catch (IOException ex) {
            if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
                Logging.report(Logging.INFORMATION, LogChannels.LC_JSR180,
                    "UDPMessageProcessor, stop(), exception raised:");
            }
        }