KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > tribe > channel > tcp > TcpServerAccepterThread


1 /**
2  * Tribe: Group communication library.
3  * Copyright (C) 2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: tribe@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.tribe.channel.tcp;
26
27 import java.util.HashMap JavaDoc;
28
29 import org.objectweb.tribe.channel.AbstractReliableFifoChannel;
30 import org.objectweb.tribe.channel.AbstractServerChannel;
31 import org.objectweb.tribe.common.IpAddress;
32 import org.objectweb.tribe.common.log.Trace;
33 import org.objectweb.tribe.exceptions.ChannelException;
34
35 /**
36  * This class defines a TcpServerAccepterThread.
37  * <p>
38  * It simply accepts connections and put them in the connection pool.
39  *
40  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
41  * @version 1.0
42  */

43 public class TcpServerAccepterThread extends Thread JavaDoc
44 {
45   private AbstractServerChannel serverSocket;
46   // IP address -> channel
47
private HashMap JavaDoc channels;
48   // channel -> TCP reader thread
49
private HashMap JavaDoc readerThreads;
50   private HashMap JavaDoc keyBuffers;
51   private boolean isKilled = false;
52
53   private static Trace logger = Trace
54                                              .getLogger("org.objectweb.tribe.channel");
55
56   /**
57    * Creates a new <code>TcpServerAccepterThread</code> object
58    *
59    * @param serverSocket serverSocket to read from
60    * @param channels TcpPool channel hashmap to update when a new connection is
61    * created.
62    */

63   public TcpServerAccepterThread(AbstractServerChannel serverSocket,
64       HashMap JavaDoc channels, HashMap JavaDoc readerThreads, HashMap JavaDoc keyBuffers)
65   {
66     super("TcpServerAccepterThread");
67     this.serverSocket = serverSocket;
68     this.channels = channels;
69     this.keyBuffers = keyBuffers;
70     this.readerThreads = readerThreads;
71   }
72
73   /**
74    * Accept new connections and put them in the pool
75    */

76   public void run()
77   {
78     if (logger.isDebugEnabled())
79       logger.debug("TcpServerAccepterThread started");
80
81     while (!isKilled)
82     {
83       try
84       {
85         AbstractReliableFifoChannel socket = serverSocket.accept();
86         IpAddress ip = (IpAddress) socket.getSourceAddress();
87         synchronized (channels)
88         {
89           if (logger.isDebugEnabled())
90             logger.debug("Accepting new connection from " + ip);
91           // TODO: Handle duplicate entries?
92
channels.put(ip, socket);
93           TcpReaderThread thread = new TcpReaderThread((TcpChannel) socket,
94               keyBuffers);
95           thread.start();
96           readerThreads.put(socket, thread);
97         }
98       }
99       catch (ChannelException e)
100       {
101         logger.info("Channel error, exiting TcpServerAccepterThread", e);
102         isKilled = true;
103       }
104     }
105
106     if (logger.isDebugEnabled())
107       logger.debug("TcpServerAccepterThread terminated.");
108   }
109
110   /**
111    * Terminates this TcpServerAccepterThread.
112    */

113   public void kill()
114   {
115     isKilled = true;
116     try
117     {
118       serverSocket.close();
119     }
120     catch (ChannelException ignore)
121     {
122     }
123   }
124 }
Popular Tags