|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  15 |     | 
     | 
  |  16 |     | 
   package diy.middleware.channels;  | 
  |  17 |     | 
     | 
  |  18 |     | 
   import java.io.IOException;  | 
  |  19 |     | 
   import java.net.InetSocketAddress;  | 
  |  20 |     | 
   import java.nio.channels.ServerSocketChannel;  | 
  |  21 |     | 
   import java.nio.channels.SocketChannel;  | 
  |  22 |     | 
   import java.rmi.server.UID;  | 
  |  23 |     | 
   import java.util.concurrent.BlockingQueue;  | 
  |  24 |     | 
     | 
  |  25 |    3 |    public class ChannelAcceptor implements Runnable { | 
  |  26 |     | 
           private BlockingQueue<ChannelConnection> channelQueue;  | 
  |  27 |     | 
           private Thread listenerThread;  | 
  |  28 |     | 
           private int port;  | 
  |  29 |     | 
           private ServerSocketChannel serverChannel;  | 
  |  30 |     | 
     | 
  |  31 |     | 
           public void setChannelQueue(BlockingQueue<ChannelConnection> channelQueue) { | 
  |  32 |    3 |                    this.channelQueue = channelQueue;  | 
  |  33 |    3 |            }  | 
  |  34 |     | 
             | 
  |  35 |     | 
           public void setPort(int port) { | 
  |  36 |    3 |                    this.port = port;  | 
  |  37 |    3 |            }  | 
  |  38 |     | 
     | 
  |  39 |     | 
           public void init() throws IOException { | 
  |  40 |    3 |                    serverChannel = ServerSocketChannel.open();  | 
  |  41 |    3 |                    serverChannel.socket().bind(new InetSocketAddress(port));  | 
  |  42 |    3 |                    serverChannel.configureBlocking(true);  | 
  |  43 |    3 |                    listenerThread = new Thread(this);  | 
  |  44 |    3 |                    listenerThread.start();  | 
  |  45 |    3 |            }  | 
  |  46 |     | 
     | 
  |  47 |     | 
           public void run() { | 
  |  48 |     | 
                   try { | 
  |  49 |     | 
                           while (true) { | 
  |  50 |    8 |                                    SocketChannel incomingConnection = serverChannel.accept();  | 
  |  51 |    5 |                                    final String sessionId = new UID().toString();  | 
  |  52 |    5 |                                    channelQueue.put(new ChannelConnection(sessionId, incomingConnection));  | 
  |  53 |    5 |                            }  | 
  |  54 |    3 |                    } catch (Exception e) { | 
  |  55 |    3 |                            e.printStackTrace();  | 
  |  56 |     | 
                   }  | 
  |  57 |    3 |            }  | 
  |  58 |     | 
             | 
  |  59 |     | 
           public void destroy() throws IOException { | 
  |  60 |    3 |                    listenerThread.interrupt();  | 
  |  61 |    3 |                    serverChannel.close();  | 
  |  62 |    3 |            }  | 
  |  63 |     | 
     | 
  |  64 |     | 
   }  |