1 23 package fr.dyade.aaa.jndi2.server; 24 25 import java.io.IOException ; 26 import java.net.ServerSocket ; 27 import java.net.Socket ; 28 29 import org.objectweb.util.monolog.api.BasicLevel; 30 31 import fr.dyade.aaa.agent.AgentId; 32 import fr.dyade.aaa.agent.AgentServer; 33 import fr.dyade.aaa.agent.Channel; 34 import fr.dyade.aaa.util.Daemon; 35 36 public class TcpServer { 37 38 private volatile ServerSocket listen; 39 40 private Monitor monitors[]; 41 42 private int timeout; 43 44 private AgentId serverId; 45 46 public TcpServer(ServerSocket listen, 47 int poolSize, 48 int timeout, 49 AgentId serverId) { 50 this.listen = listen; 51 this.timeout = timeout; 52 this.monitors = new Monitor[poolSize]; 53 this.serverId = serverId; 54 for (int i = 0; i < monitors.length; i++) { 55 monitors[i] = new Monitor( 56 "JndiServer.Monitor#" + i, timeout, this); 57 monitors[i].setDaemon(true); 58 monitors[i].setThreadGroup(AgentServer.getThreadGroup()); 59 } 60 } 61 62 public final void start() { 63 for (int i = 0; i < monitors.length; i++) { 64 monitors[i].start(); 65 } 66 } 67 68 public final void stop() { 69 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 70 Trace.logger.log( 71 BasicLevel.DEBUG, "TcpServer.stop()"); 72 try { 73 listen.close(); 74 listen = null; 75 } catch (Exception exc) {} 76 for (int i = 0; i < monitors.length; i++) { 77 monitors[i].stop(); 78 } 79 } 80 81 public final ServerSocket getListen() { 82 return listen; 83 } 84 85 public final AgentId getServerId() { 86 return serverId; 87 } 88 89 public static class Monitor extends Daemon { 90 91 private int timeout; 92 93 private TcpServer tcpServer; 94 95 protected Monitor(String name, int timeout, TcpServer tcpServer) { 96 super(name); 97 this.timeout = timeout; 98 this.tcpServer = tcpServer; 99 } 100 101 public final void run() { 102 Socket socket; 103 try { 104 loop: while (running) { 105 canStop = true; 106 try { 107 ServerSocket listen = tcpServer.getListen(); 108 if (listen != null) { 109 socket = listen.accept(); 110 socket.setTcpNoDelay(true); 111 socket.setSoTimeout(timeout); 112 socket.setSoLinger(true, 1000); 113 canStop = false; 114 } else { 115 break loop; 116 } 117 } catch (IOException exc) { 118 canStop = false; 119 Thread.interrupted(); 120 if (running) { 121 Trace.logger.log(BasicLevel.DEBUG, this.getName() 122 + ", error during accept", exc); 123 try { 124 Thread.sleep(1000); 125 } catch (InterruptedException ie) { 126 } 127 continue loop; 128 } else { 129 break loop; 130 } 131 } finally { 132 canStop = false; 133 } 134 135 if (!running) 136 break loop; 137 138 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) { 139 Trace.logger.log(BasicLevel.DEBUG, this.getName() 140 + ", connection from " + socket.getInetAddress() + ':' 141 + socket.getPort()); 142 } 143 144 try { 145 TcpRequestContext ctx = new TcpRequestContext(socket); 146 Channel.sendTo(tcpServer.getServerId(), new TcpRequestNot(ctx)); 147 } catch (Exception exc) { 148 Trace.logger.log(BasicLevel.ERROR, this.getName() 149 + ", error during send", exc); 150 if (socket != null) { 151 try { 152 socket.close(); 153 } catch (IOException exc2) { 154 } 155 } 156 } 157 } 158 } finally { 159 finish(); 160 } 161 } 162 163 protected void close() { 164 165 } 166 167 protected void shutdown() { 168 close(); 169 } 170 } 171 } 172 | Popular Tags |