Sirikata
libspace/include/sirikata/space/ServerMessage.hpp
Go to the documentation of this file.
00001 /*  Sirikata
00002  *  ServerMessage.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_SERVER_MESSAGE_HPP_
00034 #define _SIRIKATA_SERVER_MESSAGE_HPP_
00035 
00036 #include <sirikata/space/Platform.hpp>
00037 
00038 #include <sirikata/core/network/Message.hpp>
00039 #include <sirikata/core/network/ObjectMessage.hpp>
00040 
00041 #include "Protocol_ServerMessage.pbj.hpp"
00042 
00043 
00044 namespace Sirikata {
00045 
00046 class SpaceContext;
00047 
00048 typedef uint16 ServerMessagePort;
00049 
00050 // List of well known server ports.
00051 // FIXME Reduce the number of these ports (combine related ones), reorder, and renumber
00052 #define SERVER_PORT_OBJECT_MESSAGE_ROUTING     1
00053 #define SERVER_PORT_LOCATION                   13
00054 #define SERVER_PORT_PROX                       11
00055 #define SERVER_PORT_MIGRATION                  4
00056 #define SERVER_PORT_CSEG_CHANGE                6
00057 #define SERVER_PORT_LOAD_STATUS                7
00058 // 8 was previously SERVER_PORT_OSEG_MIGRATE_MOVE
00059 #define SERVER_PORT_OSEG_MIGRATE_ACKNOWLEDGE   9
00060 #define SERVER_PORT_OSEG_UPDATE                15
00061 #define SERVER_PORT_FORWARDER_WEIGHT_UPDATE    16
00062 #define SERVER_PORT_UNPROCESSED_PACKET         0xFFFF
00063 
00067 class SIRIKATA_SPACE_EXPORT Message {
00068 public:
00069     Message(const ServerID& origin);
00070     Message(ServerID src, uint16 src_port, ServerID dest, ServerID dest_port);
00071     Message(ServerID src, uint16 src_port, ServerID dest, uint16 dest_port, const std::string& pl);
00072     Message(ServerID src, uint16 src_port, ServerID dest, uint16 dest_port, const Sirikata::Protocol::Object::ObjectMessage* pl);
00073 
00074     ServerID source_server() const { return mImpl.source_server(); }
00075     void set_source_server(const ServerID sid);
00076 
00077     uint16 source_port() const { return mImpl.source_port(); }
00078     void set_source_port(const uint16 port) { mImpl.set_source_port(port); }
00079 
00080     ServerID dest_server() const { return mImpl.dest_server(); }
00081     void set_dest_server(const ServerID sid) { mImpl.set_dest_server(sid); }
00082 
00083     uint16 dest_port() const { return mImpl.dest_port(); }
00084     void set_dest_port(const uint16 port) { mImpl.set_dest_port(port); }
00085 
00086     UniqueMessageID id() const { return mImpl.id(); }
00087     // NOTE: We don't expose set_id() so we can guarantee they will be unique
00088 
00089     UniqueMessageID payload_id() const { return mImpl.payload_id(); }
00090     // NOTE: We don't expose set_id() so we guarantee it gets created properly.
00091     // Use the constructor taking an ObjectMessage to ensure this works properly.
00092 
00093     std::string payload() const { return mImpl.payload(); }
00094     void set_payload(const std::string& pl) { mImpl.set_payload(pl); }
00095 
00096 
00097     bool ParseFromString(const std::string& data) {
00098         return mImpl.ParseFromString(data);
00099     }
00100     bool ParseFromArray(const void* data, int size) {
00101         return mImpl.ParseFromArray(data, size);
00102     }
00103 
00104     // Deprecated. Remains for backwards compatibility.
00105     bool serialize(Network::Chunk* result) const;
00106     static Message* deserialize(const Network::Chunk& wire);
00107 
00108     // Deprecated. Remains for backwards compatibility.
00109     uint32 serializedSize() const;
00110     uint32 size() const { return serializedSize(); }
00111 
00112 protected:
00113     // Note: Should only be used for deserialization to ensure unique ID's are handled properly
00114     Message();
00115 
00116     void set_id(const UniqueMessageID _id) { mImpl.set_id(_id); }
00117     void set_payload_id(const UniqueMessageID _id) { mImpl.set_payload_id(_id); }
00118 private:
00119     // Helper methods to fill in message data
00120     void fillMessage(ServerID src, uint16 src_port, ServerID dest, uint16 dest_port);
00121     void fillMessage(ServerID src, uint16 src_port, ServerID dest, ServerID dest_port, const std::string& pl);
00122 
00123     Sirikata::Protocol::Server::ServerMessage mImpl;
00124     mutable uint32 mCachedSize;
00125 }; // class Message
00126 
00127 
00128 
00129 
00131 class SIRIKATA_SPACE_EXPORT MessageRecipient {
00132 public:
00133     virtual ~MessageRecipient() {}
00134 
00135     virtual void receiveMessage(Message* msg) = 0;
00136 }; // class MessageRecipient
00137 
00138 
00140 class SIRIKATA_SPACE_EXPORT ServerMessageDispatcher {
00141 public:
00142     ServerMessageDispatcher(SpaceContext* ctx);
00143     virtual ~ServerMessageDispatcher() {}
00144 
00145     void registerMessageRecipient(ServerMessagePort type, MessageRecipient* recipient);
00146     void unregisterMessageRecipient(ServerMessagePort type, MessageRecipient* recipient);
00147 
00148 
00149 protected:
00150     virtual void dispatchMessage(Message* msg) const;
00151 
00152 
00153 private:
00154     typedef std::map<ServerMessagePort, MessageRecipient*> MessageRecipientMap;
00155     MessageRecipientMap mMessageRecipients;
00156 
00157 }; // class ServerMessageDispatcher
00158 
00159 template<typename MessageType>
00160 class Router {
00161   public:
00162     virtual ~Router() {}
00163     WARN_UNUSED
00164     virtual bool route(MessageType msg) = 0;
00165 }; // class Router
00166 
00168 class SIRIKATA_SPACE_EXPORT ServerMessageRouter {
00169 public:
00170     ServerMessageRouter(SpaceContext* ctx);
00171     virtual ~ServerMessageRouter() {}
00172 
00173     virtual Router<Message*>* createServerMessageService(const String& name) = 0;
00174 };
00175 
00176 } // namespace Sirikata
00177 
00178 #endif //_SIRIKATA_SERVER_MESSAGE_HPP_