001: package demo.concurrency;
002:
003: import org.omg.CosConcurrencyControl.*;
004: import org.omg.CosTransactions.*;
005: import org.omg.CosNaming.*;
006:
007: // import LogicLand.LLsSession.*;
008: // import java.net.Socket.*;
009: // import java.net.*;
010: // import java.io.*;
011:
012: public class Client {
013: private static Control ctrl = null;
014: private static TransactionalLockSet lockset = null;
015: private static TransactionFactory tf = null;
016:
017: public static void main(String[] args) {
018: try {
019: // jacorb.security.SecurityRoot.start( args );
020: // org.omg.CORBA.ORB orb = jacorb.orb.Local.get_orb();
021: org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);
022:
023: org.omg.PortableServer.POA poa = org.omg.PortableServer.POAHelper
024: .narrow(orb.resolve_initial_references("RootPOA"));
025:
026: // Session ss = get_session( orb );
027: // org.omg.CosNaming.NamingContextExt nc = ss.get_naming();
028: NamingContextExt nc = NamingContextExtHelper.narrow(orb
029: .resolve_initial_references("NameService"));
030: NameComponent[] name = new NameComponent[1];
031: name[0] = new NameComponent("LogicLand", "transaction");
032: tf = TransactionFactoryHelper.narrow(nc.resolve(name));
033:
034: name[0] = new NameComponent("LogicLand", "lock");
035: LockSetFactory ls = LockSetFactoryHelper.narrow(nc
036: .resolve(name));
037:
038: name[0] = new NameComponent("LogicLand", "lockset");
039: lockset = TransactionalLockSetHelper.narrow(nc
040: .resolve(name));
041: print_help();
042: while (exec_command())
043: ;
044:
045: } catch (Exception e) {
046: e.printStackTrace();
047: }
048: }
049:
050: static void print_help() {
051: System.out
052: .println(" ---------------------------------------------------------------------------");
053: System.out
054: .println(" LogicLand group, Concurrency service test program");
055: System.out
056: .println(" ---------------------------------------------------------------------------");
057: System.out
058: .println(" LockMode: Read - r, Write - w, Upgrade - u, IRead - ir, IWrite iw");
059: System.out
060: .println(" ---------------------------------------------------------------------------");
061: System.out.println(" Commands :");
062: System.out
063: .println(" ---------------------------------------------------------------------------");
064: System.out
065: .println(" Lock ::= l<LockMode> // Lock resource ");
066: System.out
067: .println(" Try_Lock ::= t<LockMode> // Try lock resource ");
068: System.out
069: .println(" Unlock ::= u<LockMode> // Unlock resource");
070: System.out
071: .println(" Change ::= c<LockMode><LockMode> // Change lock from->to");
072: System.out
073: .println(" Start ::= start[=n] // n minuts timeout, default - 5");
074: System.out
075: .println(" Commit ::= commit // commit transaction ");
076: System.out
077: .println(" Rollback ::= rollback // rollback transaction");
078: System.out
079: .println(" Print ::= print // print LockSet contents");
080: System.out
081: .println(" Help ::= help // this screen");
082: System.out
083: .println(" Quit ::= quit // quit from test");
084: System.out
085: .println(" ---------------------------------------------------------------------------");
086: };
087:
088: static boolean exec_command() {
089: System.out.print("Ready ;-)");
090: String cmd = InConsole.read();
091: System.out.println("Accept:" + cmd);
092: try {
093: if (cmd.equals("quit")) {
094: return false;
095: } else if (cmd.equals("help")) {
096: print_help();
097: } else if (cmd.equals("print")) {
098: // lockset.print();
099: } else if (cmd.equals("commit")) {
100: if (ctrl == null) {
101: System.out.println("Error: Transaction not active");
102: } else {
103: try {
104: ctrl.get_terminator().commit(false);
105: System.out.println("Commit complete");
106: } catch (org.omg.CORBA.OBJECT_NOT_EXIST e) {
107: System.out
108: .println("Error: Transaction finnished");
109: }
110: ctrl = null;
111: }
112: } else if (cmd.equals("rollback")) {
113: if (ctrl == null) {
114: System.out.println("Error: Transaction not active");
115: } else {
116: try {
117: ctrl.get_terminator().rollback();
118: System.out.println("Rollback complete");
119: } catch (org.omg.CORBA.OBJECT_NOT_EXIST e) {
120: System.out
121: .println("Error: Transaction finnished");
122: }
123: ctrl = null;
124: }
125: } else if (cmd.length() >= 5
126: && cmd.substring(0, 5).equals("start")) {
127: if (ctrl != null) {
128: System.out.println("Error: Transaction is active");
129: } else {
130: int minute = 5;
131: try {
132: minute = Integer.parseInt(cmd.substring(6));
133: } catch (Exception e) {
134: e.printStackTrace();
135: System.out.println("Use default 5 min.");
136: }
137: ctrl = tf.create(minute * 60);
138: System.out.println("Transaction started with "
139: + minute + "min timeout");
140: }
141: } else {
142: if (ctrl == null) {
143: System.out.println("Error: Transaction not active");
144: return true;
145: }
146: try {
147: if (cmd.length() >= 2
148: && cmd.substring(0, 1).equals("l")) {
149: lock_mode mode = parse_mode(cmd.substring(1));
150: lockset.lock(ctrl.get_coordinator(), mode);
151: System.out.println("Lock complete");
152: } else if (cmd.length() >= 2
153: && cmd.substring(0, 1).equals("t")) {
154: lock_mode mode = parse_mode(cmd.substring(1));
155: if (lockset.try_lock(ctrl.get_coordinator(),
156: mode)) {
157: System.out.println("Lock complete");
158: } else {
159: System.out.println("Lock uncomplete");
160: }
161: } else if (cmd.length() >= 2
162: && cmd.substring(0, 1).equals("u")) {
163: lock_mode mode = parse_mode(cmd.substring(1));
164: lockset.unlock(ctrl.get_coordinator(), mode);
165: System.out.println("Unlock complete");
166: } else if (cmd.length() >= 3
167: && cmd.substring(0, 1).equals("c")) {
168: lock_mode mode = parse_mode(cmd.substring(1));
169: lock_mode mode1;
170: if (mode.equals(lock_mode.intention_read)
171: || mode
172: .equals(lock_mode.intention_write)) {
173: mode1 = parse_mode(cmd.substring(3));
174: } else {
175: mode1 = parse_mode(cmd.substring(2));
176: }
177: lockset.change_mode(ctrl.get_coordinator(),
178: mode, mode1);
179: System.out.println("Change mode complete");
180: }
181: } catch (org.omg.CORBA.TRANSACTION_ROLLEDBACK e) {
182: System.out.println("Error: Transaction RolledBack");
183: ctrl = null;
184: } catch (org.omg.CORBA.INVALID_TRANSACTION e) {
185: System.out.println("Error: Transaction invalid");
186: ctrl = null;
187: } catch (org.omg.CORBA.OBJECT_NOT_EXIST e) {
188: System.out.println("Error: Transaction finnished");
189: ctrl = null;
190: }
191: }
192: } catch (Exception e) {
193: e.printStackTrace();
194: }
195: return true;
196: }
197:
198: static lock_mode parse_mode(String mode) {
199: if (mode.substring(0, 1).equals("r")) {
200: return lock_mode.read;
201: } else if (mode.substring(0, 1).equals("w")) {
202: return lock_mode.write;
203: } else if (mode.substring(0, 1).equals("u")) {
204: return lock_mode.upgrade;
205: } else if (mode.substring(0, 2).equals("ir")) {
206: return lock_mode.intention_read;
207: } else if (mode.substring(0, 2).equals("iw")) {
208: return lock_mode.intention_write;
209: }
210: return lock_mode.read;
211: };
212: /*
213: static Session get_session( org.omg.CORBA.ORB orb ) throws Exception {
214: String line;
215: BufferedReader in;
216: Socket quote = new Socket("192.168.1.9", 9000);
217: in = new BufferedReader(new InputStreamReader(quote.getInputStream()));
218: line = in.readLine();
219: while (line.indexOf("IOR:") != 0) {
220: line = in.readLine();
221: }
222: in.close();
223: quote.close();
224:
225: org.omg.CORBA.Object o = orb.string_to_object(line);
226: return SessionHelper.narrow(o);
227: }
228: */
229: };
|