Debian Codegraph @

netsvcs/clients/Tokens/manual/manual.cpp

raw symbols SCIP
1
2 //=============================================================================
3 /**
4 * @file manual.cpp
5 *
6 * Allows manual operations on local and remote tokens.
7 *
8 * @author Tim Harrison
9 */
10 //=============================================================================
11
12
13 #include "ace/Get_Opt.h"
14 #include "ace/Local_Tokens.h"
15 #include "ace/Remote_Tokens.h"
16 #include "ace/Singleton.h"
17 #include "ace/Thread_Manager.h"
18 #include "ace/Token_Invariants.h"
19 #include "ace/Token_Collection.h"
20 #include "ace/Map_Manager.h"
21 #include "ace/Service_Config.h"
22
23 #if defined (ACE_HAS_THREADS) && defined (ACE_HAS_THREADS_LIBRARY)
24
25
26 typedef ACE_Token_Invariant_Manager ACE_TOKEN_INVARIANTS;
27
28 /**
29 * @class STDIN_Token
30 *
31 * @brief STDIN Token
32 *
33 * Translates STDIN commands to ACE Token commands.
34 */
35 class STDIN_Token : public ACE_Event_Handler
36 {
37 public:
38 /// Construction.
39 STDIN_Token ();
40
41 /// Parse command-line arguments.
42 int parse_args (int argc, ACE_TCHAR *argv[]);
43
44 //FUZZ: disable check_for_lack_ACE_OS
45 /// Register with whatever event dispatcher is needed and run.
46 ///FUZZ: enable check_for_lack_ACE_OS
47 int open (int argc, char *argv[]);
48
49 // = Event_Handler methods.
50 int handle_input (ACE_HANDLE);
51 int handle_exception (ACE_HANDLE);
52
53 typedef ACE_CString TID;
54
55 private:
56 /// Display options.
57 void display_menu ();
58
59 /// Get or make a proxy to <token> with a <tid> client id.
60 ACE_Token_Proxy *get_proxy (const char *tid, const char *token, char type);
61
62 /// Create a proxy to <token> with a <tid> client id.
63 ACE_Token_Proxy *create_proxy (const char *token, char type);
64
65 // = Mapping from tid to Token_Collection.
66 /// COLLECTION maintains a mapping from tid to a collection.
67 typedef ACE_Map_Manager<TID, ACE_Token_Collection *, ACE_Null_Mutex>
68 COLLECTIONS;
69
70 /// Allows iterations through collections_.
71 typedef ACE_Map_Iterator<TID, ACE_Token_Collection *, ACE_Null_Mutex>
72 COLLECTIONS_ITERATOR;
73
74 /// Allows iterations through collections_.
75 typedef ACE_Map_Entry<TID, ACE_Token_Collection *>
76 COLLECTIONS_ENTRY;
77
78 /// A collection for each <tid>.
79 COLLECTIONS collections_;
80
81 const char *server_host_;
82 int server_port_;
83 int ignore_deadlock_;
84 int debug_;
85 int remote_;
86 };
87
88 STDIN_Token::STDIN_Token ()
89 : server_host_ (ACE_DEFAULT_SERVER_HOST),
90 server_port_ (ACE_DEFAULT_SERVER_PORT),
91 ignore_deadlock_ (0),
92 debug_ (0),
93 remote_ (0)
94 {
95 }
96
97 int
98 STDIN_Token::parse_args (int argc, ACE_TCHAR *argv[])
99 {
100 ACE_LOG_MSG->open (argv[0], ACE_Log_Msg::STDERR);
101
102 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("h:p:diu"), 1);
103
104 for (int c; (c = get_opt ()) != -1; )
105 {
106 switch (c)
107 {
108 case 'h': // specify the host machine on which the server is running
109 server_host_ = get_opt.opt_arg ();
110 remote_ = 1;
111 break;
112 case 'p': // specify the port on which the server is running
113 server_port_ = ACE_OS::atoi (get_opt.opt_arg ());
114 remote_ = 1;
115 break;
116 case 'd':
117 debug_ = 1;
118 break;
119 case 'i':
120 ignore_deadlock_ = 1;
121 break;
122 case 'u':
123 // usage same as unknown
124 ACE_FALLTHROUGH;
125 default:
126 ACE_ERROR_RETURN ((LM_ERROR,
127 "%n:\n"
128 "[-h <remote host>]\n"
129 "[-p <remote port>]\n"
130 "[-i ignore deadlock]\n"
131 "[-d debug]\n", 1), -1);
132 }
133 }
134
135 if (remote_)
136 ACE_Remote_Mutex::set_server_address (ACE_INET_Addr (server_port_,
137 server_host_));
138
139 return 0;
140 }
141
142 int
143 STDIN_Token::open (int argc, char *argv[])
144 {
145 if (this->parse_args (argc, argv) == -1)
146 return -1;
147
148 // Register for signals.
149 if (ACE_Reactor::instance ()->register_handler
150 (SIGINT, this) == -1)
151 ACE_DEBUG ((LM_DEBUG, "Can't register signal handler\n"));
152
153 #if defined (ACE_WIN32)
154
155 #else
156 // Register for STDIN events with Reactor.
157 if (ACE_Reactor::instance ()->register_handler
158 (ACE_STDIN, this, ACE_Event_Handler::READ_MASK) == -1)
159 ACE_ERROR_RETURN ((LM_DEBUG, "Can't register signal handler\n"), 0);
160
161
162 #endif /* ACE_WIN32 */
163
164
165 this->display_menu ();
166
167 #if defined (ACE_WIN32)
168
169 #else
170 ACE_Reactor::run_event_loop ();
171 #endif /* ACE_WIN32 */
172
173 ACE_OS::printf ("Exiting...\n");
174 return 0;
175 }
176
177 int
178 STDIN_Token::handle_input (ACE_HANDLE fd)
179 {
180 ACE_UNUSED_ARG (fd);
181
182 char tid[BUFSIZ];
183 char token[BUFSIZ];
184 char type[16];
185 char operation[16];
186
187 if (::scanf ("%s %s %s %s", tid, token, type, operation) <= 0)
188 {
189 ACE_OS::printf ("Try again.\n");
190 return 0;
191 }
192
193 ACE_Token_Proxy *proxy =
194 this->get_proxy (tid, token, type[0]);
195
196 if (proxy == 0)
197 return -1;
198
199 switch (operation[0])
200 {
201 case 'a':
202 case 'A':
203 if (proxy->acquire () == 0)
204 {
205 ACE_OS::printf ("Succeeded.\n");
206 if (ACE_TOKEN_INVARIANTS::instance ()->acquired (proxy) == 0)
207 ACE_OS::printf ("Violated invariant.\n");
208 }
209 else
210 ACE_ERROR ((LM_ERROR, "%p.\n", "Acquire failed"));
211 break;
212 case 'n':
213 case 'N':
214 ACE_TOKEN_INVARIANTS::instance ()->releasing (proxy);
215 if (proxy->renew () == 0)
216 {
217 ACE_OS::printf ("Succeeded.\n");
218 if (ACE_TOKEN_INVARIANTS::instance ()->acquired (proxy) == 0)
219 ACE_OS::printf ("Violated invariant.\n");
220 }
221 else
222 ACE_ERROR ((LM_ERROR, "%p.\n", "Renew failed"));
223 break;
224
225 case 'r':
226 case 'R':
227 ACE_TOKEN_INVARIANTS::instance ()->releasing (proxy);
228 if (proxy->release () == 0)
229 ACE_OS::printf ("Succeeded.\n");
230 else
231 ACE_ERROR ((LM_ERROR, "%p.\n", "Release failed"));
232 break;
233
234 case 't':
235 case 'T':
236 if (proxy->tryacquire () == 0)
237 {
238 ACE_OS::printf ("Succeeded.\n");
239 if (ACE_TOKEN_INVARIANTS::instance ()->acquired (proxy) == 0)
240 ACE_OS::printf ("Violated invariant.\n");
241 }
242 else
243 ACE_ERROR ((LM_ERROR, "%p.\n", "Tryacquire failed"));
244 break;
245 }
246
247 this->display_menu ();
248 return 0;
249 }
250
251 void
252 STDIN_Token::display_menu ()
253 {
254 ACE_OS::printf ("<tid> <token> <type> <operation>\n");
255 }
256
257 int
258 STDIN_Token::handle_exception (ACE_HANDLE fd)
259 {
260 ACE_UNUSED_ARG (fd);
261
262 ACE_Reactor::run_event_loop ();
263 return -1;
264 }
265
266 ACE_Token_Proxy *
267 STDIN_Token::get_proxy (const char *_tid, const char *token, char type)
268 {
269 ACE_Token_Collection *proxy_collection;
270
271 TID tid (_tid);
272
273 if (collections_.find (tid, proxy_collection) == -1)
274 // We did not find a proxy_collection.
275 {
276 // Make one.
277 proxy_collection = new ACE_Token_Collection (debug_, "no name collection");
278
279 // Put it in the collections.
280 if (collections_.bind (tid, proxy_collection) == -1)
281 {
282 delete proxy_collection;
283 return 0;
284 }
285 }
286
287 // Either way, we have a proxy_collection now.
288
289 // See if the proxy already exists in the collection.
290 ACE_Token_Proxy *proxy = proxy_collection->is_member (token);
291
292 // If not, create one.
293 if (proxy == 0)
294 {
295 proxy = this->create_proxy (token, type);
296
297 // Put the new_proxy in this tid's collection.
298 if (proxy_collection->insert (*proxy) == -1)
299 ACE_ERROR_RETURN ((LM_ERROR, "insert failed\n"), 0);
300
301 // Delete our copy (one was created in the collection).
302 delete proxy;
303 proxy = proxy_collection->is_member (token);
304
305 if (proxy == 0)
306 ACE_ERROR_RETURN ((LM_ERROR, "is_member failed\n"), 0);
307
308 // Set the client_id (it was set to 1 since we're
309 // single-threaded.
310 proxy->client_id (_tid);
311 }
312
313 return proxy;
314 }
315
316 ACE_Token_Proxy *
317 STDIN_Token::create_proxy (const char *token, char type)
318 {
319 switch (type)
320 {
321 case 'm':
322 case 'M':
323 if (remote_)
324 return new ACE_Remote_Mutex (token, ignore_deadlock_, debug_);
325 else
326 return new ACE_Local_Mutex (token, ignore_deadlock_, debug_);
327
328 case 'r':
329 case 'R':
330 if (remote_)
331 return new ACE_Remote_RLock (token, ignore_deadlock_, debug_);
332 else
333 return new ACE_Local_RLock (token, ignore_deadlock_, debug_);
334
335 case 'w':
336 case 'W':
337 if (remote_)
338 return new ACE_Remote_WLock (token, ignore_deadlock_, debug_);
339 else
340 return new ACE_Local_WLock (token, ignore_deadlock_, debug_);
341 }
342
343 // should never get here, but this avoids a compiler warning . . .
344 return 0;
345 }
346
347 int
348 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
349 {
350 STDIN_Token st;
351 return st.open (argc, argv);
352 }
353
354 #else
355 int
356 ACE_TMAIN(int, ACE_TCHAR *[])
357 {
358 ACE_ERROR_RETURN ((LM_ERROR,
359 "threads or ACE_HAS_TOKENS_LIBRARY not supported on this platform\n"), -1);
360 }
361 #endif /* ACE_HAS_THREADS && ACE_HAS_TOKENS_LIBRARY */