00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef LSM_IPC_H
00020 #define LSM_IPC_H
00021
00022 #include "libstoragemgmt/libstoragemgmt_common.h"
00023 #include <yajl/yajl_parse.h>
00024 #include <yajl/yajl_gen.h>
00025 #include <stdint.h>
00026 #include <string>
00027 #include <map>
00028 #include <vector>
00029 #include <sstream>
00030 #include <stdexcept>
00031
00032
00033
00039 class LSM_DLL_LOCAL Transport {
00040 public:
00041
00045 const static int HDR_LEN = 10;
00046
00051 Transport();
00052
00057 Transport(int socket_desc);
00058
00062 ~Transport();
00063
00070 int msg_send(const std::string & msg, int &error_code);
00071
00079 std::string msg_recv(int &error_code);
00080
00087 static int socket_get(const std::string & path, int &error_code);
00088
00093 int close();
00094
00095 private:
00096 int s;
00097 };
00098
00104 template < class Type > static std::string to_string(Type v)
00105 {
00106 std::stringstream out;
00107 out << v;
00108 return out.str();
00109 }
00110
00115 class LSM_DLL_LOCAL EOFException:public std::runtime_error {
00116 public:
00117 EOFException(std::string m);
00118 };
00119
00120
00124 class LSM_DLL_LOCAL ValueException:public std::runtime_error {
00125 public:
00130 ValueException(std::string m);
00131 };
00132
00136 class LSM_DLL_LOCAL LsmException:public std::runtime_error {
00137 public:
00138
00144 LsmException(int code, std::string & msg);
00145
00152 LsmException(int code, std::string & msg, const std::string & debug_addl);
00153
00161 LsmException(int code, std::string & msg,
00162 const std::string & debug_addl,
00163 const std::string & debug_data_addl);
00164
00168 ~LsmException() throw();
00169
00170 int error_code;
00171 std::string debug;
00172 std::string debug_data;
00173 };
00174
00178 class LSM_DLL_LOCAL Value {
00179 public:
00180
00184 enum value_type {
00185 null_t, boolean_t, string_t, numeric_t, object_t, array_t
00186 };
00187
00191 Value(void);
00192
00197 Value(bool v);
00198
00203 Value(double v);
00204 Value(long double v);
00205
00210 Value(uint32_t v);
00211
00216 Value(int32_t v);
00217
00222 Value(uint64_t v);
00223
00228 Value(int64_t v);
00229
00235 Value(value_type type, const std::string & v);
00236
00241 Value(const char *v);
00242
00247 Value(const std::string & v);
00248
00253 Value(const std::map < std::string, Value > &v);
00254
00259 Value(const std::vector < Value > &v);
00260
00265 std::string serialize(void);
00266
00271 value_type valueType() const;
00272
00278 Value & operator[] (const std::string & key);
00279
00285 Value & operator[] (uint32_t i);
00286
00291 bool hasKey(const std::string & k);
00292
00297 bool isValidRequest(void);
00298
00304 Value getValue(const char *key);
00305
00309 const char *asNumString();
00310
00315 void *asVoid();
00316
00321 bool asBool();
00322
00327 double asDouble();
00328 long double asLongDouble();
00329
00334 int32_t asInt32_t();
00335
00340 int64_t asInt64_t();
00341
00346 uint32_t asUint32_t();
00347
00352 uint64_t asUint64_t();
00353
00358 std::string asString();
00359
00364 const char *asC_str();
00365
00370 std::map < std::string, Value > asObject();
00371
00376 std::vector < Value > asArray();
00377
00378 private:
00379 value_type t;
00380 std::string s;
00381 std::map < std::string, Value > obj;
00382 std::vector < Value > array;
00383
00384 void marshal(yajl_gen g);
00385 };
00386
00390 class LSM_DLL_LOCAL Payload {
00391 public:
00397 static std::string serialize(Value & v);
00398
00404 static Value deserialize(const std::string & json);
00405 };
00406
00407 class LSM_DLL_LOCAL Ipc {
00408 public:
00412 Ipc();
00413
00418 Ipc(int fd);
00419
00424 Ipc(std::string socket_path);
00425
00429 ~Ipc();
00430
00437 void requestSend(const std::string request, const Value & params,
00438 int32_t id = 100);
00443 Value readRequest(void);
00444
00450 void responseSend(const Value & response, uint32_t id = 100);
00451
00456 Value responseRead();
00457
00465 void errorSend(int error_code, std::string msg, std::string debug,
00466 uint32_t id = 100);
00467
00475 Value rpc(const std::string & request, const Value & params,
00476 int32_t id = 100);
00477
00478
00479 private:
00480 Transport t;
00481 };
00482
00483 #endif