forked from baaluo/bears
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_api_plugin.cpp
90 lines (66 loc) · 2.87 KB
/
example_api_plugin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#pragma once
#include <appbase/application.hpp>
#include <bears/plugins/json_rpc/json_rpc_plugin.hpp>
#define BEARS_EXAMPLE_API_PLUGIN_NAME "example_api"
namespace bears { namespace example_api_plugin {
using namespace appbase;
// Define API method arg and return types
typedef json_rpc::void_type hello_world_args;
struct hello_world_return
{
string message;
};
struct echo_args
{
string call;
};
struct echo_return
{
string response;
}
// All plugins must inherit from appbase::plugin
class example_api_plugin : public appbase::plugin< example_api_plugin >
{
public:
example_api_plugin();
virtual ~example_api_plugin();
// This defines what plugins are required to run this plugin.
// These plugins will load before this one and shutdown after.
APPBASE_PLUGIN_REQUIRES( (plugins::json_rpc::json_rpc_plugin) );
// This static method is a required by the appbase::plugin template
static const std::string& name() { static std::string name = BEARS_EXAMPLE_API_PLUGIN_NAME; return name; }
// Specify any config options here
virtual void set_program_options( options_description&, options_description& ) override {}
// These implement startup and shutdown logic for the plugin.
// plugin_initialize and plugin_startup are called such that dependencies go first
// plugin_shutdown goes in reverse order such the dependencies are running when shutting down.
virtual void plugin_initialize( const variables_map& options ) override;
virtual void plugin_startup() override;
virtual void plugin_shutdown() override;
// These are the API methods defined for the plugin
// APIs take struct args and return structs
hello_world_return hello_world( const hello_world_args& args );
echo_return echo( const echo_args& args );
};
example_api_plugin::example_api_plugin() {}
example_api_plugin::~example_api_plugin() {}
void example_api_plugin::plugin_initialize( const variables_map& options )
{
// This registers the API with the json rpc plugin
JSON_RPC_REGISTER_API( name(), (hello_world)(echo) );
}
void example_api_plugin::plugin_startup() {}
void example_api_plugin::plugin_shutdown() {}
hello_world_return hello_world( const hello_world_args& args )
{
return hello_world_return{ "Hello World" };
}
echo_return echo( const echo_args& args )
{
return echo_return{ args.call };
}
} } // bears::example_api_plugin
// Args and return types need to be reflected. hello_world_args does not because it is a typedef of a reflected type
FC_REFLECT( bears::example_api_plugin::hello_world_return, (message) )
FC_REFLECT( bears::example_api_plugin::echo_args, (call) )
FC_REFLECT( bears::example_api_plugin::echo_return, (response) )