interreality.org [VOS]
[Home] [About]
[Screenshots]
[Download]
[News]
[Community]
[Documentation] [Manual]
[Bugs & Requests] [Wiki]

/home/tetron/hack/vos/libs/vos/vos/site.hh

Go to the documentation of this file.
00001 /*
00002     This file is part of the Virtual Object System of
00003     the Interreality project (http://interreality.org).
00004 
00005     Copyright (C) 2001, 2002 Peter Amstutz
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00020 
00021     Peter Amstutz <http://www.interreality.org>
00022 */
00023 #ifndef _SITE_HH_
00024 #define _SITE_HH_
00025 
00026 /** @file
00027     Defines Site.
00028 */
00029 
00030 #include <deque>
00031 #include <map>
00032 #include <string>
00033 
00034 namespace VOS
00035 {
00036     class Site;
00037 }
00038 
00039 #include <vos/vos/vosdefs.hh>
00040 #include <vos/vutil/refcount.hh>
00041 #include <vos/vutil/url.hh>
00042 #include <vos/vos/siteextension.hh>
00043 #include <vos/vos/messageblock.hh>
00044 #include <vos/vos/meta_cast.hh>
00045 
00046 #define VOS_DEFAULT_PORT 4231
00047 
00048 
00049 namespace VOS
00050 {
00051     class Message;
00052     class MessageBlock;
00053     class MessageContext;
00054     class ChildChangeListener;
00055     class ParentChangeListener;
00056     class TypeChangeListener;
00057     class VobjectEvent;
00058     class LocalSite;
00059     class SiteIterator;
00060         class SiteExtension;
00061 
00062 /** @class MessageFilter site.hh vos/vos/site.hh
00063     @ingroup libvos
00064 
00065     A message filter can be attached to any site to filter the
00066     messages passing through that site.  If you attach the filter to a
00067     remote site, you will filter outbound messages going to that
00068     remote site.  If you attach a filter to a local site, you will
00069     filter inbound messages being sent to the local site.
00070  */
00071 class VOS_API MessageFilter
00072 {
00073 public:
00074     virtual ~MessageFilter();
00075 
00076     /** Do something with this message.  You are permitted to modify the message.
00077         @param m the message
00078         @return true to continue processing the message, false
00079         indicating the message should NOT be sent or delivered.
00080      */
00081     virtual bool checkMessage(Message* m) = 0;
00082 
00083     /** @copydoc VOS::MessageFilter::checkMessage(Message* m) */
00084     virtual bool checkMessage(MessageBlock* m);
00085 };
00086 
00087 /** @class ProtocolHandler site.hh vos/vos/site.hh
00088     @ingroup libvos
00089 
00090     Abstract interface that implements connecting to a remote site
00091     with some particular protocol.
00092 */
00093 class VOS_API ProtocolHandler
00094 {
00095 public:
00096     virtual ~ProtocolHandler() { }
00097     virtual VUtil::vRef<Site> connectToSite(const VUtil::URL& u) = 0;
00098 };
00099 
00100 /** @class NoSuchMessageBlockError site.hh vos/vos/site.hh
00101     @ingroup libvos
00102     Thrown when a lookup for a stored message block fails.
00103 */
00104 class VOS_API NoSuchMessageBlockError : public std::runtime_error {
00105 public:
00106     NoSuchMessageBlockError(const std::string& s) : std::runtime_error(s) { };
00107 };
00108 
00109 /** @class NameAlreadyUsedError site.hh vos/vos/site.hh
00110     @ingroup libvos
00111     Thrown when an attempt to rename a Vobject fails due to there already being
00112     a Vobject with the new name.
00113 */
00114 class VOS_API NameAlreadyUsedError : public std::runtime_error {
00115 public:
00116     NameAlreadyUsedError(const std::string& s) : std::runtime_error(s) { };
00117     static const char* errorCode() { return "409"; }
00118 };
00119 
00120 /** @class ProtocolError site.hh vos/vos/site.hh
00121     @ingroup libvos
00122     Thrown when a critical protocol error has occurred
00123 */
00124 class VOS_API ProtocolError : public std::runtime_error
00125 {
00126 public:
00127     ProtocolError(const std::string& s) : std::runtime_error(s) { };
00128 };
00129 
00130 /** @class ProgressMeterCallback site.hh vos/vos/site.hh
00131     @ingroup libvos
00132 
00133     Generic callback interface used to notify that progress has been
00134     made on some lengthy operation.
00135  */
00136 class VOS_API ProgressMeterCallback {
00137 public:
00138     virtual ~ProgressMeterCallback() { }
00139 
00140     /** Called when the some progress-monitored task is started.
00141         @param task a descriptor ID indicating the task that has just
00142         begun (the particular contents of this string should be
00143         documented in the API for calls that accept
00144         ProgressMeterCallback* as a parameter)
00145      */
00146     virtual void notifyTaskStart(const std::string& task, unsigned int total) = 0;
00147 
00148     /** Called to indicate current progress.  Percentage complete is
00149         100*(progress/total).
00150         @param action the action currently being taken (if an action has several stages)
00151         @param progress how much work has been completed
00152         @param total the total amount of work that needs to be done
00153      */
00154     virtual void notifyProgress(unsigned int progress) = 0;
00155 };
00156 
00157 
00158 /** @class Site site.hh vos/vos/site.hh
00159     @ingroup libvos
00160 
00161     A site is the root of any collection of Vobjects.  It is a normal
00162     Vobject in addition to having features particular to the site.  A
00163     site is the connection point by which a Vobject may exchange
00164     messages with another Vobject on another host across the Internet.
00165  */
00166 class VOS_API Site :  public VobjectBase
00167 {
00168 public:
00169     typedef MetaObject* (*metaobject_extender_t)(VobjectBase* superobject, const std::string& type);
00170 private:
00171     boost::mutex hostnames_mutex;
00172     std::set<std::string> hostnames;
00173 
00174     static boost::mutex& siteTable_mutex();
00175     static std::map<std::string, Site*>& siteTable();
00176 
00177     static boost::mutex& protocolHandlers_mutex();
00178     static std::map<std::string, ProtocolHandler*>& protocolHandlers();
00179 
00180     boost::mutex messageBlockTable_mutex;
00181     std::map<std::string, MessageBlock*> messageBlockTable;
00182 
00183     boost::mutex messagefilters_mutex;
00184     std::vector<MessageFilter*> messagefilters;
00185 
00186     VUtil::read_write_mutex site_extensions_mutex;
00187     std::vector< std::pair<const char*, SiteExtension*> > site_extensions;
00188 
00189     VUtil::vRef<Identity> myIdentity;
00190     VUtil::vRef<Identity> peerIdentity;
00191 
00192     VUtil::vRef<Site> localSite;
00193 
00194     static boost::mutex& localMetaObjectFactoryTable_mutex();
00195     static std::map<std::string, metaobject_extender_t>& localMetaObjectFactoryTable();
00196 
00197     static boost::mutex& remoteMetaObjectFactoryTable_mutex();
00198     static std::map<std::string, metaobject_extender_t>& remoteMetaObjectFactoryTable();
00199 
00200     static boost::mutex& classnameToVOStype_mutex();
00201     static std::map<std::string, std::string>& classnameToVOStype();
00202 
00203 protected:
00204     virtual void msgSendImpl(boost::mutex& usehandlers_mutex, HandlerMap& usehandlers, Message* msg);
00205 
00206 public:
00207     /** Constructor.
00208         @param islocal is this a local site or remote site.  You will probably always want
00209         a local site.
00210      */
00211     Site(bool islocal = true);
00212 
00213     /** Destructor */
00214     virtual ~Site();
00215 
00216     /** Create a new Vobject on this site.
00217         @param requester The object requesting this action.  If it isremote
00218         this will be access-control checked.  May be null.
00219         @param name The requested name.  This will be made unique by
00220         appending extra characters if there already exits an object
00221         with this name.  May be empty in which case a unique name will
00222         be generated.
00223         @param typelist The list of VOS types that this object should
00224         support.  MetaObject extensions for the supplied types will be
00225         added if the appropriate factories have been registered.
00226         @return the newly-constructed Vobject
00227      */
00228     virtual VUtil::vRef<Vobject> createVobjectT(Vobject* requester, const char* name,
00229                                          const std::deque<std::string>& typelist);
00230 
00231     /** Create a new Vobject on this site.
00232         @param name The requested name.  This will be made unique by
00233         appending extra characters if there already exits an object
00234         with this name.  May be empty in which case a unique name will
00235         be generated.
00236         @param typelist The list of VOS types that this object should
00237         support.  MetaObject extensions for the supplied types will be
00238         added if the appropriate factories have been registered.
00239         @return the newly-constructed Vobject
00240      */
00241     virtual VUtil::vRef<Vobject> createVobjectT(const char* name,
00242                                          const std::deque<std::string>& typelist)
00243         { return createVobjectT(0, name, typelist); }
00244 
00245     /** Create a new Vobject on this site. Use this creation method if your new
00246      * object will have to types, or you want to specify the types as strings
00247      * rather than using the template createVobject() methods.
00248      *
00249         @param name The requested name.  This will be made unique by
00250         appending extra characters if there already exits an object
00251         with this name.  May be empty in which case a unique name will
00252         be generated.
00253         @param t1...t5 The list of VOS types that this object should
00254         support.  MetaObject extensions for the supplied types will be
00255         added if the appropriate factories have been registered.
00256         @return the newly-constructed Vobject
00257      */
00258     VUtil::vRef<Vobject> createVobjectA(const char* name = "", const char* t1 = 0, const char* t2 = 0,
00259                                       const char* t3 = 0, const char* t4 = 0, const char* t5 = 0)
00260         {
00261             std::deque<std::string> typelist;
00262             if(t1) typelist.push_back(t1);
00263             if(t2) typelist.push_back(t2);
00264             if(t3) typelist.push_back(t3);
00265             if(t4) typelist.push_back(t4);
00266             if(t5) typelist.push_back(t5);
00267             return createVobjectT(name, typelist);
00268         }
00269 
00270 
00271     /** Create a new Vobject on this site.  The template parameter(s)
00272         are the MetaObject interfaces that you want this Vobject to
00273         support.
00274         @param name The requested name.  This will be made unique by
00275         appending extra characters if there already exits an object
00276         with this name.  May be empty in which case a unique name will
00277         be generated.
00278         @param ac an access control policy to assign to this object
00279         @return the newly-constructed Vobject, which has been
00280         meta_cast<> to the first template paramter.
00281      */
00282     template<class T>
00283     VUtil::vRef<T> createVobject(const std::string& name = "", const std::string& ac = "")
00284         {
00285             VUtil::vRef<T> obj = meta_cast<T>(createVobjectA(name.c_str(), typeid(T).name()));
00286             if(obj.isValid()) obj->setDefaultPolicy(ac);
00287             return obj;
00288     }
00289 
00290     /** @copydoc VOS::Site::createVobject */
00291         template<class T, class T2>
00292         VUtil::vRef<T> createVobject2(const std::string& name = "", const std::string& ac = "")
00293         {
00294             VUtil::vRef<T> obj = meta_cast<T>(createVobjectA(name.c_str(), typeid(T).name(),
00295                                                                            typeid(T2).name()));
00296             if(obj.isValid()) obj->setDefaultPolicy(ac);
00297             return obj;
00298     }
00299 
00300     /** @copydoc VOS::Site::createVobject */
00301         template<class T, class T2, class T3>
00302         VUtil::vRef<T> createVobject3(const std::string& name = "", const std::string& ac = "")
00303         {
00304             VUtil::vRef<T> obj = meta_cast<T>(createVobjectA(name.c_str(), typeid(T).name(),
00305                                                                            typeid(T2).name(), typeid(T3).name()));
00306             if(obj.isValid()) obj->setDefaultPolicy(ac);
00307             return obj;
00308     }
00309 
00310     /** @copydoc VOS::Site::createVobject */
00311         template<class T, class T2, class T3, class T4>
00312         VUtil::vRef<T> createVobject4(const std::string& name = "", const std::string& ac = "")
00313         {
00314             VUtil::vRef<T> obj = meta_cast<T>(createVobjectA(name.c_str(), typeid(T).name(),
00315                                                                            typeid(T2).name(), typeid(T3).name(), typeid(T4).name()));
00316             if(obj.isValid()) obj->setDefaultPolicy(ac);
00317             return obj;
00318     }
00319 
00320     /** @copydoc VOS::Site::createVobject */
00321         template<class T, class T2, class T3, class T4, class T5>
00322         VUtil::vRef<T> createVobject5(const std::string& name = "", const std::string& ac = "")
00323         {
00324             VUtil::vRef<T> obj = meta_cast<T>(createVobjectA(name.c_str(), typeid(T).name(),
00325                                                                            typeid(T2).name(), typeid(T3).name(), typeid(T4).name(),
00326                                                                            typeid(T5).name()));
00327             if(obj.isValid()) obj->setDefaultPolicy(ac);
00328             return obj;
00329     }
00330 
00331     /** A Vobject name unique on this site by appending characters to
00332         the given base as necesary.  Used by the createVobject
00333         methods.
00334         @param base the base name to start from
00335         @return the new name, if the name supplied in the base
00336         paramater was unique, then just the base name will be returned
00337      */
00338     virtual std::string uniqueName(const char* base);
00339 
00340     /** @return a random and unique numerical name. (Used by the createVobject methods). */
00341     virtual std::string generateUniqueName();
00342 
00343     /** Add a host alias to this site.  A host alias is a legal name
00344         by which this site may be refered to.
00345         @param h the host alias in the form "proto://hostname:port"
00346     */
00347     virtual void addHostAlias(const std::string& h);
00348 
00349     /** Remove a host alias of this site.
00350         @param h the host alias in the form "proto://hostname:port"
00351     */
00352     virtual void removeHostAlias(const std::string& h);
00353 
00354     /** Tests for the existance of a given host alias.
00355         @param h the host alias
00356         @return true if h is a legal host alias, false if not
00357     */
00358     virtual bool hasHostAlias(const std::string& h);
00359 
00360     /** Get a list of host alias.
00361         @return the set of host valid host aliases.
00362     */
00363     virtual StringIterator getHostAliases();
00364 
00365     /** Add a new URL for this site.
00366 
00367         @note at the moment this actually replaces the old URL
00368         completely; it doesn't handle having multiple URLs properly.
00369         This exists to solve a specific problem.  Don't use it.
00370      */
00371     virtual void addURL(const VUtil::URL& u);
00372 
00373     /** Add a site to the master table of known sites.
00374         @param s the site to be added
00375     */
00376     static void addSite(Site& s);
00377 
00378     /** Remove a site from the master table of known sites.
00379         @param s the site to remove
00380     */
00381     static void removeSite(Site& s);
00382 
00383     /** Find a site by its host alias.  Will contact remote sites if
00384         "contact" is true.
00385         @param s the site host alias (in the form
00386         "proto://host:port", such as "vop://interreality.org")
00387         @param contact if true, try and contact remote site, otherwise
00388         throw NoSuchSiteError if we are not already connected to this site
00389         @return the site matching that host alias.
00390         @throws NoSuchSiteError if the site is not found or could not be contacted
00391     */
00392     static VUtil::vRef<Site> findSite(const std::string& s, bool contact = true);
00393 
00394     /** Get a list of all known sites, local and remote.
00395         @return an iterator
00396     */
00397     static SiteIterator getAllSites();
00398 
00399     /** Save a message block template for later retrival and execution.
00400         @param m the message block
00401     */
00402     virtual void addMessageBlock(MessageBlock* m);
00403 
00404     /** Remove a saved message block template.
00405         @param m the message block
00406     */
00407     virtual void removeMessageBlock(MessageBlock* m);
00408 
00409     /** Retrive a saved message block template.
00410         @param s the name of the message block.
00411         @return The message block
00412         @throw NoSuchMessageBlockError if it was not found
00413     */
00414     virtual VUtil::vRef<MessageBlock> getMessageBlock(const std::string& s);
00415 
00416     virtual void sendMessage(Message* m);
00417     virtual void sendMessage(MessageBlock* m);
00418 
00419     virtual VUtil::vRef<Site> getSite() const;
00420 
00421     /** Add a message filter.
00422         @bug is this implemented?
00423     */
00424     virtual void insertMessageFilter(int pos, MessageFilter* mf);
00425 
00426     /** Add a message filter.
00427         @bug is this implemented?
00428     */
00429     virtual void removeMessageFilter(int pos);
00430 
00431     /** Add a message filter.
00432         @bug is this implemented?
00433     */
00434     virtual void removeMessageFilter(MessageFilter* m);
00435 
00436     // virtual const std::deque<MessageFilter*>& getMessageFilters();
00437 
00438     /** Attach a SiteExtension object which may extend the actions of
00439         various site-specific methods.
00440         @param sx the extension object
00441      */
00442     virtual void addSiteExtension(SiteExtension* sx);
00443 
00444     virtual void excise();
00445 
00446     /** Map a C++ MetaObject classname to the VOS type it implements.
00447         For example:
00448         @code
00449         convertClassnameToVOStype(typeid(Property).name()) == "property:property"
00450         @endcode
00451      */
00452     static std::string convertClassnameToVOStype(const std::string& n);
00453 
00454     /** Add a factory for creating MetaObjects that extend remote
00455         Vobjects.  Will replace any existing factory registered to this classname.
00456         @param classname a C++ or VOS interface this implements
00457         @param vostype the VOS type this implements
00458         @param newmethod the actual factory function that constructs the MetaObject
00459      */
00460     static void addRemoteMetaObjectFactory(const char* classname, const char* vostype, metaobject_extender_t newmethod);
00461 
00462     /** Fetch the factory function that constructs MetaObjects for the supplied class
00463         @param classname the C++ class name (via typeid(TheClass).name()) or VOS type
00464         @return the factory function (which can be called to construct
00465         a MetaObject) or 0 if there is no factory registered for this
00466         class.
00467      */
00468     static metaobject_extender_t getRemoteMetaObjectFactory(const char* classname);
00469 
00470     /** Remove the supplied factory function that is registered to this classname.
00471         Nothing will be removed unless both parameters match.
00472         @param classname the C++ class name (via
00473         typeid(TheClass).name()) or VOS type it was registered with
00474         @param oldmethod the factory function it was registered with
00475      */
00476     static void removeRemoteMetaObjectFactory(const char* classname,
00477                                               metaobject_extender_t oldmethod);
00478 
00479     /** Add a factory for creating MetaObjects that extend remote
00480         Vobjects.  Will replace any existing factory registered to this
00481         classname.
00482         @param classname a C++ or VOS interface this implements
00483         @param newmethod the actual factory function that constructs the MetaObject
00484      */
00485     static void addLocalMetaObjectFactory(const char* classname, metaobject_extender_t newmethod);
00486 
00487     /** Fetch the factory function that constructs MetaObjects for the supplied class
00488         @param classname the C++ class name (via typeid(TheClass).name()) or VOS type
00489         @return the factory function (which can be called to construct
00490         a MetaObject) or 0 if there is no factory registered for this
00491         class.
00492      */
00493     static metaobject_extender_t getLocalMetaObjectFactory(const char* classname);
00494 
00495     /** Remove the supplied factory function that is registered to this classname.
00496         Nothing will be removed unless both parameters match.
00497         @param classname the C++ class name (via
00498         typeid(TheClass).name()) or VOS type it was registered with
00499         @param oldmethod the factory function it was registered with
00500      */
00501     static void removeLocalMetaObjectFactory(const char* classname,
00502                                              metaobject_extender_t oldmethod);
00503 
00504     /** Print the factory registration table to stderr. (For debugging
00505         purposes) */
00506     static void dumpFactoryTables();
00507 
00508     /** Change the site name of this Vobject.
00509         @param requester the vobject requesting this change.  If it is
00510         remote, this action will be access control checked. (may be null)
00511         @param oldname the old Vobject
00512         @param newname the new name for the Vobject
00513         @throw NameAlreadyUsedError if there is already a Vobject with the new name
00514      */
00515     virtual void renameVobject(Vobject* requester, const std::string& oldname,
00516                                const std::string& newname);
00517 
00518     /** Change the site name of this Vobject.
00519         @param oldname the old Vobject
00520         @param newname the new name for the Vobject
00521         @throw NameAlreadyUsedError if there is already a Vobject with the new name
00522      */
00523     virtual void renameVobject(const std::string& oldname, const std::string& newname)
00524         { renameVobject(0, oldname, newname); }
00525 
00526     /** Returns the identity object on this remote site which we have
00527         authenticated ourselves to.  This identity object is used by
00528         the remote site to determine which access control policies to
00529         apply to us.  Only meaningful if this is a remote site.
00530         @return an identity object (which should always a remote
00531         object attached to this site), or a null vRef if we have not
00532         yet established our identity to this site.
00533      */
00534     virtual VUtil::vRef<Identity> getMyIdentity();
00535 
00536     /** Sets the cached value for our identity on this remote site.
00537         Does @em not actually change our effective identity on the
00538         remote site --- use an authentication mechanism such as
00539         RemotePasswordAuth to do that!
00540 
00541         @param id the identity object to use (should be a
00542         Identity Vobject attached to this remote site)
00543     */
00544     virtual void setMyIdentity(Identity* id);
00545 
00546     /** Returns the identity of this peer.  This is used to apply
00547         access control to any commands they send.
00548 
00549         @return the Identity Vobject or a null vRef if the peer has
00550         not yet established an Identity (default access control
00551         polices are applied in this case)
00552     */
00553     virtual VUtil::vRef<Identity> getPeerIdentity();
00554 
00555     /** Set the identity of this peer.  This is used to determine
00556         which access control policy to apply to this peer.  May be
00557         done manually by the application, or using an authentication
00558         metaobject such as LocalPasswordAuth.
00559         @param id the identity to use (must be a local object)
00560      */
00561     virtual void setPeerIdentity(Identity* id);
00562 
00563     /** For a remote site, set the local site object to which we are
00564         connected. */
00565     void setLocalSite(Site* s) { localSite.assign(s, true); }
00566 
00567     /** For a remote site, get the local site object to which we are
00568         connected. */
00569     VUtil::vRef<Site> getLocalSite() {
00570         if(isRemote()) return localSite;
00571         else return VUtil::vRef<Site>(this, true);
00572     }
00573 
00574     /** @return the first site extension that can be dynamic_cast<> to
00575         the template parameter C, or null if no extension has the
00576         desired type.
00577      */
00578     template<class C> C* querySiteExtension()
00579         {
00580             VUtil::scoped_read_lock lk(site_extensions_mutex);
00581 
00582             for(std::vector< std::pair<const char*, SiteExtension*> >::iterator i = site_extensions.begin();
00583                 i != site_extensions.end();
00584                 i++)
00585             {
00586                 if(C* ret = dynamic_cast<C*>((*i).second)) return ret;
00587             }
00588             return 0;
00589         }
00590 
00591     /** Add a handler for connecting to sites with some protocol.
00592 
00593         @param protocol The protocol name.  This is the first part of
00594         the URL, such as "vop" in "vop://interreality.org"
00595         @param ph The protocol handler object.  This will be called to
00596         do the work of connecting to a remote site using this protocol.
00597 
00598         @note Actually having multiple protocols at once hasn't really been
00599         tried yet, but these hooks are a start.
00600      */
00601     static void addProtocolHandler(const std::string& protocol, ProtocolHandler& ph);
00602 
00603     /** @return the protocol handler object for this protocol */
00604     static ProtocolHandler* getProtocolHandler(const std::string& protocol);
00605 
00606     /** Remove the protocol handler object for this protocol */
00607     static void removeProtocolHandler(const std::string& protocol);
00608 };
00609 
00610 #ifndef SWIG
00611 
00612 struct WaitingThread
00613 {
00614     boost::condition cond;
00615     bool nonceSeen;
00616     bool wantProgress;
00617     unsigned int bytesReceived;
00618     unsigned int bytesTotal;
00619     VUtil::vRef<Site> localsite;
00620     VUtil::vRef<MessageBlock> msgblock;
00621 };
00622 
00623 #endif
00624 
00625 }
00626 
00627 
00628 #ifndef SWIG
00629 
00630 namespace VUtil {
00631     template<> inline void iteratorReleaseItem(VOS::Site** v)
00632     {
00633         (*v)->release();
00634     }
00635 }
00636 
00637 
00638 namespace VOS {
00639 
00640 
00641 /** @class SiteIterator site.hh vos/vos/site.hh
00642     @ingroup libvos
00643 */
00644     class SiteIterator : public VUtil::Iterator<Site*>
00645     {
00646     public:
00647         SiteIterator() : VUtil::Iterator<Site*>() { }
00648         SiteIterator(const SiteIterator& i) : VUtil::Iterator<Site*>(i) { }
00649         SiteIterator(const std::map<std::string, Site*> sites) {
00650             std::set<Site*> tmp;
00651             for(std::map<std::string, Site*>::const_iterator i = sites.begin();
00652                 i != sites.end();
00653                 i++)
00654             {
00655                 tmp.insert((*i).second);
00656             }
00657             items->resize(tmp.size());
00658             int c = 0;
00659             for(std::set<Site*>::const_iterator i = tmp.begin();
00660                 i != tmp.end();
00661                 i++)
00662             {
00663                 (*i)->acquire();
00664                 (*items)[c] = (*i);
00665                 c++;
00666             }
00667         }
00668         VUtil::vRef<Site> operator*() {
00669             if(pos < items->size()) return VUtil::vRef<Site>((*items)[pos], true);
00670             else return VUtil::vRef<Site>();
00671         }
00672     };
00673 
00674 }
00675 
00676 #endif // ifndef SWIG
00677 
00678 
00679 #include <vos/vos/identity.hh>
00680 
00681 namespace VOS {
00682 template<class AccessControlType> inline std::vector<AccessControlType*>
00683 
00684 VobjectBase::getAccessControlsFor(Vobject* requester,
00685                                 PolicyDomain<AccessControlType>& accessControlPolicies)
00686         {
00687             VUtil::vRef<Identity> ident = requester->getSite()->getPeerIdentity();
00688 
00689             std::vector<AccessControlType*> accesscontrols;
00690             if(ident.isValid()) {
00691                 StringIterator acs = accesscontrolstate->getPolicy(accessControlPolicies.getDomain(), ident);
00692                 for(; acs.hasMore(); acs++) {
00693                     AccessControlType* act = accessControlPolicies.getPolicyImpl(*acs);
00694                     if(act) accesscontrols.push_back(act);
00695                 }
00696             } else {
00697                 AccessControlType* act = accessControlPolicies.getPolicyImpl(accesscontrolstate->getDefaultPolicy(accessControlPolicies.getDomain()));
00698                 if(act) accesscontrols.push_back(act);
00699             }
00700             return accesscontrols;
00701         }
00702 
00703 }
00704 
00705 #endif