Coverage Report - diy.middleware.channels.ChannelAcceptor
 
Classes in this File Line Coverage Branch Coverage Complexity
ChannelAcceptor
100%
21/21
N/A
0
 
 1  
 /*
 2  
         Copyright 2007 Alexey Akhunov
 3  
 
 4  
    Licensed under the Apache License, Version 2.0 (the "License");
 5  
    you may not use this file except in compliance with the License.
 6  
    You may obtain a copy of the License at
 7  
 
 8  
        http://www.apache.org/licenses/LICENSE-2.0
 9  
 
 10  
    Unless required by applicable law or agreed to in writing, software
 11  
    distributed under the License is distributed on an "AS IS" BASIS,
 12  
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
    See the License for the specific language governing permissions and
 14  
    limitations under the License.
 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  
 }