1 25 26 package lb; 27 28 import java.rmi.RemoteException; 29 30 import javax.naming.Context; 31 import javax.naming.InitialContext; 32 import javax.naming.NamingException; 33 34 38 public class Client { 39 40 static Context initialContext = null; 41 42 static ManagerHome home = null; 43 44 static Manager manager = null; 45 46 static boolean m_noinit = false; 47 48 static int m_accounts = 4; 49 50 static int m_valmove = 100; 51 52 static int m_loops = 10; 53 54 static int inival = 1000; 55 56 public static void main(String[] args) { 57 58 for (int argn = 0; argn < args.length; argn++) { 60 String s_arg = args[argn]; 61 Integer i_arg; 62 if (s_arg.equals("-ni")) { 63 m_noinit = true; 64 } else if (s_arg.equals("-a")) { 65 s_arg = args[++argn]; 66 i_arg = java.lang.Integer.valueOf(s_arg); 67 m_accounts = i_arg.intValue(); 68 } else if (s_arg.equals("-m")) { 69 s_arg = args[++argn]; 70 i_arg = java.lang.Integer.valueOf(s_arg); 71 m_valmove = i_arg.intValue(); 72 } else if (s_arg.equals("-l")) { 73 s_arg = args[++argn]; 74 i_arg = java.lang.Integer.valueOf(s_arg); 75 m_loops = i_arg.intValue(); 76 } else { 77 usage(); 78 fatalError("wrong usage"); 79 } 80 } 81 82 info("Calling lb.Client with : -a " + m_accounts + " -m " + m_valmove + " -l " + m_loops); 83 84 try { 86 initialContext = new InitialContext(); 87 } catch (NamingException e) { 88 fatalError("Cannot get InitialContext: " + e); 89 } 90 91 try { 93 home = (ManagerHome) javax.rmi.PortableRemoteObject.narrow(initialContext.lookup("ManagerHome"), 94 ManagerHome.class); 95 manager = home.create(inival); 96 } catch (Exception e) { 97 fatalError("Cannot create manager: " + e); 98 } 99 100 if (!m_noinit) { 102 info("Re-initialization of the accounts database table"); 103 try { 104 manager.createAll(m_accounts); 105 } catch (Exception e) { 106 fatalError("Cannot create initial accounts: " + e); 107 } 108 } 109 110 try { 112 manager.setValue(m_valmove); 113 } catch (Exception e) { 114 fatalError("Cannot init Session: " + e); 115 } 116 117 try { 119 for (int i = 0; i < m_loops; i++) { 120 int d1 = random(m_accounts); 122 int c1 = random(m_accounts); 123 info(" Movement " + d1 + " -> " + c1 + ""); 124 manager.setAccounts(d1, c1); 126 try { 128 manager.movement(); 129 } catch (RemoteException r) { 130 error("movement raised exception. ignoring..."); 131 } 132 if (manager.checkAccount(d1) == false) { 134 error("Bad Account after move on account " + d1); 135 error("Stopping this session because some accounts are not ok"); 136 break; 137 } 138 } 139 } catch (Exception e) { 140 fatalError("Exception in main loop :" + e); 141 } 142 143 try { 145 if (manager.checkAll() == false) { 146 error("FAIL when checking all accounts"); 147 } else { 148 info("PASS when checking all accounts"); 149 } 150 } catch (RemoteException e) { 151 error("checkAll() :" + e); 152 } 153 154 try { 156 manager.remove(); 157 } catch (Exception e) { 158 error("Cannot remove manager session: " + e); 159 } 160 161 } 162 163 166 static int random(int max) { 167 168 double d = Math.random(); 169 int ret = (int) (max * d); 170 return ret; 171 } 172 173 176 static void usage() { 177 info("lb.Client [-ni] [-a accounts] [-m value] [-l loops]"); 178 } 179 180 183 static void info(String infoMsg) { 184 System.out.println(infoMsg); 185 } 186 187 190 static void error(String errMsg) { 191 System.out.println("lb.Client error: " + errMsg); 192 } 193 194 197 static void fatalError(String errMsg) { 198 System.out.println("lb.Client fatal error: " + errMsg); 199 System.exit(2); 200 } 201 202 } 203 204 | Popular Tags |