00001
00002
00003 #include "ace/Remote_Tokens.h"
00004
00005 #if defined (ACE_HAS_TOKENS_LIBRARY)
00006
00007 #include "ace/Singleton.h"
00008
00009 #if !defined (__ACE_INLINE__)
00010 #include "ace/Remote_Tokens.inl"
00011 #endif
00012
00013
00014 ACE_RCSID (ace,
00015 Remote_Tokens,
00016 "$Id: Remote_Tokens.cpp 84455 2009-02-13 13:31:02Z johnnyw $")
00017
00018
00019 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00020 #define ACE_TSS_CONNECTION_MUTEX ACE_Thread_Mutex
00021 #else
00022 #define ACE_TSS_CONNECTION_MUTEX ACE_Null_Mutex
00023 #endif
00024
00025 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00026
00027
00028 typedef ACE_Singleton<ACE_TSS_Connection, ACE_TSS_CONNECTION_MUTEX> ACE_Token_Connections;
00029
00030
00031 ACE_INET_Addr ACE_TSS_Connection::server_address_;
00032
00033
00034
00035 void
00036 ACE_TSS_Connection::set_server_address (const ACE_INET_Addr &server_address)
00037 {
00038 ACE_TRACE ("ACE_TSS_Connection::set_server_address");
00039 server_address_ = server_address;
00040 }
00041
00042
00043 ACE_TSS_Connection::ACE_TSS_Connection (void)
00044 {
00045 ACE_TRACE ("ACE_TSS_Connection::ACE_TSS_Connection");
00046 }
00047
00048 ACE_TSS_Connection::~ACE_TSS_Connection (void)
00049 {
00050 ACE_TRACE ("ACE_TSS_Connection::~ACE_TSS_Connection");
00051 }
00052
00053 ACE_SOCK_Stream *
00054 ACE_TSS_Connection::get_connection (void)
00055 {
00056 return ACE_TSS<ACE_SOCK_Stream>::operator-> ();
00057 }
00058
00059 ACE_SOCK_Stream *
00060 ACE_TSS_Connection::make_TSS_TYPE (void) const
00061 {
00062 ACE_TRACE ("ACE_TSS_Connection::make_TSS_TYPE");
00063
00064 ACE_SOCK_Connector connector;
00065 ACE_SOCK_Stream *stream = 0;
00066
00067 ACE_NEW_RETURN (stream,
00068 ACE_SOCK_Stream,
00069 0);
00070
00071 if (connector.connect (*stream, server_address_) == -1)
00072 {
00073 delete stream;
00074 errno = ECONNREFUSED;
00075 return 0;
00076 }
00077
00078 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("ACE_TSS_Connection new connection\n")));
00079 return stream;
00080 }
00081
00082 ACE_TSS_Connection::operator ACE_SOCK_Stream *(void)
00083 {
00084 return this->get_connection ();
00085 }
00086
00087 void
00088 ACE_TSS_Connection::dump (void) const
00089 {
00090 #if defined (ACE_HAS_DUMP)
00091 ACE_TRACE ("ACE_TSS_Connection::dump");
00092 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00093 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("ACE_TSS_Connection::dump:\n")));
00094 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("server_address_\n")));
00095 server_address_.dump ();
00096 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("base:\n")));
00097 ACE_TSS<ACE_SOCK_Stream>::dump ();
00098 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00099 #endif
00100 }
00101
00102 ACE_Remote_Token_Proxy::ACE_Remote_Token_Proxy (void)
00103 {
00104 ACE_TRACE ("ACE_Remote_Token_Proxy::ACE_Remote_Token_Proxy");
00105 }
00106
00107 ACE_Remote_Token_Proxy::~ACE_Remote_Token_Proxy (void)
00108 {
00109 ACE_TRACE ("ACE_Remote_Token_Proxy::~ACE_Remote_Token_Proxy");
00110 }
00111
00112 int
00113 ACE_Remote_Token_Proxy::open (const ACE_TCHAR *name,
00114 int ignore_deadlock,
00115 int debug)
00116 {
00117 ACE_TRACE ("ACE_Remote_Token_Proxy::open");
00118 ignore_shadow_deadlock_ = ignore_deadlock;
00119 return ACE_Token_Proxy::open (name, 0, debug);
00120 }
00121
00122 void
00123 ACE_Remote_Token_Proxy::set_server_address (const ACE_INET_Addr &server_address)
00124 {
00125 ACE_TRACE ("ACE_Remote_Token_Proxy::set_server_address");
00126 ACE_Token_Connections::instance ()->set_server_address (server_address);
00127 }
00128
00129 int
00130 ACE_Remote_Token_Proxy::initiate_connection (void)
00131 {
00132 ACE_TRACE ("ACE_Remote_Token_Proxy::initiate_connection");
00133 if (token_ == 0)
00134 {
00135 errno = ENOENT;
00136 ACE_ERROR_RETURN ((LM_ERROR,
00137 ACE_TEXT ("ACE_Remote_Token_Proxy not open.\n")), -1);
00138 }
00139
00140 ACE_SOCK_Stream *peer = ACE_Token_Connections::instance ()->get_connection ();
00141 return peer == 0 ? 0 : 1;
00142 }
00143
00144
00145
00146 int
00147 ACE_Remote_Token_Proxy::request_reply (ACE_Token_Request &request,
00148 ACE_Synch_Options &)
00149 {
00150 ACE_TRACE ("ACE_Remote_Token_Proxy::request_reply");
00151 void *buffer;
00152 ssize_t length;
00153
00154 if ((length = request.encode (buffer)) == -1)
00155 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("encode failed")), -1);
00156
00157 ACE_SOCK_Stream *peer = ACE_Token_Connections::instance ()->get_connection ();
00158
00159 if (peer == 0)
00160 ACE_ERROR_RETURN ((LM_ERROR,
00161 ACE_TEXT("(%P|%t) %p\n"),
00162 ACE_TEXT("BIG PROBLEMS with get_connection")), -1);
00163
00164
00165
00166 if (peer->send_n (buffer, length) != length)
00167 ACE_ERROR_RETURN ((LM_ERROR,
00168 ACE_TEXT ("%p\n"),
00169 ACE_TEXT ("send_n failed")), -1);
00170 else
00171 {
00172 ACE_Token_Reply reply;
00173
00174
00175
00176 if (peer->recv (&reply, sizeof reply) != sizeof reply)
00177 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("recv failed")), -1);
00178
00179 if (reply.decode () == -1)
00180 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("decode failed")), -1);
00181
00182 errno = int (reply.errnum ());
00183 if (errno != 0)
00184 return -1;
00185 else
00186 return 0;
00187 }
00188 }
00189
00190 int
00191 ACE_Remote_Token_Proxy::acquire (int notify,
00192 void (*sleep_hook)(void *),
00193 ACE_Synch_Options &options)
00194 {
00195 ACE_TRACE ("ACE_Remote_Token_Proxy::acquire");
00196
00197
00198 if (ACE_Token_Proxy::acquire (notify,
00199 sleep_hook,
00200 ACE_Synch_Options::asynch) == -1)
00201 {
00202
00203 switch (errno)
00204 {
00205 case EWOULDBLOCK :
00206
00207 if (debug_)
00208 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) shadow: acquire will block, owner is %s\n"),
00209 this->token_->owner_id ()));
00210
00211 break;
00212
00213 case EDEADLK :
00214 if (debug_)
00215 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) shadow: deadlock detected\n")));
00216
00217 if (ignore_shadow_deadlock_)
00218 break;
00219 else
00220 {
00221 errno = EDEADLK;
00222 ACE_RETURN (-1);
00223 }
00224
00225 default :
00226 ACE_ERROR_RETURN ((LM_ERROR,
00227 ACE_TEXT ("(%t) %p shadow acquire failed\n"),
00228 ACE_TEXT ("ACE_Remote_Token_Proxy")),
00229 -1);
00230 }
00231 }
00232
00233 ACE_Token_Request request (token_->type (),
00234 this->type (),
00235 ACE_Token_Request::ACQUIRE,
00236 this->name (),
00237 this->client_id (),
00238 options);
00239
00240 request.notify (notify);
00241
00242 int result = this->request_reply (request, options);
00243
00244 if (result == -1)
00245 {
00246
00247 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("error on remote acquire, releasing shadow mutex.\n")));
00248 ACE_Token_Proxy::release ();
00249 }
00250 else
00251 {
00252 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) acquired %s remotely.\n"), this->name ()));
00253
00254
00255
00256
00257
00258 token_->make_owner (waiter_);
00259 }
00260
00261 return result;
00262 }
00263
00264 int
00265 ACE_Remote_Token_Proxy::tryacquire (void (*sleep_hook)(void *))
00266 {
00267 ACE_TRACE ("ACE_Remote_Token_Proxy::tryacquire");
00268
00269
00270
00271 if (ACE_Token_Proxy::tryacquire (sleep_hook) == -1)
00272 {
00273 if (debug_)
00274 {
00275
00276 ACE_Errno_Guard error (errno);
00277 ACE_DEBUG ((LM_DEBUG,
00278 ACE_TEXT ("shadow try acquire failed\n")));
00279 }
00280
00281 return -1;
00282 }
00283
00284 ACE_Token_Request request (token_->type (),
00285 this->type (),
00286 ACE_Token_Request::TRY_ACQUIRE,
00287 this->name (),
00288 this->client_id (),
00289 ACE_Synch_Options::synch);
00290
00291 return this->request_reply (request,
00292 ACE_Synch_Options::synch);
00293 }
00294
00295 int
00296 ACE_Remote_Token_Proxy::renew (int requeue_position,
00297 ACE_Synch_Options &options)
00298 {
00299 ACE_TRACE ("ACE_Remote_Token_Proxy::renew");
00300
00301 if (ACE_Token_Proxy::renew (requeue_position,
00302 ACE_Synch_Options::asynch) == -1)
00303 {
00304
00305 if (errno != EWOULDBLOCK)
00306 return -1;
00307 else if (debug_)
00308 ACE_DEBUG ((LM_DEBUG,
00309 ACE_TEXT ("(%t) shadow: renew would block. owner %s.\n"),
00310 this->token_->owner_id ()));
00311 }
00312
00313 ACE_Token_Request request (token_->type (),
00314 this->type (),
00315 ACE_Token_Request::RENEW,
00316 this->name (),
00317 this->client_id (),
00318 options);
00319
00320 request.requeue_position (requeue_position);
00321
00322 int result = this->request_reply (request, options);
00323
00324 if (result == -1)
00325 {
00326 {
00327
00328 ACE_Errno_Guard error (errno);
00329 ACE_Token_Proxy::release ();
00330 }
00331 ACE_ERROR_RETURN ((LM_ERROR,
00332 ACE_TEXT ("%p error on remote renew, releasing shadow mutex.\n"),
00333 ACE_TEXT ("ACE_Remote_Token_Proxy")), -1);
00334 }
00335 else
00336 {
00337 if (debug_)
00338 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) renewed %s remotely.\n"), this->name ()));
00339
00340 token_->make_owner (waiter_);
00341 return result;
00342 }
00343 }
00344
00345 int
00346 ACE_Remote_Token_Proxy::release (ACE_Synch_Options &options)
00347 {
00348 ACE_TRACE ("ACE_Remote_Token_Proxy::release");
00349
00350 ACE_Token_Request request (token_->type (),
00351 this->type (),
00352 ACE_Token_Request::RELEASE,
00353 this->name (),
00354 this->client_id (),
00355 options);
00356
00357 int result = this->request_reply (request, options);
00358 if (result == 0)
00359 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) released %s remotely.\n"), this->name ()));
00360
00361
00362
00363
00364 if (ACE_Token_Proxy::release () == -1)
00365 ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%t) shadow: release failed\n")));
00366
00367 return result;
00368 }
00369
00370 int
00371 ACE_Remote_Token_Proxy::remove (ACE_Synch_Options &)
00372 {
00373 ACE_TRACE ("ACE_Remote_Token_Proxy::remove");
00374 return 0;
00375 }
00376
00377 void
00378 ACE_Remote_Token_Proxy::token_acquired (ACE_TPQ_Entry *)
00379 {
00380 ACE_TRACE ("ACE_Remote_Token_Proxy::token_acquired");
00381 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) %s shadow token %s acquired\n"),
00382 this->client_id (),
00383 this->name ()));
00384
00385 }
00386
00387 const ACE_TCHAR*
00388 ACE_Remote_Token_Proxy::owner_id (void)
00389 {
00390 ACE_TRACE ("ACE_Remote_Token_Proxy::owner_id");
00391 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("owner_id called\n")));
00392
00393 return 0;
00394 }
00395
00396 void
00397 ACE_Remote_Token_Proxy::dump (void) const
00398 {
00399 #if defined (ACE_HAS_DUMP)
00400 ACE_TRACE ("ACE_Remote_Token_Proxy::dump");
00401 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00402 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("ACE_Tokens::dump:\n")
00403 ACE_TEXT (" ignore_shadow_deadlock_ = %d\n"),
00404 ignore_shadow_deadlock_));
00405 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("base:\n")));
00406 ACE_Token_Proxy::dump ();
00407 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00408 #endif
00409 }
00410
00411 ACE_Token_Proxy *
00412 ACE_Remote_Mutex::clone (void) const
00413 {
00414 ACE_Token_Proxy *temp;
00415 ACE_NEW_RETURN (temp,
00416 ACE_Remote_Mutex (this->name (),
00417 ignore_deadlock_,
00418 debug_),
00419 0);
00420 return temp;
00421 }
00422
00423 ACE_Tokens *
00424 ACE_Remote_Mutex::create_token (const ACE_TCHAR *name)
00425 {
00426 ACE_Tokens *temp;
00427 ACE_NEW_RETURN (temp,
00428 ACE_Mutex_Token (name),
00429 0);
00430 return temp;
00431 }
00432
00433 void
00434 ACE_Remote_Mutex::dump (void) const
00435 {
00436 #if defined (ACE_HAS_DUMP)
00437 ACE_TRACE ("ACE_Remote_Mutex::dump");
00438 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00439 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("ACE_Remote_Mutex::dump:\n")));
00440 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("base:\n")));
00441 ACE_Remote_Token_Proxy::dump ();
00442 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00443 #endif
00444 }
00445
00446 ACE_Tokens *
00447 ACE_Remote_RLock::create_token (const ACE_TCHAR *name)
00448 {
00449 ACE_Tokens *temp = 0;
00450 ACE_NEW_RETURN (temp,
00451 ACE_RW_Token (name),
00452 0);
00453 return temp;
00454 }
00455
00456 int
00457 ACE_Remote_RLock::type (void) const
00458 {
00459 return ACE_RW_Token::READER;
00460 }
00461
00462 ACE_Token_Proxy *
00463 ACE_Remote_RLock::clone (void) const
00464 {
00465 ACE_Token_Proxy *temp = 0;
00466 ACE_NEW_RETURN (temp,
00467 ACE_Remote_RLock (this->name (),
00468 ignore_deadlock_,
00469 debug_),
00470 0);
00471 return temp;
00472 }
00473
00474 void
00475 ACE_Remote_RLock::dump (void) const
00476 {
00477 #if defined (ACE_HAS_DUMP)
00478 ACE_TRACE ("ACE_Remote_RLock::dump");
00479 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00480 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("ACE_Remote_RLock::dump:\n")));
00481 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("base:\n")));
00482 ACE_Remote_Token_Proxy::dump ();
00483 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00484 #endif
00485 }
00486
00487
00488 ACE_Tokens *
00489 ACE_Remote_WLock::create_token (const ACE_TCHAR *name)
00490 {
00491 ACE_Tokens *temp = 0;
00492 ACE_NEW_RETURN (temp,
00493 ACE_RW_Token (name),
00494 0);
00495 return temp;
00496 }
00497
00498 int
00499 ACE_Remote_WLock::type (void) const
00500 {
00501 return ACE_RW_Token::WRITER;
00502 }
00503
00504 ACE_Token_Proxy *
00505 ACE_Remote_WLock::clone (void) const
00506 {
00507 ACE_Token_Proxy *temp = 0;
00508 ACE_NEW_RETURN (temp,
00509 ACE_Remote_WLock (this->name (),
00510 ignore_deadlock_,
00511 debug_),
00512 0);
00513 return temp;
00514 }
00515
00516 void
00517 ACE_Remote_WLock::dump (void) const
00518 {
00519 #if defined (ACE_HAS_DUMP)
00520 ACE_TRACE ("ACE_Remote_WLock::dump");
00521 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00522 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("ACE_Remote_WLock::dump:\n")));
00523 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("base:\n")));
00524 ACE_Remote_Token_Proxy::dump ();
00525 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00526 #endif
00527 }
00528
00529 ACE_END_VERSIONED_NAMESPACE_DECL
00530
00531 #endif