001: package demo.any;
002:
003: import java.io.*;
004: import org.omg.CORBA.Any;
005: import org.omg.PortableServer.*;
006: import org.omg.CosNaming.*;
007: import org.omg.CORBA.OctetSeqHelper;
008: import org.omg.CORBA.StringSeqHelper;
009:
010: public class Client extends AnyServerPOA {
011: public static AnyServer s = null;
012: private static int counter;
013:
014: private synchronized static void incr(int val) {
015: counter += val;
016: }
017:
018: public java.lang.String generic(Any a) {
019: System.out.println("Someone called me!");
020: incr(-1);
021: return "call back succeeded!";
022: }
023:
024: public static void main(String[] args) {
025: try {
026: org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);
027:
028: // get hold of the naming service
029: NamingContextExt nc = NamingContextExtHelper.narrow(orb
030: .resolve_initial_references("NameService"));
031:
032: AnyServer s = AnyServerHelper.narrow(nc.resolve(nc
033: .to_name("AnyServer.service")));
034:
035: // create a new any
036: Any a = org.omg.CORBA.ORB.init().create_any();
037:
038: // char
039: char ch = 'c';
040: System.out.print("Passing a char...");
041: a.insert_char((char) ch);
042: System.out.println(s.generic(a));
043:
044: // long
045: long l = 4711;
046: System.out.print("Passing a longlong...");
047: a.insert_longlong(l);
048: System.out.println(s.generic(a));
049:
050: // short
051: System.out.print("Passing a short...");
052: a.insert_short((short) 5);
053: System.out.println(s.generic(a));
054:
055: // float
056: System.out.print("Passing a float...");
057: a.insert_float((float) 3.14);
058: System.out.println(s.generic(a));
059:
060: // string
061: System.out.print("Passing a string...");
062: a.type(orb.create_string_tc(0));
063: a.insert_string("Hi there");
064: System.out.println(s.generic(a));
065:
066: // wstring
067: System.out.print("Passing a Wstring...");
068: //myWStringHelper.insert( a, "WWWWWSTring-äääääß" );
069: //a.insert_wstring( "WWWWWSTring-äääääß" );
070: a.insert_wstring("Hi there");
071:
072: System.out.println("Any.kind: " + a.type().kind().value());
073:
074: System.out.println(s.generic(a));
075:
076: // sequences
077:
078: String[] str = { "hello", "world" };
079: MyStringSeqHelper.insert(a, str);
080: System.out.print("Alias? " + a.type());
081: System.out.print("Passing a sequence of strings...");
082: System.out.println(s.generic(a));
083:
084: System.out.print("Passing a sequence of octets...");
085: byte[] octets = { 1, 2, 3, 4 };
086: OctetSeqHelper.insert(a, octets);
087: System.out.println(s.generic(a));
088:
089: // array
090:
091: System.out.print("Passing an array...");
092: String[] str2 = { "hello", "another", "world" };
093: stringsHelper.insert(a, str2);
094: System.out.println(s.generic(a));
095:
096: // struct
097: System.out.print("Passing a struct...");
098: Node tail = new Node("tail", new Node[0]);
099: Node head = new Node("head", new Node[] { tail });
100: NodeHelper.insert(a, head);
101: System.out.println(s.generic(a));
102:
103: // union
104: System.out.print("Passing a union...");
105: Nums n = new Nums();
106: n.l(4711);
107: NumsHelper.insert(a, n);
108: System.out.println(s.generic(a));
109:
110: /* There are two ways to insert object references: */
111:
112: incr(1); // remember how many call backs we have to expect
113:
114: System.out.print("Passing an object...");
115: POA poa = POAHelper.narrow(orb
116: .resolve_initial_references("RootPOA"));
117: poa.the_POAManager().activate();
118:
119: Client c = new Client();
120: poa.activate_object(c);
121:
122: /* insert an untyped reference */
123:
124: a.insert_Object(c._this _object());
125: System.out.println("Output of generic: " + s.generic(a));
126:
127: incr(1);
128:
129: System.out.print("Passing object again");
130:
131: /* insert an typed reference */
132:
133: AnyServerHelper.insert(a, c._this ());
134: System.out.println("Output of generic: " + s.generic(a));
135:
136: /* insert an any */
137:
138: System.out.print("Passing an any");
139: Any inner_any = orb.create_any();
140: inner_any.insert_string("Hello in any");
141: a.insert_any(inner_any);
142: System.out.println("Output of generic: " + s.generic(a));
143:
144: while (counter > 0) {
145: System.out
146: .print("Going to sleep to wait for incoming calls");
147: Thread.currentThread().sleep(3000);
148: }
149: orb.shutdown(true);
150: } catch (Exception e) {
151: e.printStackTrace();
152: }
153: }
154: }
|