Sirikata
libcore/include/sirikata/core/odp/DelegateService.hpp
Go to the documentation of this file.
00001 // Copyright (c) 2010 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_ODP_DELEGATE_SERVICE_HPP_
00006 #define _SIRIKATA_ODP_DELEGATE_SERVICE_HPP_
00007 
00008 #include <sirikata/core/odp/Service.hpp>
00009 #include <sirikata/core/xdp/DelegatePort.hpp>
00010 
00011 namespace Sirikata {
00012 namespace ODP {
00013 
00014 class DelegateService;
00015 
00016 typedef Sirikata::XDP::DelegatePort<Endpoint, DelegateService, Port> DelegatePort;
00017 
00033 class SIRIKATA_EXPORT DelegateService : public Service {
00034 public:
00035     typedef std::tr1::function<DelegatePort*(DelegateService*,const SpaceObjectReference&,PortID)> PortCreateFunction;
00036 
00043     DelegateService(PortCreateFunction create_func);
00044 
00045     virtual ~DelegateService();
00046 
00047     // Service Interface
00048     virtual Port* bindODPPort(const SpaceID& space, const ObjectReference& objref, PortID port);
00049     virtual Port* bindODPPort(const SpaceObjectReference& sor, PortID port);
00050     virtual Port* bindODPPort(const SpaceID& space, const ObjectReference& objref);
00051     virtual Port* bindODPPort(const SpaceObjectReference& sor);
00052     virtual PortID unusedODPPort(const SpaceID& space, const ObjectReference& objref);
00053     virtual PortID unusedODPPort(const SpaceObjectReference& sor);
00054     virtual void registerDefaultODPHandler(const MessageHandler& cb);
00055 
00062     bool deliver(const Endpoint& src, const Endpoint& dst, MemoryReference data) const;
00063 
00064     // Deallocates the port by removing it from the data structure.  Should only
00065     // be used by DelegatePort destructor.
00066     // This should be private with friend class DelegatePort but that doesn't
00067     // work because DelegatePort is a typedef. It doesn't particularly matter
00068     // since other's shouldn't see DelegatePort* anyway, they use Port*.
00069     void deallocatePort(DelegatePort* port);
00070 private:
00071     typedef std::tr1::unordered_map<PortID, DelegatePort*, PortID::Hasher> PortMap;
00072     typedef std::tr1::unordered_map<SpaceObjectReference, PortMap*, SpaceObjectReference::Hasher> SpacePortMap;
00073 
00074     // Helper. Gets an existing PortMap for the specified space or returns NULL
00075     // if one doesn't exist yet.
00076     PortMap* getPortMap(const SpaceObjectReference& sor) const;
00077     // Helper. Gets an existing PortMap for the specified space or creates one
00078     // if one doesn't exist yet.
00079     PortMap* getOrCreatePortMap(const SpaceObjectReference& sor);
00080 
00081     PortCreateFunction mCreator;
00082     SpacePortMap mSpacePortMap;
00083     MessageHandler mDefaultHandler;
00084 }; // class DelegateService
00085 
00086 } // namespace ODP
00087 } // namespace Sirikata
00088 
00089 #endif //_SIRIKATA_ODP_DELEGATE_SERVICE_HPP_