Sirikata
libspace/include/sirikata/space/ObjectHostSession.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_LIBSPACE_OBJECT_HOST_SESSION_HPP_
00006 #define _SIRIKATA_LIBSPACE_OBJECT_HOST_SESSION_HPP_
00007 
00008 #include <sirikata/space/Platform.hpp>
00009 #include <sirikata/core/util/ListenerProvider.hpp>
00010 #include <sirikata/core/ohdp/SST.hpp>
00011 #include <sirikata/space/SpaceContext.hpp>
00012 
00013 namespace Sirikata {
00014 
00015 // These classes are thin wrappers around ObjectHostConnectionManager and it's
00016 // affiliated classes for tracking sessions
00017 
00018 class SIRIKATA_SPACE_EXPORT ObjectHostSession {
00019   public:
00020     ObjectHostSession(const OHDP::NodeID& id, OHDPSST::Stream::Ptr strm)
00021         : mID(id),
00022         mSSTStream(strm),
00023         mSeqNo(new SeqNo())
00024         {
00025         }
00026 
00027     ~ObjectHostSession() {
00028         if (mSSTStream) mSSTStream->close(true);
00029     }
00030 
00031     const OHDP::NodeID& id() const { return mID; }
00032     const OHDPSST::Stream::Ptr& stream() const { return mSSTStream; }
00033     const SeqNoPtr& seqNoPtr() const { return mSeqNo; }
00034 
00035   private:
00036     OHDP::NodeID mID;
00037     OHDPSST::Stream::Ptr mSSTStream;
00038     SeqNoPtr mSeqNo;
00039 };
00040 typedef std::tr1::shared_ptr<ObjectHostSession> ObjectHostSessionPtr;
00041 
00042 class SIRIKATA_SPACE_EXPORT ObjectHostSessionListener {
00043   public:
00044     virtual ~ObjectHostSessionListener() {}
00045 
00046     virtual void onObjectHostSession(const OHDP::NodeID& id, ObjectHostSessionPtr) {}
00047     virtual void onObjectHostSessionEnded(const OHDP::NodeID& id) {}
00048 };
00049 
00054 class ObjectHostSessionManager : public Provider<ObjectHostSessionListener*> {
00055   public:
00056     ObjectHostSessionManager(SpaceContext* ctx) {
00057         ctx->mObjectHostSessionManager = this;
00058     }
00059 
00060     // Owner interface
00061     void fireObjectHostSession(const OHDP::NodeID& id, OHDPSST::Stream::Ptr oh_stream) {
00062         ObjectHostSessionPtr sess(new ObjectHostSession(id, oh_stream));
00063         mOHSessions[id] = sess;
00064         notify(&ObjectHostSessionListener::onObjectHostSession, id, sess);
00065 
00066     }
00067     void fireObjectHostSessionEnded(const OHDP::NodeID& id) {
00068         notify(&ObjectHostSessionListener::onObjectHostSessionEnded, id);
00069         ObjectHostSessionMap::const_iterator it = mOHSessions.find(id);
00070         if (it != mOHSessions.end())
00071             mOHSessions.erase(it);
00072     }
00073 
00074 
00075     // User interface
00076     ObjectHostSessionPtr getSession(const OHDP::NodeID& id) {
00077         ObjectHostSessionMap::const_iterator it = mOHSessions.find(id);
00078         if (it == mOHSessions.end()) return ObjectHostSessionPtr();
00079         return it->second;
00080     }
00081 
00082   private:
00083     typedef std::tr1::unordered_map<OHDP::NodeID, ObjectHostSessionPtr, OHDP::NodeID::Hasher> ObjectHostSessionMap;
00084     ObjectHostSessionMap mOHSessions;
00085 };
00086 
00087 } // namespace Sirikata
00088 
00089 #endif //_SIRIKATA_LIBSPACE_OBJECT_HOST_SESSION_HPP_