Sirikata
libcore/include/sirikata/core/ohdp/DelegateService.hpp
Go to the documentation of this file.
00001 // Copyright (c) 2011 Sirikata Authors. All rights reserved.
00002 // Use of this source code is governed by a BSD-style license that can
00003 // be found in the LICENSE file.
00004 
00005 #ifndef _SIRIKATA_OHDP_DELEGATE_SERVICE_HPP_
00006 #define _SIRIKATA_OHDP_DELEGATE_SERVICE_HPP_
00007 
00008 #include <sirikata/core/ohdp/Service.hpp>
00009 #include <sirikata/core/xdp/DelegatePort.hpp>
00010 
00011 namespace Sirikata {
00012 namespace OHDP {
00013 
00014 class DelegateService;
00015 
00016 typedef Sirikata::XDP::DelegatePort<Endpoint, DelegateService, Port> DelegatePort;
00017 
00022 class SIRIKATA_EXPORT DelegateService : public Service {
00023 public:
00024     typedef std::tr1::function<DelegatePort*(DelegateService*, const Endpoint&)> PortCreateFunction;
00025 
00032     DelegateService(PortCreateFunction create_func);
00033 
00034     virtual ~DelegateService();
00035 
00036     // Service Interface
00037     virtual Port* bindOHDPPort(const SpaceID& space, const NodeID& node, PortID port);
00038     virtual Port* bindOHDPPort(const SpaceID& space, const NodeID& node);
00039     virtual PortID unusedOHDPPort(const SpaceID& space, const NodeID& node);
00040     virtual void registerDefaultOHDPHandler(const MessageHandler& cb);
00041 
00048     bool deliver(const Endpoint& src, const Endpoint& dst, MemoryReference data) const;
00049 
00050     // Deallocates the port by removing it from the data structure.  Should only
00051     // be used by DelegatePort destructor.
00052     // This should be private with friend class DelegatePort but that doesn't
00053     // work because DelegatePort is a typedef. It doesn't particularly matter
00054     // since other's shouldn't see DelegatePort* anyway, they use Port*.
00055     void deallocatePort(DelegatePort* port);
00056 private:
00057     // We need a key that includes SpaceID and NodeID. This is a minimal pair
00058     // class to serve that purpose.
00059     struct SpaceIDNodeID : public TotallyOrdered<SpaceIDNodeID> {
00060         SpaceIDNodeID() {}
00061         SpaceIDNodeID(const SpaceID& _s, const NodeID& _n)
00062          : space(_s), node(_n)
00063         {}
00064 
00065         // Basic comparisons, rest are provided by TotallyOrdered
00066         bool operator==(const SpaceIDNodeID& rhs) const;
00067         bool operator<(const SpaceIDNodeID& rhs) const;
00068 
00069         struct Hasher {
00070             size_t operator()(const SpaceIDNodeID& snid) const;
00071         };
00072 
00073         SpaceID space;
00074         NodeID node;
00075     };
00076 
00077     typedef std::tr1::unordered_map<PortID, DelegatePort*, PortID::Hasher> PortMap;
00078     typedef std::tr1::unordered_map<SpaceIDNodeID, PortMap*, SpaceIDNodeID::Hasher> SpacePortMap;
00079 
00080     // Helper. Gets an existing PortMap for the specified space or returns NULL
00081     // if one doesn't exist yet.
00082     PortMap* getPortMap(const SpaceIDNodeID& snid) const;
00083     // Helper. Gets an existing PortMap for the specified space or creates one
00084     // if one doesn't exist yet.
00085     PortMap* getOrCreatePortMap(const SpaceIDNodeID& snid);
00086 
00087     PortCreateFunction mCreator;
00088     SpacePortMap mSpacePortMap;
00089     MessageHandler mDefaultHandler;
00090 }; // class DelegateService
00091 
00092 } // namespace OHDP
00093 } // namespace Sirikata
00094 
00095 #endif //_SIRIKATA_OHDP_DELEGATE_SERVICE_HPP_