56 /* |
56 /* |
57 * On initialization, export remote objects and register |
57 * On initialization, export remote objects and register |
58 * them with server. |
58 * them with server. |
59 */ |
59 */ |
60 public void run() { |
60 public void run() { |
61 try { |
61 try { |
62 int i = 0; |
62 int i = 0; |
63 |
63 |
64 /* |
64 /* |
65 * Locate apple user object in registry. The lookup will |
65 * Locate apple user object in registry. The lookup will |
66 * occur until it is successful or fails LOOKUP_ATTEMPTS times. |
66 * occur until it is successful or fails LOOKUP_ATTEMPTS times. |
67 * These repeated attempts allow the ApplicationServer |
67 * These repeated attempts allow the ApplicationServer |
68 * to be started before the AppleUserImpl. |
68 * to be started before the AppleUserImpl. |
69 */ |
69 */ |
70 Exception exc = null; |
70 Exception exc = null; |
71 for (i = 0; i < LOOKUP_ATTEMPTS; i++) { |
71 for (i = 0; i < LOOKUP_ATTEMPTS; i++) { |
72 try { |
72 try { |
73 Registry registry = LocateRegistry.getRegistry( |
73 Registry registry = LocateRegistry.getRegistry( |
74 registryHost, 2006); |
74 registryHost, 2006); |
75 user = (AppleUser) registry.lookup("AppleUser"); |
75 user = (AppleUser) registry.lookup("AppleUser"); |
76 user.startTest(); |
76 user.startTest(); |
77 break; //successfully obtained AppleUser |
77 break; //successfully obtained AppleUser |
78 } catch (Exception e) { |
78 } catch (Exception e) { |
79 exc = e; |
79 exc = e; |
80 Thread.sleep(10000); //sleep 10 seconds and try again |
80 Thread.sleep(10000); //sleep 10 seconds and try again |
81 } |
81 } |
82 } |
82 } |
83 if (user == null) { |
83 if (user == null) { |
84 logger.log(Level.SEVERE, "Failed to lookup AppleUser:", exc); |
84 logger.log(Level.SEVERE, "Failed to lookup AppleUser:", exc); |
85 return; |
85 return; |
86 } |
86 } |
87 |
87 |
88 /* |
88 /* |
89 * Create and export apple implementations. |
89 * Create and export apple implementations. |
90 */ |
90 */ |
91 try { |
91 try { |
92 for (i = 0; i < numApples; i++) { |
92 for (i = 0; i < numApples; i++) { |
93 apples[i] = new AppleImpl("AppleImpl #" + (i + 1)); |
93 apples[i] = new AppleImpl("AppleImpl #" + (i + 1)); |
94 } |
94 } |
95 } catch (RemoteException e) { |
95 } catch (RemoteException e) { |
96 logger.log(Level.SEVERE, |
96 logger.log(Level.SEVERE, |
97 "Failed to create AppleImpl #" + (i + 1) + ":", e); |
97 "Failed to create AppleImpl #" + (i + 1) + ":", e); |
98 user.reportException(e); |
98 user.reportException(e); |
99 return; |
99 return; |
100 } |
100 } |
101 |
101 |
102 /* |
102 /* |
103 * Hand apple objects to apple user. |
103 * Hand apple objects to apple user. |
104 */ |
104 */ |
105 try { |
105 try { |
106 for (i = 0; i < numApples; i++) { |
106 for (i = 0; i < numApples; i++) { |
107 user.useApple(apples[i]); |
107 user.useApple(apples[i]); |
108 } |
108 } |
109 } catch (RemoteException e) { |
109 } catch (RemoteException e) { |
110 logger.log(Level.SEVERE, |
110 logger.log(Level.SEVERE, |
111 "Failed to register callbacks for " + apples[i] + ":", e); |
111 "Failed to register callbacks for " + apples[i] + ":", e); |
112 user.reportException(e); |
112 user.reportException(e); |
113 return; |
113 return; |
114 } |
114 } |
115 } catch (Exception e) { |
115 } catch (Exception e) { |
116 logger.log(Level.SEVERE, "Unexpected exception:", e); |
116 logger.log(Level.SEVERE, "Unexpected exception:", e); |
117 } |
117 } |
118 } |
118 } |
119 |
119 |
120 private static void usage() { |
120 private static void usage() { |
121 System.err.println("Usage: ApplicationServer [-numApples <numApples>]"); |
121 System.err.println("Usage: ApplicationServer [-numApples <numApples>]"); |
122 System.err.println(" [-registryHost <host>]"); |
122 System.err.println(" [-registryHost <host>]"); |
123 System.err.println(" numApples The number of apples (threads) to use."); |
123 System.err.println(" numApples The number of apples (threads) to use."); |
124 System.err.println(" The default is 10 apples."); |
124 System.err.println(" The default is 10 apples."); |
125 System.err.println(" host The host running rmiregistry " + |
125 System.err.println(" host The host running rmiregistry " + |
126 "which contains AppleUser."); |
126 "which contains AppleUser."); |
127 System.err.println(" The default is \"localhost\"."); |
127 System.err.println(" The default is \"localhost\"."); |
128 System.err.println(); |
128 System.err.println(); |
129 } |
129 } |
130 |
130 |
131 public static void main(String[] args) { |
131 public static void main(String[] args) { |
132 int num = DEFAULT_NUMAPPLES; |
132 int num = DEFAULT_NUMAPPLES; |
133 String host = DEFAULT_REGISTRYHOST; |
133 String host = DEFAULT_REGISTRYHOST; |
134 |
134 |
135 // parse command line args |
135 // parse command line args |
136 try { |
136 try { |
137 for (int i = 0; i < args.length ; i++ ) { |
137 for (int i = 0; i < args.length ; i++ ) { |
138 String arg = args[i]; |
138 String arg = args[i]; |
139 if (arg.equals("-numApples")) { |
139 if (arg.equals("-numApples")) { |
140 i++; |
140 i++; |
141 num = Integer.parseInt(args[i]); |
141 num = Integer.parseInt(args[i]); |
142 } else if (arg.equals("-registryHost")) { |
142 } else if (arg.equals("-registryHost")) { |
143 i++; |
143 i++; |
144 host = args[i]; |
144 host = args[i]; |
145 } else { |
145 } else { |
146 usage(); |
146 usage(); |
147 } |
147 } |
148 } |
148 } |
149 } catch (Throwable t) { |
149 } catch (Throwable t) { |
150 usage(); |
150 usage(); |
151 throw new RuntimeException("TEST FAILED: Bad argument"); |
151 throw new RuntimeException("TEST FAILED: Bad argument"); |
152 } |
152 } |
153 |
153 |
154 // start the client server |
154 // start the client server |
155 Thread server = new Thread(new ApplicationServer(num,host)); |
155 Thread server = new Thread(new ApplicationServer(num,host)); |
156 server.start(); |
156 server.start(); |
157 // main should exit once all exported remote objects are gc'd |
157 // main should exit once all exported remote objects are gc'd |
158 } |
158 } |
159 } |
159 } |