KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > jndi2 > server > TcpServer


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - 2006 ScalAgent Distributed Technologies
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): Sofiane Chibani
21  * Contributor(s): David Feliot, Nicolas Tachker
22  */

23 package fr.dyade.aaa.jndi2.server;
24
25 import java.io.IOException JavaDoc;
26 import java.net.ServerSocket JavaDoc;
27 import java.net.Socket JavaDoc;
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 JavaDoc listen;
39
40   private Monitor monitors[];
41
42   private int timeout;
43
44   private AgentId serverId;
45
46   public TcpServer(ServerSocket JavaDoc 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 JavaDoc exc) {}
76     for (int i = 0; i < monitors.length; i++) {
77       monitors[i].stop();
78     }
79   }
80
81   public final ServerSocket JavaDoc 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 JavaDoc 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 JavaDoc socket;
103       try {
104         loop: while (running) {
105           canStop = true;
106           try {
107             ServerSocket JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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