Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "srv-msg-router.h"
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
00047 MessageRouter::~MessageRouter(void) throw() { }
00048 StateUpdates::~StateUpdates(void) throw() { }
00049
00050
00051
00052 static const int s_major = 0;
00053 static const int s_minor = 1;
00054
00055
00057
00058
00059
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
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
00163
00164 static const handler_entry_t s_handlers[] = {
00165
00166
00167
00168 HANDLER_ENTRY("converse-reply", handlerConverseReply)
00169 HANDLER_ENTRY("tcp", handlerTcpConnect)
00170 HANDLER_ENTRY("new-game", handlerNewGame)
00171 HANDLER_ENTRY("shutdown", handlerShutdown)
00172
00173
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
00194 return p->handler;
00195 }
00196 }
00197
00198
00199 DPRINTF("Unrecognized command: '%s'", cmd);
00200 return NULL;
00201 }
00202
00203
00204
00206
00207
00208
00210
00211 class Router : public MessageRouter {
00212 public:
00213 ~Router(void) throw() { }
00214
00215
00216 void initialize(void);
00217
00218
00219 msg_handler_fn getHandler(IN const tcp_payload_t& payload);
00220
00221 private:
00222
00223
00224
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
00247
00248 return lookupHandler(payload.namespace_.c_str(),
00249 payload.command.c_str());
00250 }
00251
00252
00253
00255
00256
00257
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 };
00277