1 package demo.concurrency; 2 3 import org.omg.CosConcurrencyControl.*; 4 import org.omg.CosTransactions.*; 5 import org.omg.CosNaming.*; 6 11 public class Client { 12 private static Control ctrl = null; 13 private static TransactionalLockSet lockset = null; 14 private static TransactionFactory tf = null; 15 public static void main( String [] args ){ 16 try { 17 org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null); 20 21 org.omg.PortableServer.POA poa = 22 org.omg.PortableServer.POAHelper.narrow(orb.resolve_initial_references("RootPOA")); 23 24 NamingContextExt nc = NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService")); 27 NameComponent [] name = new NameComponent[1]; 28 name[0] = new NameComponent( "LogicLand", "transaction"); 29 tf = TransactionFactoryHelper.narrow(nc.resolve(name)); 30 31 name[0] = new NameComponent( "LogicLand", "lock"); 32 LockSetFactory ls = LockSetFactoryHelper.narrow( nc.resolve(name) ); 33 34 name[0] = new NameComponent( "LogicLand", "lockset"); 35 lockset = TransactionalLockSetHelper.narrow( nc.resolve(name) ); 36 print_help(); 37 while ( exec_command() ); 38 39 } catch ( Exception e ){ 40 e.printStackTrace(); 41 } 42 } 43 static void print_help(){ 44 System.out.println(" ---------------------------------------------------------------------------"); 45 System.out.println(" LogicLand group, Concurrency service test program"); 46 System.out.println(" ---------------------------------------------------------------------------"); 47 System.out.println(" LockMode: Read - r, Write - w, Upgrade - u, IRead - ir, IWrite iw"); 48 System.out.println(" ---------------------------------------------------------------------------"); 49 System.out.println(" Commands :"); 50 System.out.println(" ---------------------------------------------------------------------------"); 51 System.out.println(" Lock ::= l<LockMode> // Lock resource "); 52 System.out.println(" Try_Lock ::= t<LockMode> // Try lock resource "); 53 System.out.println(" Unlock ::= u<LockMode> // Unlock resource"); 54 System.out.println(" Change ::= c<LockMode><LockMode> // Change lock from->to"); 55 System.out.println(" Start ::= start[=n] // n minuts timeout, default - 5"); 56 System.out.println(" Commit ::= commit // commit transaction "); 57 System.out.println(" Rollback ::= rollback // rollback transaction"); 58 System.out.println(" Print ::= print // print LockSet contents"); 59 System.out.println(" Help ::= help // this screen"); 60 System.out.println(" Quit ::= quit // quit from test"); 61 System.out.println(" ---------------------------------------------------------------------------"); 62 }; 63 static boolean exec_command(){ 64 System.out.print( "Ready ;-)" ); 65 String cmd = InConsole.read(); 66 System.out.println( "Accept:"+cmd ); 67 try { 68 if( cmd.equals("quit") ) { 69 return false; 70 } else if( cmd.equals( "help" ) ) { 71 print_help(); 72 } else if( cmd.equals( "print" ) ) { 73 } else if( cmd.equals( "commit" ) ) { 75 if( ctrl == null ) { 76 System.out.println( "Error: Transaction not active" ); 77 } else { 78 try { 79 ctrl.get_terminator().commit( false ); 80 System.out.println( "Commit complete" ); 81 } catch ( org.omg.CORBA.OBJECT_NOT_EXIST e ){ 82 System.out.println( "Error: Transaction finnished" ); 83 } 84 ctrl = null; 85 } 86 } else if( cmd.equals( "rollback" ) ) { 87 if( ctrl == null ) { 88 System.out.println( "Error: Transaction not active" ); 89 } else { 90 try { 91 ctrl.get_terminator().rollback(); 92 System.out.println( "Rollback complete" ); 93 } catch ( org.omg.CORBA.OBJECT_NOT_EXIST e ){ 94 System.out.println( "Error: Transaction finnished" ); 95 } 96 ctrl = null; 97 } 98 } else if( cmd.length() >= 5 && cmd.substring(0,5).equals( "start" ) ) { 99 if( ctrl != null ){ 100 System.out.println("Error: Transaction is active"); 101 } else { 102 int minute = 5; 103 try { 104 minute = Integer.parseInt( cmd.substring(6) ); 105 } catch ( Exception e ){ 106 e.printStackTrace(); 107 System.out.println( "Use default 5 min." ); 108 } 109 ctrl = tf.create( minute*60 ); 110 System.out.println( "Transaction started with "+minute+"min timeout" ); 111 } 112 } else { 113 if( ctrl == null ){ 114 System.out.println("Error: Transaction not active"); 115 return true; 116 } 117 try { 118 if( cmd.length() >= 2 && cmd.substring(0,1).equals( "l" ) ) { 119 lock_mode mode = parse_mode( cmd.substring(1) ); 120 lockset.lock( ctrl.get_coordinator(), mode ); 121 System.out.println( "Lock complete" ); 122 } else if( cmd.length() >=2 && cmd.substring(0,1).equals( "t" ) ) { 123 lock_mode mode = parse_mode( cmd.substring(1) ); 124 if ( lockset.try_lock( ctrl.get_coordinator(), mode ) ) { 125 System.out.println( "Lock complete" ); 126 } else { 127 System.out.println( "Lock uncomplete" ); 128 } 129 } else if( cmd.length() >=2 && cmd.substring(0,1).equals( "u" ) ) { 130 lock_mode mode = parse_mode( cmd.substring(1) ); 131 lockset.unlock( ctrl.get_coordinator(), mode ); 132 System.out.println( "Unlock complete" ); 133 } else if( cmd.length() >=3 && cmd.substring(0,1).equals( "c" ) ) { 134 lock_mode mode = parse_mode( cmd.substring(1) ); 135 lock_mode mode1; 136 if( mode.equals(lock_mode.intention_read) || mode.equals(lock_mode.intention_write) ){ 137 mode1 = parse_mode( cmd.substring(3) ); 138 } else { 139 mode1 = parse_mode( cmd.substring(2) ); 140 } 141 lockset.change_mode( ctrl.get_coordinator(), mode, mode1 ); 142 System.out.println( "Change mode complete" ); 143 } 144 } catch ( org.omg.CORBA.TRANSACTION_ROLLEDBACK e ) { 145 System.out.println( "Error: Transaction RolledBack" ); 146 ctrl = null; 147 } catch ( org.omg.CORBA.INVALID_TRANSACTION e ) { 148 System.out.println( "Error: Transaction invalid" ); 149 ctrl = null; 150 } catch ( org.omg.CORBA.OBJECT_NOT_EXIST e ) { 151 System.out.println( "Error: Transaction finnished" ); 152 ctrl = null; 153 } 154 } 155 } catch ( Exception e ) { 156 e.printStackTrace(); 157 } 158 return true; 159 } 160 static lock_mode parse_mode( String mode ){ 161 if( mode.substring(0,1).equals( "r" ) ){ 162 return lock_mode.read; 163 } else if( mode.substring(0,1).equals( "w" ) ) { 164 return lock_mode.write; 165 } else if( mode.substring(0,1).equals( "u" ) ) { 166 return lock_mode.upgrade; 167 } else if( mode.substring(0,2).equals( "ir" ) ) { 168 return lock_mode.intention_read; 169 } else if( mode.substring(0,2).equals( "iw" ) ) { 170 return lock_mode.intention_write; 171 } 172 return lock_mode.read; 173 }; 174 191 }; 192 193 | Popular Tags |