| 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 |
#ifndef __NET_RPC_LIB_RPC_UTIL_H__ |
|---|
| 33 |
#define __NET_RPC_LIB_RPC_UTIL_H__ |
|---|
| 34 |
|
|---|
| 35 |
#include <string> |
|---|
| 36 |
#include <vector> |
|---|
| 37 |
|
|---|
| 38 |
#include <whisperlib/net/base/address.h> |
|---|
| 39 |
#include <whisperlib/net/rpc/lib/types/rpc_all_types.h> |
|---|
| 40 |
#include <whisperlib/net/rpc/lib/codec/rpc_codec_id.h> |
|---|
| 41 |
#include <whisperlib/net/rpc/lib/client/irpc_client_connection.h> |
|---|
| 42 |
|
|---|
| 43 |
namespace rpc { |
|---|
| 44 |
// Creates an all-in-one address containing all the given parameters. |
|---|
| 45 |
// e.g.: "http://192.168.2.3:8081/rpc/manager/master?codec=json" |
|---|
| 46 |
string CreateUri(const net::HostPort& host_port, |
|---|
| 47 |
const string& path, |
|---|
| 48 |
rpc::CONNECTION_TYPE connection_type, |
|---|
| 49 |
rpc::CODEC_ID codec_id); |
|---|
| 50 |
|
|---|
| 51 |
// The reverse of CreateUri. |
|---|
| 52 |
// input: |
|---|
| 53 |
// [IN] address: the all-in-one master address to be parsed |
|---|
| 54 |
// [OUT] host_port: receives the ip & port |
|---|
| 55 |
// [OUT] path: receives the request path (usefull only for HTTP) |
|---|
| 56 |
// [OUT] connection_type: receives the connection type (HTTP, TCP) |
|---|
| 57 |
// [OUT] codec_id: receives the codec id (RPC_CID_BINARY, RPC_CID_JSON) |
|---|
| 58 |
// returns: |
|---|
| 59 |
// success state. |
|---|
| 60 |
// e.g.: |
|---|
| 61 |
// for uri: "http://192.168.2.3:8081/rpc/manager/master?codec=json" returns: |
|---|
| 62 |
// host_port: 192.168.2.3:8081 |
|---|
| 63 |
// path: "/rpc/manager/master" |
|---|
| 64 |
// connection_type: rpc::CONNECTION_HTTP |
|---|
| 65 |
// codec_id: RPC_CID_JSON |
|---|
| 66 |
// for uri: "tcp://192.168.2.3:1234?codec=binary" returns: |
|---|
| 67 |
// host_port: 192.168.2.3:1234 |
|---|
| 68 |
// path: "" |
|---|
| 69 |
// connection_type: rpc::CONNECTION_TCP |
|---|
| 70 |
// codec_id: RPC_CID_BINARY |
|---|
| 71 |
bool ParseUri(const string& uri, |
|---|
| 72 |
net::HostPort& host_port, |
|---|
| 73 |
string& path, |
|---|
| 74 |
rpc::CONNECTION_TYPE& connection_type, |
|---|
| 75 |
rpc::CODEC_ID& codec_id); |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
namespace rpc_util { |
|---|
| 79 |
|
|---|
| 80 |
template<typename T> |
|---|
| 81 |
void AppendToRpcArrayT(const vector<T>& v, rpc::Array<T>& out) { |
|---|
| 82 |
uint32 i = out.Size(); |
|---|
| 83 |
out.Resize(out.Size() + v.size()); |
|---|
| 84 |
for ( typename vector<T>::const_iterator it = v.begin(); |
|---|
| 85 |
it != v.end(); ++it, ++i ) { |
|---|
| 86 |
const T& t = *it; |
|---|
| 87 |
out.Set(i, t); |
|---|
| 88 |
} |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
template<typename T> |
|---|
| 92 |
rpc::Array<T> MakeRpcArrayT(const vector<T>& v) { |
|---|
| 93 |
rpc::Array<T> out; |
|---|
| 94 |
AppendToRpcArrayT(v, out); |
|---|
| 95 |
return out; |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
template <typename T> |
|---|
| 99 |
void AppendToStdArrayT(const rpc::Array<T>& v, vector<T>& out) { |
|---|
| 100 |
out.reserve(out.size() + v.Size()); |
|---|
| 101 |
for ( uint32 i = 0; i < v.Size(); ++i ) { |
|---|
| 102 |
out.push_back(v.Get(i)); |
|---|
| 103 |
} |
|---|
| 104 |
} |
|---|
| 105 |
|
|---|
| 106 |
template <typename T> |
|---|
| 107 |
vector<T> MakeStdArrayT(const rpc::Array<T>& v) { |
|---|
| 108 |
vector<T> out; |
|---|
| 109 |
AppendToStdArrayT(v, out); |
|---|
| 110 |
return out; |
|---|
| 111 |
} |
|---|
| 112 |
|
|---|
| 113 |
///////////////////////////////////////////////////////////////////////// |
|---|
| 114 |
// specializations of MakeRpcArray, MakeStdArray for basic types: string, int .. |
|---|
| 115 |
|
|---|
| 116 |
void AppendToRpcArray(const vector<string>& v, rpc::Array<rpc::String>& out); |
|---|
| 117 |
void AppendToRpcArray(const set<string>& v, rpc::Array<rpc::String>& out); |
|---|
| 118 |
rpc::Array<rpc::String> MakeRpcArray(const vector<string>& v); |
|---|
| 119 |
rpc::Array<rpc::String> MakeRpcArray(const set<string>& v); |
|---|
| 120 |
|
|---|
| 121 |
void AppendToStdArray(const rpc::Array<rpc::String>& v, vector<string>& out); |
|---|
| 122 |
vector<string> MakeStdArray(const rpc::Array<rpc::String>& v); |
|---|
| 123 |
|
|---|
| 124 |
}; // namespace rpc_util |
|---|
| 125 |
|
|---|
| 126 |
#endif // __NET_RPC_LIB_RPC_UTIL_H__ |
|---|