KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > any > Client


1 package demo.any;
2
3 import java.io.*;
4 import org.omg.CORBA.Any JavaDoc;
5 import org.omg.PortableServer.*;
6 import org.omg.CosNaming.*;
7 import org.omg.CORBA.OctetSeqHelper JavaDoc;
8 import org.omg.CORBA.StringSeqHelper JavaDoc;
9
10 public class Client
11     extends AnyServerPOA
12 {
13     public static AnyServer s = null;
14     private static int counter;
15
16     private synchronized static void incr(int val)
17     {
18     counter += val;
19     }
20
21     public java.lang.String JavaDoc generic(Any JavaDoc a)
22     {
23     System.out.println("Someone called me!");
24     incr(-1);
25     return "call back succeeded!";
26     }
27
28     public static void main( String JavaDoc[] args )
29     {
30     try
31     {
32         org.omg.CORBA.ORB JavaDoc orb = org.omg.CORBA.ORB.init(args,null);
33
34         // get hold of the naming service
35
NamingContextExt nc =
36                 NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService"));
37
38         AnyServer s =
39               AnyServerHelper.narrow(nc.resolve(nc.to_name("AnyServer.service")));
40
41         // create a new any
42
Any JavaDoc a = org.omg.CORBA.ORB.init().create_any();
43         
44         // char
45
char ch = 'c';
46         System.out.print("Passing a char...");
47         a.insert_char( (char)ch );
48         System.out.println( s.generic( a ) );
49
50         // long
51
long l = 4711;
52         System.out.print("Passing a longlong...");
53         a.insert_longlong( l );
54         System.out.println( s.generic( a ) );
55
56         // short
57
System.out.print("Passing a short...");
58         a.insert_short( (short)5 );
59         System.out.println( s.generic( a ) );
60
61         // float
62
System.out.print("Passing a float...");
63         a.insert_float( (float)3.14);
64         System.out.println( s.generic( a ) );
65            
66         // string
67
System.out.print("Passing a string...");
68             a.type( orb.create_string_tc(0));
69         a.insert_string("Hi there");
70         System.out.println( s.generic( a ) );
71
72         // wstring
73
System.out.print("Passing a Wstring...");
74        //myWStringHelper.insert( a, "WWWWWSTring-äääääß" );
75
//a.insert_wstring( "WWWWWSTring-äääääß" );
76
a.insert_wstring( "Hi there" );
77
78        System.out.println("Any.kind: " + a.type().kind().value() );
79
80         System.out.println( s.generic( a ) );
81
82         // sequences
83

84         String JavaDoc [] str = {"hello","world"};
85         MyStringSeqHelper.insert( a, str );
86         System.out.print("Alias? " + a.type());
87         System.out.print("Passing a sequence of strings...");
88         System.out.println( s.generic( a ) );
89
90         System.out.print("Passing a sequence of octets...");
91         byte [] octets = {1,2,3,4};
92         OctetSeqHelper.insert( a, octets );
93         System.out.println( s.generic( a ) );
94
95         // array
96

97         System.out.print("Passing an array...");
98         String JavaDoc [] str2 = {"hello","another","world"};
99         stringsHelper.insert( a, str2 );
100         System.out.println( s.generic( a ) );
101
102         // struct
103
System.out.print("Passing a struct...");
104         Node tail = new Node("tail" , new Node[0] );
105         Node head = new Node("head" , new Node[]{tail} );
106         NodeHelper.insert( a, head );
107         System.out.println( s.generic( a ) );
108
109         // union
110
System.out.print("Passing a union...");
111         Nums n = new Nums();
112         n.l(4711);
113         NumsHelper.insert( a, n );
114         System.out.println( s.generic( a ) );
115             
116         /* There are two ways to insert object references: */
117
118         incr(1); // remember how many call backs we have to expect
119

120         System.out.print("Passing an object...");
121         POA poa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
122         poa.the_POAManager().activate();
123
124         Client c = new Client();
125         poa.activate_object(c);
126
127         /* insert an untyped reference */
128
129         a.insert_Object(c._this_object());
130         System.out.println( "Output of generic: " + s.generic( a ) );
131
132         incr(1);
133
134         System.out.print("Passing object again");
135
136         /* insert an typed reference */
137
138         AnyServerHelper.insert( a, c._this());
139         System.out.println( "Output of generic: " + s.generic( a ) );
140
141
142         /* insert an any */
143
144         System.out.print("Passing an any");
145         Any JavaDoc inner_any = orb.create_any();
146         inner_any.insert_string("Hello in any");
147         a.insert_any(inner_any);
148         System.out.println( "Output of generic: " + s.generic( a ) );
149
150         while( counter > 0 )
151         {
152         System.out.print("Going to sleep to wait for incoming calls");
153         Thread.currentThread().sleep(3000);
154         }
155         orb.shutdown(true);
156     }
157     catch ( Exception JavaDoc e)
158     {
159         e.printStackTrace();
160     }
161     }
162 }
163
164
165
Popular Tags