KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > il > oil > OILClientILService


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.mq.il.oil;
8
9 import java.io.BufferedInputStream JavaDoc;
10 import java.io.BufferedOutputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.ObjectInputStream JavaDoc;
13 import java.io.ObjectOutputStream JavaDoc;
14 import java.net.ServerSocket JavaDoc;
15 import java.net.Socket JavaDoc;
16 import java.rmi.RemoteException JavaDoc;
17
18 import org.jboss.mq.Connection;
19 import org.jboss.mq.ReceiveRequest;
20 import org.jboss.mq.SpyDestination;
21
22 /**
23  * The RMI implementation of the ConnectionReceiver object
24  *
25  * @author Norbert Lataille (Norbert.Lataille@m4x.org)
26  * @author Hiram Chirino (Cojonudo14@hotmail.com)
27  * @version $Revision: 1.14 $
28  * @created August 16, 2001
29  */

30 public final class OILClientILService
31    implements java.lang.Runnable JavaDoc,
32       org.jboss.mq.il.ClientILService
33 {
34    private final static org.jboss.logging.Logger cat = org.jboss.logging.Logger.getLogger(OILClientILService.class);
35
36    //the client IL
37
private OILClientIL clientIL;
38
39    //The thread that is doing the Socket reading work
40
private Thread JavaDoc worker;
41
42    // the connected client
43
private Socket JavaDoc socket = null;
44
45    //A link on my connection
46
private Connection connection;
47
48    //Should this service be running ?
49
private boolean running;
50
51    //The server socket we listen for a connection on
52
private ServerSocket JavaDoc serverSocket;
53
54    /**
55     * Number of OIL Worker threads started.
56     */

57    private static int threadNumber= 0;
58
59    /**
60     * If the TcpNoDelay option should be used on the socket.
61     */

62    private boolean enableTcpNoDelay=false;
63
64    /**
65     * getClientIL method comment.
66     *
67     * @return The ClientIL value
68     * @exception java.lang.Exception Description of Exception
69     */

70    public org.jboss.mq.il.ClientIL getClientIL()
71           throws java.lang.Exception JavaDoc
72    {
73       return clientIL;
74    }
75
76    /**
77     * init method comment.
78     *
79     * @param connection Description of Parameter
80     * @param props Description of Parameter
81     * @exception java.lang.Exception Description of Exception
82     */

83    public void init(org.jboss.mq.Connection connection, java.util.Properties JavaDoc props)
84           throws java.lang.Exception JavaDoc
85    {
86       this.connection = connection;
87       serverSocket = new ServerSocket JavaDoc(0);
88
89       String JavaDoc t = props.getProperty(OILServerILFactory.OIL_TCPNODELAY_KEY);
90       if (t != null)
91          enableTcpNoDelay = t.equals("yes");
92
93       clientIL = new OILClientIL(java.net.InetAddress.getLocalHost(), serverSocket.getLocalPort(), enableTcpNoDelay);
94       
95    }
96
97    /**
98     * Main processing method for the OILClientILService object
99     */

100    public void run()
101    {
102       int code = 0;
103       ObjectOutputStream JavaDoc out = null;
104       ObjectInputStream JavaDoc in = null;
105       socket = null;
106       int serverPort = serverSocket.getLocalPort();
107
108       try
109       {
110          if( cat.isDebugEnabled() )
111             cat.debug("Waiting for the server to connect to me on port " +serverSocket.getLocalPort());
112
113          // We may close() before we get a connection so we need to
114
// periodicaly check to see if we were !running.
115
//
116
serverSocket.setSoTimeout(1000);
117          while (running && socket == null)
118          {
119             try
120             {
121                socket = serverSocket.accept();
122             }
123             catch (java.io.InterruptedIOException JavaDoc e)
124             {
125                // do nothing, running flag will be checked
126
continue;
127             }
128             catch (IOException JavaDoc e)
129             {
130                if (running)
131                   connection.asynchFailure("Error accepting connection from server in OILClientILService.", e);
132                return; // finally block will clean up!
133
}
134          }
135
136          if(running)
137          {
138             socket.setTcpNoDelay(enableTcpNoDelay);
139             socket.setSoTimeout(0);
140             out = new ObjectOutputStream JavaDoc(new BufferedOutputStream JavaDoc(socket.getOutputStream()));
141             out.flush();
142             in = new ObjectInputStream JavaDoc(new BufferedInputStream JavaDoc(socket.getInputStream()));
143          }
144          else
145          {
146             // not running so exit
147
// let the finally block do the clean up
148
//
149
return;
150          }
151       }
152       catch (IOException JavaDoc e)
153       {
154          connection.asynchFailure("Could not initialize the OILClientIL Service.", e);
155          return;
156       }
157       finally
158       {
159          try
160          {
161             serverSocket.close();
162             serverSocket = null;
163          }
164          catch (IOException JavaDoc e)
165          {
166             if(cat.isDebugEnabled())
167                cat.debug("run: an error occured closing the server socket", e);
168          }
169       }
170
171       // now process request from the client.
172
//
173
while (running)
174       {
175          try
176          {
177             code = in.readByte();
178          }
179          catch (java.io.InterruptedIOException JavaDoc e)
180          {
181             continue;
182          }
183          catch (IOException JavaDoc e)
184          {
185             // Server has gone, bye, bye
186
break;
187          }
188
189          try
190          {
191
192             switch (code)
193             {
194                case OILConstants.RECEIVE:
195                   int numReceives = in.readInt();
196                   org.jboss.mq.ReceiveRequest[] messages = new org.jboss.mq.ReceiveRequest[numReceives];
197                   for (int i = 0; i < numReceives; ++i)
198                   {
199                      messages[i] = new ReceiveRequest();
200                      messages[i].readExternal(in);
201                   }
202                   connection.asynchDeliver(messages);
203                   break;
204
205                case OILConstants.DELETE_TEMPORARY_DESTINATION:
206                   connection.asynchDeleteTemporaryDestination((SpyDestination)in.readObject());
207                   break;
208
209                case OILConstants.CLOSE:
210                   connection.asynchClose();
211                   break;
212
213                case OILConstants.PONG:
214                   connection.asynchPong(in.readLong());
215                   break;
216
217                default:
218                   throw new RemoteException JavaDoc("Bad method code !");
219             }
220
221             //Everthing was OK
222
//
223
try
224             {
225                out.writeByte(OILConstants.SUCCESS);
226                out.flush();
227             }
228             catch (IOException JavaDoc e)
229             {
230                connection.asynchFailure("Connection failure(1)", e);
231                break; // exit the loop
232
}
233          }
234          catch (Exception JavaDoc e)
235          {
236             if (!running)
237             {
238                // if not running then don't bother to log an error
239
//
240
break;
241             }
242
243             try
244             {
245                cat.error("Exception handling server request", e);
246                out.writeByte(OILConstants.EXCEPTION);
247                out.writeObject(e);
248                out.reset();
249                out.flush();
250             }
251             catch (IOException JavaDoc e2)
252             {
253                connection.asynchFailure("Connection failure(2)", e2);
254                break;
255             }
256          }
257       } // end while
258

259       // exited loop, so clean up the conection
260
//
261
try
262       {
263          cat.debug("Closing receiver connections on port: " + serverPort);
264          out.close();
265          in.close();
266          socket.close();
267          socket = null;
268       }
269       catch (IOException JavaDoc e)
270       {
271          connection.asynchFailure("Connection failure", e);
272       }
273
274       // ensure the flag is set correctly
275
//
276
running = false;
277    }
278
279    /**
280     * start method comment.
281     *
282     * @exception java.lang.Exception Description of Exception
283     */

284    public void start()
285           throws java.lang.Exception JavaDoc
286    {
287
288       running = true;
289       worker = new Thread JavaDoc(Connection.getThreadGroup(), this, "OILClientILService-" +threadNumber++);
290       worker.setDaemon(true);
291       worker.start();
292
293    }
294
295    /**
296     * @exception java.lang.Exception Description of Exception
297     */

298    public void stop()
299           throws java.lang.Exception JavaDoc
300    {
301       cat.trace("Stop called on OILClientService");
302       running = false;
303       worker.interrupt();
304    }
305 }
306 // vim:expandtab:tabstop=3:shiftwidth=3
307

308
309
310
311
312
313
314
Popular Tags