Robust Distributed System Nucleus (rDSN)  ver 1.0.0
service_app.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  * application model atop zion in c++
30  *
31  * Revision history:
32  * Mar., 2015, @imzhenyu (Zhenyu Guo), first version
33  * xxxx-xx-xx, author, fix bug about xxx
34  */
35 
36 # pragma once
37 
38 # include <dsn/service_api_c.h>
39 # include <dsn/cpp/auto_codes.h>
40 # include <dsn/cpp/address.h>
41 # include <vector>
42 # include <string>
43 
44 namespace dsn
45 {
52  {
53  public:
55  : _started(false), _gpid(gpid) { }
56 
57  virtual ~service_app(void) {}
58 
59  virtual ::dsn::error_code start(int argc, char** argv) = 0;
60 
61  virtual ::dsn::error_code stop(bool cleanup = false) = 0;
62 
63  //
64  // inquery routines
65  //
66  bool is_started() const { return _started; }
67 
68  ::dsn::rpc_address primary_address() const { return _address; }
69 
70  const std::string& name() const { return _name; }
71 
72  dsn_gpid get_gpid() const { return _gpid; }
73 
74  private:
75  bool _started;
76  ::dsn::rpc_address _address;
77  std::string _name;
78  dsn_gpid _gpid;
79 
80  public:
81  template<typename TServiceApp>
82  static void* app_create(const char* /*tname*/, dsn_gpid gpid)
83  {
84  auto svc = new TServiceApp(gpid);
85  return (void*)(dynamic_cast<service_app*>(svc));
86  }
87 
88  static dsn_error_t app_start(void* app, int argc, char** argv)
89  {
90  service_app* sapp = (service_app*)app;
91  sapp->_address = dsn_primary_address();
92  sapp->_name = std::string(argv[0]);
93 
94  auto r = sapp->start(argc, argv);
95  if (r == ::dsn::ERR_OK)
96  {
97  sapp->_started = true;
98  }
99  return r;
100  }
101 
102  static dsn_error_t app_destroy(void* app, bool cleanup)
103  {
104  service_app* sapp = (service_app*)(app);
105  auto err = sapp->stop(cleanup);
106  if (ERR_OK == err) sapp->_started = false;
107  return err;
108  }
109  };
110 
112  template<typename TServiceApp>
113  void register_app(const char* type_name)
114  {
115  dsn_app app;
116  memset(&app, 0, sizeof(app));
117  app.mask = DSN_APP_MASK_APP;
118  strncpy(app.type_name, type_name, sizeof(app.type_name));
119  app.layer1.create = service_app::app_create<TServiceApp>;
120  app.layer1.start = service_app::app_start;
121  app.layer1.destroy = service_app::app_destroy;
122 
123  dsn_register_app(&app);
124  }
125 
127 } // end namespace dsn::service
128 
developers define the following dsn_app data structure, and passes it to rDSN through dsn_register_ap...
Definition: app_model.h:333
DSN_API dsn_address_t dsn_primary_address()
get the primary address of the rpc engine attached to the current thread
dsn_app_start start
callback to start the app, similar to main
Definition: app_model.h:342
dsn_app_destroy destroy
callback to stop and destroy the app
Definition: app_model.h:343
Definition: auto_codes.h:105
Definition: api_layer1.h:683
dsn_app_create create
callback to create the context for the app
Definition: app_model.h:341
#define DSN_APP_MASK_APP
app mask
Definition: app_model.h:290
DSN_API bool dsn_register_app(dsn_app *app_type)
register application/framework into rDSN runtime
Definition: service_app.h:51
Definition: address.h:52
char type_name[DSN_MAX_APP_TYPE_NAME_LENGTH]
type
Definition: app_model.h:336
void register_app(const char *type_name)
C++ wrapper of the dsn_register_app function.
Definition: service_app.h:113
uint64_t mask
application capability mask
Definition: app_model.h:335
Definition: address.h:61