KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * C-JDBC: Clustered JDBC. Copyright (C) 2002-2004 French National Institute For
3  * Research In Computer Science And Control (INRIA). Contact:
4  * c-jdbc@objectweb.org This library is free software; you can redistribute it
5  * and/or modify it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation; either version 2.1 of the License,
7  * or any later version. This library is distributed in the hope that it will be
8  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
10  * General Public License for more details. You should have received a copy of
11  * the GNU Lesser General Public License along with this library; if not, write
12  * to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
13  * 02111-1307 USA. Initial developer(s): Nicolas Modrzyk Contributor(s):
14  * ______________________.
15  */

16
17 package org.objectweb.cjdbc.scenario.standalone.socket;
18
19 import java.io.BufferedInputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.ObjectInputStream JavaDoc;
22 import java.net.ServerSocket JavaDoc;
23 import java.net.Socket JavaDoc;
24
25 /**
26  *
27  * This class defines a UTFServer class
28  *
29  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
30  */

31 public class UTFServer implements Runnable JavaDoc
32 {
33   int serverPort = 7603;
34
35   ServerSocket JavaDoc serverSocket;
36   String JavaDoc hexa;
37
38   /**
39    * Starts the UTFServer thread
40    */

41   public void run()
42   {
43     try
44     {
45       serverSocket = new ServerSocket JavaDoc(serverPort);
46       Socket JavaDoc clientSocket = serverSocket.accept();
47
48       ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(clientSocket
49           .getInputStream());
50       boolean useBufferedStream = in.readBoolean();
51       if (useBufferedStream)
52         in = new ObjectInputStream JavaDoc(new BufferedInputStream JavaDoc(clientSocket
53             .getInputStream()));
54
55       boolean useWriteObject = in.readBoolean();
56       boolean useGC = in.readBoolean();
57       boolean useReset = in.readBoolean();
58       int loop = in.readInt();
59
60       System.out.println("Starting new test with:"
61           + clientSocket.getInetAddress().getHostName());
62       System.out.println("useWriteObject:" + useWriteObject);
63       System.out.println("useGC:" + useGC);
64       System.out.println("useReset:" + useReset);
65       System.out.println("loop:" + loop);
66       System.gc();
67       long freeStart = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
68       System.out.println("Free Memory at start:" + (freeStart / 1024 / 1024)
69           + " MBytes.");
70
71       
72       if (useWriteObject)
73       {
74         for (int i = 0; i < loop; i++)
75         {
76           hexa = (String JavaDoc) in.readObject();
77           if (useGC)
78             System.gc();
79           if (useReset)
80             in.reset();
81         }
82       }
83       else
84       {
85         for (int i = 0; i < loop; i++)
86         {
87
88           int size = in.readInt();
89           byte[] bytes = new byte[size];
90           in.readFully(bytes);
91           hexa = new String JavaDoc(bytes);
92         }
93       }
94
95       long freeEnd = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
96       long usedMemory = (freeEnd - freeStart);
97       System.out.println("Free Memory at end:" + freeEnd / 1024 / 1024
98           + " MBytes");
99       System.out.println("Used Memory:" + usedMemory / 1024 / 1024 + " MBytes");
100       System.out.println("Ending test with:"
101           + clientSocket.getInetAddress().getHostName());
102
103     }
104     catch (Exception JavaDoc e)
105     {
106       // TODO Auto-generated catch block
107
e.printStackTrace();
108     }
109     finally
110     {
111       try
112       {
113         serverSocket.close();
114       }
115       catch (IOException JavaDoc e1)
116       {
117         // TODO Auto-generated catch block
118
e1.printStackTrace();
119       }
120     }
121   }
122
123   /**
124    * @return Returns the hexa.
125    */

126   public String JavaDoc getHexa()
127   {
128     return hexa;
129   }
130
131   /**
132    * @return Returns the serverPort.
133    */

134   public int getServerPort()
135   {
136     return serverPort;
137   }
138
139   /**
140    * @param serverPort The serverPort to set.
141    */

142   public void setServerPort(int serverPort)
143   {
144     this.serverPort = serverPort;
145   }
146
147   /**
148    * Standalone application
149    * @param args not needed
150    * @throws Exception if fails
151    */

152   public static void main(String JavaDoc[] args) throws Exception JavaDoc
153   {
154     UTFServer sutf = new UTFServer();
155     while (true)
156     {
157       sutf.run();
158     }
159   }
160 }
Popular Tags