Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

orb.hh

00001 /*
00002 *  Name:      orb.hh
00003 *  Author:    Rafael Jesus Alcantara Perez
00004 *  Summary:   CORBA ORB class
00005 *  Date:      $Date: 2003/04/14 00:18:31 $
00006 *  Revision:  $Revision: 1.1 $
00007 *
00008 *  Copyright (C) 2001-2002  Rafael Jesus Alcantara Perez <rafa@dedalo-ing.com>
00009 *
00010 *  This program is free software; you can redistribute it and/or modify
00011 *  it under the terms of the GNU General Public License as published by
00012 *  the Free Software Foundation; either version 2 of the License, or
00013 *  (at your option) any later version.
00014 *
00015 *  This program is distributed in the hope that it will be useful,
00016 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 *  GNU General Public License for more details.
00019 *
00020 *  You should have received a copy of the GNU General Public License
00021 *  along with this program; if not, write to the Free Software
00022 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00023 *  MA 02111-1307, USA.
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           //  C O N S T R U C T O R S
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           //  C O N S T R U C T O R S
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             //  To clarify declarations.
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   // <FIXME comment="is this needed?">
00165             qtObject = enrol<TObject, TObjectReference> (ptOBJECT);
00166   // </FIXME>
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             //  Binds the new object into current context.
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           //  S E L E C T O R S
00271           //
00272 
00273           template <typename TObjectReference>
00274           TString name (TObjectReference qtOBJECT)
00275           {
00276             return qtOrb->object_to_string (qtOBJECT);
00277           }
00278 
00279       };  // class TOrb
00280 
00281     }  // namespace corba
00282 
00283   }  // namespace net
00284 
00285 }  // namespace mpcl
00286 
00287 
00288 #endif  // not _MPCL_NET_CORBA_ORB__

Generated on Mon Oct 13 02:35:23 2003 for MPCL by doxygen1.2.18