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
  * provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
  * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
  * 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
  *
  * .
  *
  */
 /* This class is entirely derived from TCPMessageProcessor,
  *  by making some minor changes.
  *
  *               Daniel J. Martinez Manzano <dani@dif.um.es>
  * Acknowledgement: Jeff Keyser suggested that a
  * Stop mechanism be added to this. Niklas Uhrberg suggested that
  * a means to limit the number of simultaneous active connections
  * should be added. Mike Andrews suggested that the thread be
  * accessible so as to implement clean stop using Thread.join().
  *
  */

Product of NIST/ITL Advanced Networking Technologies Division (ANTD). * /
 
 package gov.nist.javax.sip.stack;
 
 
 
Sit in a loop waiting for incoming tls connections and start a new thread to handle each new connection. This is the active object that creates new TLS MessageChannels (one for each new accept socket).

Author(s):
M. Ranganathan
Version:
1.2 $Revision: 1.29 $ $Date: 2010-12-02 22:04:13 $
 
 public class TLSMessageProcessor extends MessageProcessor {
 	private static StackLogger logger = CommonLogger.getLogger(TLSMessageProcessor.class);
     protected int nConnections;
 
     private boolean isRunning;
 
 
     private ServerSocket sock;
 
     protected int useCount = 0;
 
Constructor.

Parameters:
ipAddress -- inet address where I am listening.
sipStack SIPStack structure.
port port where this message processor listens.
 
     protected TLSMessageProcessor(InetAddress ipAddressSIPTransactionStack sipStackint port) {
         super(ipAddressport"tls",sipStack);
         this. = sipStack;
         this. = new Hashtable<StringTLSMessageChannel>();
         this. = new ArrayList<TLSMessageChannel>();
    }

    
Start the processor.
    public void start() throws IOException {
        Thread thread = new Thread(this);
        thread.setName("TLSMessageProcessorThread");
        // ISSUE 184
        thread.setPriority(.getThreadPriority());
        thread.setDaemon(true);
        this. = .getNetworkLayer().createSSLServerSocket(this.getPort(), 0,
                this.getIpAddress());
            // we set it to true in Default case as well to keep backward compatibility and default behavior            
            ((SSLServerSocketthis.).setWantClientAuth(true);            
        } else {
            ((SSLServerSocketthis.).setWantClientAuth(false);
        }
        if(.getClientAuth() == .) {
            ((SSLServerSocketthis.).setNeedClientAuth(true);            
        } else {
            ((SSLServerSocketthis.).setNeedClientAuth(false);
        }            
        ((SSLServerSocketthis.).setUseClientMode(false);
        String []enabledCiphers = ((SipStackImpl)).getEnabledCipherSuites();
        ((SSLServerSocketthis.).setEnabledCipherSuites(enabledCiphers);        
            // we set it to true in Default case as well to keep backward compatibility and default behavior            
            ((SSLServerSocketthis.).setWantClientAuth(true);            
        } else {
            ((SSLServerSocketthis.).setWantClientAuth(false);
        }     
            .logDebug("SSLServerSocket want client auth " + ((SSLServerSocketthis.).getWantClientAuth());
            .logDebug("SSLServerSocket need client auth " + ((SSLServerSocketthis.).getNeedClientAuth());
        }
        
        this. = true;
        thread.start();
    }

    
Run method for the thread that gets created for each accept socket.
    public void run() {
        // Accept new connectins on our socket.
        while (this.) {
            try {
            	 
                synchronized (this) {
                    // sipStack.maxConnections == -1 means we are
                    // willing to handle an "infinite" number of
                    // simultaneous connections (no resource limitation).
                    // This is the default behavior.
                    while (. != -1
                            && this. >= .) {
                        try {
                        	
                            this.wait();
                            if (!this.)
                                return;
                        } catch (InterruptedException ex) {
                            break;
                        }
                    }
                    this.++;
                }
                if (.isLoggingEnabled(.)) {
                    .logDebug(" waiting to accept new connection!");
                }
                
                Socket newsock = .accept();
               
                if (.isLoggingEnabled(.)) {
                    .logDebug("Accepting new connection!");
                }
                
               // Note that for an incoming message channel, the
               // thread is already running
                .add(new TLSMessageChannel(newsockthis"TLSMessageChannelThread-" + ));
            } catch (SocketException ex) {
                if ( this. ) {
                  .logError(
                    "Fatal - SocketException occured while Accepting connection"ex);
                  this. = false;
                  break;
                }
            } catch (SSLException ex) {
                this. = false;
                .logError(
                        "Fatal - SSSLException occured while Accepting connection"ex);
                break;
            } catch (IOException ex) {
                // Problem accepting connection.
                .logError("Problem Accepting Connection"ex);
                continue;
            } catch (Exception ex) {
                .logError("Unexpected Exception!"ex);
            }
        }
    }

    
Returns the stack.

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

    
Stop the message processor. Feature suggested by Jeff Keyser.
    public synchronized void stop() {
        if (!)
            return;
         = false;
        try {
            .close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Collection en = .values();
        for (Iterator it = en.iterator(); it.hasNext();) {
            TLSMessageChannel next = (TLSMessageChannelit.next();
            next.close();
        }
        for (Iterator incomingMCIterator = .iterator(); incomingMCIterator
                .hasNext();) {
            TLSMessageChannel next = (TLSMessageChannelincomingMCIterator.next();
            next.close();
        }
        this.notify();
    }
    protected synchronized void remove(TLSMessageChannel tlsMessageChannel) {
        String key = tlsMessageChannel.getKey();
            .logDebug(Thread.currentThread() + " removing " + key);
        }

        
May have been removed already
        if (.get(key) == tlsMessageChannel)
            this..remove(key);
        
        .remove(tlsMessageChannel);
    }
    public synchronized MessageChannel createMessageChannel(HostPort targetHostPort)
            throws IOException {
        String key = MessageChannel.getKey(targetHostPort"TLS");
        if (.get(key) != null) {
            return (TLSMessageChannelthis..get(key);
        } else {
            TLSMessageChannel retval = new TLSMessageChannel(targetHostPort.getInetAddress(),
                    targetHostPort.getPort(), this);
            this..put(keyretval);
            retval.isCached = true;
            if (.isLoggingEnabled(.)) {
                .logDebug("key " + key);
                .logDebug("Creating " + retval);
            }
            return retval;
        }
    }
    protected synchronized void cacheMessageChannel(TLSMessageChannel messageChannel) {
        String key = messageChannel.getKey();
        TLSMessageChannel currentChannel = (TLSMessageChannel.get(key);
        if (currentChannel != null) {
            if (.isLoggingEnabled(.))
                .logDebug("Closing " + key);
            currentChannel.close();
        }
            .logDebug("Caching " + key);
        this..put(keymessageChannel);
    }
    public synchronized MessageChannel createMessageChannel(InetAddress hostint port)
            throws IOException {
        try {
            String key = MessageChannel.getKey(hostport"TLS");
            if (.get(key) != null) {
                return (TLSMessageChannelthis..get(key);
            } else {
                TLSMessageChannel retval = new TLSMessageChannel(hostportthis);
                this..put(keyretval);
                retval.isCached = true;
                if (.isLoggingEnabled(.)) {
                    .logDebug("key " + key);
                    .logDebug("Creating " + retval);
                }
                return retval;
            }
        } catch (UnknownHostException ex) {
            throw new IOException(ex.getMessage());
        }
    }

    
TLS can handle an unlimited number of bytes.
    public int getMaximumMessageSize() {
        return .;
    }
    public boolean inUse() {
        return this. != 0;
    }

    
Default target port for TLS
    public int getDefaultTargetPort() {
        return 5061;
    }

    
TLS is a secure protocol.
    public boolean isSecure() {
        return true;
    }
New to GrepCode? Check out our FAQ X