Robust Distributed System Nucleus (rDSN)  ver 1.0.0
tool_api.h
1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2015 Microsoft Corporation
5  *
6  * -=- Robust Distributed System Nucleus (rDSN) -=-
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26 
27 /*
28  * Description:
29  * define the interface for implementing and plug-in the tools &
30  * runtime components into rDSN.
31  * In rDSN, both developement tools and runtime libraries
32  * (e.g., high performance components) are considered tools.
33  *
34  * Revision history:
35  * Mar., 2015, @imzhenyu (Zhenyu Guo), first version
36  * xxxx-xx-xx, author, fix bug about xxx
37  */
38 
51 # pragma once
52 
53 // providers
54 # include <dsn/utility/factory_store.h>
55 # include <dsn/tool-api/global_config.h>
56 # include <dsn/tool-api/command.h>
57 # include <dsn/tool-api/global_checkers.h>
58 # include <dsn/tool-api/task_queue.h>
59 # include <dsn/tool-api/task_worker.h>
60 # include <dsn/tool-api/admission_controller.h>
61 # include <dsn/tool-api/network.h>
62 # include <dsn/tool-api/aio_provider.h>
63 # include <dsn/tool-api/env_provider.h>
64 # include <dsn/tool-api/nfs.h>
65 # include <dsn/tool-api/zlock_provider.h>
66 # include <dsn/tool-api/message_parser.h>
67 # include <dsn/tool-api/logging_provider.h>
68 # include <dsn/tool-api/memory_provider.h>
69 # include <dsn/tool-api/timer_service.h>
70 # include <dsn/tool-api/partition_resolver.h>
71 
72 namespace dsn { namespace tools {
73 
78 class tool_base
79 {
80 public:
81  virtual ~tool_base()
82  {
83  }
84 
85  DSN_API explicit tool_base(const char* name);
86 
87  const std::string& name() const { return _name; }
88 
89 protected:
90  std::string _name;
91 };
92 
93 class toollet : public tool_base
94 {
95 public:
96  template <typename T> static toollet* create(const char* name)
97  {
98  return new T(name);
99  }
100 
101  typedef toollet* (*factory)(const char*);
102 
103 public:
104  DSN_API toollet(const char* name);
105 
106  virtual void install(service_spec& spec) = 0;
107 };
108 
109 class tool_app : public tool_base
110 {
111 public:
112  template <typename T> static tool_app* create(const char* name)
113  {
114  return new T(name);
115  }
116 
117  typedef tool_app* (*factory)(const char*);
118 
119 public:
120  DSN_API tool_app(const char* name);
121 
122  virtual void install(service_spec& spec) = 0;
123 
124  // this routine will be invoked in the main thread as the tool driver (if necessary for the tool, e.g., model checking)
125  virtual void run()
126  {
127  start_all_apps();
128  }
129 
130 public:
131  DSN_API virtual void start_all_apps();
132  DSN_API virtual void stop_all_apps(bool cleanup);
133 
134  DSN_API static const service_spec& get_service_spec();
135 };
136 
137 namespace internal_use_only
138 {
139  DSN_API bool register_component_provider(const char* name, timer_service::factory f, ::dsn::provider_type type);
140  DSN_API bool register_component_provider(const char* name, task_queue::factory f, ::dsn::provider_type type);
141  DSN_API bool register_component_provider(const char* name, task_worker::factory f, ::dsn::provider_type type);
142  DSN_API bool register_component_provider(const char* name, admission_controller::factory f, ::dsn::provider_type type);
143  DSN_API bool register_component_provider(const char* name, lock_provider::factory f, ::dsn::provider_type type);
144  DSN_API bool register_component_provider(const char* name, lock_nr_provider::factory f, ::dsn::provider_type type);
145  DSN_API bool register_component_provider(const char* name, rwlock_nr_provider::factory f, ::dsn::provider_type type);
146  DSN_API bool register_component_provider(const char* name, semaphore_provider::factory f, ::dsn::provider_type type);
147  DSN_API bool register_component_provider(const char* name, network::factory f, ::dsn::provider_type type);
148  DSN_API bool register_component_provider(const char* name, aio_provider::factory f, ::dsn::provider_type type);
149  DSN_API bool register_component_provider(const char* name, env_provider::factory f, ::dsn::provider_type type);
150  DSN_API bool register_component_provider(const char* name, perf_counter::factory f, ::dsn::provider_type type);
151  DSN_API bool register_component_provider(const char* name, logging_provider::factory f, ::dsn::provider_type type);
152  DSN_API bool register_component_provider(const char* name, memory_provider::factory f, ::dsn::provider_type type);
153  DSN_API bool register_component_provider(const char* name, nfs_node::factory f, ::dsn::provider_type type);
154  DSN_API bool register_component_provider(network_header_format fmt, const std::vector<const char*>& signatures, message_parser::factory f, message_parser::factory2 f2, size_t sz);
155  DSN_API bool register_component_provider(const char* name, ::dsn::dist::partition_resolver::factory f, ::dsn::provider_type type);
156  DSN_API bool register_toollet(const char* name, toollet::factory f, ::dsn::provider_type type);
157  DSN_API bool register_tool(const char* name, tool_app::factory f, ::dsn::provider_type type);
158  DSN_API toollet* get_toollet(const char* name, ::dsn::provider_type type);
159 }
160 
165 DSN_API extern join_point<void> sys_init_before_app_created;
166 DSN_API extern join_point<void> sys_init_after_app_created;
167 DSN_API extern join_point<void, sys_exit_type> sys_exit;
170 template <typename T> bool register_component_provider(const char* name) { return internal_use_only::register_component_provider(name, T::template create<T>, ::dsn::PROVIDER_TYPE_MAIN); }
171 template <typename T> bool register_component_aspect(const char* name) { return internal_use_only::register_component_provider(name, T::template create<T>, ::dsn::PROVIDER_TYPE_ASPECT); }
172 template <typename T> bool register_message_header_parser(network_header_format fmt, const std::vector<const char*>& signatures);
173 
174 template <typename T> bool register_toollet(const char* name) { return internal_use_only::register_toollet(name, toollet::template create<T>, ::dsn::PROVIDER_TYPE_MAIN); }
175 template <typename T> bool register_tool(const char* name) { return internal_use_only::register_tool(name, tool_app::template create<T>, ::dsn::PROVIDER_TYPE_MAIN); }
176 template <typename T> T* get_toollet(const char* name) { return (T*)internal_use_only::get_toollet(name, ::dsn::PROVIDER_TYPE_MAIN); }
177 DSN_API tool_app* get_current_tool();
178 DSN_API const service_spec& spec();
179 DSN_API const char* get_service_node_name(service_node* node);
180 DSN_API bool is_engine_ready();
181 
182 /*
183  @}
184  */
185 
186 // --------- inline implementation -----------------------------
187 template <typename T> bool register_message_header_parser(network_header_format fmt, const std::vector<const char*>& signatures)
188 {
189  return internal_use_only::register_component_provider(fmt, signatures, T::template create<T>, T::template create2<T>, sizeof(T));
190 }
191 
192 }} // end namespace dsn::tools
193 
Definition: tool_api.h:93
Definition: tool_api.h:78
Definition: address.h:52
Definition: tool_api.h:109