KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > channel > TCPAcceptSocketImpl


1 /**
2  * Dream
3  * Copyright (C) 2003-2004 INRIA Rhone-Alpes
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 of the License, or (at your option) 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 USA
18  *
19  * Contact : dream@objectweb.org
20  *
21  * Initial developer(s): Matthieu Leclercq
22  * Contributor(s): Vivien Quema
23  */

24
25 package org.objectweb.dream.channel;
26
27 import java.io.IOException JavaDoc;
28 import java.net.BindException JavaDoc;
29 import java.net.ServerSocket JavaDoc;
30 import java.net.Socket JavaDoc;
31 import java.net.SocketException JavaDoc;
32
33 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
34 import org.objectweb.util.monolog.api.BasicLevel;
35
36 /**
37  * Wraps TCP ServerSocket.
38  */

39 public class TCPAcceptSocketImpl extends AbstractAcceptSocketImpl
40     implements
41       TCPAcceptSocketAttributeController
42 {
43
44   /** The {@link ServerSocket}used by this component */
45   protected ServerSocket JavaDoc listen;
46
47   // ---------------------------------------------------------------------------
48
// Attributes fields
49
// ---------------------------------------------------------------------------
50

51   protected int listeningPort = -1;
52   protected int openRetry = OPEN_RETRY_DEFAULT;
53   protected boolean tcpNoDelay = true;
54   protected int soTimeout = 0;
55   protected int soLinger = 60;
56
57   // ---------------------------------------------------------------------------
58
// Implementation of abstract method
59
// ---------------------------------------------------------------------------
60

61   /**
62    * @see AbstractAcceptSocketImpl#acceptSocket()
63    */

64   protected SocketState acceptSocket() throws IOException JavaDoc
65   {
66     if (listen == null || listen.isClosed())
67     {
68       logger.log(BasicLevel.DEBUG, "ServerSocket closed, create new one");
69       listen = createServerSocket();
70     }
71     logger.log(BasicLevel.DEBUG, "waiting connection");
72     Socket JavaDoc socket = listen.accept();
73     setSocketOption(socket);
74     SocketState socketState = new SocketStateImpl();
75     socketState.setSocket(socket);
76     return socketState;
77   }
78
79   // ---------------------------------------------------------------------------
80
// Attribute controller interface
81
// ---------------------------------------------------------------------------
82

83   /**
84    * @see TCPAcceptSocketAttributeController#getListeningPort()
85    */

86   public int getListeningPort()
87   {
88     return listeningPort;
89   }
90
91   /**
92    * @see TCPAcceptSocketAttributeController#setListeningPort(int)
93    */

94   public void setListeningPort(int port) throws IllegalLifeCycleException
95   {
96     if (getFcState() == STARTED)
97     {
98       throw new IllegalLifeCycleException(
99           "The component is started, can't change the listeningPort attribute");
100     }
101     listeningPort = port;
102   }
103
104   /**
105    * @see TCPAcceptSocketAttributeController#getOpenRetry()
106    */

107   public int getOpenRetry()
108   {
109     return openRetry;
110   }
111
112   /**
113    * @see TCPAcceptSocketAttributeController#setOpenRetry(int)
114    */

115   public void setOpenRetry(int retry)
116   {
117     openRetry = retry;
118   }
119
120   /**
121    * @see TCPAcceptSocketAttributeController#getSoLinger()
122    */

123   public int getSoLinger()
124   {
125     return soLinger;
126   }
127
128   /**
129    * @see TCPAcceptSocketAttributeController#setSoLinger(int)
130    */

131   public void setSoLinger(int timeout)
132   {
133     soLinger = timeout;
134   }
135
136   /**
137    * @see TCPAcceptSocketAttributeController#getSoTimeout()
138    */

139   public int getSoTimeout()
140   {
141     return soTimeout;
142   }
143
144   /**
145    * @see TCPAcceptSocketAttributeController#setSoTimeout(int)
146    */

147   public void setSoTimeout(int timeout)
148   {
149     soTimeout = timeout;
150   }
151
152   /**
153    * @see TCPAcceptSocketAttributeController#getTcpNoDelay()
154    */

155   public boolean getTcpNoDelay()
156   {
157     return tcpNoDelay;
158   }
159
160   /**
161    * @see TCPAcceptSocketAttributeController#setTcpNoDelay(boolean)
162    */

163   public void setTcpNoDelay(boolean tcpNoDelay)
164   {
165     this.tcpNoDelay = tcpNoDelay;
166   }
167
168   //---------------------------------------------------------------------------
169
// Overriden Lifecycle controller methods
170
// ---------------------------------------------------------------------------
171

172   /**
173    * @see org.objectweb.fractal.api.control.LifeCycleController#startFc()
174    */

175   public void startFc() throws IllegalLifeCycleException
176   {
177     if (listeningPort < 0)
178     {
179       throw new IllegalLifeCycleException("Unset attribute : listeningPort");
180     }
181     super.startFc();
182   }
183
184   /**
185    * @see org.objectweb.fractal.api.control.LifeCycleController#stopFc()
186    */

187   public void stopFc() throws IllegalLifeCycleException
188   {
189     if (listen != null && !listen.isClosed())
190     {
191       try
192       {
193         listen.close();
194       }
195       catch (IOException JavaDoc e)
196       {
197         logger.log(BasicLevel.WARN,
198             "An error occured while closing server socket", e);
199       }
200       listen = null;
201     }
202     super.stopFc();
203   }
204
205   // ---------------------------------------------------------------------------
206
// Utility Methods
207
// ---------------------------------------------------------------------------
208

209   protected ServerSocket JavaDoc createServerSocket() throws IOException JavaDoc
210   {
211     for (int i = 0;; i++)
212     {
213       try
214       {
215         return new ServerSocket JavaDoc(listeningPort);
216       }
217       catch (BindException JavaDoc exc)
218       {
219         if (i > openRetry)
220         {
221           throw exc;
222         }
223         try
224         {
225           Thread.sleep(i * 250);
226         }
227         catch (InterruptedException JavaDoc ignored)
228         {
229           // ignored
230
}
231       }
232     }
233   }
234
235   protected void setSocketOption(Socket JavaDoc sock) throws SocketException JavaDoc
236   {
237     sock.setTcpNoDelay(tcpNoDelay);
238     sock.setSoTimeout(soTimeout);
239     if (soLinger >= 0)
240     {
241       sock.setSoLinger(true, soLinger);
242     }
243     else
244     {
245       sock.setSoLinger(false, 0);
246     }
247   }
248
249 }
Popular Tags