Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages | Examples

vos/corelibs/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 <tetron@interreality.org>
00022 */
00023 #ifndef _SITE_HH_
00024 #define _SITE_HH_
00025 
00026 /** @file
00027     Defines Site.
00028 */
00029 
00030 #include <vos/corelibs/vos/vosdefs.hh>
00031 #include <vos/corelibs/vos/messagecontext.hh>
00032 #include <vos/corelibs/vos/metaobject.hh>
00033 
00034 #define VOS_DEFAULT_PORT 4231
00035 
00036 namespace VOS
00037 {
00038 
00039     class Message;
00040     class MessageBlock;
00041     class MessageContext;
00042 
00043 class VOS_API NotifyEvent
00044 {
00045 public:
00046     virtual ~NotifyEvent() {};
00047     virtual void notify() = 0;
00048 };
00049 
00050 /** @class MessageFilter site.hh vos/corelibs/vos/site.hh
00051  *
00052  * A message filter can be attached to any site to filter the
00053  *  messages passing through that site.  If you attach the filter to a
00054  *  remote site, you will filter outbound messages going to that
00055  *  remote site.  If you attach a filter to a local site, you will
00056  *  filter inbound messages being sent to the local site.
00057  */
00058 class VOS_API MessageFilter
00059 {
00060 public:
00061     virtual ~MessageFilter();
00062 
00063     /** Do something with this message.  You are permitted to modify the message.
00064         @param m the message
00065         @return true to continue processing the message, false
00066         indicating the message should NOT be sent or delivered.
00067      */
00068     virtual bool checkMessage(Message* m) = 0;
00069     virtual bool checkMessage(MessageBlock* m);
00070 };
00071 
00072     class ChildChangeListener;
00073     class ParentChangeListener;
00074     class TypeChangeListener;
00075     class VobjectEvent;
00076     class LocalSite;
00077     class RemoteSite;
00078 
00079 class VOS_API VobjectNotifyEvent : public NotifyEvent
00080 {
00081 private:
00082     ChildChangeListener* childlistener;
00083     ParentChangeListener* parentlistener;
00084     TypeChangeListener* typelistener;
00085     VobjectEvent& event;
00086 
00087 public:
00088     VobjectNotifyEvent(ChildChangeListener* cl, VobjectEvent& e);
00089     VobjectNotifyEvent(ParentChangeListener* pl, VobjectEvent& e);
00090     VobjectNotifyEvent(TypeChangeListener* tl, VobjectEvent& e);
00091 
00092     virtual ~VobjectNotifyEvent();
00093     virtual void notify();
00094 };
00095 
00096 /** @class Site site.hh vos/corelibs/vos/site.hh
00097  *
00098  *  A site is the root of any collection of Vobjects.  It is a normal
00099     Vobject itself, but also supports a number of other methods.
00100     Think of sites as the connecting lines across which a Vobject may
00101     exchange messages with another Vobject on another host across the
00102     Internet.
00103  */
00104 class VOS_API Site : public virtual MetaObject
00105 {
00106 private:
00107     set<string> hostnames;
00108     boost::mutex hostnames_mutex;
00109 
00110     static map<string, Site*> siteTable;
00111     static boost::mutex siteTable_mutex;
00112 
00113     static LocalSite* defaultPeer;
00114     map<string, MessageBlock*> messageBlockTable;
00115     map<string, Message*> outgoingTraps;
00116     bool greeted;
00117 
00118 protected:
00119     Site();
00120     Site(MetaObject* superobject);
00121     deque<MessageFilter*> messagefilters;
00122 
00123 public:
00124     class NoSuchMessageBlockError : public runtime_error {
00125     public:
00126         NoSuchMessageBlockError(const string& s) : runtime_error(s) { };
00127     };
00128 
00129     MessageContext defaultContext;
00130 
00131     /** All sockets open to this process.  Used so that the process,
00132         if desired, can sleep on select(), blocking indefinitely, and
00133         wake up when data comes in from any open socket.
00134     */
00135     static set<int> allOpenSockets;
00136 
00137 public:
00138 
00139 
00140     /** Create a new MetaObject on this site.
00141       @param name  Request this name for the new object. If name is 0, empty or
00142         contains a name already in use, a new name will be generated.
00143       @param type0,...    Type strings indicating type extentions to be included
00144         with the new objects. The list of type strings must be terminated by a 0.
00145 
00146       For example:
00147       @code
00148            // Create an example object with two types: 3D cube and Hypercard.
00149            vRef<MetaObject> obj1 = site.createMetaObject("example", "object3D:cube", typeid(Hypercard).name(), 0);
00150 
00151            // Create another example object with no types. Because the same
00152            // name an the previos object ("example") is specified, this object will be
00153            // renamed when created, probably to "example1" or something similiar.
00154            vRef<MetaObject> obj2 = site.createMetaObject("example" , 0);
00155       @endcode
00156      */
00157     virtual MetaObject* createMetaObject(const char* name, const char* firstType, ...) = 0;
00158 
00159     /** Same as the other createMetaObject method, but takes a deque of type
00160      * strings rather than a variable list of strings.
00161      * @see createMetaObject(const char*, const char*, ...)
00162      */
00163     virtual MetaObject* createMetaObject(const char* name, const deque<string>& typelist) = 0;
00164 
00165     /** Create a new metaobject with one type, and cast it to that type.
00166      * @param T     Desired MetaObject subclass
00167      * @param name  If given, site-name of the new object
00168      */ 
00169     template<class T> T* createMetaObjectT(const char* name = 0) {
00170         return meta_cast<T*>(createMetaObject(name, typeid(T).name(), 0));
00171     }
00172 
00173     /** Create an object with no types */
00174     MetaObject* createMetaObject(const char* name = 0) {
00175         return createMetaObject(name, 0);
00176     }
00177 
00178     /** Generate unique object name by appending successive integers to the
00179      * given base (if necesary).  (Used by the createMetaObject methods).
00180      */
00181     virtual string uniqueName(const char* base);
00182 
00183     /// Generate random and unique numerical name. (Used by the createMetaObject methods).
00184     virtual string generateUniqueName();
00185 
00186     /** Flush any incoming buffers (eg read any data waiting in sockets).
00187      */
00188     virtual void flushIncomingBuffers() = 0;
00189 
00190 
00191     /** Add a host alias to this site.  A host alias is a legal name
00192         by which this site may be refered to.
00193         @param h the host alias in the form "hostname:port"
00194     */
00195     virtual void addHostAlias(const string& h);
00196 
00197     /** Remove a host alias of this site.
00198         @param h the host alias in the form "hostname:port"
00199     */
00200     virtual void removeHostAlias(const string& h);
00201 
00202     /** Tests for the existance of a given host alias.
00203         @param h the host alias
00204         @return true if h is a legal host alias, false if not
00205     */
00206     virtual bool hasHostAlias(const string& h);
00207 
00208     /** Get a list of host alias.
00209         @return the set of host valid host aliases.
00210     */
00211     virtual set<string>& getHostAliases();
00212 
00213     /** Set the base URL ("vop://host:port") that this site will use.
00214         @param u some URL.
00215     */
00216     virtual void setURL(const URL& u) = 0;
00217 
00218     /** Add a site to the master table of known sites.
00219         @param s the site to be added
00220     */
00221     static void addSite(Site* s);
00222 
00223     /** Remove a site from the master table of known sites.
00224         @param s the site to remove
00225     */
00226     static void removeSite(Site& s);
00227 
00228     /** Find a site by its host alias.  Will contact remote sites
00229         as necessary.
00230         @param s the site host alias (in the form "host:port")
00231         @return the site matching that host alias.   NOTE YOU MUST CALL
00232 release() WHEN DONE OR USE A vRef<>
00233         @throws NoSuchSiteError if the site is not found or could not be contacted
00234     */
00235     static Site& findSite(const string& s) throw (NoSuchSiteError);
00236 
00237     /** Get the local site with which new remote sites will peered.
00238         @return the default peer.    NOTE YOU MUST CALL release() WHEN DONE OR USE A vRef<>
00239      */
00240     static LocalSite* getDefaultPeer();
00241 
00242     /** Set the local site with which new remote sites will peered.
00243         @param localsite the default peer
00244      */
00245     static void setDefaultPeer(LocalSite* localsite);
00246 
00247     /** Get the current table of known sites, which maps host aliases to sites.
00248         @return the site table
00249     */
00250     static const map<string,Site*>& getAllSites();
00251 
00252 
00253     /** Save a message block template for later retrival and execution.
00254         @param m the message block
00255     */
00256     virtual void addMessageBlock(MessageBlock* m);
00257 
00258     /** Remove a saved message block template.
00259         @param m the message block
00260     */
00261     virtual void removeMessageBlock(MessageBlock* m);
00262 
00263     /** Retrive a saved message block template.
00264         @param s the name of the message block.
00265         @return The message block, or 0 if not found.   NOTE YOU MUST CALL release() WHEN DONE OR USE A vRef<>
00266     */
00267     virtual MessageBlock* getMessageBlock(const string& s) throw (NoSuchMessageBlockError);
00268 
00269     virtual bool isConnected() = 0;
00270 
00271     virtual void sendMessage(Message* m);
00272     virtual void sendMessage(MessageBlock* m);
00273 
00274     virtual void trapOutgoingReply(Message* m);
00275     virtual bool getGreeted();
00276     virtual void setGreeted(bool g);
00277     virtual void addNotification(NotifyEvent* ev) = 0;
00278     virtual void flushNotifications() = 0;
00279     virtual void lockNotificationFlush() = 0;
00280     virtual void unlockNotificationFlush() = 0;
00281 
00282     virtual const string getType();
00283     virtual Site& getSite();
00284 
00285     virtual void insertMessageFilter(int pos, MessageFilter* mf);
00286     virtual void removeMessageFilter(int pos);
00287     virtual void removeMessageFilter(MessageFilter* m);
00288     virtual const deque<MessageFilter*>& getMessageFilters();
00289 
00290     static string detectHostname(int fd, unsigned char ipaddr[4]);
00291     static struct hostent* gethostbyaddr_locked(const char *addr, int len, int type);
00292     static struct hostent* gethostbyname_locked(const char *addr);
00293     static struct hostent* hostent_deepcopy(struct hostent* hp);
00294     static void freeHostEnt(struct hostent* hp);
00295 
00296     static void doSitePeering(LocalSite* ls, RemoteSite* rs, bool isspooftest, bool waitforhello);
00297 
00298     virtual ~Site();
00299 };
00300 }
00301 
00302 #endif

Generated on Tue Aug 12 03:55:42 2003 for Interreality Project - VOS by doxygen 1.3.2