Coverage Report - diy.middleware.sockets.SocketAcceptor
 
Classes in this File Line Coverage Branch Coverage Complexity
SocketAcceptor
100%
19/19
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.sockets;
 17  
 
 18  
 import java.io.IOException;
 19  
 import java.net.ServerSocket;
 20  
 import java.net.Socket;
 21  
 import java.rmi.server.UID;
 22  
 import java.util.concurrent.BlockingQueue;
 23  
 
 24  3
 public class SocketAcceptor implements Runnable {
 25  
 
 26  
         private BlockingQueue<SocketConnection> socketQueue;
 27  
 
 28  
         private ServerSocket serverSocket;
 29  
         
 30  
         private Thread listenerThread;
 31  
 
 32  
         private int port;
 33  
 
 34  
         public void setSocketQueue(BlockingQueue<SocketConnection> socketQueue) {
 35  3
                 this.socketQueue = socketQueue;
 36  3
         }
 37  
 
 38  
         public void setPort(int port) {
 39  3
                 this.port = port;
 40  3
         }
 41  
 
 42  
         public void init() throws IOException {
 43  3
                 serverSocket = new ServerSocket(port);
 44  3
                 listenerThread = new Thread(this);
 45  3
                 listenerThread.start();
 46  3
         }
 47  
 
 48  
         public void run() {
 49  
                 try {
 50  
                         while (true) {
 51  8
                                 Socket incomingConnection = serverSocket.accept();
 52  5
                                 final String sessionId = new UID().toString();
 53  5
                                 socketQueue.put(new SocketConnection(sessionId,
 54  
                                                 incomingConnection));
 55  5
                         }
 56  3
                 } catch (Exception e) {
 57  3
                         e.printStackTrace();
 58  
                 }
 59  3
         }
 60  
 
 61  
         public void destroy() throws IOException, InterruptedException {
 62  3
                 listenerThread.interrupt();
 63  3
                 serverSocket.close();
 64  3
         }
 65  
 }