KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > coach > tracing > server > ServerTest


1 /***************************************************************************/
2 /* Copyright (C) 2003 Lucent Technologies Nederland BV */
3 /* Bell Labs Advanced Technologies - EMEA */
4 /* */
5 /* Initial developer(s): Wim Hellenthal */
6 /* */
7 /* This library is free software; you can redistribute it and/or */
8 /* modify it under the terms of the GNU Lesser General Public */
9 /* License as published by the Free Software Foundation; either */
10 /* version 2.1 of the License, or (at your option) any later version. */
11 /* */
12 /* This library is distributed in the hope that it will be useful, */
13 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
14 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
15 /* Lesser General Public License for more details. */
16 /* */
17 /* You should have received a copy of the GNU Lesser General Public */
18 /* License along with this library; if not, write to the Free Software */
19 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
20 /***************************************************************************/
21
22 package org.coach.tracing.server;
23
24 import org.coach.tracing.api.*;
25 import org.omg.CORBA.*;
26 import java.io.*;
27 import java.util.*;
28 import java.net.*;
29
30 public class ServerTest
31 {
32     private i_Trace connection;
33     private org.omg.CORBA.ORB JavaDoc orb;
34     // process specific event counter
35
private int event_counter = 0;
36     private String JavaDoc appl_id;
37
38     public ServerTest ()
39     {
40     }
41
42     /**
43      * This routine initializes the ORB and resolves the i_Trace interface using the Name Service
44      *
45      * @param args The command line arguments.
46      */

47     void init (String JavaDoc args[])
48     {
49         try
50         {
51             args = org.objectweb.openccm.corba.TheORB.initialize(args);
52
53             orb = org.objectweb.openccm.corba.TheORB.getORB();
54
55             org.omg.CORBA.Object JavaDoc obj = orb.resolve_initial_references("NameService");
56
57             org.omg.CosNaming.NamingContext JavaDoc nc = org.omg.CosNaming.NamingContextHelper.narrow(obj);
58
59             org.omg.CosNaming.NameComponent JavaDoc[] ncomp = new org.omg.CosNaming.NameComponent JavaDoc[1];
60
61             ncomp[0] = new org.omg.CosNaming.NameComponent JavaDoc("COACH_TracingServer", "");
62
63             obj = nc.resolve(ncomp);
64
65             connection = i_TraceHelper.narrow (obj);
66
67             java.io.File JavaDoc tmp = java.io.File.createTempFile("tmp", "");
68
69             tmp.deleteOnExit();
70
71             appl_id = tmp.getName().substring(3);
72         }
73         catch (Exception JavaDoc _ex)
74         {
75             _ex.printStackTrace();
76
77             System.exit(-1);
78         }
79     }
80
81     /**
82      * The main method. This method initializes the server and creates some test events.
83      *
84      * @param args The command line arguments.
85      */

86     public static void main(String JavaDoc[] args)
87     {
88         ServerTest application = new ServerTest();
89
90         application.init(args);
91
92         // start sending events to server
93

94         if ( args.length == 0 )
95             application.send_invocations(1);
96         else
97             application.send_invocations(Integer.parseInt(args[0]));
98     }
99
100     public void send_invocations(int cnt)
101     {
102         IdentityDescriptor cmp1 = createObject (appl_id, "Container_A", "Component_1", "Hello_A");
103         IdentityDescriptor cmp2 = createObject (appl_id, "Container_A", "Component_2", "Hello_B");
104         IdentityDescriptor cmp3 = createObject (appl_id, "Container_B", "Component_1", "Hello_C");
105         IdentityDescriptor cmp4 = createObject (appl_id, "Container_C", "Component_1", "Hello_D");
106
107         // generating some invocations
108

109         Parameter[] parameter = new Parameter[] {
110             new Parameter("in", "string", "v1")
111         };
112
113         Any[] value = new Any[1];
114
115         value[0] = orb.create_any();
116         value[0].insert_string("Hello World");
117
118         System.out.println ("Coach Tracing Server Test, n = " + cnt);
119
120         for (int i = 0; i < cnt; i++)
121         {
122             invocation ("hello_world", cmp1, cmp2, parameter, value, true); // oneway invocation
123
invocation ("hi_there", cmp2, cmp3, parameter, value, false);
124             invocation ("how_are_you", cmp3, cmp4, parameter, value, false);
125             invocation ("I'm_fine", cmp3, cmp2, parameter, value, false);
126             invocation ("thank_you", cmp2, cmp1, parameter, value, false);
127         }
128     }
129
130     public void invocation (String JavaDoc operation_name,
131                             IdentityDescriptor from,
132                             IdentityDescriptor to,
133                             Parameter[] parameters,
134                             Any[] values,
135                             boolean oneway)
136     {
137         /* thread of execution for a given interaction point. This must be a unique id meaning that you must be able
138          * to discriminate between threads with the same id running on two different machines for example */

139
140         String JavaDoc exec_thread = "";
141
142         try
143         {
144             exec_thread = Thread.currentThread().getName() + "_" + InetAddress.getLocalHost().getHostName();
145         }
146         catch (Exception JavaDoc _ex)
147         {
148             _ex.printStackTrace();
149         }
150
151
152         /* trail id - every trail (sequence of causaly related events) has a starting point identified by the trail id. */
153         String JavaDoc trail_id = new Integer JavaDoc (event_counter).toString();
154         /* message id - each invocation has a unique message id to associate the invoker with the receiver. */
155         String JavaDoc message_id = trail_id + "_" + event_counter + "_";
156         // create event for STUB_OUT
157

158         /*
159          * The traceserver orders events per process using the process event counter.
160          * Events with the same message id are associated with each other and form a message between invoker and receiver.
161          * The trace server calculates the relative ordering of events using the message id, event counter and trail id.
162          * All events with the same trail id are causaly related to each other.
163          * The traceserver maintains a total ordering of events using the exact relative ordering of causaly related events and
164          * physical timestamp comparison for causaly unrelated event.
165          */

166         TraceEvent stub_out_event = new TraceEvent (
167             System.currentTimeMillis(), /* physical time */
168             oneway ? InteractionPoint.ONEWAY_STUB_OUT :
169                      InteractionPoint.STUB_OUT, /* interaction point */
170             "", /* trail label - user defined label to mark a trail segment,
171                                                          * not used by the trace viewer yet (giving trail segments a
172                                                          * different color for example */

173             message_id + "F", /* The message id to associate the originator with the receiver of the invocation */
174             exec_thread, /* thread of execution */
175             trail_id, /* trail id */
176             event_counter++, /* global event counter per process, incremented on every send and receive event */
177             operation_name, /* operation name */
178             from, /* object that initiated the invocation */
179             parameters, /* parameters (dir, type, name) */
180             values); /* parameter values */
181
182         // create event for POA_IN
183

184         TraceEvent poa_in_event = new TraceEvent (
185             System.currentTimeMillis(), /* physical time */
186             oneway ? InteractionPoint.ONEWAY_POA_IN :
187                      InteractionPoint.POA_IN, /* interaction point */
188             "", /* trail label - user defined label to mark a trail segment,
189                                                          * not used by the trace viewer yet (giving trail segments a
190                                                          * different color for example */

191             message_id + "F", /* The message id to associate the originator with the receiver of the invocation */
192             exec_thread, /* simulating this is a collocated call (same thread of execution
193                                                          * as the stub */

194             trail_id, /* trail id */
195             event_counter++, /* logical clock incremented for every interaction point along the trail*/
196             operation_name, /* operation name */
197             to, /* object that receives invocation */
198             parameters, /* parameters (dir, type, name)*/
199             values); /* parameter values */
200
201         if ( oneway )
202         {
203             // a oneway invocation contains two event - ONEWAY_STUB_OUT and ONEWAY_POA_IN
204

205             TraceEvent[] events = new TraceEvent[2];
206
207             events[0] = stub_out_event;
208             events[1] = poa_in_event;
209
210             connection.receiveEvent(events);
211
212             return;
213         }
214
215         // create event for POA_OUT
216

217         TraceEvent poa_out_event = new TraceEvent (
218             System.currentTimeMillis(), /* physical time */
219             InteractionPoint.POA_OUT, /* interaction point */
220             "", /* trail label - user defined label to mark a trail segment,
221                                                          * not used by the trace viewer yet (giving trail segments a
222                                                          * different color for example */

223             message_id + "B", /* The message id to associate the originator with the receiver of the invocation */
224             exec_thread, /* simulating this is a collocated call (same thread of execution
225                                                          * as the stub */

226             trail_id, /* trail id */
227             event_counter++, /* logical clock incremented for every interaction point along the trail*/
228             operation_name, /* operation name */
229             to, /* object that receives invocation */
230             parameters, /* parameters (dir, type, name)*/
231             values); /* parameter values */
232
233         // create event for STUB_IN
234

235         TraceEvent stub_in_event = new TraceEvent (
236             System.currentTimeMillis(), /* physical time */
237             InteractionPoint.STUB_IN, /* interaction point */
238             "", /* trail label - user defined label to mark a trail segment,
239                                                          * not used by the trace viewer yet (giving trail segments a
240                                                          * different color for example */

241             message_id + "B", /* The message id to associate the originator with the receiver of the invocation */
242             exec_thread, /* simulating this is a collocated call (same thread of execution
243                                                          * as the skeleton */

244             trail_id, /* trail id */
245             event_counter++, /* logical clock incremented for every interaction point along the trail*/
246             operation_name, /* operation name */
247             from, /* object that receives invocation */
248             parameters, /* parameters (dir, type, name)*/
249             values); /* parameter values */
250
251         TraceEvent[] events = new TraceEvent[4];
252
253         events[0] = stub_out_event;
254         events[1] = poa_in_event;
255         events[2] = poa_out_event;
256         events[3] = stub_in_event;
257
258         connection.receiveEvent(events);
259     }
260
261
262     public IdentityDescriptor createObject( String JavaDoc process_id,
263                                             String JavaDoc container_name,
264                                             String JavaDoc component_name,
265                                             String JavaDoc object_name)
266     {
267         // used to describe a sending or receiving entity such as a facet instance, component instance etc.
268

269         IdentityDescriptor identity = new IdentityDescriptor();
270
271         try
272         {
273             identity.process_id = process_id;
274             identity.node_name = InetAddress.getLocalHost().getHostName();
275             identity.node_ip = InetAddress.getLocalHost().getHostAddress();
276             identity.object_instance_id = object_name;
277             identity.object_repository_id = "facet";
278             identity.cmp_name = component_name;
279             identity.cmp_type = "component";
280             identity.cnt_name = container_name;
281             identity.cnt_type = "container";
282         }
283         catch (Exception JavaDoc _ex)
284         {
285             _ex.printStackTrace();
286         }
287
288         return identity;
289     }
290 }
291
292
Popular Tags