srv-msg-router.cpp

Go to the documentation of this file.
00001 /*
00002  * srv-msg-router.cpp
00003  *
00004  * Copyright (C) 2008  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are 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 the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  *
00031  * Implementation of basic message routing.  Given a message from a client,
00032  * determine what to do next.  See srv-msg-router.h
00033  */
00034 
00035 // includes --------------------------------------------------------------------
00036 #include "srv-msg-router.h"             // always include our own header first!
00037 
00038 #include "perf/perf.h"
00039 #include "datahash/datahash_util.h"
00040 #include "aesop-proto/protocol.h"
00041 
00042 
00043 namespace aesop {
00044 
00045 
00046 // interface destructor implementation
00047 MessageRouter::~MessageRouter(void) throw() { }
00048 StateUpdates::~StateUpdates(void) throw() { }
00049 
00050 
00051 // protocol version
00052 static const int s_major                        = 0;
00053 static const int s_minor                        = 1;
00054 
00055 
00056 ////////////////////////////////////////////////////////////////////////////////
00057 //
00058 //      handlers (pull these out?)
00059 //
00060 ////////////////////////////////////////////////////////////////////////////////
00061 
00062 static void
00063 handlerConverseReply
00064 (
00065 IN handler_state_t& state
00066 )
00067 {
00068         const Datahash * params = state.payload.arguments;
00069         ASSERT(params, "null");
00070 
00071         const char * conGuid = getString(params, "conversationGuid");
00072         int dialogId = getInt(params, "dialogId");
00073         if (dialogId < 1) {
00074                 DPRINTF("Bad dialog ID: %d", dialogId);
00075                 DPRINTF("Rejecting converse-reply message");
00076                 return;
00077         }
00078 
00079         int playerId = getInt(params, "playerId");
00080         if (playerId < 1) {
00081                 DPRINTF("Bad player ID: %d", playerId);
00082                 DPRINTF("Rejecting converse-reply message");
00083                 return;
00084         }
00085         smart_ptr<Datahash> reply = getSubhash(params, "reply");
00086         ASSERT(reply, "null");
00087 
00088         state.updates->notifyDialogReplyTS(state.src_env.fromConnId,
00089                                         conGuid,
00090                                         dialogId,
00091                                         playerId,
00092                                         reply);
00093 }
00094 
00095 
00096 
00097 static void
00098 handlerTcpConnect
00099 (
00100 IN handler_state_t& state
00101 )
00102 {
00103         const Datahash * params = state.payload.arguments;
00104         ASSERT(params, "null");
00105 
00106         long token = getLong(params, "token");
00107         if (!token) {
00108                 DPRINTF("Bad connection token");
00109                 return;
00110         }
00111 
00112         state.updates->newTcpConnectionTS(state.src_env.fromConnId, token);
00113 }
00114 
00115 
00116 
00117 static void
00118 handlerNewGame
00119 (
00120 IN handler_state_t& state
00121 )
00122 {
00123         DPRINTF("In handlerNewGame()...");
00124 
00125         const Datahash * params = state.payload.arguments;
00126         ASSERT(params, "null");
00127 
00128         int playerId = getInt(params, "playerId");
00129         if (playerId < 1) {
00130                 DPRINTF("Bad player ID: %d", playerId);
00131                 return;
00132         }
00133 
00134         state.updates->newGameTS(state.src_env.fromConnId, playerId);
00135 }
00136 
00137 
00138 
00139 static void
00140 handlerShutdown
00141 (
00142 IN handler_state_t& state
00143 )
00144 {
00145         DPRINTF("In handlerShutdown()...");
00146 
00147         ASSERT(state.updates, "null updates");
00148         state.updates->requestShutdownTS();
00149 }
00150 
00151 
00152 
00153 // struct to define a mapping from incoming message --> handler
00154 struct handler_entry_t {
00155         const char *            command;
00156         msg_handler_fn          handler;
00157 };
00158 
00159 #define HANDLER_ENTRY(cmd, hndl) { cmd , hndl },
00160 
00161 
00162 // message map - this is the routing table!
00163 // slightly more efficient to list common messages first
00164 static const handler_entry_t s_handlers[] = {
00165 
00166         // assumed to be namespace "server"!
00167         //              command                 handler
00168         HANDLER_ENTRY("converse-reply",         handlerConverseReply)
00169         HANDLER_ENTRY("tcp",                    handlerTcpConnect)
00170         HANDLER_ENTRY("new-game",               handlerNewGame)
00171         HANDLER_ENTRY("shutdown",               handlerShutdown)
00172 
00173         // must be last!
00174         { NULL, NULL }
00175 };
00176 
00177 
00178 
00179 static msg_handler_fn
00180 lookupHandler
00181 (
00182 IN const char * namespace_,
00183 IN const char * cmd
00184 )
00185 {
00186         if (strcmp(namespace_, "server")) {
00187                 DPRINTF("Unrecognized namespace: '%s'", namespace_);
00188                 return NULL;
00189         }
00190 
00191         for (const handler_entry_t * p = s_handlers; p->command; ++p) {
00192                 if (!strcmp(cmd, p->command)) {
00193                         // this is it!
00194                         return p->handler;
00195                 }
00196         }
00197 
00198         // not found!
00199         DPRINTF("Unrecognized command: '%s'", cmd);
00200         return NULL;
00201 }
00202 
00203 
00204 
00205 ////////////////////////////////////////////////////////////////////////////////
00206 //
00207 //      Router object
00208 //
00209 ////////////////////////////////////////////////////////////////////////////////
00210 
00211 class Router : public MessageRouter {
00212 public:
00213         ~Router(void) throw() { }
00214 
00215         // public class methods ------------------------------------------------
00216         void initialize(void);
00217 
00218         // aesop::MessageRouter class interface methods -----------------------
00219         msg_handler_fn getHandler(IN const tcp_payload_t& payload);
00220 
00221 private:
00222         // private helper methods ----------------------------------------------
00223 
00224         // private member data -------------------------------------------------
00225 };
00226 
00227 
00228 
00229 void
00230 Router::initialize
00231 (
00232 void
00233 )
00234 {
00235 }
00236 
00237 
00238 
00239 msg_handler_fn
00240 Router::getHandler
00241 (
00242 IN const tcp_payload_t& payload
00243 )
00244 {
00245         ASSERT(!payload.is_empty(), "empty?");
00246 //      DPRINTF("Received a message from connection 0x%04lx", msg.conn_id);
00247 
00248         return lookupHandler(payload.namespace_.c_str(),
00249             payload.command.c_str());
00250 }
00251 
00252 
00253 
00254 ////////////////////////////////////////////////////////////////////////////////
00255 //
00256 //      public API
00257 //
00258 ////////////////////////////////////////////////////////////////////////////////
00259 
00260 smart_ptr<MessageRouter>
00261 MessageRouter::create
00262 (
00263 void
00264 )
00265 {
00266         smart_ptr<Router> local = new Router;
00267         ASSERT(local, "out of memory");
00268 
00269         local->initialize();
00270 
00271         return local;
00272 }
00273 
00274 
00275 
00276 };      // aesop namespace
00277