| 1 | /*************************************************************************** |
|---|
| 2 | * rpcJsonServer.cc |
|---|
| 3 | * |
|---|
| 4 | * Mon Jul 07 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/rpcJsonServer.h> |
|---|
| 33 | #include <libdodo/types.h> |
|---|
| 34 | #include <libdodo/dataFormatJsonProcessor.h> |
|---|
| 35 | #include <libdodo/dataFormatJsonNode.h> |
|---|
| 36 | #include <libdodo/ioChannel.h> |
|---|
| 37 | #include <libdodo/rpcServer.h> |
|---|
| 38 | #include <libdodo/rpcJsonMethod.h> |
|---|
| 39 | #include <libdodo/rpcMethod.h> |
|---|
| 40 | #include <libdodo/rpcJsonResponse.h> |
|---|
| 41 | #include <libdodo/rpcResponse.h> |
|---|
| 42 | #include <libdodo/exceptionBasic.h> |
|---|
| 43 | |
|---|
| 44 | using namespace dodo::rpc::json; |
|---|
| 45 | |
|---|
| 46 | __additional__::__additional__(dodo::string &version, |
|---|
| 47 | long &id) : version(version), |
|---|
| 48 | id(id) |
|---|
| 49 | { |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | //------------------------------------------------------------------- |
|---|
| 53 | |
|---|
| 54 | server::server(io::channel &io) : rpc::server(io), |
|---|
| 55 | rpVersion("1.1"), |
|---|
| 56 | rqId(0), |
|---|
| 57 | rpId(0) |
|---|
| 58 | { |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | //------------------------------------------------------------------- |
|---|
| 62 | |
|---|
| 63 | server::~server() |
|---|
| 64 | { |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | //------------------------------------------------------------------- |
|---|
| 68 | |
|---|
| 69 | dodo::rpc::method |
|---|
| 70 | server::processCallRequest() |
|---|
| 71 | { |
|---|
| 72 | dodo::data::format::json::processor jsonValue; |
|---|
| 73 | |
|---|
| 74 | dodo::data::format::json::node node = jsonValue.process(io); |
|---|
| 75 | |
|---|
| 76 | return method::jsonToMethod(node, rqVersion, rqId); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | //------------------------------------------------------------------- |
|---|
| 80 | |
|---|
| 81 | void |
|---|
| 82 | server::processCallResult(const rpc::response &resp) |
|---|
| 83 | { |
|---|
| 84 | dodo::data::format::json::processor jsonValue; |
|---|
| 85 | |
|---|
| 86 | jsonValue.make(response::responseToJson(resp, rpVersion, rpId), io); |
|---|
| 87 | |
|---|
| 88 | io.flush(); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | //------------------------------------------------------------------- |
|---|
| 92 | |
|---|
| 93 | void |
|---|
| 94 | server::setResponseVersion(const dodo::string &version) |
|---|
| 95 | { |
|---|
| 96 | rpVersion = version; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | //------------------------------------------------------------------- |
|---|
| 100 | |
|---|
| 101 | void |
|---|
| 102 | server::serve() |
|---|
| 103 | { |
|---|
| 104 | dodo_try { |
|---|
| 105 | rpc::method meth = processCallRequest(); |
|---|
| 106 | |
|---|
| 107 | dodo::string version = rqVersion; |
|---|
| 108 | |
|---|
| 109 | __additional__ idata(rqVersion, rqId); |
|---|
| 110 | |
|---|
| 111 | rpId = rqId; |
|---|
| 112 | __additional__ odata(rpVersion, rpId); |
|---|
| 113 | |
|---|
| 114 | dodoMap<dodo::string, handler, dodoMapStringCompare>::iterator handler = handlers.find(meth.name); |
|---|
| 115 | |
|---|
| 116 | if (handler == handlers.end()) |
|---|
| 117 | processCallResult(defaultHandler(meth.name, meth.arguments, &idata, &odata)); |
|---|
| 118 | else |
|---|
| 119 | processCallResult(handler->second(meth.name, meth.arguments, &idata, &odata)); |
|---|
| 120 | |
|---|
| 121 | rpVersion = version; |
|---|
| 122 | } dodo_catch (exception::basic *e UNUSED) { |
|---|
| 123 | rpc::response response; |
|---|
| 124 | response.fault(e->errStr); |
|---|
| 125 | |
|---|
| 126 | rpId = rqId; |
|---|
| 127 | |
|---|
| 128 | processCallResult(response); |
|---|
| 129 | } |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | //------------------------------------------------------------------- |
|---|
| 133 | |
|---|