Sirikata
space/src/ServerMessageQueue.hpp
Go to the documentation of this file.
00001 /*  Sirikata
00002  *  ServerMessageQueue.hpp
00003  *
00004  *  Copyright (c) 2010, 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_SERVER_MESSAGE_QUEUE_HPP_
00034 #define _SIRIKATA_SERVER_MESSAGE_QUEUE_HPP_
00035 
00036 #include <sirikata/core/util/Platform.hpp>
00037 #include <sirikata/space/SpaceContext.hpp>
00038 #include <sirikata/space/SpaceNetwork.hpp>
00039 #include <sirikata/space/CoordinateSegmentation.hpp>
00040 #include "RateEstimator.hpp"
00041 
00042 namespace Sirikata{
00043 
00044 class ServerMessageQueue : public SpaceNetwork::SendListener, CoordinateSegmentation::Listener {
00045 public:
00049     class Sender {
00050       public:
00051         virtual ~Sender() {}
00052 
00057         virtual Message* serverMessagePull(ServerID dest) = 0;
00058 
00059         virtual bool serverMessageEmpty(ServerID dest) = 0;
00060     };
00061 
00062     ServerMessageQueue(SpaceContext* ctx, SpaceNetwork* net, Sender* sender);
00063     virtual ~ServerMessageQueue();
00064 
00068     virtual void messageReady(ServerID sid) = 0;
00069 
00070     // Get the total used weight feeding into this queue. (Sum of used_weight's
00071     // received via updateReceiverStats()).
00072     double totalUsedWeight();
00073     // Get the capacity of this receiver in bytes per second.
00074     double capacity();
00075 
00076     // Invoked by Forwarder when it needs to update the weight for a given
00077     // server.  Implementations shouldn't override, instead they should
00078     // implement the protected handleUpdateSenderStats which will occur on
00079     // receiver strand.
00080     void updateReceiverStats(ServerID sid, double total_weight, double used_weight);
00081     bool isBlocked() const{
00082         return mBlocked;
00083     }
00084   protected:
00085     //actually initiate a connection, must be called from the mSenderStrand
00086     void connect(const ServerID&dest);
00087     // SpaceNetwork::SendListener Interface
00088     virtual void networkReadyToSend(const ServerID& from) = 0;
00089     // CoordinateSegmentation::Listener Interface
00090     virtual void updatedSegmentation(CoordinateSegmentation* cseg, const std::vector<SegmentationInfo>& new_segmentation);
00091 
00092     // ServerMessageReceiver Protected (Implementation) Interface
00093     virtual void handleUpdateReceiverStats(ServerID sid, double total_weight, double used_weight) = 0;
00094 
00095     // Tries to send the Message to the SpaceNetwork, and tags it for analysis if
00096     // successful. Helper method for implementations.
00097     // If sent, returns the size of the serialized packet.  Otherwise, returns 0.
00098     uint32 trySend(const ServerID& addr, const Message* msg);
00099     double mCapacityOverestimate;
00100     SpaceContext* mContext;
00101     Network::IOStrand* mSenderStrand;
00102     SpaceNetwork* mNetwork;
00103     TimeProfiler::Stage* mProfiler;
00104     Sender* mSender;
00105     typedef std::tr1::unordered_map<ServerID, SpaceNetwork::SendStream*> SendStreamMap;
00106     SendStreamMap mSendStreams;
00107 
00108     // Total weights are handled by the main strand since that's the only place
00109     // they are needed. Handling of used weights is implementation dependent and
00110     // goes to the receiver strand.
00111     typedef std::tr1::unordered_map<ServerID, double> WeightMap;
00112     WeightMap mUsedWeights;
00113     double mUsedWeightSum;
00114 
00115     SimpleRateEstimator mCapacityEstimator;
00116     bool mBlocked; // Implementations should set this when the network gets
00117                    // blocked.  It will cause us to report actual measured
00118                    // capacity instead of an overestimate.
00119 };
00120 }
00121 
00122 #endif //_SIRIKATA_SERVER_MESSAGE_QUEUE_HPP