KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > poa_monitor > client > Client


1 package demo.poa_monitor.client;
2
3 import demo.poa_monitor.foox.*;
4 import org.omg.CosNaming.*;
5 import java.io.*;
6
7 public class Client
8 {
9     public static int speed = 0;
10     public static int cost = 0;
11     public static String JavaDoc serverDescription = "description not available";
12
13     private static TestFrame frame;
14     private static FooFactory factory;
15     private static Foo [] foos;
16     private static RequestGenerator [] [] generators;
17     
18     private static int allConsumed;
19     private static int allEffort;
20     private static int firstPOAConsumed;
21     private static int firstPOAEffort;
22     private static int firstObjectConsumed;
23     private static int firstObjectEffort;
24     private static int allICount;
25     private static int firstPOAICount;
26     private static int firstObjectICount;
27     private static boolean firstPOAContact = true;
28
29     public static void actionCancel()
30     {
31         actionStop();
32         try {
33             for (int i=0; i<generators.length; i++) {
34                 for (int k=0; k<generators[i].length; k++) {
35                     generators[i][k].join();
36                 }
37             }
38         } catch (Throwable JavaDoc e) {
39         }
40         System.exit(0);
41     }
42
43     public static void actionStart(int objects, int threads)
44     {
45         foos = new Foo[objects];
46         generators = new RequestGenerator[objects][threads];
47         try
48         {
49             for (int i=0; i<foos.length; i++)
50             {
51
52                 if (firstPOAContact)
53                 {
54                     long startTime = System.currentTimeMillis();
55                     foos[i] = factory.createFoo(""+(1000+i));
56                     long stopTime = System.currentTimeMillis();
57                     Client.addTime((int)(stopTime-startTime), 0, false);
58                     firstPOAContact = false;
59                     
60                 } else {
61                     foos[i] = factory.createFoo(""+(1000+i));
62                 }
63                 if (foos[i] == null) throw new Error JavaDoc("error: createFoo returns null");
64
65                 for (int k=0; k<threads; k++) {
66                     generators[i][k] = new RequestGenerator(foos[i], k==0);
67                     if (k==0) generators[i][k].start();
68                 }
69             }
70             Thread.currentThread().sleep(1000);
71             for (int i=0; i<generators.length; i++) {
72                 for (int k=1; k<generators[i].length; k++) {
73                     generators[i][k].start();
74                 }
75             }
76             System.out.println("[ "+(objects*threads)+" RequestGenerators started ]");
77         } catch (Exception JavaDoc e) {
78             e.printStackTrace();
79         }
80     }
81
82     public static void actionStop()
83     {
84         for (int i=0; i<generators.length; i++)
85         {
86             for (int k=0; k<generators[i].length; k++)
87             {
88                 generators[i][k].active = false;
89             }
90         }
91
92         System.out.println("[ RequestGenerators stopped ]");
93
94         for (int i=0; i<foos.length; i++)
95         {
96             try
97             {
98                 foos[i].deactivate();
99             }
100             catch (Throwable JavaDoc e)
101             {
102                 System.out.println("[ exception occured during object deactivation ]");
103                 System.out.println(e.toString());
104             }
105         }
106         System.out.println("[ Remote objects deactivated (don't worry about wrong policy exception messages s\n in server 3 (Servant Locator) ]");
107         printTime();
108     }
109
110     synchronized public static void addTime(int consumed, int effort, boolean firstObjectContact) {
111         if (firstPOAContact) {
112             firstPOAConsumed += consumed;
113             firstPOAEffort += effort;
114             firstPOAICount++;
115             return;
116         }
117         if (firstObjectContact) {
118             firstObjectConsumed += consumed;
119             firstObjectEffort += effort;
120             firstObjectICount++;
121         }
122         allConsumed += consumed;
123         allEffort += effort;
124         allICount++;
125     }
126
127     public static void main(String JavaDoc args[])
128     {
129         try
130         {
131             org.omg.CORBA.ORB JavaDoc orb = org.omg.CORBA.ORB.init(args, null);
132             // get hold of the naming service and create a fooFactory object
133
NamingContextExt nc = NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService"));
134             factory = FooFactoryHelper.narrow(nc.resolve(nc.to_name("FooFactory.service") ));
135             System.out.println("[ FooFactory created ]");
136             
137             // get the server description
138
serverDescription = factory.getServerDescription();
139             
140             // set up the gui
141
frame = new TestFrame();
142             frame.setVisible(true);
143         }
144         catch (Exception JavaDoc e) {
145             e.printStackTrace();
146         }
147     }
148
149     public static void printTime()
150     {
151         
152         /*
153           long startTime = System.currentTimeMillis();
154           while (nb-- > 0)
155           ret_vals = server.intTransfer(myInt);
156
157           // for(int i = 0; i < size; System.out.print(" " + ret_vals[i++]));
158
159           long stopTime = System.currentTimeMillis();
160           System.out.println(">>> Elapsed time = "
161           + (stopTime - startTime)/1000
162           + " secs Average time = "
163           + ((stopTime - startTime) / (float)loop)
164           + " msecs");
165                        
166           if (firstPOAContact) {
167           firstPOAConsumed += consumed;
168           firstPOAEffort += effort;
169           } else if (firstObjectContact) {
170           firstObjectConsumed += consumed;
171           firstObjectEffort += effort;
172           }
173           allConsumed += consumed;
174           allEffort += effort;
175           invocationCount++;
176         */

177         String JavaDoc v;
178         int w = 12;
179         int its_p = firstPOAConsumed-firstPOAEffort;
180         int its_o = firstObjectConsumed-firstObjectEffort;
181         int its_a = allConsumed-allEffort;
182         float ita_p = its_p/(float)firstPOAICount;
183         float ita_o = its_o/(float)firstObjectICount;
184         float ita_a = its_a/(float)allICount;
185         
186         System.out.println(" IS CTS(s) CTA(ms) ETS(s) ETA(ms) ITS(s) ITA(ms)");
187         System.out.println(" ---------------------------------------------------------------------------------------");
188         System.out.print("FPCL: ");
189         v = firstPOAICount+"";
190         System.out.print(v); for(int i=v.length(); i<=w; i++) System.out.print(" ");
191         v = firstPOAConsumed/(float)1000+"";
192         System.out.print(v); for(int i=v.length(); i<=w; i++) System.out.print(" ");
193         v = firstPOAConsumed/(float)firstPOAICount+"";
194         System.out.print(v); for(int i=v.length(); i<=w; i++) System.out.print(" ");
195         v = firstPOAEffort/(float)1000+"";
196         System.out.print(v); for(int i=v.length(); i<=w; i++) System.out.print(" ");
197         v = firstPOAEffort/(float)firstPOAICount+"";
198         System.out.print(v); for(int i=v.length(); i<=w; i++) System.out.print(" ");
199         v = its_p/(float)1000+"";
200         System.out.print(v); for(int i=v.length(); i<=w; i++) System.out.print(" ");
201         v = ita_p+"";
202         System.out.print(v); for(int i=v.length(); i<=w; i++) System.out.print(" ");
203         
204         System.out.println("\n");
205         System.out.print("FOCL: ");
206         v = firstObjectICount+"";
207         System.out.print(v); for(int i=v.length(); i<=w; i++) System.out.print(" ");
208         v = firstObjectConsumed/(float)1000+"";
209         System.out.print(v); for(int i=v.length(); i<=w; i++) System.out.print(" ");
210         v = firstObjectConsumed/(float)firstObjectICount+"";
211         System.out.print(v); for(int i=v.length(); i<=w; i++) System.out.print(" ");
212         v = firstObjectEffort/(float)1000+"";
213         System.out.print(v); for(int i=v.length(); i<=w; i++) System.out.print(" ");
214         v = firstObjectEffort/(float)firstObjectICount+"";
215         System.out.print(v); for(int i=v.length(); i<=w; i++) System.out.print(" ");
216         v = its_o/(float)1000+"";
217         System.out.print(v); for(int i=v.length(); i<=w; i++) System.out.print(" ");
218         v = ita_o+"";
219         System.out.print(v); for(int i=v.length(); i<=w; i++) System.out.print(" ");
220         
221         System.out.println("\n");
222         System.out.print("All : ");
223         v = allICount+"";
224         System.out.print(v); for(int i=v.length(); i<=w; i++) System.out.print(" ");
225         v = allConsumed/(float)1000+"";
226         System.out.print(v); for(int i=v.length(); i<=w; i++) System.out.print(" ");
227         v = allConsumed/(float)allICount+"";
228         System.out.print(v); for(int i=v.length(); i<=w; i++) System.out.print(" ");
229         v = allEffort/(float)1000+"";
230         System.out.print(v); for(int i=v.length(); i<=w; i++) System.out.print(" ");
231         v = allEffort/(float)allICount+"";
232         System.out.print(v); for(int i=v.length(); i<=w; i++) System.out.print(" ");
233         v = its_a/(float)1000+"";
234         System.out.print(v); for(int i=v.length(); i<=w; i++) System.out.print(" ");
235         v = ita_a+"";
236         System.out.print(v); for(int i=v.length(); i<=w; i++) System.out.print(" ");
237           
238         System.out.println("");
239         System.out.println(" ---------------------------------------------------------------------------------------");
240         System.out.println("");
241         System.out.println(" I...invocation C...consumed T...time S...sum A...average");
242         System.out.println(" F...first P...poa - O...object C...contact L...latency");
243         System.out.println("");
244     }
245 }
246
Popular Tags