1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
|
// $Id: Token_Handler.cpp 91673 2010-09-08 18:49:47Z johnnyw $
#include "ace/Get_Opt.h"
#include "Token_Handler.h"
#if defined (ACE_HAS_TOKENS_LIBRARY)
#include "ace/Signal.h"
int
ACE_Token_Acceptor::parse_args (int argc, ACE_TCHAR *argv[])
{
ACE_TRACE ("ACE_Token_Acceptor::parse_args");
u_short svc_port = ACE_DEFAULT_SERVER_PORT;
ACE_LOG_MSG->open (ACE_TEXT ("Token Service"));
ACE_Get_Opt get_opt (argc, argv, ACE_TEXT ("p:"), 0);
for (int c; (c = get_opt ()) != -1; )
{
switch (c)
{
case 'p':
svc_port = static_cast<u_short> (ACE_OS::atoi (get_opt.opt_arg ()));
break;
default:
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%n:\n[-p server-port]\n"), 1),
-1);
}
}
this->service_addr_.set (svc_port);
return 0;
}
int
ACE_Token_Acceptor::init (int argc, ACE_TCHAR *argv[])
{
ACE_TRACE ("ACE_Token_Acceptor::init");
// Use the options hook to parse the command line arguments and set
// options.
this->parse_args (argc, argv);
// Set the acceptor endpoint into listen mode (use the Singleton
// global Reactor...).
if (this->open (this->service_addr_, ACE_Reactor::instance (),
0, 0, 0,
&this->scheduling_strategy_,
ACE_TEXT ("Token Server"),
ACE_TEXT ("ACE token service")) == -1)
ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%n: %p on port %d\n"),
ACE_TEXT ("acceptor::open failed"),
this->service_addr_.get_port_number ()), -1);
// Ignore SIGPIPE so that each <SVC_HANDLER> can handle this on its
// own.
ACE_Sig_Action sig ((ACE_SignalHandler) SIG_IGN, SIGPIPE);
ACE_UNUSED_ARG (sig);
ACE_INET_Addr server_addr;
if (this->acceptor ().get_local_addr (server_addr) == -1)
ACE_ERROR_RETURN
((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("get_remote_addr")), -1);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("starting up Token Server at port %d on handle %d\n"),
server_addr.get_port_number (),
this->acceptor ().get_handle ()));
return 0;
}
// The following is a "Factory" used by the ACE_Service_Config and
// svc.conf file to dynamically initialize the state of the Naming
// Server.
ACE_SVC_FACTORY_DEFINE (ACE_Token_Acceptor)
// Default constructor.
ACE_Token_Handler::ACE_Token_Handler (ACE_Thread_Manager *tm)
: ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_NULL_SYNCH> (tm),
collection_ (1),
timeout_id_ (0)
{
ACE_TRACE ("ACE_Token_Handler::ACE_Token_Handler");
}
// Create and send a reply to the client.
/* VIRTUAL */ int
ACE_Token_Handler::send_reply (ACE_UINT32 err)
{
ACE_TRACE ("ACE_Token_Handler::send_reply");
void *buf;
size_t len;
ssize_t n;
this->token_reply_.errnum (err);
len = this->token_reply_.encode (buf);
n = this->peer ().send (buf, len);
if (n != (ssize_t) len)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p, expected len = %d, actual len = %d\n"),
ACE_TEXT ("send failed"), len, n), -1);
else
return 0;
}
// Acquire the token.
/* VIRTUAL */ int
ACE_Token_Handler::acquire (ACE_Token_Proxy *proxy)
{
ACE_TRACE ("ACE_Token_Handler::acquire");
#if 0
ACE_DEBUG ((LM_DEBUG, "in acquire for client id = %s\n",
proxy->client_id ()));
#endif /* 0 */
// @@ add notify in token request reply
if (proxy->acquire (0, 0, ACE_Synch_Options::asynch) == -1)
{
if (errno != EWOULDBLOCK)
// bad bad bad
return this->send_reply (errno);
// acquire would block
if (request_options_[ACE_Synch_Options::USE_TIMEOUT] == 1)
{
// check for polling
if ((request_options_.timeout ().sec () == 0) &&
(request_options_.timeout ().usec () == 0))
return this->send_reply (EWOULDBLOCK);
// schedule a timer
this->timeout_id_ = this->reactor ()->schedule_timer
(this, (void *) proxy, request_options_.timeout ());
if (timeout_id_ == -1)
{
ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"),
ACE_TEXT ("schedule_timer")));
return this->send_reply (errno);
}
}
// send no reply. wait until we acquire it or until the timer
// goes off.
return 0;
}
else // success
return this->send_reply (0);
}
// Try to acquire the token. Never block.
/* VIRTUAL */ int
ACE_Token_Handler::try_acquire (ACE_Token_Proxy *proxy)
{
ACE_TRACE ("ACE_Token_Handler::try_acquire");
#if 0
ACE_DEBUG ((LM_DEBUG, "in try_acquire for client id = %s\n",
proxy->client_id ()));
#endif /* 0 */
// @@ add notify in token request reply
if (proxy->tryacquire () == -1)
return this->send_reply (errno);
else
return this->send_reply (0);
}
// Release the token and allow the next client that is waiting to
// proceed.
/* VIRTUAL */ int
ACE_Token_Handler::release (ACE_Token_Proxy *proxy)
{
ACE_TRACE ("ACE_Token_Handler::release");
#if 0
ACE_DEBUG ((LM_DEBUG,
"in release for client id = %s\n",
proxy->client_id ()));
#endif /* 0 */
if (proxy->release (ACE_Synch_Options::asynch) == -1)
// oops, it failed
return this->send_reply (ACE_LOG_MSG->errnum ());
// success
if (this->timeout_id_ != 0)
{
this->reactor ()->cancel_timer (timeout_id_);
this->timeout_id_ = 0;
}
return this->send_reply (0);
}
// Yield the token if any clients are waiting, otherwise keep the
// token.
/* VIRTUAL */ int
ACE_Token_Handler::renew (ACE_Token_Proxy *proxy)
{
ACE_TRACE ("ACE_Token_Handler::renew");
#if 0
ACE_DEBUG ((LM_DEBUG, "in renew for client id = %s\n",
proxy->client_id ()));
#endif /* 0 */
if (proxy->renew (token_request_.requeue_position (),
ACE_Synch_Options::asynch) == -1)
{
int result = ACE_LOG_MSG->errnum ();
if (result != EWOULDBLOCK)
// bad bad bad
return this->send_reply (result);
// acquire would block
if (request_options_[ACE_Synch_Options::USE_TIMEOUT] == 1)
{
this->timeout_id_ = this->reactor ()->schedule_timer
(this, 0, request_options_.timeout ());
if (timeout_id_ == -1)
{
ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"),
ACE_TEXT ("schedule_timer")));
return this->send_reply (ACE_LOG_MSG->errnum ());
}
}
// Send no reply. wait until we acquire it or until the timer
// goes off.
return 0;
}
else
// Success, we still hold the token.
return this->send_reply (0);
}
/* VIRTUAL */ int
ACE_Token_Handler::remove (ACE_Token_Proxy * /* proxy */)
{
ACE_TRACE ("ACE_Token_Handler::remove");
#if 0
ACE_DEBUG ((LM_DEBUG, "in remove for client id = %s\n",
proxy->client_id ()));
#endif /* 0 */
ACE_ERROR
((LM_ERROR,
ACE_TEXT ("sorry: ACE_Token_Handler::remove() is not implemented")));
return this->send_reply (ENOTSUP);
}
// Enable clients to limit the amount of time they'll wait for a
// token.
/* VIRTUAL */ int
ACE_Token_Handler::handle_timeout (const ACE_Time_Value &,
const void *tp)
{
ACE_TRACE ("ACE_Token_Handler::handle_timeout");
this->timeout_id_ = 0;
// @@ add a try acquire here!
// Try to acquire the token, but if we can't get it immediately
// then abandon the wait.
// if (this->try_acquire (&token_entry) == -1)
// return this->abandon (token_entry);
ACE_Token_Proxy *proxy = (ACE_Token_Proxy *) tp;
#if 0
ACE_DEBUG ((LM_DEBUG, "in handle_timeout for client id = %s\n",
proxy->client_id ()));
#endif /* 0 */
// Remove ourselves from the waiter list.
proxy->release ();
this->send_reply (ETIME);
return 0;
}
// Dispatch the appropriate operation to handle the client request.
ACE_Token_Proxy *
ACE_Token_Handler::get_proxy (void)
{
ACE_TRACE ("ACE_Token_Handler::get_proxy");
// See if the proxy already exists in the collection.
ACE_Token_Proxy *proxy = collection_.is_member (token_request_.token_name ());
// If not, create one.
if (proxy == 0)
{
proxy = this->create_proxy ();
// Put the new_proxy in this client_id's collection.
if (collection_.insert (*proxy) == -1)
ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("insert failed\n")), 0);
// Delete our copy (one was created in the collection).
delete proxy;
proxy = collection_.is_member (token_request_.token_name ());
if (proxy == 0)
ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("is_member failed\n")), 0);
// Set the client_id (it was set to 1 since we're
// single-threaded.
proxy->client_id (token_request_.client_id ());
}
return proxy;
}
ACE_Token_Proxy *
ACE_Token_Handler::create_proxy (void)
{
ACE_TRACE ("ACE_Token_Handler::create_proxy");
ACE_Token_Proxy *proxy;
switch (token_request_.token_type ())
{
case ACE_Tokens::RWLOCK:
if (token_request_.proxy_type () == ACE_RW_Token::READER)
ACE_NEW_RETURN (proxy,
ACE_TS_RLock (token_request_.token_name (), this),
0);
else
ACE_NEW_RETURN (proxy,
ACE_TS_WLock (token_request_.token_name (), this),
0);
break;
case ACE_Tokens::MUTEX:
ACE_NEW_RETURN (proxy,
ACE_TS_Mutex (token_request_.token_name (), this),
0);
break;
default:
// Nonexistent token type.
errno = EINVAL;
return 0;
}
// Check for failed new.
if (proxy == 0)
errno = ENOMEM;
return proxy;
}
int
ACE_Token_Handler::dispatch (void)
{
ACE_TRACE ("ACE_Token_Handler::dispatch");
ACE_Token_Proxy *proxy = this->get_proxy ();
if (proxy == 0)
return -1;
// Dispatch the appropriate request.
switch (this->token_request_.operation_type ())
{
case ACE_Token_Request::ACQUIRE:
return this->acquire (proxy);
case ACE_Token_Request::TRY_ACQUIRE:
return this->try_acquire (proxy);
case ACE_Token_Request::RELEASE:
return this->release (proxy);
case ACE_Token_Request::RENEW:
return this->renew (proxy);
case ACE_Token_Request::REMOVE:
return this->remove (proxy);
default:
ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("invalid type = %d\n"),
this->token_request_.operation_type ()), -1);
/* NOTREACHED */
}
}
// Receive, frame, and decode the client's request.
// Note, this method should use non-blocking I/O.
/* VIRTUAL */ int
ACE_Token_Handler::recv_request (void)
{
ACE_TRACE ("ACE_Token_Handler::recv_request");
ssize_t n;
// Read the first 4 bytes to get the length of the message
// This implementation assumes that the first 4 bytes are
// the length of the message.
n = this->peer ().recv ((void *) &this->token_request_,
sizeof (ACE_UINT32));
switch (n)
{
case -1:
/* FALLTHROUGH */
default:
ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p got %d bytes, expected %d bytes\n"),
ACE_TEXT ("recv failed"), n, sizeof (ACE_UINT32)));
/* FALLTHROUGH */
case 0:
// We've shutdown unexpectedly, let's abandon the connection.
this->abandon (0);
return -1;
/* NOTREACHED */
case sizeof (ACE_UINT32):
{
// Transform the length into host byte order.
ssize_t length = this->token_request_.length ();
// Do a sanity check on the length of the message.
if (length > (ssize_t) sizeof this->token_request_)
{
ACE_ERROR ((LM_ERROR, ACE_TEXT ("length %d too long\n"), length));
return this->abandon (1);
}
// Receive the rest of the request message.
// @@ beware of blocking read!!!.
n = this->peer ().recv ((void *) (((char *) &this->token_request_)
+ sizeof (ACE_UINT32)),
length - sizeof (ACE_UINT32));
// Subtract off the size of the part we skipped over...
if (n != (length - (ssize_t) sizeof (ACE_UINT32)))
{
ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p expected %d, got %d\n"),
ACE_TEXT ("invalid length"), length, n));
return this->abandon (1);
}
// Decode the request into host byte order.
if (this->token_request_.decode () == -1)
{
ACE_ERROR
((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("decode failed")));
return this->abandon (1);
}
// if (OS::debug)
this->token_request_.dump ();
}
}
return 0;
}
// Callback method invoked by the ACE_Reactor when
// events arrive from the client.
/* VIRTUAL */ int
ACE_Token_Handler::handle_input (ACE_HANDLE)
{
ACE_TRACE ("ACE_Token_Handler::handle_input");
#if 0
ACE_DEBUG ((LM_DEBUG, "****************** in handle_input\n"));
#endif /* 0 */
if (this->recv_request () == -1)
return -1;
else
return this->dispatch ();
}
void
ACE_Token_Handler::sleep_hook (void)
{
ACE_TRACE ("ACE_Token_Handler::sleep_hook");
// @@ what should we do?
return;
}
void
ACE_Token_Handler::token_acquired (ACE_TPQ_Entry *)
{
ACE_TRACE ("ACE_Token_Handler::token_acquired");
if (this->timeout_id_ != 0)
{
this->reactor ()->cancel_timer (this->timeout_id_);
this->timeout_id_ = 0;
}
this->send_reply (0);
}
int
ACE_Token_Handler::abandon (int send_error)
{
ACE_TRACE ("ACE_Token_Handler::abandon");
// Release ownership or remove us from the waiter list.
if (this->timeout_id_ != 0)
{
this->reactor ()->cancel_timer (timeout_id_);
this->timeout_id_ = 0;
}
// @@ release all tokens
collection_.release ();
if (send_error)
return this->send_reply (EIO);
else
return -1;
}
// ************************************************************
// ************************************************************
// ************************************************************
ACE_TS_Mutex::ACE_TS_Mutex (const ACE_TCHAR *name,
ACE_Token_Handler *th)
: ACE_Local_Mutex (name, 0, 1), // The 1 is debug.
th_ (th)
{
ACE_TRACE ("ACE_TS_Mutex::ACE_TS_Mutex");
}
ACE_TS_Mutex::ACE_TS_Mutex (const ACE_TS_Mutex &m)
: ACE_Local_Mutex (m),
th_ (m.th_)
{
ACE_TRACE ("ACE_TS_Mutex::ACE_TS_Mutex");
this->open (m.name (), m.ignore_deadlock_, m.debug_);
}
void
ACE_TS_Mutex::sleep_hook (void)
{
ACE_TRACE ("ACE_TS_Mutex::sleep_hook");
th_->sleep_hook ();
return;
}
void
ACE_TS_Mutex::token_acquired (ACE_TPQ_Entry *e)
{
ACE_TRACE ("ACE_TS_Mutex::token_acquired");
// Notify the token handler.
th_->token_acquired (e);
return;
}
ACE_Token_Proxy *
ACE_TS_Mutex::clone (void) const
{
ACE_TRACE ("ACE_TS_Mutex::clone");
ACE_Token_Proxy *temp;
ACE_NEW_RETURN (temp, ACE_TS_Mutex (*this), 0);
return temp;
}
// ************************************************************
ACE_TS_RLock::ACE_TS_RLock (const ACE_TCHAR *name,
ACE_Token_Handler *th)
: ACE_Local_RLock (name, 0, 1), // The 1 is debug.
th_ (th)
{
ACE_TRACE ("ACE_TS_RLock::ACE_TS_RLock");
}
ACE_TS_RLock::ACE_TS_RLock (const ACE_TS_RLock &r)
: ACE_Local_RLock (r),
th_ (r.th_)
{
ACE_TRACE ("ACE_TS_RLock::ACE_TS_RLock");
this->open (r.name (), r.ignore_deadlock_, r.debug_);
}
void
ACE_TS_RLock::sleep_hook (void)
{
ACE_TRACE ("ACE_TS_RLock::sleep_hook");
th_->sleep_hook ();
return;
}
void
ACE_TS_RLock::token_acquired (ACE_TPQ_Entry *e)
{
ACE_TRACE ("ACE_TS_RLock::token_acquired");
// Notify the token handler.
th_->token_acquired (e);
return;
}
ACE_Token_Proxy *
ACE_TS_RLock::clone (void) const
{
ACE_TRACE ("ACE_TS_RLock::clone");
ACE_Token_Proxy *temp;
ACE_NEW_RETURN (temp, ACE_TS_RLock (*this), 0);
return temp;
}
// ************************************************************
ACE_TS_WLock::ACE_TS_WLock (const ACE_TCHAR *name,
ACE_Token_Handler *th)
: ACE_Local_WLock (name, 0, 1), // The 1 is debug.
th_ (th)
{
ACE_TRACE ("ACE_TS_WLock::ACE_TS_WLock");
}
ACE_TS_WLock::ACE_TS_WLock (const ACE_TS_WLock &w)
: ACE_Local_WLock (w),
th_ (w.th_)
{
ACE_TRACE ("ACE_TS_WLock::ACE_TS_WLock");
this->open (w.name (), w.ignore_deadlock_, w.debug_);
}
void
ACE_TS_WLock::sleep_hook (void)
{
ACE_TRACE ("ACE_TS_WLock::sleep_hook");
th_->sleep_hook ();
return;
}
void
ACE_TS_WLock::token_acquired (ACE_TPQ_Entry *e)
{
ACE_TRACE ("ACE_TS_WLock::token_acquired");
// Notify the token handler.
th_->token_acquired (e);
return;
}
ACE_Token_Proxy *
ACE_TS_WLock::clone (void) const
{
ACE_TRACE ("ACE_TS_WLock::clone");
ACE_Token_Proxy *temp;
ACE_NEW_RETURN (temp, ACE_TS_WLock (*this), 0);
return temp;
}
#endif /* ACE_HAS_TOKENS_LIBRARY */
|