Sirikata
liboh/include/sirikata/oh/SpaceNodeConnection.hpp
Go to the documentation of this file.
00001 /*  Sirikata
00002  *  SpaceNodeConnection.hpp
00003  *
00004  *  Copyright (c) 2009, Ewen Cheslack-Postava
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions are
00009  *  met:
00010  *  * Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  *  * Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in
00014  *    the documentation and/or other materials provided with the
00015  *    distribution.
00016  *  * Neither the name of Sirikata nor the names of its contributors may
00017  *    be used to endorse or promote products derived from this software
00018  *    without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00021  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00022  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00023  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00024  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00025  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00026  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  */
00032 
00033 #ifndef _SIRIKATA_LIBOH_SPACE_NODE_CONNECTION_HPP_
00034 #define _SIRIKATA_LIBOH_SPACE_NODE_CONNECTION_HPP_
00035 
00036 #include <sirikata/oh/Platform.hpp>
00037 #include <sirikata/oh/ObjectHostContext.hpp>
00038 #include <sirikata/core/service/Service.hpp>
00039 #include <sirikata/core/service/TimeProfiler.hpp>
00040 #include <sirikata/core/network/ObjectMessage.hpp>
00041 
00042 #include <sirikata/core/network/Stream.hpp>
00043 #include <sirikata/core/util/SpaceID.hpp>
00044 #include <sirikata/core/util/Liveness.hpp>
00045 
00046 #include "QueueRouterElement.hpp"
00047 
00048 namespace Sirikata {
00049 
00050 // Connections to servers
00051 struct SIRIKATA_OH_EXPORT SpaceNodeConnection : public Liveness {
00052   public:
00053     typedef OHDPSST::Stream OHSSTStream;
00054     typedef OHSSTStream::Ptr OHSSTStreamPtr;
00055     typedef OHDPSST::Endpoint OHSSTEndpoint;
00056     typedef OHDPSST::BaseDatagramLayer OHSSTBaseDatagramLayer;
00057     typedef OHDPSST::BaseDatagramLayer::Ptr OHSSTBaseDatagramLayerPtr;
00058 
00059     typedef std::tr1::function<void(SpaceNodeConnection*)> GotSpaceConnectionCallback;
00060     typedef std::tr1::function<void(SpaceNodeConnection*)> ReceiveCallback;
00061 
00062     typedef std::tr1::function<void(const Network::Stream::ConnectionStatus, const std::string&)> ConnectionEventCallback;
00063 
00064     SpaceNodeConnection(ObjectHostContext* ctx, Network::IOStrand* ioStrand, TimeProfiler::Stage* handle_read_stage, OptionSet *streamOptions, const SpaceID& spaceid, ServerID sid, OHDP::Service* ohdp_service, ConnectionEventCallback ccb, ReceiveCallback rcb);
00065     ~SpaceNodeConnection();
00066 
00067     // Push a packet to be sent out
00068     bool push(const ObjectMessage& msg);
00069 
00070     // Pull a packet from the receive queue
00071     ObjectMessage* pull();
00072 
00073     bool empty();
00074     void shutdown();
00075 
00076     const SpaceID& space() const { return mSpace; }
00077     const ServerID& server() const { return mServer; }
00078 
00079     OHSSTStreamPtr stream() const { return mOHSSTStream; }
00080 
00081     void connect(const Network::Address& addr);
00082     void failConnection();
00083 
00084     bool connecting() const { return mConnecting; }
00085 
00086     void addCallback(GotSpaceConnectionCallback cb) { mConnectCallbacks.push_back(cb); }
00087     void invokeAndClearCallbacks(bool connected);
00088   private:
00089     // Thread Safe
00090     ObjectHostContext* mContext;
00091     TimeProfiler::Stage* mHandleReadStage;
00092     SpaceID mSpace;
00093     ServerID mServer;
00094     OHDP::Service* mOHDPService;
00095     Network::Stream* socket;
00096     Network::Address mAddr;
00097 
00098     // Callback for connection event
00099     void handleConnectionEvent(const Network::Stream::ConnectionStatus status, const std::string&reason);
00100 
00101     void setupConnectionStream(const Network::Stream::ConnectionStatus status, const std::string& reason, uint8 retries);
00102 
00103     // Callback for stream connection event
00104     void handleStreamConnected(const Network::Stream::ConnectionStatus status, const std::string& reason, int err, OHSSTStreamPtr strm, uint8 retries);
00105 
00106     // Callback for when the connection receives data
00107     void handleRead(const Sirikata::Network::Chunk& chunk, const Sirikata::Network::Stream::PauseReceiveCallback& pause);
00108 
00109     // Main Strand
00110     typedef std::vector<GotSpaceConnectionCallback> ConnectionCallbackList;
00111     ConnectionCallbackList mConnectCallbacks;
00112     bool mConnecting;
00113 
00114     // IO Strand
00115     QueueRouterElement<ObjectMessage> receive_queue;
00116 
00117     ConnectionEventCallback mConnectCB;
00118     ReceiveCallback mReceiveCB;
00119 
00120     OHSSTStreamPtr mOHSSTStream;
00121 };
00122 
00123 } // namespace Sirikata
00124 
00125 
00126 #endif //_SIRIKATA_LIBOH_SPACE_NODE_CONNECTION_HPP_