00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <iostream.h>
00024 #include <OB/CORBA.h>
00025 #include <OB/CosNaming.h>
00026 #include "BridgeStub.h"
00027 #include "ObjCtx.h"
00028 #include "SoapBridge.h"
00029 #include "SoapBridge_skel.h"
00030
00032 class SOAPBridge_ContextManager_i
00033 : public POA_SOAPBridge::ContextManager
00034 , public PortableServer::RefCountServantBase {
00035 private:
00036 public:
00037 SOAPBridge_ContextManager_i();
00038 virtual ~SOAPBridge_ContextManager_i();
00039
00040 SOAPBridge::ContextManager::SOAPContext* allocateContext(CORBA::UShort timeoutSeconds, const char* userData)
00041 throw (CORBA::SystemException, SOAPBridge::ContextManager::InvalidTimeout);
00042 SOAPBridge::ContextManager::SOAPContext* queryContextInfo(const char* contextKey)
00043 throw (CORBA::SystemException, SOAPBridge::ContextManager::ContextKeyNotFound);
00044 void freeContext(const char* contextKey)
00045 throw (CORBA::SystemException, SOAPBridge::ContextManager::ContextKeyNotFound);
00046 void setContextUserData(const char* contextKey, const char* userData)
00047 throw (CORBA::SystemException, SOAPBridge::ContextManager::ContextKeyNotFound);
00048 void setContextTimeout(const char* contextKey, CORBA::UShort timeoutSeconds)
00049 throw (CORBA::SystemException, SOAPBridge::ContextManager::InvalidTimeout,
00050 SOAPBridge::ContextManager::ContextKeyNotFound);
00052 void resetTimeout (const char* contextKey)
00053 throw (CORBA::SystemException, SOAPBridge::ContextManager::InvalidTimeout,
00054 SOAPBridge::ContextManager::ContextKeyNotFound)
00055 {
00056 SOAPBridge::ContextManager::SOAPContext_var ctxInfo = queryContextInfo (contextKey);
00057 setContextTimeout (contextKey, ctxInfo->initialTimeout);
00058 };
00059 };
00060
00062 class SOAPBridge_ObjectContextManager_i: public POA_SOAPBridge::ObjectContextManager,
00063 public PortableServer::RefCountServantBase {
00064 private:
00065 public:
00067 void CleanupContexts();
00068 SOAPBridge_ObjectContextManager_i();
00069 virtual ~SOAPBridge_ObjectContextManager_i();
00070
00071
00072 char* storeObjectReference(const char* contextKey, CORBA::Object_ptr objectReference)
00073 throw (CORBA::SystemException, SOAPBridge::ContextManager::ContextKeyNotFound);
00074 CORBA::Object_ptr resolveStoredObjectReference(const char* storedObjectReference)
00075 throw (CORBA::SystemException, SOAPBridge::ObjectContextManager::ObjectReferenceNotFound,
00076 SOAPBridge::ContextManager::ContextKeyNotFound);
00077 void freeStoredObjectReference(const char* storedObjectReference)
00078 throw (CORBA::SystemException, SOAPBridge::ObjectContextManager::ObjectReferenceNotFound,
00079 SOAPBridge::ContextManager::ContextKeyNotFound);
00080 SOAPBridge::ObjectContextManager::SOAPContextSeq* listCurrentContexts()
00081 throw (CORBA::SystemException);
00082 SOAPBridge::ObjectContextManager::StoredIORSeq* listStoredIORs(const char* contextKey)
00083 throw (CORBA::SystemException, SOAPBridge::ContextManager::ContextKeyNotFound);
00084 SOAPBridge::ContextManager::SOAPContext* allocateContext(CORBA::UShort timeoutSeconds, const char* userData)
00085 throw (CORBA::SystemException, SOAPBridge::ContextManager::InvalidTimeout);
00086 SOAPBridge::ContextManager::SOAPContext* queryContextInfo(const char* contextKey)
00087 throw (CORBA::SystemException, SOAPBridge::ContextManager::ContextKeyNotFound);
00088 void freeContext(const char* contextKey)
00089 throw (CORBA::SystemException, SOAPBridge::ContextManager::ContextKeyNotFound);
00090 void setContextUserData(const char* contextKey, const char* userData)
00091 throw (CORBA::SystemException, SOAPBridge::ContextManager::ContextKeyNotFound);
00092 void setContextTimeout(const char* contextKey, CORBA::UShort timeoutSeconds)
00093 throw (CORBA::SystemException, SOAPBridge::ContextManager::InvalidTimeout,
00094 SOAPBridge::ContextManager::ContextKeyNotFound);
00095 };
00096
00098 static SqContextManager *pCtxMan = NULL;
00099
00101 static CORBA::ORB_ptr orb;
00102
00104 static SOAPBridge_ContextManager_i* mySOAPBridge_ContextManager_i;
00106 static SOAPBridge_ObjectContextManager_i* mySOAPBridge_ObjectContextManager_i;
00107
00109 JTCMonitor contextMonitor;
00110
00112 static void testValidTimeout (unsigned short toSec)
00113 {
00114 if (toSec < 60 || toSec > 3600) {
00115 SOAPBridge::ContextManager::InvalidTimeout ito;
00116 ito.minimumTimeout = 60;
00117 ito.maximumTimeout = 3600;
00118 ito.requestedTimeout = toSec;
00119 throw (ito);
00120 }
00121 }
00122
00124 static SqContext *findContext (const char *contextKey)
00125 {
00126 unsigned long key = pCtxMan->DecodeKey (contextKey);
00127 SqContext *pCtx = pCtxMan->FindContext (key);
00128 if (!pCtx) {
00129 SOAPBridge::ContextManager::ContextKeyNotFound cknf;
00130 cknf.requestedContextKey = CORBA::string_dup (contextKey);
00131 throw (cknf);
00132 }
00133 return pCtx;
00134 }
00135
00137 static char *storeObjRef (const char* contextKey, CORBA::Object_ptr objectReference)
00138 {
00139 unsigned long key = pCtxMan->DecodeKey (contextKey);
00140 SqContext *pCtx = pCtxMan->FindContext (key);
00141 if (!pCtx) {
00142 SOAPBridge::ContextManager::ContextKeyNotFound cknf;
00143 cknf.requestedContextKey = CORBA::string_dup (contextKey);
00144 throw (cknf);
00145 }
00146 pCtx->SetLastAct (time (NULL));
00147 CORBA::String_var sior = orb->object_to_string (objectReference);
00148 unsigned long seq = pCtx->getNextObjSeq ();
00149 (void) new SqObjectReference (pCtx, seq, sior);
00150 CString ret = contextKey;
00151 ret += "/";
00152 char ul [20];
00153 sprintf (ul, "%lu", seq);
00154 ret += ul;
00155 return CORBA::string_dup (ret);
00156 }
00157
00159 SOAPBridge_ContextManager_i::SOAPBridge_ContextManager_i()
00160 {
00161 }
00162
00164 SOAPBridge_ContextManager_i::~SOAPBridge_ContextManager_i()
00165 {
00166 pCtxMan->DeleteAllContext ();
00167 cout << "ContextManager has been destroyed." << endl;
00168 }
00169
00171 SOAPBridge::ContextManager::SOAPContext* SOAPBridge_ContextManager_i::allocateContext(CORBA::UShort timeoutSeconds, const char* userData)
00172 throw (CORBA::SystemException, SOAPBridge::ContextManager::InvalidTimeout)
00173 {
00174 JTCSynchronized synchronized (contextMonitor);
00175 testValidTimeout (timeoutSeconds);
00176 SOAPBridge::ContextManager::SOAPContext* pSctx = new SOAPBridge::ContextManager::SOAPContext;
00177 pSctx->contextKey = CORBA::string_dup (pCtxMan->CreateContext (timeoutSeconds, userData));
00178 pSctx->initialTimeout = timeoutSeconds;
00179 pSctx->timeoutRemaining = timeoutSeconds;
00180 pSctx->storedObjectReferenceCount = 0;
00181 pSctx->userData = CORBA::string_dup (userData);
00182 return pSctx;
00183 }
00184
00186 SOAPBridge::ContextManager::SOAPContext* SOAPBridge_ContextManager_i::queryContextInfo(const char* contextKey)
00187 throw (CORBA::SystemException, SOAPBridge::ContextManager::ContextKeyNotFound)
00188 {
00189 JTCSynchronized synchronized (contextMonitor);
00190 SqContext *pCtx = findContext (contextKey);
00191 SOAPBridge::ContextManager::SOAPContext* pSctx = new SOAPBridge::ContextManager::SOAPContext;
00192 pSctx->contextKey = CORBA::string_dup (contextKey);
00193 pSctx->initialTimeout = pCtx->GetTimeout ();
00194 pSctx->timeoutRemaining = pCtx->timeoutRemaining ();
00195 pSctx->storedObjectReferenceCount = pCtx->GetObjectReferenceCount ();
00196 pSctx->userData = CORBA::string_dup (pCtx->GetUserData ());
00197 return pSctx;
00198 }
00199
00201 void SOAPBridge_ContextManager_i::freeContext(const char* contextKey)
00202 throw (CORBA::SystemException, SOAPBridge::ContextManager::ContextKeyNotFound)
00203 {
00204 JTCSynchronized synchronized (contextMonitor);
00205 SqContext *pCtx = findContext (contextKey);
00206 delete pCtx;
00207 }
00208
00210 void SOAPBridge_ContextManager_i::setContextUserData(const char* contextKey, const char* userData)
00211 throw (CORBA::SystemException, SOAPBridge::ContextManager::ContextKeyNotFound)
00212 {
00213 JTCSynchronized synchronized (contextMonitor);
00214 SqContext *pCtx = findContext (contextKey);
00215 pCtx->SetUserData (userData);
00216 pCtx->SetLastAct (time (NULL));
00217 }
00218
00220 void SOAPBridge_ContextManager_i::setContextTimeout(const char* contextKey, CORBA::UShort timeoutSeconds)
00221 throw (CORBA::SystemException, SOAPBridge::ContextManager::InvalidTimeout,
00222 SOAPBridge::ContextManager::ContextKeyNotFound)
00223 {
00224 JTCSynchronized synchronized (contextMonitor);
00225 testValidTimeout (timeoutSeconds);
00226 SqContext *pCtx = findContext (contextKey);
00227 pCtx->SetTimeout (timeoutSeconds);
00228 pCtx->SetLastAct (time (NULL));
00229 }
00230
00232 SOAPBridge_ObjectContextManager_i::SOAPBridge_ObjectContextManager_i()
00233 {
00234 }
00235
00237 SOAPBridge_ObjectContextManager_i::~SOAPBridge_ObjectContextManager_i()
00238 {
00239 }
00240
00251 char* SOAPBridge_ObjectContextManager_i::storeObjectReference(const char* contextKey, CORBA::Object_ptr objectReference)
00252 throw (CORBA::SystemException, SOAPBridge::ContextManager::ContextKeyNotFound)
00253 {
00254 JTCSynchronized synchronized (contextMonitor);
00255
00256 if (pCtxMan->DecodeKey (contextKey) == pCtxMan->DecodeKey ("GL0BAL")) {
00257 SOAPBridge::ContextManager::ContextKeyNotFound cknf;
00258 cknf.requestedContextKey = CORBA::string_dup ("Not allowed in the gl0bal context!");
00259 throw (cknf);
00260 }
00261 return storeObjRef (contextKey, objectReference);
00262 }
00263
00270 CORBA::Object_ptr SOAPBridge_ObjectContextManager_i::resolveStoredObjectReference(const char* storedObjectReference)
00271 throw (CORBA::SystemException, SOAPBridge::ObjectContextManager::ObjectReferenceNotFound,
00272 SOAPBridge::ContextManager::ContextKeyNotFound)
00273 {
00274 JTCSynchronized synchronized (contextMonitor);
00275 SqContext *pCtx = findContext (storedObjectReference);
00276 const char *slash = strrchr (storedObjectReference, '/');
00277 if (!slash) {
00278 SOAPBridge::ObjectContextManager::ObjectReferenceNotFound ornf;
00279 ornf.requestedStoredObjectReference = CORBA::string_dup (storedObjectReference);
00280 throw (ornf);
00281 }
00282 unsigned long seq = strtoul (slash + 1, NULL, 10);
00283 SqObjectReference *pObj = pCtx->FindObjectReference (seq);
00284 if (!pObj) {
00285 SOAPBridge::ObjectContextManager::ObjectReferenceNotFound ornf;
00286 ornf.requestedStoredObjectReference = CORBA::string_dup (storedObjectReference);
00287 throw (ornf);
00288 }
00289 CORBA::Object_ptr obj = orb->string_to_object (pObj->GetIOR ());
00290 return obj;
00291 }
00292
00298 void SOAPBridge_ObjectContextManager_i::freeStoredObjectReference(const char* storedObjectReference)
00299 throw (CORBA::SystemException, SOAPBridge::ObjectContextManager::ObjectReferenceNotFound,
00300 SOAPBridge::ContextManager::ContextKeyNotFound)
00301 {
00302 JTCSynchronized synchronized (contextMonitor);
00303
00304 if (pCtxMan->DecodeKey (storedObjectReference) == pCtxMan->DecodeKey ("GL0BAL")) {
00305 SOAPBridge::ContextManager::ContextKeyNotFound cknf;
00306 cknf.requestedContextKey = CORBA::string_dup ("Not allowed in the gl0bal context!");
00307 throw (cknf);
00308 }
00309 SqContext *pCtx = findContext (storedObjectReference);
00310 const char *slash = strrchr (storedObjectReference, '/');
00311 if (!slash) {
00312 SOAPBridge::ObjectContextManager::ObjectReferenceNotFound ornf;
00313 ornf.requestedStoredObjectReference = CORBA::string_dup (storedObjectReference);
00314 throw (ornf);
00315 }
00316 unsigned long seq = strtoul (slash + 1, NULL, 10);
00317 SqObjectReference *pObj = pCtx->FindObjectReference (seq);
00318 if (!pObj) {
00319 SOAPBridge::ObjectContextManager::ObjectReferenceNotFound ornf;
00320 ornf.requestedStoredObjectReference = CORBA::string_dup (storedObjectReference);
00321 throw (ornf);
00322 }
00323 delete pObj;
00324 }
00325
00327 SOAPBridge::ObjectContextManager::SOAPContextSeq* SOAPBridge_ObjectContextManager_i::listCurrentContexts()
00328 throw (CORBA::SystemException)
00329 {
00330 JTCSynchronized synchronized (contextMonitor);
00331 SOAPBridge::ObjectContextManager::SOAPContextSeq_var pCtxSeq = new SOAPBridge::ObjectContextManager::SOAPContextSeq;
00332 pCtxSeq->length (pCtxMan->GetContextCount ());
00333 int i = 0;
00334 SqContextManager::ContextIterator iter (pCtxMan);
00335 while (++iter) {
00336 pCtxSeq [i].contextKey = CORBA::string_dup (iter->GetContextKey ());
00337 pCtxSeq [i].initialTimeout = iter->GetTimeout ();
00338 pCtxSeq [i].timeoutRemaining = iter->timeoutRemaining ();
00339 pCtxSeq [i].storedObjectReferenceCount = iter->GetObjectReferenceCount ();
00340 pCtxSeq [i].userData = CORBA::string_dup (iter->GetUserData ());
00341 i ++;
00342 }
00343 return pCtxSeq._retn ();
00344 }
00345
00351 SOAPBridge::ObjectContextManager::StoredIORSeq* SOAPBridge_ObjectContextManager_i::listStoredIORs(const char* contextKey)
00352 throw (CORBA::SystemException, SOAPBridge::ContextManager::ContextKeyNotFound)
00353 {
00354 JTCSynchronized synchronized (contextMonitor);
00355 SqContext *pCtx = findContext (contextKey);
00356 SOAPBridge::ObjectContextManager::StoredIORSeq_var pIOR= new SOAPBridge::ObjectContextManager::StoredIORSeq;
00357 pIOR->length (pCtx->GetObjectReferenceCount ());
00358 int i = 0;
00359 SqContext::ObjectReferenceIterator iter (pCtx);
00360 while (++iter) {
00361 CString ret = contextKey;
00362 ret += "/";
00363 char ul [20];
00364 sprintf (ul, "%lu", iter->GetObjSeq ());
00365 ret += ul;
00366 pIOR [i].storedObjectReference = CORBA::string_dup (ret);
00367 pIOR [i].storedObject = CORBA::string_dup (iter->GetIOR ());
00368 i ++;
00369 }
00370 return pIOR._retn ();
00371 }
00372
00374 SOAPBridge::ContextManager::SOAPContext* SOAPBridge_ObjectContextManager_i::allocateContext(CORBA::UShort timeoutSeconds, const char* userData)
00375 throw (CORBA::SystemException, SOAPBridge::ContextManager::InvalidTimeout)
00376 {
00377 return mySOAPBridge_ContextManager_i->allocateContext (timeoutSeconds, userData);
00378 }
00379
00381 SOAPBridge::ContextManager::SOAPContext* SOAPBridge_ObjectContextManager_i::queryContextInfo(const char* contextKey)
00382 throw (CORBA::SystemException, SOAPBridge::ContextManager::ContextKeyNotFound)
00383 {
00384 return mySOAPBridge_ContextManager_i->queryContextInfo (contextKey);
00385 }
00386
00388 void SOAPBridge_ObjectContextManager_i::freeContext(const char* contextKey)
00389 throw (CORBA::SystemException, SOAPBridge::ContextManager::ContextKeyNotFound)
00390 {
00391 mySOAPBridge_ContextManager_i->freeContext (contextKey);
00392 }
00393
00395 void SOAPBridge_ObjectContextManager_i::setContextUserData(const char* contextKey, const char* userData)
00396 throw (CORBA::SystemException, SOAPBridge::ContextManager::ContextKeyNotFound)
00397 {
00398 mySOAPBridge_ContextManager_i->setContextUserData (contextKey, userData);
00399 }
00400
00402 void SOAPBridge_ObjectContextManager_i::setContextTimeout(const char* contextKey, CORBA::UShort timeoutSeconds)
00403 throw (CORBA::SystemException, SOAPBridge::ContextManager::InvalidTimeout,
00404 SOAPBridge::ContextManager::ContextKeyNotFound)
00405 {
00406 mySOAPBridge_ContextManager_i->setContextTimeout (contextKey, timeoutSeconds);
00407 }
00408
00410 void SOAPBridge_ObjectContextManager_i::CleanupContexts()
00411 {
00412 JTCSynchronized synchronized (contextMonitor);
00413 time_t now = time (NULL);
00414 SqContextManager::ContextIterator iter (pCtxMan);
00415 while (++iter) {
00416
00417
00418 if (iter->GetTimeout() && iter->timeoutRemaining (&now) < 1) {
00419 delete iter;
00420 }
00421 }
00422 }
00423
00425 class ContextCleaner : public JTCThread
00426 {
00427 public:
00429 virtual void run ()
00430 {
00431 while (mySOAPBridge_ObjectContextManager_i != NULL) {
00432 mySOAPBridge_ObjectContextManager_i->CleanupContexts ();
00433 this->sleep (1000);
00434 }
00435 };
00436 };
00437
00439 int main (int argn, char *argv [])
00440 {
00441 ANYCFG_H cfgHdl = sbInitialize (argn, argv);
00442 SqContextManager CtxMan;
00443 pCtxMan = &CtxMan;
00444 CORBA_H corbaHdl = initCorba (argn, argv);
00445 orb = (CORBA::ORB_ptr) corbaHdl;
00446 try {
00447 CosNaming::Name name;
00448 CosNaming::NamingContext_var inc;
00449 CosNaming::NamingContext_var myinc;
00450 PortableServer::POA_var poa;
00451 PortableServer::POAManager_var mgr;
00452 try {
00453 CORBA::Object_var nms = orb->resolve_initial_references ("NameService");
00454 inc = CosNaming::NamingContext::_narrow (nms);
00455 assert (!CORBA::is_nil (inc));
00456 name.length (1);
00457 printf ("Binding %s to 'SOAPBridge' naming context\n", argv [0]);
00458 name [0].id = CORBA::string_dup ("SOAPBridge");
00459 name [0].kind = CORBA::string_dup ("");
00460 try {
00461 myinc = inc->bind_new_context (name);
00462 }
00463 catch (const CosNaming::NamingContext::AlreadyBound &) {
00464
00465 CORBA::Object_var ncb = inc->resolve (name);
00466 myinc = CosNaming::NamingContext::_narrow (ncb);
00467 }
00468 catch (...) {
00469 cout << "Fatal error: some exception while calling NameService!" << endl;
00470 }
00471 }
00472 catch (...) {
00473 cout << "Fatal error: could not contact NameService!" << endl;
00474 return -1;
00475 }
00476 try {
00477 CORBA::Object_var obj = orb->resolve_initial_references ("RootPOA");
00478 poa = PortableServer::POA::_narrow (obj);
00479 mgr = poa->the_POAManager ();
00480 mgr->activate ();
00481 }
00482 catch (...) {
00483 cout << "Fatal error: could not activate POAManager!" << endl;
00484 return -1;
00485 }
00486 try {
00487 mySOAPBridge_ContextManager_i = new SOAPBridge_ContextManager_i();
00488 srand (time (NULL));
00489 mySOAPBridge_ObjectContextManager_i = new SOAPBridge_ObjectContextManager_i();
00490 PortableServer::ObjectId_var mySOAPBridge_ContextManager_iid = poa->activate_object(mySOAPBridge_ContextManager_i);
00491 PortableServer::ObjectId_var mySOAPBridge_ObjectContextManager_iid = poa->activate_object(mySOAPBridge_ObjectContextManager_i);
00492
00493 {
00494
00495 CORBA::Object_var ref = mySOAPBridge_ContextManager_i->_this();
00496 CosNaming::Name name;
00497 name.length (1);
00498 name [0].id = CORBA::string_dup ("ContextManager");
00499 name [0].kind = CORBA::string_dup ("");
00500 myinc->rebind (name, ref);
00501 CORBA::String_var sior = storeObjRef ("GL0BAL", ref);
00502 }
00503
00504 {
00505
00506 CORBA::Object_var ref = mySOAPBridge_ObjectContextManager_i->_this();
00507 CosNaming::Name name;
00508 name.length (1);
00509 name [0].id = CORBA::string_dup ("ObjectContextManager");
00510 name [0].kind = CORBA::string_dup ("");
00511 myinc->rebind (name, ref);
00512 CORBA::String_var sior = storeObjRef ("GL0BAL", ref);
00513 }
00514
00515 ContextCleaner *cleaner = new ContextCleaner;
00516 cleaner->start ();
00517 orb->run ();
00518 }
00519 catch (...) {
00520 cout << "Fatal error: could not create client!" << endl;
00521 return -1;
00522 }
00523 }
00524 catch(CORBA::SystemException&) {
00525 cerr << "Caught CORBA::SystemException." << endl;
00526 }
00527 catch(CORBA::Exception&) {
00528 cerr << "Caught CORBA::Exception." << endl;
00529 }
00530 catch(...) {
00531 cerr << "Caught unknown exception." << endl;
00532 }
00533 try {
00534 orb->shutdown (true);
00535 orb->destroy ();
00536 }
00537 catch(...) {
00538 cerr << "Caught unknown exception." << endl;
00539 }
00540
00541 termCorba (corbaHdl);
00542 sbTerminate (cfgHdl);
00543 return 0;
00544 }
00545