Sirikata
liboh/include/sirikata/oh/SpaceNodeSession.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_LIBOH_SPACE_NODE_SESSION_HPP_
00006 #define _SIRIKATA_LIBOH_SPACE_NODE_SESSION_HPP_
00007 
00008 #include <sirikata/oh/Platform.hpp>
00009 #include <sirikata/core/util/ListenerProvider.hpp>
00010 #include <sirikata/core/ohdp/SST.hpp>
00011 
00012 namespace Sirikata {
00013 
00014 class SIRIKATA_OH_EXPORT SpaceNodeSessionListener {
00015   public:
00016     virtual ~SpaceNodeSessionListener() {}
00017 
00018     virtual void onSpaceNodeSession(const OHDP::SpaceNodeID& id, OHDPSST::Stream::Ptr sn_stream) {}
00019     virtual void onSpaceNodeSessionEnded(const OHDP::SpaceNodeID& id) {}
00020 };
00021 
00022 typedef Provider<SpaceNodeSessionListener*> SpaceNodeSessionProvider;
00023 
00028 class SpaceNodeSessionManager : public SpaceNodeSessionProvider {
00029   public:
00030     SpaceNodeSessionManager() {
00031     }
00032 
00033     // Owner interface
00034     void fireSpaceNodeSession(const OHDP::SpaceNodeID& id, OHDPSST::Stream::Ptr sn_stream) {
00035         notify(&SpaceNodeSessionListener::onSpaceNodeSession, id, sn_stream);
00036         mSNSessions[id] = sn_stream;
00037     }
00038     void fireSpaceNodeSessionEnded(const OHDP::SpaceNodeID& id) {
00039         notify(&SpaceNodeSessionListener::onSpaceNodeSessionEnded, id);
00040         SpaceNodeSessionMap::const_iterator it = mSNSessions.find(id);
00041         if (it != mSNSessions.end()) {
00042             // Force closure of the stream
00043             if (it->second) it->second->close(true);
00044             mSNSessions.erase(it);
00045         }
00046     }
00047 
00048 
00049     // User interface
00050     OHDPSST::Stream::Ptr getSession(const OHDP::SpaceNodeID& id) {
00051         SpaceNodeSessionMap::const_iterator it = mSNSessions.find(id);
00052         if (it == mSNSessions.end()) return OHDPSST::Stream::Ptr();
00053         return it->second;
00054     }
00055 
00056   private:
00057     typedef std::tr1::unordered_map<OHDP::SpaceNodeID, OHDPSST::Stream::Ptr, OHDP::SpaceNodeID::Hasher> SpaceNodeSessionMap;
00058     SpaceNodeSessionMap mSNSessions;
00059 };
00060 
00061 } // namespace Sirikata
00062 
00063 #endif //_SIRIKATA_LIBOH_SPACE_NODE_SESSION_HPP_