KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
28 import java.net.InetSocketAddress JavaDoc;
29 import java.net.ServerSocket JavaDoc;
30
31 import org.objectweb.tribe.channel.AbstractReliableFifoChannel;
32 import org.objectweb.tribe.channel.AbstractServerChannel;
33 import org.objectweb.tribe.common.Address;
34 import org.objectweb.tribe.common.IpAddress;
35 import org.objectweb.tribe.exceptions.ChannelException;
36 import org.objectweb.tribe.exceptions.NotConnectedException;
37
38 /**
39  * This class defines a TcpServerChannel
40  *
41  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
42  * @version 1.0
43  */

44 public class TcpServerChannel extends AbstractServerChannel
45 {
46
47   private ServerSocket JavaDoc socket; // underlying TCP server socket
48

49   /**
50    * Creates a new unbound <code>TcpServerChannel</code>
51    *
52    * @throws IOException if an error occurs
53    */

54   public TcpServerChannel() throws IOException JavaDoc
55   {
56     socket = new ServerSocket JavaDoc();
57   }
58
59   /**
60    * Creates a new <code>TcpServerChannel</code> bound to the specified port.
61    * If port is 0, then the socket is bound to an available port chosen by the
62    * system.
63    *
64    * @param port bind the socket to the given port (0 let the system choose an
65    * available port).
66    * @throws IOException if an error occurs
67    */

68   public TcpServerChannel(int port) throws IOException JavaDoc
69   {
70     socket = new ServerSocket JavaDoc(port);
71   }
72
73   /**
74    * @see org.objectweb.tribe.serverSocket.AbstractServerChannel#bind(org.objectweb.tribe.common.Address)
75    */

76   public void bind(Address JavaDoc source) throws ChannelException
77   {
78     if (!(source instanceof IpAddress))
79       throw new ChannelException("TCP Channels require IP addresses.");
80     IpAddress ip = (IpAddress) source;
81     try
82     {
83       socket.bind(new InetSocketAddress JavaDoc(ip.getAddress(), ip.getPort()));
84     }
85     catch (IOException JavaDoc e)
86     {
87       throw new ChannelException("Error while binding serverSocket on "
88           + source, e);
89     }
90   }
91
92   /**
93    * @see org.objectweb.tribe.channel.AbstractServerChannel#getBindAddress()
94    */

95   public Address JavaDoc getBindAddress() throws NotConnectedException
96   {
97     if (socket == null)
98       throw new NotConnectedException();
99     return new IpAddress(socket.getInetAddress(), socket.getLocalPort());
100   }
101
102   /**
103    * @see org.objectweb.tribe.serverSocket.AbstractServerChannel#accept()
104    */

105   public AbstractReliableFifoChannel accept() throws ChannelException
106   {
107     try
108     {
109       return new TcpChannel(socket.accept());
110     }
111     catch (Exception JavaDoc e)
112     {
113       throw new ChannelException("Error while accepting new connection", e);
114     }
115   }
116
117   /**
118    * @see org.objectweb.tribe.serverSocket.AbstractServerChannel#close()
119    */

120   public void close() throws ChannelException
121   {
122     try
123     {
124       socket.close();
125     }
126     catch (IOException JavaDoc e)
127     {
128       throw new ChannelException("Error while closing the serverSocket", e);
129     }
130     finally
131     {
132       socket = null;
133     }
134   }
135
136 }
Popular Tags