00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00032 #ifndef S3_RPC_H
00033 #define S3_RPC_H
00034
00035 #include <s3fc/s3_exception.h>
00036 #include <s3fc/s3_thread_base.h>
00037 #include <s3fc/s3_message.h>
00038
00039 #include <string>
00040 #include <vector>
00041 #include <map>
00042
00043
00047 struct s3_rpc_common
00048 {
00049
00050 static const std::string cmd_tag_call;
00051
00052 static std::string rpc_server_name(const std::string& name);
00053
00054 static std::string rpc_random_client_name(const std::string& name);
00055 };
00056
00057
00058 template<typename T_obj> class s3_rpc_caller_m;
00059
00063 template <typename T_obj>
00064 struct s3_rpc_type_common
00065 {
00066
00067 typedef std::map<std::string, s3_rpc_caller_m<T_obj>*> callmap_t;
00068
00069 static void clear_callmap(callmap_t& cmap);
00070 };
00071
00077 class s3_rpc_exception : public s3_exception
00078 {
00079 public:
00084 s3_rpc_exception(const std::string& where,
00085 const std::string& what);
00086 };
00087
00112 template<typename T_obj>
00113 class s3_rpc_caller_m
00114 {
00115 public:
00119 virtual void call(T_obj* obj,
00120 const std::vector<std::string>& args) = 0;
00125 virtual unsigned int get_num_params() = 0;
00126 protected:
00136 static s3_rpc_exception err_invalid_args(
00137 const std::string& where,
00138 const std::vector<std::string>& args);
00139 };
00140
00145 template<typename T_obj>
00146 struct s3_rpc_caller_m_v_v : public s3_rpc_caller_m<T_obj>
00147 {
00148 public:
00149
00150 typedef void (T_obj::* methodptr_t)(void);
00151 protected:
00152
00153 methodptr_t method;
00154 public:
00155
00156 s3_rpc_caller_m_v_v(methodptr_t n_method);
00157
00158 virtual void call(T_obj* obj,
00159 const std::vector<std::string>& args);
00160
00161 virtual unsigned int get_num_params();
00162 };
00163
00168 template<typename T_obj, typename T_arg1>
00169 struct s3_rpc_caller_m_v_t1 : public s3_rpc_caller_m<T_obj>
00170 {
00171 public:
00172
00173 typedef void (T_obj::* methodptr_t)(const T_arg1&);
00174 protected:
00175
00176 methodptr_t method;
00177 public:
00178
00179 s3_rpc_caller_m_v_t1(methodptr_t n_method);
00180
00181 virtual void call(T_obj* obj,
00182 const std::vector<std::string>& args);
00183
00184 virtual unsigned int get_num_params();
00185 };
00186
00192 template<typename T_obj, typename T_arg1, typename T_arg2>
00193 struct s3_rpc_caller_m_v_t2 : public s3_rpc_caller_m<T_obj>
00194 {
00195 public:
00196
00197 typedef void (T_obj::* methodptr_t)(const T_arg1&, const T_arg2&);
00198 protected:
00199
00200 methodptr_t method;
00201 public:
00202
00203 s3_rpc_caller_m_v_t2(methodptr_t n_method);
00204
00205 virtual void call(T_obj* obj,
00206 const std::vector<std::string>& args);
00207
00208 virtual unsigned int get_num_params();
00209 };
00210
00223 template<typename T_obj>
00224 struct s3_rpc_callmap;
00225
00226
00253 template<typename T_obj>
00254 class s3_rpc_server : public s3_thread_base
00255 {
00256 protected:
00260 s3_message_box mb;
00264 T_obj* obj;
00268 typename s3_rpc_type_common<T_obj>::callmap_t cmap;
00269
00270 s3_semaphore started;
00271 public:
00279 s3_rpc_server(const std::string& n_name,
00280 T_obj& n_obj);
00284 virtual ~s3_rpc_server();
00285 protected:
00286
00287 virtual void main_loop();
00288
00289 void handle_message(const s3_message& msg);
00290
00291 void exec_cmd_call(const s3_message& msg,
00292 std::string body_str);
00293
00294 std::string remove_first(std::string& str);
00295
00296 static void reply_client(s3_message_box& mb,
00297 const s3_message& msg,
00298 const std::string& err,
00299 bool err);
00300 };
00301
00304 template<typename T_obj>
00305 class s3_rpc_client
00306 {
00307 protected:
00311 s3_message_box mb;
00315 std::string server_name;
00319 typename s3_rpc_type_common<T_obj>::callmap_t cmap;
00320 public:
00326 s3_rpc_client(const std::string& n_server_name);
00330 ~s3_rpc_client();
00336 std::string call(const std::string& method);
00343 template<typename T_arg1>
00344 std::string call(const std::string& method,
00345 const T_arg1& arg1);
00353 template<typename T_arg1, typename T_arg2>
00354 std::string call(const std::string& method,
00355 const T_arg1& arg1,
00356 const T_arg2& arg2);
00357 protected:
00362 std::string call_common(const std::string& method,
00363 const std::string& args);
00373 void assert_method_args(const std::string& where,
00374 const std::string& method,
00375 unsigned int num_args);
00376 };
00377
00378
00379 #include <s3fc/s3_rpc.tcc>
00380
00381 #endif