Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  * Conditions Of Use
  * This software was developed by employees of the National Institute of
  * Standards and Technology (NIST), an agency of the Federal Government.
  * Pursuant to title 15 Untied States Code Section 105, works of NIST
  * employees are not subject to copyright protection in the United States
  * and are considered to be in the public domain.  As a result, a formal
  * license is not needed to use the software.
 * This software is provided by NIST as a service and is expressly
 * AND DATA ACCURACY.  NIST does not warrant or make any representations
 * regarding the use of the software or the results thereof, including but
 * not limited to the correctness, accuracy, reliability or usefulness of
 * the software.
 * Permission to use this software is contingent upon your acceptance
 * of the terms of this agreement
 * .
Product of NIST/ITL Advanced Networking Technologies Division (ANTD). * /
 package gov.nist.javax.sip.stack;
Sit in a loop and handle incoming udp datagram messages. For each Datagram packet, a new UDPMessageChannel is created (upto the max thread pool size). Each UDP message is processed in its own thread).

M. Ranganathan
See the implementation sequence diagram for processing incoming requests. Acknowledgement: Jeff Keyser contributed ideas on starting and stoppping the stack that were incorporated into this code. Niklas Uhrberg suggested that thread pooling be added to limit the number of threads and improve performance.
1.2 $Revision: $ $Date: 2010-12-02 01:41:35 $
 public class UDPMessageProcessor extends MessageProcessor {
The Mapped port (in case STUN suport is enabled)
     private int port;

Incoming messages are queued here.
     protected LinkedList messageQueue;

A list of message channels that we have started.
     protected LinkedList messageChannels;

Max # of udp message channels
     protected int threadPoolSize;
     protected DatagramSocket sock;

A flag that is set to false to exit the message processor (suggestion by Jeff Keyser).
     protected boolean isRunning;
     private static final int HIGHWAT=5000;
     private static final int LOWAT=2500;


sipStack pointer to the stack.
     protected UDPMessageProcessor(InetAddress ipAddress,
             SIPTransactionStack sipStackint portthrows IOException {
        this. = sipStack;
        this. = new LinkedList();
        this. = port;
        try {
            this. = sipStack.getNetworkLayer().createDatagramSocket(port,
            // Create a new datagram socket.

If the thread auditor is enabled, define a socket timeout value in order to prevent sock.receive() from blocking forever
            if (sipStack.getThreadAuditor().isEnabled()) {
            if ( ipAddress.getHostAddress().equals()  ||
                // Store the address to which we are actually bound
                // Note that on WINDOWS this is actually broken. It will
                // return IN_ADDR_ANY again. On linux it will return the
                // address to which the socket was actually bound.
                super.setIpAddress.getLocalAddress() );
        } catch (SocketException ex) {
            throw new IOException(ex.getMessage());
Get port on which to listen for incoming stuff.

port on which I am listening.
    public int getPort() {
        return this.;

Start our processor thread.
    public void start() throws IOException {
    	this. = true;
    		this. = new BlockingQueueDispatchAuditor(this.);
    	Thread thread = new Thread(this);
        // Issue #32 on
        // Issue #184

Thread main routine.
    public void run() {
        // Check for running flag.
        this. = new LinkedList();
        // start all our messageChannels (unless the thread pool size is
        // infinity.
        if (. != -1) {
            for (int i = 0; i < .i++) {
                UDPMessageChannel channel = new UDPMessageChannel(,
        // Ask the auditor to monitor this thread
        // Somebody asked us to exit. if isRunnning is set to false.
        while (this.) {
            try {
                // Let the thread auditor know we're up and running
                int bufsize = .getReceiveBufferSize();
                byte message[] = new byte[bufsize];
                DatagramPacket packet = new DatagramPacket(messagebufsize);
                // Count of # of packets in process.
                // this.useCount++;
                if (. != -1) {
                    // Note: the only condition watched for by threads
                    // synchronizing on the messageQueue member is that it is
                    // not empty. As soon as you introduce some other
                    // condition you will have to call notifyAll instead of
                    // notify below.
                    synchronized (this.) {
                        // was addLast
                        this..add(new DatagramQueuedMessageDispatch(packet, System.currentTimeMillis()));
                } else {
                    new UDPMessageChannel(thispacket);
            } catch (SocketTimeoutException ex) {
              // This socket timeout alows us to ping the thread auditor periodically
            } catch (SocketException ex) {
                if (.isLoggingEnabled(.))
                            .logDebug("UDPMessageProcessor: Stopping");
                 = false;
                // The notifyAll should be in a synchronized block.
                // ( bug report by Niklas Uhrberg ).
                synchronized (this.) {
            } catch (IOException ex) {
                 = false;
                if (.isLoggingEnabled(.))
                            .logDebug("UDPMessageProcessor: Got an IO Exception");
            } catch (Exception ex) {
                if (.isLoggingEnabled(.))
                            .logDebug("UDPMessageProcessor: Unexpected Exception - quitting");

Shut down the message processor. Close the socket for recieving incoming messages.
    public void stop() {
        synchronized (this.) {
            this. = false;
            if(this. != null) {

Return the transport string.

the transport string
    public String getTransport() {
        return "udp";

Returns the stack.

my sip stack.
    public SIPTransactionStack getSIPStack() {
        return ;

Create and return new TCPMessageChannel for the given host/port.
    public MessageChannel createMessageChannel(HostPort targetHostPort)
            throws UnknownHostException {
        return new UDPMessageChannel(targetHostPort.getInetAddress(),
                targetHostPort.getPort(), this);
    public MessageChannel createMessageChannel(InetAddress hostint port)
            throws IOException {
        return new UDPMessageChannel(hostportthis);

Default target port for UDP
    public int getDefaultTargetPort() {
        return 5060;

UDP is not a secure protocol.
    public boolean isSecure() {
        return false;

UDP can handle a message as large as the MAX_DATAGRAM_SIZE.
    public int getMaximumMessageSize() {
        return 8*1024;

Return true if there are any messages in use.
    public boolean inUse() {
        synchronized () {
            return .size() != 0;
New to GrepCode? Check out our FAQ X