KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > maxConnectionEnforcement > Client


1 package test.maxConnectionEnforcement;
2
3 import java.io.*;
4 import java.util.*;
5 import org.omg.CORBA.*;
6 import org.omg.PortableServer.*;
7 import org.omg.BiDirPolicy.*;
8
9 import org.jacorb.util.*;
10
11 public class Client
12     extends CallbackIfPOA
13 {
14     static TestIf remoteObj = null;
15     static long callInterval = 0;
16     static Random rnd = new Random();
17
18     static CallbackIf myself = null;
19
20     public void opOnCallback()
21     {
22         System.out.println( "opOnCallback called" );
23     }
24
25     public static void main( String JavaDoc args[] )
26     {
27         if( args.length != 3 )
28         {
29             System.out.println( "Usage: jaco test.maxConnectionEnforcement.Client <ior_file> <call interval> <# of threads>" );
30             System.exit( 1 );
31         }
32
33         try
34         {
35             File f = new File( args[ 0 ] );
36
37             //check if file exists
38
if( ! f.exists() )
39             {
40                 System.out.println("File " + args[0] +
41                                    " does not exist.");
42                 
43                 System.exit( -1 );
44             }
45             
46             //check if args[0] points to a directory
47
if( f.isDirectory() )
48             {
49                 System.out.println("File " + args[0] +
50                                    " is a directory.");
51                 
52                 System.exit( -1 );
53             }
54
55             Properties props = new Properties();
56
57             props.put( "org.omg.PortableInterceptor.ORBInitializerClass.bidir_init",
58                        "org.jacorb.orb.connection.BiDirConnectionInitializer" );
59
60
61             //init ORB
62
ORB orb = ORB.init( args, props );
63
64             //init POA
65
POA root_poa =
66                 POAHelper.narrow( orb.resolve_initial_references( "RootPOA" ));
67
68             Any any = orb.create_any();
69             BidirectionalPolicyValueHelper.insert( any, BOTH.value );
70
71             Policy[] policies = new Policy[4];
72             policies[0] =
73                 root_poa.create_lifespan_policy(LifespanPolicyValue.TRANSIENT);
74
75             policies[1] =
76                 root_poa.create_id_assignment_policy(IdAssignmentPolicyValue.SYSTEM_ID);
77
78             policies[2] =
79                 root_poa.create_implicit_activation_policy( ImplicitActivationPolicyValue.IMPLICIT_ACTIVATION );
80
81             policies[3] = orb.create_policy( BIDIRECTIONAL_POLICY_TYPE.value,
82                                              any );
83         
84             POA bidir_poa = root_poa.create_POA( "BiDirPOA",
85                                                  root_poa.the_POAManager(),
86                                                  policies );
87             bidir_poa.the_POAManager().activate();
88
89             myself = CallbackIfHelper.narrow( bidir_poa.servant_to_reference( new Client() ));
90
91             BufferedReader br =
92                 new BufferedReader( new FileReader( f ));
93
94             // get object reference from command-line argument file
95
org.omg.CORBA.Object JavaDoc obj =
96                 orb.string_to_object( br.readLine() );
97
98             br.close();
99
100             //narrow to test interface
101
remoteObj = TestIfHelper.narrow( obj );
102
103             callInterval = Integer.parseInt( args[1] );
104
105             int threads = Integer.parseInt( args[2] );
106             for( int i = 0; i < threads; i++ )
107             {
108                 (new Thread JavaDoc( new Runnable JavaDoc()
109                     {
110                         public void run()
111                         {
112                             try
113                             {
114                                 while( true )
115                                 {
116                                     if( Math.abs( rnd.nextInt() ) % 3 > 0 )
117                                     {
118                                         //call remote op
119
remoteObj.op();
120                                         System.out.println(
121                                             "Thread " +
122                                             Thread.currentThread().getName() +
123                                             " made normal call" );
124                                     }
125                                     else
126                                     {
127                                         remoteObj.doCallback( myself );
128                                         System.out.println(
129                                             "Thread " +
130                                             Thread.currentThread().getName() +
131                                             " made bidir call" );
132                                     }
133
134                                     Thread.sleep( Math.abs( rnd.nextLong() ) % callInterval );
135                                 }
136                             }
137                             catch( Exception JavaDoc e )
138                             {
139                                 e.printStackTrace();
140                             }
141
142                             System.exit( -1 );
143                         }
144                     })).start();
145             }
146             
147             Thread.sleep( Long.MAX_VALUE );
148         }
149         catch( Exception JavaDoc ex )
150         {
151             ex.printStackTrace();
152         }
153     }
154 }
155
156
157
Popular Tags