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

SubjectManagerImpl.cpp

Go to the documentation of this file.
00001 /***************** <Tweek heading BEGIN do not edit this line> ****************
00002  * Tweek
00003  *
00004  * -----------------------------------------------------------------
00005  * File:          $RCSfile: SubjectManagerImpl.cpp,v $
00006  * Date modified: $Date: 2003/11/25 23:18:52 $
00007  * Version:       $Revision: 1.19 $
00008  * -----------------------------------------------------------------
00009  ***************** <Tweek heading END do not edit this line> *****************/
00010 
00011 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
00012  *
00013  * VR Juggler is (C) Copyright 1998-2003 by Iowa State University
00014  *
00015  * Original Authors:
00016  *   Allen Bierbaum, Christopher Just,
00017  *   Patrick Hartling, Kevin Meinert,
00018  *   Carolina Cruz-Neira, Albert Baker
00019  *
00020  * This library is free software; you can redistribute it and/or
00021  * modify it under the terms of the GNU Library General Public
00022  * License as published by the Free Software Foundation; either
00023  * version 2 of the License, or (at your option) any later version.
00024  *
00025  * This library is distributed in the hope that it will be useful,
00026  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00027  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00028  * Library General Public License for more details.
00029  *
00030  * You should have received a copy of the GNU Library General Public
00031  * License along with this library; if not, write to the
00032  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00033  * Boston, MA 02111-1307, USA.
00034  *
00035  *************** <auto-copyright.pl END do not edit this line> ***************/
00036 
00037 #include <tweek/tweekConfig.h>
00038 
00039 #include <vpr/vpr.h>
00040 #include <vpr/System.h>
00041 #include <vpr/Sync/Guard.h>
00042 #include <vpr/Util/Debug.h>
00043 #include <vpr/IO/Socket/InetAddr.h>
00044 
00045 #include <tweek/Util/Debug.h>
00046 #include <tweek/CORBA/CorbaManager.h>
00047 #include <tweek/CORBA/SubjectImpl.h>
00048 #include <tweek/CORBA/SubjectManagerImpl.h>
00049 
00050 
00051 namespace tweek
00052 {
00053 
00054 const std::string SubjectManagerImpl::USERNAME_KEY("Username");
00055 const std::string SubjectManagerImpl::APPNAME_KEY("Application");
00056 
00057 SubjectManagerImpl::~SubjectManagerImpl()
00058 {
00059    subject_map_t::iterator i;
00060    std::string name_str;
00061 
00062    for ( i = mSubjects.begin(); i != mSubjects.end(); ++i )
00063    {
00064       vprDEBUG(tweekDBG_CORBA, vprDBG_CRITICAL_LVL)
00065          << "Deactivating subject named '" << (*i).first
00066          << "' in tweek::SubjectManagerImpl destructor\n" << vprDEBUG_FLUSH;
00067 
00068       // Deactivate the object in the POA.
00069       name_str = (*i).first;
00070       mCorbaMgr.getChildPOA()->deactivate_object(mSubjectIds[name_str]);
00071       Subject_ptr subj = mSubjects[name_str];
00072       CORBA::release(subj);
00073    }
00074 }
00075 
00076 void SubjectManagerImpl::registerSubject(SubjectImpl* subjectServant,
00077                                          const char* name)
00078 {
00079    std::string name_str(name);
00080 
00081    mSubjectIdsMutex.acquire();
00082    {
00083       // We have to register servant with POA first.
00084       mSubjectIds[name_str] =
00085          mCorbaMgr.getChildPOA()->activate_object(subjectServant);
00086    }
00087    mSubjectIdsMutex.release();
00088 
00089    registerSubject(subjectServant->_this(), name_str);
00090 }
00091 
00092 vpr::ReturnStatus SubjectManagerImpl::unregisterSubject(const char* name)
00093 {
00094    vpr::ReturnStatus status;
00095    std::string name_str(name);
00096    vpr::Guard<vpr::Mutex> guard(mSubjectsMutex);
00097 
00098    if ( mSubjects.count(name_str) > 0 )
00099    {
00100       mSubjectIdsMutex.acquire();
00101       {
00102          // Deactivate the object in the POA.
00103          mCorbaMgr.getChildPOA()->deactivate_object(mSubjectIds[name_str]);
00104       }
00105       mSubjectIdsMutex.release();
00106 
00107       Subject_ptr subj = mSubjects[name_str];
00108       CORBA::release(subj);
00109       mSubjects.erase(name_str);
00110    }
00111    else
00112    {
00113       vprDEBUG(vprDBG_ALL, vprDBG_CRITICAL_LVL)
00114          << clrOutBOLD(clrRED, "ERROR:")
00115          << " No subject registered under the name '" << name_str
00116          << "'\n" << vprDEBUG_FLUSH;
00117       status.setCode(vpr::ReturnStatus::Fail);
00118    }
00119 
00120    return status;
00121 }
00122 
00123 void SubjectManagerImpl::registerSubject(Subject_ptr subject,
00124                                          const std::string& name)
00125 {
00126    vpr::Guard<vpr::Mutex> guard(mSubjectsMutex);
00127 
00128    vprDEBUG(tweekDBG_CORBA, vprDBG_STATE_LVL)
00129       << "Registering subject named '" << name << "'\n" << vprDEBUG_FLUSH;
00130 
00131    mSubjects[name] = Subject::_duplicate(subject);
00132 }
00133 
00134 Subject_ptr SubjectManagerImpl::getSubject(const char* name)
00135 {
00136    vprDEBUG_OutputGuard(tweekDBG_CORBA, vprDBG_STATE_LVL,
00137                         "tweek::SubjectManagerImpl::getSubject() entered\n",
00138                         "tweek::SubjectManagerImpl::getSubject() done\n");
00139 
00140    Subject_ptr subject;
00141    std::string name_str(name);
00142    vpr::Guard<vpr::Mutex> guard(mSubjectsMutex);
00143 
00144    subject_map_t::iterator i;
00145 
00146    i = mSubjects.find(name_str);
00147 
00148    if ( i != mSubjects.end() )
00149    {
00150       subject = Subject::_duplicate((*i).second);
00151       vprDEBUG(tweekDBG_CORBA, vprDBG_STATE_LVL)
00152          << "Returning subject named '" << name << "'\n" << vprDEBUG_FLUSH;
00153    }
00154    else
00155    {
00156       subject = Subject::_nil();
00157       vprDEBUG(vprDBG_ALL, vprDBG_WARNING_LVL)
00158          << clrOutBOLD(clrYELLOW, "WARNING:") << " No subject named '" << name
00159          << "' registered with Subject Manager!\n" << vprDEBUG_FLUSH;
00160    }
00161 
00162    return subject;
00163 }
00164 
00165 tweek::SubjectManager::SubjectList* SubjectManagerImpl::getAllSubjects()
00166 {
00167    vprDEBUG_OutputGuard(tweekDBG_CORBA, vprDBG_STATE_LVL,
00168                         "tweek::SubjectManagerImpl::getAllSubjects() entered\n",
00169                         "tweek::SubjectManagerImpl::getAllSubjects() done\n");
00170 
00171    subject_map_t::iterator i;
00172    CORBA::ULong j;
00173 
00174    vprDEBUG(tweekDBG_CORBA, vprDBG_STATE_LVL)
00175       << "Constructing sequence of subjects to return to caller ...\n"
00176       << vprDEBUG_FLUSH;
00177 
00178    // Lock mSubjects while we read from it.
00179    vpr::Guard<vpr::Mutex> guard(mSubjectsMutex);
00180 
00181    // Create the sequence and size it.
00182    tweek::SubjectManager::SubjectList* subjects =
00183       new tweek::SubjectManager::SubjectList();
00184    subjects->length(mSubjects.size());
00185 
00186    vprDEBUG(tweekDBG_CORBA, vprDBG_VERB_LVL)
00187       << "Sequence size: " << subjects->length() << std::endl
00188       << vprDEBUG_FLUSH;
00189 
00190    for ( i = mSubjects.begin(), j = 0; i != mSubjects.end(); ++i, ++j )
00191    {
00192       tweek::SubjectManager::RegisteredSubject rs;
00193       rs.subject_name = CORBA::string_dup((*i).first.c_str());
00194       rs.subject_ref  = Subject::_duplicate((*i).second);
00195 
00196       vprDEBUG(tweekDBG_CORBA, vprDBG_VERB_LVL)
00197          << "Adding subject[" << j << "]: " << rs.subject_name << std::endl
00198          << vprDEBUG_FLUSH;
00199 
00200       (*subjects)[j] = rs;
00201    }
00202 
00203    return subjects;
00204 }
00205 
00206 SubjectManager::SubjectManagerInfoList* SubjectManagerImpl::getInfo()
00207 {
00208    std::map<std::string, std::string>::iterator i;
00209    CORBA::ULong j;
00210 
00211    vprDEBUG(tweekDBG_CORBA, vprDBG_STATE_LVL)
00212       << "Constructing sequence of info items to return to caller ...\n"
00213       << vprDEBUG_FLUSH;
00214 
00215    // Use a guard here so that the lock is exception safe.
00216    vpr::Guard<vpr::Mutex> lock(mInfoMapMutex);
00217 
00218    // Create the sequence and size it.
00219    tweek::SubjectManager::SubjectManagerInfoList* info_items =
00220       new tweek::SubjectManager::SubjectManagerInfoList();
00221    info_items->length(mInfoMap.size());
00222 
00223    vprDEBUG(tweekDBG_CORBA, vprDBG_VERB_LVL)
00224       << "Sequence size: " << info_items->length() << std::endl
00225       << vprDEBUG_FLUSH;
00226 
00227    for ( i = mInfoMap.begin(), j = 0; i != mInfoMap.end(); ++i, ++j )
00228    {
00229       tweek::SubjectManager::SubjectMgrInfoItem item;
00230       item.key   = CORBA::string_dup((*i).first.c_str());
00231       item.value = CORBA::string_dup((*i).second.c_str());
00232 
00233       vprDEBUG(tweekDBG_CORBA, vprDBG_VERB_LVL)
00234          << "Adding item[" << j << "]: " << item.key << " => " << item.value
00235          << std::endl << vprDEBUG_FLUSH;
00236 
00237       (*info_items)[j] = item;
00238    }
00239 
00240    vprDEBUG(tweekDBG_CORBA, vprDBG_STATE_LVL)
00241       << "Returning all info items to caller\n" << vprDEBUG_FLUSH;
00242 
00243    return info_items;
00244 }
00245 
00246 char* SubjectManagerImpl::getName()
00247 {
00248    return CORBA::string_dup(mGUID.toString().c_str());
00249 }
00250 
00251 void SubjectManagerImpl::initInfoMap()
00252 {
00253    vpr::InetAddr local_addr;
00254    vpr::InetAddr::getLocalHost(local_addr);
00255 
00256    mInfoMap["Hostname"] = local_addr.getHostname();
00257 
00258    std::string user_name;
00259 
00260    if ( vpr::System::getenv("USER", user_name).success() )
00261    {
00262       mInfoMap[USERNAME_KEY] = user_name;
00263    }
00264    else
00265    {
00266       mInfoMap[USERNAME_KEY] = "unknown";
00267    }
00268 
00269    mInfoMap[APPNAME_KEY] = "unknown";
00270 }
00271 
00272 SubjectManagerImpl::SubjectManagerImpl(const SubjectManagerImpl& sm)
00273    :
00274 #ifdef OMNIORB_VER
00275      omniServant(sm)
00276    , tweek::_impl_SubjectManager(sm)
00277    ,
00278 #endif
00279      PortableServer::ServantBase(sm)
00280    , POA_tweek::SubjectManager(sm)
00281    , PortableServer::RefCountServantBase(sm)
00282    , mCorbaMgr(sm.mCorbaMgr)
00283    , mInfoMap(sm.mInfoMap)
00284 {
00285    /* Do nothing. */ ;
00286 }
00287 
00288 } // End of tweek namespace

Generated on Sun May 2 14:41:03 2004 for Tweek by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002