UDPMessageProcessorpublic class UDPMessageProcessor extends MessageProcessor implements RunnableSit 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 | runningFlag indicating the mesage processor is already running. | protected static final int | MAX_DATAGRAM_SIZEMax datagram size. | private SIPMessageStack | sipStackOur stack (that created us). | private DatagramConnection | dcCurrent datagram connection handle. | int | portInbound message port number. | private Thread | threadMessage processor thread. |
Constructors Summary |
---|
protected UDPMessageProcessor(SIPMessageStack sipStack, int port)Constructor with initial stack and port number.
this.sipStack = sipStack;
this.port = port;
|
Methods Summary |
---|
public MessageChannel | createMessageChannel(HostPort targetHostPort)Create and return new TCPMessageChannel for the given host/port.
return new UDPMessageChannel(targetHostPort.getHost().getHostname(),
targetHostPort.getPort(), sipStack, this);
| public MessageChannel | createMessageChannel(java.lang.String host, int port)Create and return new TCPMessageChannel for the given host/port.
return new UDPMessageChannel(host, port, sipStack, this);
| public int | getPort()Gets the port from which the UDPMessageProcessor is reading messages
return this.port;
| public SIPMessageStack | getSIPStack()Gets the SIP Stack.
return sipStack;
| public SIPMessageStack | getSipStack()Returns the stack.
return sipStack;
| public java.lang.String | getTransport()Returns the transport string.
return "udp";
| public boolean | isSecure()UDP is not a secure protocol.
return false;
| public void | run()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 void | start()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 void | stop()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:");
}
}
|
|