Clover coverage report - brownies library - 1.0-beta-1
Coverage timestamp: 月 8 16 2004 17:14:42 GMT+09:00
file stats: LOC: 178   Methods: 15
NCLOC: 129   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
SocketServer.java 0% 0% 0% 0%
coverage
 1   
 /*
 2   
  * Created on 2004/06/03
 3   
  */
 4   
 package org.asyrinx.brownie.net.socket;
 5   
 
 6   
 import java.io.BufferedReader;
 7   
 import java.io.IOException;
 8   
 import java.io.InputStreamReader;
 9   
 import java.net.ServerSocket;
 10   
 import java.net.Socket;
 11   
 
 12   
 import org.apache.commons.logging.Log;
 13   
 import org.apache.commons.logging.LogFactory;
 14   
 
 15   
 /**
 16   
  * @author akima
 17   
  */
 18   
 public class SocketServer implements Runnable {
 19   
     public static final String DEFAULT_TERMINATE_MESSAGE = "<<terminate>>";
 20   
 
 21   
     /**
 22   
      *  
 23   
      */
 24  0
     public SocketServer(SocketServerListener listener, int port) {
 25  0
         this(listener, port, DEFAULT_TERMINATE_MESSAGE);
 26   
     }
 27   
 
 28   
     /**
 29   
      *  
 30   
      */
 31  0
     public SocketServer(SocketServerListener listener, int port,
 32   
             String terminateMessage) {
 33  0
         super();
 34  0
         this.port = port;
 35  0
         this.listener = listener;
 36  0
         this.terminateMessage = terminateMessage;
 37   
     }
 38   
 
 39   
     private final int port;
 40   
 
 41   
     private final String terminateMessage;
 42   
 
 43   
     private final SocketServerListener listener;
 44   
 
 45   
     private final Log log = LogFactory.getLog(this.getClass());
 46   
 
 47   
     private boolean runnging = false;
 48   
 
 49   
     private ServerSocket serverSocket = null;
 50   
 
 51  0
     protected void connect() throws IOException {
 52  0
         if (isConnected())
 53  0
             return;
 54  0
         this.serverSocket = new ServerSocket(port);
 55  0
         log.info("socket connected port " + port);
 56   
     }
 57   
 
 58  0
     protected void disconnect() throws IOException {
 59  0
         if (!isConnected())
 60  0
             return;
 61  0
         log.info("socket disconnected");
 62  0
         this.serverSocket.close();
 63  0
         this.serverSocket = null;
 64   
     }
 65   
 
 66  0
     public void stop() {
 67  0
         runnging = false;
 68   
     }
 69   
 
 70  0
     protected void sendTerminateMessage() {
 71  0
         final SocketClient client = new SocketClient("localhost", this.port);
 72  0
         try {
 73  0
             client.execute(this.terminateMessage);
 74   
         } catch (IOException e) {
 75  0
             log.error("failed to stop socket.");
 76   
         }
 77   
     }
 78   
 
 79  0
     public boolean isConnected() {
 80  0
         return this.serverSocket != null;
 81   
     }
 82   
 
 83   
     private IOException occurred = null;
 84   
 
 85   
     /**
 86   
      * @return Returns the occurred.
 87   
      */
 88  0
     public IOException getOccurred() {
 89  0
         return occurred;
 90   
     }
 91   
 
 92  0
     public void executeAsThread() {
 93  0
         final Thread thread = new Thread(this);
 94  0
         thread.run();
 95   
     }
 96   
 
 97   
     /*
 98   
      * (non-Javadoc)
 99   
      * 
 100   
      * @see java.lang.Runnable#run()
 101   
      */
 102  0
     public void run() {
 103  0
         try {
 104  0
             connect();
 105  0
             runnging = true;
 106  0
             try {
 107  0
                 while (runnging) {
 108  0
                     receive();
 109   
                 }
 110   
             } finally {
 111  0
                 disconnect();
 112   
             }
 113   
         } catch (IOException e) {
 114  0
             occurred = e;
 115  0
             if (isConnected()) {
 116  0
                 try {
 117  0
                     disconnect();
 118   
                 } catch (IOException e1) {
 119   
                     //ignore intensionally
 120   
                 }
 121   
             }
 122   
         }
 123   
     }
 124   
 
 125  0
     protected void receive() throws IOException {
 126   
         // クライアントからの接続を待ちます
 127  0
         log.info("socket waiting....");
 128  0
         final Socket socket = serverSocket.accept();
 129  0
         log.info("socket accepted");
 130  0
         try {
 131  0
             final BufferedReader in = new BufferedReader(new InputStreamReader(
 132   
                     socket.getInputStream()));
 133  0
             try {
 134  0
                 String line;
 135  0
                 while ((line = in.readLine()) != null) {
 136  0
                     log.info("socket accepted message: '" + line + "'");
 137  0
                     if (line.equals(this.terminateMessage)) {
 138  0
                         runnging = false;
 139  0
                         log.info("accept terminateMessage");
 140   
                     } else {
 141  0
                         notifyToClient(line);
 142   
                     }
 143   
                 }
 144   
             } finally {
 145  0
                 in.close();
 146   
             }
 147   
         } finally {
 148  0
             socket.close();
 149  0
             log.info("socket closed");
 150   
         }
 151   
     }
 152   
 
 153  0
     protected void notifyToClient(String line) {
 154  0
         synchronized (listener) {
 155  0
             listener.receipt(line);
 156   
         }
 157   
     }
 158   
 
 159   
     private static SocketServer server = null;
 160   
 
 161  0
     public static void main(String[] args) {
 162  0
         server = new SocketServer(new SocketServerListener() {
 163   
             private int count = 0;
 164   
 
 165  0
             public void receipt(String line) {
 166  0
                 count++;
 167  0
                 System.out.println(count + "reciept: " + line);
 168  0
                 SocketServer.received(line, count);
 169   
             }
 170   
         }, 5555);
 171  0
         server.run();
 172   
     }
 173   
 
 174  0
     protected static void received(String line, int count) {
 175  0
         if (count >= 5)
 176  0
             server.stop();
 177   
     }
 178   
 }