KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > standalone > socket > UTFClient


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@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): Nicolas Modrzyk
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.standalone.socket;
26
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.ObjectOutputStream JavaDoc;
30 import java.net.Socket JavaDoc;
31 import java.net.UnknownHostException JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.ResourceBundle JavaDoc;
34
35 import org.objectweb.cjdbc.common.stream.encoding.HexaEncoding;
36 import org.objectweb.cjdbc.scenario.tools.ScenarioUtility;
37
38 /**
39  * This class defines a UTFClient class
40  *
41  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
42  */

43 public class UTFClient implements Runnable JavaDoc
44 {
45   String JavaDoc hexa;
46   int serverPort = 7603;
47   boolean useWriteObject = false;
48   private int loop = 1;
49   String JavaDoc imageFile = "/image/cinema.pdf";
50   String JavaDoc serverName = "localhost";
51   private boolean useBufferedStream = false;
52   private boolean useGC = false;
53   private boolean useSameObject;
54   private boolean useReset;
55
56   /**
57    * @return Returns the serverName.
58    */

59   public String JavaDoc getServerName()
60   {
61     return serverName;
62   }
63
64   /**
65    * @param serverName The serverName to set.
66    */

67   public void setServerName(String JavaDoc serverName)
68   {
69     this.serverName = serverName;
70   }
71
72   /**
73    * @return Returns the loop.
74    */

75   public int getLoop()
76   {
77     return loop;
78   }
79
80   /**
81    * @param loop The loop to set.
82    */

83   public void setLoop(int loop)
84   {
85     this.loop = loop;
86   }
87
88   /**
89    * @return Returns the serverPort.
90    */

91   public int getServerPort()
92   {
93     return serverPort;
94   }
95
96   /**
97    * @param serverPort The serverPort to set.
98    */

99   public void setServerPort(int serverPort)
100   {
101     this.serverPort = serverPort;
102   }
103
104   /**
105    * @return Returns the useWriteObject.
106    */

107   public boolean getUseWriteObject()
108   {
109     return useWriteObject;
110   }
111
112   /**
113    * @param useWriteObject The useWriteObject to set.
114    */

115   public void setUseWriteObject(boolean useWriteObject)
116   {
117     this.useWriteObject = useWriteObject;
118   }
119
120   /**
121    * @return Returns the imageFile.
122    */

123   public String JavaDoc getImageFile()
124   {
125     return imageFile;
126   }
127
128   /**
129    * @param imageFile The imageFile to set.
130    */

131   public void setImageFile(String JavaDoc imageFile)
132   {
133     this.imageFile = imageFile;
134   }
135
136   /**
137    * @see java.lang.Runnable#run()
138    */

139   public void run()
140   {
141     try
142     {
143
144       File JavaDoc image = new File JavaDoc(getClass().getResource(imageFile).getFile());
145       hexa = HexaEncoding.data2hex(ScenarioUtility.readBinary(image));
146       byte[] bytes = hexa.getBytes();
147       Socket JavaDoc socket = new Socket JavaDoc(serverName, serverPort);
148       ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(socket.getOutputStream());
149
150       out.writeBoolean(useBufferedStream);
151       if (useBufferedStream)
152         out = new ObjectOutputStream JavaDoc(socket.getOutputStream());
153
154       out.writeBoolean(useWriteObject);
155       out.writeBoolean(useGC);
156       out.writeBoolean(false);
157       out.writeInt(loop);
158       out.flush();
159
160       if (useWriteObject)
161       {
162         for (int i = 0; i < loop; i++)
163         {
164           System.out.println(i);
165           if (!useSameObject)
166             out.writeObject(HexaEncoding.data2hex(ScenarioUtility
167                 .readBinary(image)));
168           else
169             out.writeObject(hexa);
170           out.flush();
171           if (useGC)
172             System.gc();
173           if (useReset)
174             out.reset();
175         }
176       }
177       else
178       {
179         for (int i = 0; i < loop; i++)
180         {
181           System.out.println(i);
182           out.writeInt(bytes.length);
183           out.write(bytes);
184           out.flush();
185         }
186       }
187
188       socket.close();
189     }
190     catch (UnknownHostException JavaDoc e)
191     {
192       // TODO Auto-generated catch block
193
e.printStackTrace();
194     }
195     catch (IOException JavaDoc e)
196     {
197       // TODO Auto-generated catch block
198
e.printStackTrace();
199     }
200
201   }
202
203   /**
204    * @return Returns the hexa.
205    */

206   public String JavaDoc getHexa()
207   {
208     return hexa;
209   }
210
211   /**
212    * @return Returns the useBufferedStream.
213    */

214   public boolean getUseBufferedStream()
215   {
216     return useBufferedStream;
217   }
218
219   /**
220    * @param useBufferedStream The useBufferedStream to set.
221    */

222   public void setUseBufferedStream(boolean useBufferedStream)
223   {
224     this.useBufferedStream = useBufferedStream;
225   }
226
227   /**
228    * Standalone application
229    *
230    * @param args not needed, use the properties file instead
231    * @throws Exception if fails
232    */

233   public static void main(String JavaDoc[] args) throws Exception JavaDoc
234   {
235
236     // Get test properties
237
ResourceBundle JavaDoc rb = ResourceBundle.getBundle("socketTest");
238     Enumeration JavaDoc enu = rb.getKeys();
239     String JavaDoc key;
240     while (enu.hasMoreElements())
241     {
242       key = (String JavaDoc) enu.nextElement();
243       System.out.println("Using test property <" + key + "> with value <"
244           + rb.getObject(key) + ">");
245     }
246     int serverPort = Integer.parseInt(rb.getString("serverPort"));
247     String JavaDoc imageFile = rb.getString("imageFile");
248     int numberOfLoop = Integer.parseInt(rb.getString("numberOfLoop"));
249     String JavaDoc serverName = rb.getString("serverName");
250     boolean useBufferedStream = new Boolean JavaDoc(rb.getString("useBufferedStream"))
251         .booleanValue();
252     boolean useWriteObject = new Boolean JavaDoc(rb.getString("useWriteObject"))
253         .booleanValue();
254     boolean useSameObject = new Boolean JavaDoc(rb.getString("useSameObject"))
255         .booleanValue();
256     boolean useGC = new Boolean JavaDoc(rb.getString("useGC")).booleanValue();
257     boolean useReset = new Boolean JavaDoc(rb.getString("useReset")).booleanValue();
258
259     // Set properties of the client
260
UTFClient cutf = new UTFClient();
261     cutf.setServerPort(serverPort);
262     cutf.setLoop(numberOfLoop);
263     cutf.setImageFile(imageFile);
264     cutf.setServerName(serverName);
265     cutf.setUseBufferedStream(useBufferedStream);
266     cutf.setUseWriteObject(useWriteObject);
267     cutf.setUseGC(useGC);
268     cutf.setUseSameObject(useSameObject);
269     cutf.setUseReset(useReset);
270
271     // Start test
272
long started = System.currentTimeMillis();
273     long freeStart = Runtime.getRuntime().totalMemory()
274         - Runtime.getRuntime().freeMemory();
275     Thread JavaDoc client = new Thread JavaDoc(cutf);
276     client.start();
277
278     // Wait
279
client.join();
280     long end = System.currentTimeMillis();
281     long freeEnd = Runtime.getRuntime().totalMemory()
282         - Runtime.getRuntime().freeMemory();
283     long usedMemory = (freeEnd - freeStart);
284     long last = end - started;
285     long time = last / 1000;
286     float average1 = ((float) numberOfLoop / (float) time);
287     float average2 = ((float) time / (float) numberOfLoop);
288
289     System.out.println("The test lasted " + time + " s. (" + last
290         + " ms.) for an average of " + average1
291         + " loop(s) per second (or 1 loop takes :" + average2 + "seconds");
292     System.out.println("Used memory was:" + usedMemory / 1024 / 1024 + " Mb, "
293         + usedMemory + " bytes.");
294   }
295
296   /**
297    * @param useReset
298    */

299   void setUseReset(boolean useReset)
300   {
301     this.useReset = useReset;
302   }
303
304   /**
305    * @param useSameObject
306    */

307   void setUseSameObject(boolean useSameObject)
308   {
309     this.useSameObject = useSameObject;
310   }
311
312   /**
313    * @param useGC
314    */

315   void setUseGC(boolean useGC)
316   {
317     this.useGC = useGC;
318
319   }
320 }
Popular Tags