| 1 | /*************************************************************************** |
|---|
| 2 | * rpcServer.cc |
|---|
| 3 | * |
|---|
| 4 | * Sat Mar 29 2008 |
|---|
| 5 | * Copyright 2008 Dmytro Milinevskyy |
|---|
| 6 | * milinevskyy@gmail.com |
|---|
| 7 | ****************************************************************************/ |
|---|
| 8 | |
|---|
| 9 | /* |
|---|
| 10 | * This program is free software; you can redistribute it and/or modify |
|---|
| 11 | * it under the terms of the GNU Lesser General Public License version 2.1 as published by |
|---|
| 12 | * the Free Software Foundation; |
|---|
| 13 | * |
|---|
| 14 | * This program is distributed in the hope that it will be useful, |
|---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | * GNU Library General Public License for more details. |
|---|
| 18 | * |
|---|
| 19 | * You should have received a copy of the GNU Lesser General Public License |
|---|
| 20 | * along with this program; if not, write to the Free Software |
|---|
| 21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 22 | */ |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * vim indentation settings |
|---|
| 26 | * set tabstop=4 |
|---|
| 27 | * set shiftwidth=4 |
|---|
| 28 | */ |
|---|
| 29 | |
|---|
| 30 | #include <libdodo/directives.h> |
|---|
| 31 | |
|---|
| 32 | #include <libdodo/rpcServer.h> |
|---|
| 33 | #include <libdodo/ioChannel.h> |
|---|
| 34 | #include <libdodo/types.h> |
|---|
| 35 | #include <libdodo/rpcValue.h> |
|---|
| 36 | #include <libdodo/rpcResponse.h> |
|---|
| 37 | #include <libdodo/rpcMethod.h> |
|---|
| 38 | #include <libdodo/exceptionBasic.h> |
|---|
| 39 | |
|---|
| 40 | using namespace dodo::rpc; |
|---|
| 41 | |
|---|
| 42 | server::server(io::channel &io) : defaultHandler(&rpcDefaultHandler), |
|---|
| 43 | io(io) |
|---|
| 44 | { |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | //------------------------------------------------------------------- |
|---|
| 48 | |
|---|
| 49 | server::~server() |
|---|
| 50 | { |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | //------------------------------------------------------------------- |
|---|
| 54 | |
|---|
| 55 | response |
|---|
| 56 | server::rpcDefaultHandler(const dodo::string &, |
|---|
| 57 | const dodoArray<value> &, |
|---|
| 58 | const void *, |
|---|
| 59 | void *) |
|---|
| 60 | { |
|---|
| 61 | response response; |
|---|
| 62 | response.fault("rpcDefaultHandler"); |
|---|
| 63 | |
|---|
| 64 | return response; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | //------------------------------------------------------------------- |
|---|
| 68 | |
|---|
| 69 | void |
|---|
| 70 | server::setDefault(handler handler) |
|---|
| 71 | { |
|---|
| 72 | defaultHandler = handler; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | //------------------------------------------------------------------- |
|---|
| 76 | |
|---|
| 77 | void |
|---|
| 78 | server::setHandler(const dodo::string &method, |
|---|
| 79 | handler handler) |
|---|
| 80 | { |
|---|
| 81 | handlers.insert(std::make_pair(method, handler)); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | //------------------------------------------------------------------- |
|---|
| 85 | |
|---|
| 86 | void |
|---|
| 87 | server::removeHandler(const dodo::string &method) |
|---|
| 88 | { |
|---|
| 89 | handlers.erase(method); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | //------------------------------------------------------------------- |
|---|
| 93 | |
|---|
| 94 | void |
|---|
| 95 | server::serve() |
|---|
| 96 | { |
|---|
| 97 | dodo_try { |
|---|
| 98 | method meth = processCallRequest(); |
|---|
| 99 | |
|---|
| 100 | dodoMap<dodo::string, handler, dodoMapStringCompare>::iterator handler = handlers.find(meth.name); |
|---|
| 101 | |
|---|
| 102 | if (handler == handlers.end()) |
|---|
| 103 | processCallResult(defaultHandler(meth.name, meth.arguments, NULL, NULL)); |
|---|
| 104 | else |
|---|
| 105 | processCallResult(handler->second(meth.name, meth.arguments, NULL, NULL)); |
|---|
| 106 | } dodo_catch (exception::basic *e UNUSED) { |
|---|
| 107 | response response; |
|---|
| 108 | response.fault(e->errStr); |
|---|
| 109 | |
|---|
| 110 | processCallResult(response); |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | //------------------------------------------------------------------- |
|---|