KickJava   Java API By Example, From Geeks To Geeks.

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


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):
23  */

24
25 package org.objectweb.dream.channel;
26
27 import java.io.IOException JavaDoc;
28 import java.net.InetAddress JavaDoc;
29 import java.net.Socket JavaDoc;
30 import java.net.SocketException JavaDoc;
31
32 import org.objectweb.dream.AbstractComponent;
33 import org.objectweb.dream.pool.ObjectPool;
34 import org.objectweb.fractal.api.NoSuchInterfaceException;
35 import org.objectweb.fractal.api.control.IllegalBindingException;
36 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
37 import org.objectweb.util.monolog.api.BasicLevel;
38
39 /**
40  * {@link IPSocketManager }that handles one TCP connection at a time. A new
41  * connection is established for each
42  * {@link IPSocketManager#getSocket(InetAddress, int) }method call, and
43  * connections are closed by
44  * {@link IPSocketManager#releaseSocket(SocketState, boolean) }method.
45  */

46 public class IPSocketManagerTCPImpl extends AbstractComponent
47     implements
48       IPSocketManager,
49       IPSocketManagerTCPAttributeController
50 {
51
52   // ---------------------------------------------------------------------------
53
// attribute fields
54
// ---------------------------------------------------------------------------
55

56   protected int cnxRetry = CNX_RETRY_DEFAULT;
57   protected boolean tcpNoDelay = true;
58   protected int soTimeout = 0;
59   protected int soLinger = 60;
60
61   // ---------------------------------------------------------------------------
62
// Client interfaces
63
// ---------------------------------------------------------------------------
64

65   /** The name of the client interface used to pool socket state instances */
66   public static final String JavaDoc SOCKET_STATE_POOL_ITF_NAME = "socket-state-pool";
67   protected ObjectPool socketStatePoolItf = null;
68
69   // ---------------------------------------------------------------------------
70
// Implementation of IPSocketManager interface
71
// ---------------------------------------------------------------------------
72

73   /**
74    * @see IPSocketManager#getSocket(InetAddress, int)
75    */

76   public SocketState getSocket(InetAddress JavaDoc address, int port)
77       throws IOException JavaDoc
78   {
79     for (int i = 0;; i++)
80     {
81       try
82       {
83         logger.log(BasicLevel.DEBUG, "try to connect");
84         Socket JavaDoc sock = new Socket JavaDoc(address, port);
85         setSocketOption(sock);
86         logger.log(BasicLevel.DEBUG, "connected");
87         SocketState socketState = (SocketState) socketStatePoolItf
88             .newInstance();
89         socketState.setSocket(sock);
90         return socketState;
91       }
92       catch (IOException JavaDoc exc)
93       {
94         if (i > cnxRetry)
95         {
96           throw exc;
97         }
98         logger.log(BasicLevel.WARN, "connection failed on address " + address
99             + " will retry");
100         try
101         {
102           Thread.sleep(i * 250);
103         }
104         catch (InterruptedException JavaDoc ignored)
105         {
106           // ignored
107
}
108       }
109     }
110   }
111
112   /**
113    * @see IPSocketManager#releaseSocket(SocketState, boolean)
114    */

115   public void releaseSocket(SocketState socketState, boolean error)
116   {
117     socketState.close();
118     socketStatePoolItf.recycleInstance(socketState);
119   }
120
121   protected void setSocketOption(Socket JavaDoc sock) throws SocketException JavaDoc
122   {
123     sock.setTcpNoDelay(tcpNoDelay);
124     sock.setSoTimeout(soTimeout);
125     if (soLinger >= 0)
126     {
127       sock.setSoLinger(true, soLinger);
128     }
129     else
130     {
131       sock.setSoLinger(false, 0);
132     }
133   }
134
135   // ---------------------------------------------------------------------------
136
// Implementation of AttributeController interface
137
// ---------------------------------------------------------------------------
138

139   /**
140    * @see IPSocketManagerTCPAttributeController#getCnxRetry()
141    */

142   public int getCnxRetry()
143   {
144     return cnxRetry;
145   }
146
147   /**
148    * @see IPSocketManagerTCPAttributeController#setCnxRetry(int)
149    */

150   public void setCnxRetry(int cnxRetry)
151   {
152     this.cnxRetry = cnxRetry;
153   }
154
155   /**
156    * @see IPSocketManagerTCPAttributeController#getTcpNoDelay()
157    */

158   public boolean getTcpNoDelay()
159   {
160     return tcpNoDelay;
161   }
162
163   /**
164    * @see org.objectweb.dream.channel.IPSocketManagerTCPAttributeController#setTcpNoDelay(boolean)
165    */

166   public void setTcpNoDelay(boolean tcpNoDelay)
167   {
168     this.tcpNoDelay = tcpNoDelay;
169   }
170
171   /**
172    * @see IPSocketManagerTCPAttributeController#getSoTimeout()
173    */

174   public int getSoTimeout()
175   {
176     return soTimeout;
177   }
178
179   /**
180    * @see IPSocketManagerTCPAttributeController#setSoTimeout(int)
181    */

182   public void setSoTimeout(int timeout)
183   {
184     soTimeout = timeout;
185   }
186
187   /**
188    * @see IPSocketManagerTCPAttributeController#getSoLinger()
189    */

190   public int getSoLinger()
191   {
192     return soLinger;
193   }
194
195   /**
196    * @see IPSocketManagerTCPAttributeController#setSoLinger(int)
197    */

198   public void setSoLinger(int timeout)
199   {
200     soLinger = timeout;
201   }
202
203   // ---------------------------------------------------------------------------
204
// Implementation of BindingController interface
205
// ---------------------------------------------------------------------------
206

207   /**
208    * @see org.objectweb.fractal.api.control.BindingController#listFc()
209    */

210   public String JavaDoc[] listFc()
211   {
212     return new String JavaDoc[]{SOCKET_STATE_POOL_ITF_NAME};
213   }
214
215   /**
216    * @see org.objectweb.fractal.api.control.BindingController#bindFc(String,
217    * Object)
218    */

219   public void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
220       throws NoSuchInterfaceException, IllegalBindingException,
221       IllegalLifeCycleException
222   {
223     super.bindFc(clientItfName, serverItf);
224     if (clientItfName.equals(SOCKET_STATE_POOL_ITF_NAME))
225     {
226       socketStatePoolItf = (ObjectPool) serverItf;
227     }
228   }
229 }
Popular Tags