rpc_json.h
//
// Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// See accompanying file COPYING.TXT file for licensing details.
//
#ifndef CPPCMS_RPC_JSON_OBJECT_H
#define CPPCMS_RPC_JSON_OBJECT_H
#include <cppcms/application.h>
#include <booster/function.h>
#include <cppcms/json.h>
#include <cppcms/cppcms_error.h>
namespace cppcms {
namespace rpc {
class CPPCMS_API call_error : public cppcms_error {
public:
call_error(std::string const &message);
};
class json_rpc_server;
class CPPCMS_API json_call : public booster::noncopyable {
public:
~json_call();
std::string method();
bool notification();
json::array const &params();
http::context &context();
void return_result(json::value const &);
void return_error(json::value const &);
private:
json_call(http::context &context);
friend class json_rpc_server;
void return_result(http::context &,json::value const &);
void return_error(http::context &,json::value const &);
void attach_context(booster::shared_ptr<http::context> context);
void check_not_notification();
booster::shared_ptr<http::context> context_;
json::value id_;
json::array params_;
std::string method_;
bool notification_;
struct _data;
booster::hold_ptr<_data> d;
};
class CPPCMS_API json_rpc_server : public application {
public:
typedef enum {
any_role,
method_role,
notification_role
} role_type;
typedef booster::function<void(json::array const &)> method_type;
void bind(std::string const &name,method_type const &,role_type type = any_role);
void smd(json::value const &);
void smd_raw(std::string const &);
void smd_from_file(std::string const &);
virtual void main(std::string);
booster::shared_ptr<json_call> release_call();
json_rpc_server(cppcms::service &srv);
~json_rpc_server();
std::string method();
bool notification();
json::array const &params();
void return_result(json::value const &);
void return_error(json::value const &);
private:
void check_call();
struct method_data {
method_type method;
role_type role;
};
typedef std::map<std::string,method_data> methods_map_type;
methods_map_type methods_;
booster::shared_ptr<json_call> current_call_;
std::string smd_;
struct _data;
booster::hold_ptr<_data> d;
};
namespace details {
template<typename T> struct fw_ret { typedef T type; };
template<typename T> struct fw_ret<T const &> { typedef T type; };
template<typename T> struct fw_ret<T const> { typedef T type; };
template<> struct fw_ret<json::value> { typedef json::value const &type; };
template<> struct fw_ret<json::object> { typedef json::object const &type; };
template<> struct fw_ret<json::array> { typedef json::array const &type; };
template<> struct fw_ret<std::string> { typedef std::string const &type; };
template<> struct fw_ret<json::value const &> { typedef json::value const &type; };
template<> struct fw_ret<json::object const &> { typedef json::object const &type; };
template<> struct fw_ret<json::array const &> { typedef json::array const &type; };
template<> struct fw_ret<std::string const &> { typedef std::string const &type; };
template<> struct fw_ret<json::value const> { typedef json::value const &type; };
template<> struct fw_ret<json::object const> { typedef json::object const &type; };
template<> struct fw_ret<json::array const> { typedef json::array const &type; };
template<> struct fw_ret<std::string const> { typedef std::string const &type; };
template<typename T>
struct fw_ret_handle {
static typename fw_ret<T>::type extract(json::value const &v)
{
return v.get_value<typename fw_ret<T>::type>();
}
};
template<>
struct fw_ret_handle<json::value const &>
{
static json::value const &extract(json::value const &v)
{
return v;
}
};
template<>
struct fw_ret_handle<json::array const &>
{
static json::array const &extract(json::value const &v)
{
return v.array();
}
};
template<>
struct fw_ret_handle<json::object const &>
{
static json::object const &extract(json::value const &v)
{
return v.object();
}
};
template<>
struct fw_ret_handle<std::string const &>
{
static std::string const &extract(json::value const &v)
{
return v.str();
}
};
template <typename T>
inline typename fw_ret<T>::type forward_value(json::value const &v)
{
typedef typename fw_ret<T>::type return_type;
return fw_ret_handle<return_type>::extract(v);
}
}
#define CPPCMS_JSON_RPC_BINDER(N) \
namespace details { \
template<typename Class,typename Ptr CPPCMS_TEMPLATE_PARAMS> \
struct binder##N { \
Ptr object; \
void (Class::*member)(CPPCMS_FUNC_PARAMS); \
void operator()(json::array const &a) const \
{ \
if(a.size()!=N) \
throw call_error("Invalid parametres number"); \
((*object).*member)(CPPCMS_CALL_PARAMS); \
} \
}; \
} \
template<typename Class,typename Ptr CPPCMS_TEMPLATE_PARAMS> \
details::binder##N<Class,Ptr CPPCMS_BINDER_PARAMS> \
json_method(void (Class::*m)(CPPCMS_FUNC_PARAMS),Ptr p) \
{ details::binder##N<Class,Ptr CPPCMS_BINDER_PARAMS> tmp={p,m}; return tmp; } \
#define CPPCMS_TEMPLATE_PARAMS
#define CPPCMS_FUNC_PARAMS
#define CPPCMS_CALL_PARAMS
#define CPPCMS_BINDER_PARAMS
CPPCMS_JSON_RPC_BINDER(0)
#undef CPPCMS_TEMPLATE_PARAMS
#undef CPPCMS_FUNC_PARAMS
#undef CPPCMS_CALL_PARAMS
#undef CPPCMS_BINDER_PARAMS
#define CPPCMS_TEMPLATE_PARAMS ,typename P1
#define CPPCMS_FUNC_PARAMS P1
#define CPPCMS_CALL_PARAMS forward_value<P1>(a[0])
#define CPPCMS_BINDER_PARAMS ,P1
CPPCMS_JSON_RPC_BINDER(1)
#undef CPPCMS_TEMPLATE_PARAMS
#undef CPPCMS_FUNC_PARAMS
#undef CPPCMS_CALL_PARAMS
#undef CPPCMS_BINDER_PARAMS
#define CPPCMS_TEMPLATE_PARAMS ,typename P1,typename P2
#define CPPCMS_FUNC_PARAMS P1,P2
#define CPPCMS_CALL_PARAMS forward_value<P1>(a[0]), forward_value<P2>(a[1])
#define CPPCMS_BINDER_PARAMS ,P1,P2
CPPCMS_JSON_RPC_BINDER(2)
#undef CPPCMS_TEMPLATE_PARAMS
#undef CPPCMS_FUNC_PARAMS
#undef CPPCMS_CALL_PARAMS
#undef CPPCMS_BINDER_PARAMS
#define CPPCMS_TEMPLATE_PARAMS ,typename P1,typename P2,typename P3
#define CPPCMS_FUNC_PARAMS P1,P2,P3
#define CPPCMS_CALL_PARAMS forward_value<P1>(a[0]), forward_value<P2>(a[1]), forward_value<P3>(a[2])
#define CPPCMS_BINDER_PARAMS ,P1,P2,P3
CPPCMS_JSON_RPC_BINDER(3)
#undef CPPCMS_TEMPLATE_PARAMS
#undef CPPCMS_FUNC_PARAMS
#undef CPPCMS_CALL_PARAMS
#undef CPPCMS_BINDER_PARAMS
#define CPPCMS_TEMPLATE_PARAMS ,typename P1,typename P2,typename P3,typename P4
#define CPPCMS_FUNC_PARAMS P1,P2,P3,P4
#define CPPCMS_CALL_PARAMS forward_value<P1>(a[0]), forward_value<P2>(a[1]), forward_value<P3>(a[2]), forward_value<P4>(a[3])
#define CPPCMS_BINDER_PARAMS ,P1,P2,P3,P4
CPPCMS_JSON_RPC_BINDER(4)
#undef CPPCMS_TEMPLATE_PARAMS
#undef CPPCMS_FUNC_PARAMS
#undef CPPCMS_CALL_PARAMS
#undef CPPCMS_BINDER_PARAMS
#undef CPPCMS_JSON_RPC_BINDER
} // rpc
} // cppcms
#endif
cppcms::rpc::json_rpc_server::method_type
booster::function< void(json::array const &)> method_type
Definition: rpc_json.h:128
cppcms::json::value
This class is central representation of json objects.
Definition: json.h:142
booster::hold_ptr< _data >
cppcms::rpc::json_rpc_server::any_role
Method may receive notification and return result.
Definition: rpc_json.h:120
cppcms::service
This class represent the central event loop of the CppCMS applications.
Definition: service.h:59
cppcms::rpc::call_error
The error thrown in case of bad call - parameters mismatch or invalid request.
Definition: rpc_json.h:29
cppcms::cppcms_error
Exception thrown by CppCMS framework.
Definition: cppcms_error.h:22
cppcms::json::object
std::map< string_key, value > object
The json::object - std::map of json::value's.
Definition: json.h:53
cppcms::rpc::json_rpc_server::role_type
role_type
Definition: rpc_json.h:119
cppcms::http::context
context is a central class that holds all specific connection related information. It encapsulates CGI request and response, cache, session and locale information
Definition: http_context.h:45
booster::shared_ptr< http::context >
booster::function< void(json::array const &)>
cppcms::rpc::json_call
This class represents single call of json-rpc method.
Definition: rpc_json.h:46
cppcms::rpc::json_rpc_server::method_role
Method can't be used with notification calls.
Definition: rpc_json.h:121
cppcms::application
application class is the base class for all user created applications.
Definition: application.h:81
booster::locale::message
basic_message< char > message
Definition: message.h:494
cppcms::rpc::json_rpc_server
JSON-RPC service application.
Definition: rpc_json.h:114
cppcms::json::array
std::vector< value > array
The json::array - std::vector of json::value's.
Definition: json.h:49
booster::noncopyable
This class makes impossible to copy any class derived from this one.
Definition: noncopyable.h:15