00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef _MPCL_NET_CORBA_ORB__
00027 #define _MPCL_NET_CORBA_ORB__
00028
00029 #include <algorithm>
00030 #include <list>
00031 #include "../../text/string.hh"
00032 #include CORBA_H
00033
00034
00036 namespace mpcl
00037 {
00038
00040 namespace net
00041 {
00042
00044 namespace corba
00045 {
00046
00047 using text::TString;
00048
00050 class TOrb
00051 {
00052
00053 private:
00054
00059 bool gIsColocated;
00060
00062 bool gHasNameService;
00063
00065 CORBA::ORB_var qtOrb;
00066
00068 PortableServer::POA_var qtPoa;
00069
00071 PortableServer::POAManager_var qtPoaManager;
00072
00074 CosNaming::NamingContext_var qtRootNamingContext;
00075
00076
00077 private:
00078
00079
00080
00081
00082
00091 CosNaming::NamingContext_var bindContext ( CosNaming::NamingContext_var& rqtNAMING_CONTEXT ,
00092 const TString& rkyCONTEXT_NAME );
00093
00101 CosNaming::NamingContext_var bindContext (const TString& rkyCONTEXT_NAME)
00102 {
00103 return bindContext (qtRootNamingContext, rkyCONTEXT_NAME);
00104 }
00105
00106
00107 public:
00108
00109
00110
00111
00112
00113 TOrb (int iPARAMETER_COUNT, const char** ppkcPARAMETERS);
00114
00115 ~TOrb (void)
00116 {
00117 qtOrb->destroy();
00118 }
00119
00125 template <typename TObject, typename TObjectReference>
00126 TObjectReference enrol (TObject* ptOBJECT)
00127 {
00128 TObjectReference qtObject;
00129
00130 qtPoa->activate_object (ptOBJECT);
00131 qtObject = ptOBJECT->_this();
00132 ptOBJECT->_remove_ref();
00133 return qtObject;
00134 }
00135
00143 template <typename TObject, typename TObjectReference>
00144 TObjectReference enrol (TObject* ptOBJECT, const TString& rkyCOMPOUND_NAME)
00145 {
00146
00147
00148
00149 typedef
00150 std::list<TString>
00151 TStringList;
00152
00153 TStringList::const_iterator I;
00154 TObjectReference qtObject;
00155 TStringList tContextList;
00156 CosNaming::Name tObjectName;
00157 CosNaming::NamingContext_var qtCurrentContext;
00158 CosNaming::NamingContext_var qtNewContext;
00159 TString yObjectId;
00160 TString yObjectIdAndKind;
00161 TString yObjectKind;
00162 TString::size_type zDotPosition;
00163
00164
00165 qtObject = enrol<TObject, TObjectReference> (ptOBJECT);
00166
00167 Split ( rkyCOMPOUND_NAME.begin() ,
00168 rkyCOMPOUND_NAME.end() ,
00169 '/' ,
00170 std::back_insert_iterator<TStringList> (tContextList) );
00171 qtCurrentContext = qtRootNamingContext;
00172 yObjectIdAndKind = tContextList.back();
00173 tContextList.pop_back();
00174 for (I = tContextList.begin(); ( I != tContextList.end() ) ;++I)
00175 {
00176 qtNewContext = bindContext (qtCurrentContext, *I);
00177 qtCurrentContext = qtNewContext;
00178 }
00179
00180
00181
00182
00183 zDotPosition = yObjectIdAndKind.find ('.');
00184 if ( !zDotPosition )
00185 {
00186 yObjectId = yObjectIdAndKind;
00187 }
00188 else
00189 {
00190 yObjectId = yObjectIdAndKind.substr (0, zDotPosition);
00191 yObjectKind = yObjectIdAndKind.substr (zDotPosition + 1);
00192 }
00193 tObjectName.length(1);
00194 tObjectName [0].id = yObjectId.c_str();
00195 tObjectName [0].kind = yObjectKind.c_str();
00196 try
00197 {
00198 qtCurrentContext->bind (tObjectName, qtObject);
00199 }
00200 catch (const CosNaming::NamingContext::AlreadyBound&)
00201 {
00202 qtCurrentContext->rebind (tObjectName, qtObject);
00203 }
00204 return qtObject;
00205 }
00206
00207 void finish (void)
00208 {
00209 qtOrb->shutdown (false);
00210 }
00211
00221 template <typename TObject, typename TObjectReference>
00222 TObjectReference request (const TString& rkyOBJECT_IDENTIFIER)
00223 {
00224 CORBA::Object_var qtObject = qtOrb->string_to_object (rkyOBJECT_IDENTIFIER.c_str());
00225
00226 return TObject::_narrow (qtObject);
00227 }
00228
00238 template <typename TObject, typename TObjectReference>
00239 TObjectReference request (const char* pkcOBJECT_IDENTIFIER)
00240 {
00241 CORBA::Object_var qtObject = qtOrb->string_to_object (pkcOBJECT_IDENTIFIER);
00242
00243 return TObject::_narrow (qtObject);
00244 }
00245
00251 void setColocated (bool gCOLOCATED)
00252 {
00253 gIsColocated = gCOLOCATED;
00254 }
00255
00256 int start (void)
00257 {
00258 qtPoaManager->activate();
00259 if ( !gIsColocated )
00260 {
00261 qtOrb->run();
00262 }
00263 return 0;
00264 }
00265
00266
00267 public:
00268
00269
00270
00271
00272
00273 template <typename TObjectReference>
00274 TString name (TObjectReference qtOBJECT)
00275 {
00276 return qtOrb->object_to_string (qtOBJECT);
00277 }
00278
00279 };
00280
00281 }
00282
00283 }
00284
00285 }
00286
00287
00288 #endif // not _MPCL_NET_CORBA_ORB__