| 1 |
// Copyright (c) 2009, Whispersoft s.r.l. |
|---|
| 2 |
// All rights reserved. |
|---|
| 3 |
// |
|---|
| 4 |
// Redistribution and use in source and binary forms, with or without |
|---|
| 5 |
// modification, are permitted provided that the following conditions are |
|---|
| 6 |
// met: |
|---|
| 7 |
// |
|---|
| 8 |
// * Redistributions of source code must retain the above copyright |
|---|
| 9 |
// notice, this list of conditions and the following disclaimer. |
|---|
| 10 |
// * Redistributions in binary form must reproduce the above |
|---|
| 11 |
// copyright notice, this list of conditions and the following disclaimer |
|---|
| 12 |
// in the documentation and/or other materials provided with the |
|---|
| 13 |
// distribution. |
|---|
| 14 |
// * Neither the name of Whispersoft s.r.l. nor the names of its |
|---|
| 15 |
// contributors may be used to endorse or promote products derived from |
|---|
| 16 |
// this software without specific prior written permission. |
|---|
| 17 |
// |
|---|
| 18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 19 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 20 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 21 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 22 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 25 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 26 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 27 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 28 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 29 |
// |
|---|
| 30 |
// Author: Cosmin Tudorache |
|---|
| 31 |
|
|---|
| 32 |
#include "common/base/log.h" |
|---|
| 33 |
#include "net/rpc/lib/types/rpc_message.h" |
|---|
| 34 |
#include "net/rpc/lib/log_indent.h" |
|---|
| 35 |
|
|---|
| 36 |
namespace rpc { |
|---|
| 37 |
|
|---|
| 38 |
const char* MessageTypeName(int32 type) { |
|---|
| 39 |
switch ( type ) { |
|---|
| 40 |
case RPC_CALL: return "Call"; |
|---|
| 41 |
case RPC_REPLY: return "Reply"; |
|---|
| 42 |
default: return "Unknown"; |
|---|
| 43 |
} |
|---|
| 44 |
} |
|---|
| 45 |
|
|---|
| 46 |
const char* ReplyStatusName(int32 status) { |
|---|
| 47 |
switch ( status ) { |
|---|
| 48 |
case RPC_SUCCESS: |
|---|
| 49 |
// RPC executed successfully. |
|---|
| 50 |
return "Success"; |
|---|
| 51 |
case RPC_SERVICE_UNAVAIL: |
|---|
| 52 |
// Service not registered. |
|---|
| 53 |
return "ServiceNotFound"; |
|---|
| 54 |
case RPC_PROC_UNAVAIL: |
|---|
| 55 |
// No such procedure in the given service. |
|---|
| 56 |
return "ProcedureNotFound"; |
|---|
| 57 |
case RPC_GARBAGE_ARGS: |
|---|
| 58 |
// Illegal number of params or wrong type for at least 1 param. |
|---|
| 59 |
return "GarbageArguments"; |
|---|
| 60 |
case RPC_SYSTEM_ERR: |
|---|
| 61 |
// RPC Server internal error |
|---|
| 62 |
return "SystemError"; |
|---|
| 63 |
case RPC_SERVER_BUSY: |
|---|
| 64 |
// Too many parallel calls in execution. |
|---|
| 65 |
return "ServerBusy"; |
|---|
| 66 |
case RPC_QUERY_TIMEOUT: |
|---|
| 67 |
// RPC query timeout. |
|---|
| 68 |
return "QueryTimeout"; |
|---|
| 69 |
case RPC_TOO_MANY_QUERIES: |
|---|
| 70 |
// RPC Client transport flooded. |
|---|
| 71 |
return "TooManyQueries"; |
|---|
| 72 |
case RPC_CONN_CLOSED: |
|---|
| 73 |
// RPC Client transport closed. |
|---|
| 74 |
return "ConnectionClosed"; |
|---|
| 75 |
case RPC_CONN_ERROR: |
|---|
| 76 |
// RPC Client transport error. |
|---|
| 77 |
return "ConnectionError"; |
|---|
| 78 |
default: |
|---|
| 79 |
return "Unknown"; |
|---|
| 80 |
} |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
string rpc::Message::Header::ToString() const { |
|---|
| 85 |
ostringstream ss; |
|---|
| 86 |
ss << "rpc::Header:" << endl; |
|---|
| 87 |
ss << INDENT_RPCHEADER "xid: " << xid_ << endl; |
|---|
| 88 |
ss << INDENT_RPCHEADER "type: " << msgType_ |
|---|
| 89 |
<< " (" << rpc::MessageTypeName(msgType_) << ")"; |
|---|
| 90 |
// no newline! the caller will end the log entry. |
|---|
| 91 |
return ss.str(); |
|---|
| 92 |
} |
|---|
| 93 |
|
|---|
| 94 |
rpc::Message::CallBody::CallBody() |
|---|
| 95 |
: service_(), method_(), params_() { |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
rpc::Message::CallBody::~CallBody() { |
|---|
| 99 |
} |
|---|
| 100 |
string rpc::Message::CallBody::ToString() const { |
|---|
| 101 |
ostringstream ss; |
|---|
| 102 |
ss << "rpc::CallBody: " << endl; |
|---|
| 103 |
ss << INDENT_RPCBODY "service: " << service_ << endl; |
|---|
| 104 |
ss << INDENT_RPCBODY "method: " << method_ << endl; |
|---|
| 105 |
#ifdef _DEBUG |
|---|
| 106 |
ss << INDENT_RPCBODY "params: [" << params_.DebugString() << "]"; |
|---|
| 107 |
#endif |
|---|
| 108 |
// no newline! the aller will end it |
|---|
| 109 |
return ss.str(); |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
rpc::Message::ReplyBody::ReplyBody() |
|---|
| 113 |
: replyStatus_(), result_() { |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
rpc::Message::ReplyBody::~ReplyBody() { |
|---|
| 117 |
} |
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
string rpc::Message::ReplyBody::ToString() const { |
|---|
| 121 |
ostringstream ss; |
|---|
| 122 |
ss << "rpc::ReplyBody:" << endl |
|---|
| 123 |
<< INDENT_RPCBODY "status: " << replyStatus_ << " " |
|---|
| 124 |
<< rpc::ReplyStatusName(replyStatus_) << endl; |
|---|
| 125 |
#ifdef _DEBUG |
|---|
| 126 |
ss << INDENT_RPCBODY "result: " << result_.DebugString(); |
|---|
| 127 |
#endif |
|---|
| 128 |
// no newline! the caller will end it |
|---|
| 129 |
return ss.str(); |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
#ifdef _DEBUG |
|---|
| 133 |
bool rpc::Message::Header::operator==(const rpc::Message::Header& h) const { |
|---|
| 134 |
return (xid_ == h.xid_ && |
|---|
| 135 |
msgType_ == h.msgType_); |
|---|
| 136 |
} |
|---|
| 137 |
bool rpc::Message::CallBody::operator==( |
|---|
| 138 |
const rpc::Message::CallBody& c) const { |
|---|
| 139 |
return (service_ == c.service_ && |
|---|
| 140 |
method_ == c.method_ && |
|---|
| 141 |
strutil::StrTrim(params_.DebugString()) == |
|---|
| 142 |
strutil::StrTrim(c.params_.DebugString())); |
|---|
| 143 |
} |
|---|
| 144 |
bool rpc::Message::ReplyBody::operator==( |
|---|
| 145 |
const rpc::Message::ReplyBody& r) const { |
|---|
| 146 |
return (replyStatus_ == r.replyStatus_ && |
|---|
| 147 |
strutil::StrTrim(result_.DebugString()) == |
|---|
| 148 |
strutil::StrTrim(r.result_.DebugString())); |
|---|
| 149 |
} |
|---|
| 150 |
bool rpc::Message::operator==(const rpc::Message& msg) const { |
|---|
| 151 |
return header_ == msg.header_ && |
|---|
| 152 |
((header_.msgType_ == RPC_CALL && |
|---|
| 153 |
cbody_ == msg.cbody_) || |
|---|
| 154 |
(header_.msgType_ == RPC_REPLY && |
|---|
| 155 |
rbody_ == msg.rbody_)); |
|---|
| 156 |
} |
|---|
| 157 |
#else |
|---|
| 158 |
bool rpc::Message::Header::operator==(const rpc::Message::Header& h) const { |
|---|
| 159 |
LOG_ERROR << "=======>>> PROBLEM - do not compare messages in RELEASE MODE "; |
|---|
| 160 |
return true; |
|---|
| 161 |
} |
|---|
| 162 |
bool rpc::Message::CallBody::operator==( |
|---|
| 163 |
const rpc::Message::CallBody& c) const { |
|---|
| 164 |
LOG_ERROR << "=======>>> PROBLEM - do not compare messages in RELEASE MODE "; |
|---|
| 165 |
return true; |
|---|
| 166 |
} |
|---|
| 167 |
bool rpc::Message::ReplyBody::operator==( |
|---|
| 168 |
const rpc::Message::ReplyBody& r) const { |
|---|
| 169 |
LOG_ERROR << "=======>>> PROBLEM - do not compare messages in RELEASE MODE "; |
|---|
| 170 |
return true; |
|---|
| 171 |
} |
|---|
| 172 |
bool rpc::Message::operator==(const rpc::Message& msg) const { |
|---|
| 173 |
LOG_ERROR << "=======>>> PROBLEM - do not compare messages in RELEASE MODE "; |
|---|
| 174 |
return true; |
|---|
| 175 |
} |
|---|
| 176 |
#endif |
|---|
| 177 |
|
|---|
| 178 |
string rpc::Message::ToString() const { |
|---|
| 179 |
ostringstream ss; |
|---|
| 180 |
const Header& header = header_; |
|---|
| 181 |
ss << "rpc::Message:" << endl |
|---|
| 182 |
<< INDENT_RPCMESSAGE << header << endl; |
|---|
| 183 |
if ( header_.msgType_ == RPC_CALL ) { |
|---|
| 184 |
const CallBody& body = cbody_; |
|---|
| 185 |
ss << INDENT_RPCMESSAGE << body; |
|---|
| 186 |
// no newline! the caller will end it |
|---|
| 187 |
} else { |
|---|
| 188 |
CHECK_EQ(header_.msgType_, RPC_REPLY); |
|---|
| 189 |
// for additional types consider adding another else branch |
|---|
| 190 |
const ReplyBody& body = rbody_; |
|---|
| 191 |
ss << INDENT_RPCMESSAGE << body; // no newline! the caller will end it |
|---|
| 192 |
} |
|---|
| 193 |
return ss.str(); |
|---|
| 194 |
} |
|---|
| 195 |
} |
|---|