001: package demo.dii;
002:
003: /**
004: * An example for using the Dynamic Invocation Interface
005: */
006: import org.omg.CosNaming.*;
007:
008: public class Client {
009: public static void main(String[] args) {
010: org.omg.CORBA.ORB orb = null;
011:
012: try {
013: orb = org.omg.CORBA.ORB.init(args, null);
014:
015: NamingContextExt nc = NamingContextExtHelper.narrow(orb
016: .resolve_initial_references("NameService"));
017:
018: org.omg.CORBA.Object s = nc.resolve(nc
019: .to_name("dii.example"));
020:
021: // a simple request
022: org.omg.CORBA.Request r = s._request("_get_long_number");
023:
024: r.set_return_type(orb
025: .get_primitive_tc(org.omg.CORBA.TCKind.tk_long));
026:
027: r.invoke();
028:
029: if (r.env().exception() != null) {
030: throw r.env().exception();
031: } else {
032: System.out.println("0: " + r.return_value());
033: }
034:
035: // a request with out args
036: org.omg.CORBA.Request r_out = s._request("add");
037:
038: r_out.add_in_arg().insert_long(3);
039: r_out.add_in_arg().insert_long(4);
040:
041: org.omg.CORBA.Any out_arg = r_out.add_out_arg();
042: out_arg.type(orb
043: .get_primitive_tc(org.omg.CORBA.TCKind.tk_long));
044:
045: r_out.set_return_type(orb
046: .get_primitive_tc(org.omg.CORBA.TCKind.tk_void));
047:
048: r_out.invoke();
049:
050: if (r_out.env().exception() != null) {
051: throw r_out.env().exception();
052: } else {
053: System.out.println("1: " + out_arg.extract_long());
054: }
055:
056: // another simple request with a string argumente, oneway
057: org.omg.CORBA.Request r1 = s._request("notify");
058: r1.add_in_arg().insert_string("hallo");
059: r1.send_oneway();
060:
061: // a request with a return value
062: org.omg.CORBA.Request r2 = s._request("writeNumber");
063: r2.add_in_arg().insert_long(5);
064: r2.set_return_type(orb
065: .get_primitive_tc(org.omg.CORBA.TCKind.tk_string));
066: r2.invoke();
067: if (r2.env().exception() != null) {
068: throw r2.env().exception();
069: } else {
070: System.out.println("2: " + r2.return_value());
071: }
072:
073: // deferred asynchronous operation, synchronize with result
074: org.omg.CORBA.Request r3 = s._request("writeNumber");
075: r3.add_in_arg().insert_long(5);
076: r3.set_return_type(orb
077: .get_primitive_tc(org.omg.CORBA.TCKind.tk_string));
078:
079: r3.send_deferred();
080: r3.get_response();
081: if (r3.env().exception() != null) {
082: throw r3.env().exception();
083: } else {
084: System.out.println("3: " + r3.return_value());
085: }
086:
087: // polling until response is there
088: org.omg.CORBA.Request r4 = s._request("writeNumber");
089: r4.add_in_arg().insert_long(5);
090: r4.set_return_type(orb
091: .get_primitive_tc(org.omg.CORBA.TCKind.tk_string));
092:
093: r4.send_deferred();
094:
095: while (!r4.poll_response()) {
096: /* we could be doing s.th. useful here instead of
097: sleeping...*/
098: try {
099: Thread.currentThread().sleep(10);
100: } catch (InterruptedException i) {
101: }
102: System.out.print(".");
103: }
104:
105: if (r4.env().exception() != null) {
106: throw r4.env().exception();
107: } else {
108: System.out.println("4: " + r2.return_value());
109: }
110:
111: //send a request which throws an exception
112: org.omg.CORBA.Request r5 = s._request("writeNumberWithEx");
113: r5.add_in_arg().insert_long(5);
114: org.omg.CORBA.ExceptionList exceptions = r5.exceptions();
115: org.omg.CORBA.TypeCode tc = orb
116: .create_exception_tc(
117: "IDL:dii/server/e:1.0",
118: "e",
119: new org.omg.CORBA.StructMember[] { new org.omg.CORBA.StructMember(
120: "why", orb.create_string_tc(0),
121: null) });
122: exceptions.add(tc);
123:
124: r5.invoke();
125: if (r5.env().exception() != null) {
126: System.out.println("5: Got exception "
127: + r5.env().exception());
128: //Hint: what you get here is a
129: //org.omg.CORBA.portable.ApplicationException
130: //which contains an any containing the real
131: //exception.
132: }
133: } catch (Exception e) {
134: e.printStackTrace();
135: }
136: orb.shutdown(false);
137: }
138: }
|