001: package demo.policies;
002:
003: import java.io.*;
004: import org.omg.CORBA.*;
005: import org.omg.Messaging.*;
006:
007: /**
008: * A simple demo that shows how to use CORBA QoS policies at the level
009: * of individual objects, or ORB-wide. This is a variaton on hello
010: * world and relies on IOR files rather than the naming service.
011: *
012: * @author Gerald Brose
013: */
014:
015: public class Client {
016: public static final int MSEC_FACTOR = 10000;
017: public static final int ONE_SECOND = 1000 * MSEC_FACTOR;
018:
019: public static void main(String args[]) {
020: if (args.length != 1) {
021: System.out
022: .println("Usage: jaco demo.policies.Client <ior_file>");
023: System.exit(1);
024: }
025:
026: try {
027: // read IOR from file
028: File f = new File(args[0]);
029: if (!f.exists()) {
030: System.out.println("File " + args[0]
031: + " does not exist.");
032: System.exit(-1);
033: }
034:
035: //check if args[0] points to a directory
036: if (f.isDirectory()) {
037: System.out.println("File " + args[0]
038: + " is a directory.");
039: System.exit(-1);
040: }
041: BufferedReader br = new BufferedReader(new FileReader(f));
042:
043: // initialize the ORB
044: ORB orb = ORB.init(args, null);
045:
046: // get object reference from command-line argument file
047: org.omg.CORBA.Object obj = orb.string_to_object(br
048: .readLine());
049:
050: br.close();
051:
052: // and narrow it to HelloWorld.GoodDay
053: // if this fails, a BAD_PARAM will be thrown
054: GoodDay goodDay = GoodDayHelper.narrow(obj);
055:
056: // get PolicyManager and create policies ....
057:
058: PolicyManager policyManager = PolicyManagerHelper
059: .narrow(orb
060: .resolve_initial_references("ORBPolicyManager"));
061:
062: // create an timeout value of 1 sec. The unit is a time
063: // step of 100 nano secs., so 10000 of these make up a
064: // micro second.
065: Any rrtPolicyAny = orb.create_any();
066: rrtPolicyAny.insert_ulonglong(ONE_SECOND);
067:
068: // create a relative roundtrip timeout policy and set this
069: // policy ORB-wide
070: Policy rrtPolicy = orb
071: .create_policy(
072: RELATIVE_RT_TIMEOUT_POLICY_TYPE.value,
073: rrtPolicyAny);
074:
075: policyManager.set_policy_overrides(
076: new Policy[] { rrtPolicy },
077: SetOverrideType.ADD_OVERRIDE);
078:
079: // create a sync scope policy
080: Any syncPolicyAny = orb.create_any();
081: syncPolicyAny.insert_short(SYNC_WITH_SERVER.value);
082: Policy syncPolicy = orb.create_policy(
083: SYNC_SCOPE_POLICY_TYPE.value, syncPolicyAny);
084:
085: // set the sync scope policy on an object reference
086: goodDay._set_policy_override(new Policy[] { syncPolicy },
087: SetOverrideType.ADD_OVERRIDE);
088:
089: // try to invoke the operation and print the result: this
090: // should result in a timeout exception because the server
091: // will sleep longer than the timeout (sleep is int msecs.)
092: System.out
093: .println("Should see a TIMEOUT exception soon...");
094: try {
095: System.out.println(goodDay.hello(2000));
096: System.out
097: .println("... should not be here, no exception!");
098: } catch (org.omg.CORBA.TIMEOUT t) {
099: System.out.println("here it is: " + t.getMessage());
100: }
101:
102: // create another timeout poliy
103: Any objRrtPolicyAny = orb.create_any();
104: objRrtPolicyAny.insert_ulonglong(4 * ONE_SECOND);
105:
106: Policy objRrtPolicy = orb.create_policy(
107: RELATIVE_RT_TIMEOUT_POLICY_TYPE.value,
108: objRrtPolicyAny);
109:
110: goodDay._set_policy_override(new Policy[] { objRrtPolicy },
111: SetOverrideType.ADD_OVERRIDE);
112:
113: // see what the effective policy is now...
114: Policy objPol = goodDay
115: ._get_policy(RELATIVE_RT_TIMEOUT_POLICY_TYPE.value);
116:
117: if (objPol != null) {
118: long timoutValue = ((RelativeRoundtripTimeoutPolicy) objPol)
119: .relative_expiry();
120: System.out.println("Object-level timeout is "
121: + timoutValue + " timesteps (100ns)");
122:
123: // try the invocation again, in this case the override
124: // policy's new timeout should be sufficient to let
125: // the call come back
126: System.out.println(goodDay.hello(100));
127: } else {
128: System.out
129: .println("ERROR, no Object-level timeout found");
130: }
131: } catch (Exception ex) {
132: ex.printStackTrace();
133: }
134: }
135: }
|