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

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

Go to the documentation of this file.
00001 #ifndef _LOCALVIPSITE_HH_
00002 #define _LOCALVIPSITE_HH_
00003 
00004 #include <stdexcept>
00005 #include <set>
00006 
00007 namespace VOS {
00008     class LocalVipSiteExtension;
00009 }
00010 
00011 #include <vos/vip/socketmultiplexer.hh>
00012 #include <vos/vos/siteextension.hh>
00013 #include <vos/vos/remotevipsite.hh>
00014 #include <vos/vos/iterator.hh>
00015 #include <vos/vos/sitepeering.hh>
00016 
00017 #define VOS_DEFAULT_PORT 4231
00018 
00019 namespace VOS {
00020     /** @class RVSEIterator localvipsite.hh vos/vos/localvipsite.hh
00021         @ingroup libvos
00022 
00023         Iterator subclass used to iterate over pointers to
00024         RemoteVipSiteExtension objects.
00025     */
00026     class VOS_API RVSEIterator : public VUtil::Iterator<RemoteVipSiteExtension*>
00027     {
00028     public:
00029         RVSEIterator(const RVSEIterator& i) : VUtil::Iterator<RemoteVipSiteExtension*>(i) { }
00030         RVSEIterator(const std::set<RemoteVipSiteExtension*>& rsses) {
00031             items->resize(rsses.size());
00032             int n = 0;
00033             for(std::set<RemoteVipSiteExtension*>::const_iterator i = rsses.begin();
00034                 i != rsses.end();
00035                 i++)
00036             {
00037                 (*items)[n] = *i;
00038                 n++;
00039             }
00040         }
00041 
00042         RemoteVipSiteExtension* operator*() {
00043             if(pos < items->size()) return (*items)[pos];
00044             else return 0;
00045         }
00046     };
00047 
00048 
00049     /** @class LocalVipSiteExtension localvipsite.hh vos/vos/localvipsite.hh
00050         @ingroup libvos
00051 
00052         This class sets up the actual vip that the VOS site will
00053         listen on and manages a list of the remote sites we are
00054         connected to.
00055     */
00056     class VOS_API LocalVipSiteExtension : public LocalSiteExtension, public VIP::NewConnectionCallback
00057     {
00058     private:
00059         unsigned short int listenport;
00060 
00061         VUtil::read_write_mutex remoteSites_mutex;
00062         std::set<RemoteVipSiteExtension*> remoteSites;
00063 
00064         VUtil::vRef<VIP::SocketMultiplexer> sktmpx;
00065 
00066         void init(const std::string& defaultHostname, unsigned short int port);
00067     public:
00068         /** When creating an outgoing connection, the remote site will
00069             be peered with this local site.
00070          */
00071         static LocalVipSiteExtension* defaultLocalVipSite;
00072 
00073         /** Default constructor.  This will attempt to autodetect your
00074             hostname.  It will listen on the first available port it
00075             finds, starting from 4231 and working its way upward.
00076             This will use the VOS_HOSTNAME environment variable (in
00077             the form hostname:port) to define the hostname and port.
00078          */
00079         LocalVipSiteExtension();
00080 
00081         /** Use the supplied hostname and port.
00082             @param defaultHostname the canonical hostname for this site
00083             @param port the TCP port for the site to listen on
00084             @throw PortBindingError if the port is not available.
00085          */
00086         LocalVipSiteExtension(const std::string& defaultHostname, unsigned short int port);
00087 
00088         /** Use the supplied hostname.  Will use the first available
00089             port, starting from 4231 and working upward.
00090             @param defaultHostname the canonical hostname for this site
00091          */
00092         LocalVipSiteExtension(const std::string& defaultHostname);
00093 
00094         /** Use the supplied port.  Will autodetect hostname, or take
00095             the canonical hostname from VOS_HOSTNAME (ignoring the
00096             port!)
00097 
00098             @param port the TCP port to listen on
00099             @throw PortBindingError if the port is not available.
00100          */
00101         LocalVipSiteExtension(unsigned short int port);
00102 
00103         /** Destructor */
00104         virtual ~LocalVipSiteExtension();
00105 
00106         /** Add a remote site that we're peered with.
00107             @param rs the RemoteVipSiteExtension object
00108             representing the connection to the remote site
00109          */
00110         virtual void addRemoteSite(VOS::RemoteSiteExtension* rs);
00111 
00112         /** Remove a remote site that we're peered with.
00113             @param rs the RemoteVipSiteExtension object
00114             representing the connection to the remote site
00115          */
00116         virtual void removeRemoteSite(VOS::RemoteSiteExtension* rs);
00117 
00118         /** @return the remote sites we are peered with */
00119         RVSEIterator getRemoteSites();
00120 
00121         /** Called by the Site object indicating that this extension
00122             has been attached to that site.
00123         */
00124         virtual void siteExtensionAttachedTo(Site* st);
00125 
00126         VUtil::vRef<VIP::SocketMultiplexer> getSocketMultiplexer()
00127             { return sktmpx; }
00128 
00129         virtual void notifyNewConnection(VIP::Connection* m, bool inbound);
00130     };
00131 
00132     class VOS_API VipSitePeeringThread : public SitePeeringThread
00133     {
00134     private:
00135         LocalVipSiteExtension* lvse;
00136         RemoteVipSiteExtension* rvse;
00137         std::string host;
00138         int port;
00139         VUtil::vRef<VIP::Connection> conn;
00140 
00141     public:
00142 
00143         /** Construct object and initialize it so that
00144             SitePeeringThread::operator()() will use the supplied
00145             existing vip.
00146 
00147             @param l the local vip site that is the local peer
00148             @param sock the vip file descriptor
00149             @param sd the vip address of the remote site, used to
00150             do a reverse-lookup and figure out the hostname
00151             @param isClient are we a client?  default false
00152          */
00153         VipSitePeeringThread(LocalVipSiteExtension* l, VIP::Connection* c,
00154                              bool isClient = false);
00155 
00156         /** Construct object and initialize it so that
00157             SitePeeringThread::operator()() will attempt an outgoing
00158             connection on the supplied host and port.
00159 
00160             @param l the local vip site that is the local peer
00161             @param host the host to connect to
00162             @param port the port to connect on
00163             @param isClient are we a client?  default true
00164          */
00165         VipSitePeeringThread(LocalVipSiteExtension* l,
00166                              const std::string& host,
00167                              int port, bool isClient = true);
00168 
00169         /** Copy constructor, required because boost::thread makes a
00170             copy of the thread object it runs.
00171          */
00172         VipSitePeeringThread(const VipSitePeeringThread& spt);
00173 
00174         virtual void init();
00175 
00176         virtual void done();
00177     };
00178 
00179 }
00180 
00181 #endif