iFun Engine API  1.0.0-b2601
Great Technology for Great Games
 All Classes Files Functions Variables Typedefs Macros
dedicated_server_manager.h
Go to the documentation of this file.
1  // Copyright (C) 2016-2018 iFunFactory Inc. All Rights Reserved.
2 //
3 // This work is confidential and proprietary to iFunFactory Inc. and
4 // must not be used, disclosed, copied, or distributed without the prior
5 // consent of iFunFactory Inc.
6 
9 #ifndef INCLUDE_FUNAPI_SERVICE_DEDICATED_SERVER_MANAGER_H_
10 #define INCLUDE_FUNAPI_SERVICE_DEDICATED_SERVER_MANAGER_H_
11 
12 #include <string>
13 #include <vector>
14 
15 #include <boost/function.hpp>
16 #include <boost/noncopyable.hpp>
17 
18 #include <funapi/types.h>
19 
20 
21 namespace fun {
22 
23 class DedicatedServerManager : private boost::noncopyable {
24  public:
25  typedef boost::function<void (const fun::Uuid &, /* match_id */
26  const std::vector<std::string> &, /* users */
27  bool /* success */)> SendCallback;
28 
29  typedef boost::function<void (const fun::Uuid &, /* match_id */
30  const fun::Json &, /* match_data */
31  bool /* success */)> MatchResultCallback;
32 
33  typedef boost::function<void(const fun::Uuid &, /* match_id */
34  const fun::Json &, /* game_state */
35  bool /* success */)> GetGameStateCallback;
36 
37  typedef boost::function<void(const fun::Uuid &match_id,
38  const std::string &account)> UserJoinedCallback;
39 
40  typedef boost::function<void(const fun::Uuid &match_id,
41  const std::string &account)> UserLeftCallback;
42 
43  typedef boost::function<void (const fun::Uuid &match_id,
44  const fun::Json &data)> CustomCallback;
45 
46  // NOTE: `reserved_instance_count` is a value from MANIFEST file.
47  typedef boost::function<uint32_t (uint32_t current_instance_count,
48  uint32_t idle_instance_count,
49  uint32_t min_instance_count,
50  uint32_t max_instance_count,
51  uint32_t reserved_instance_count)>
52  ConfigureReservedInstanceCountCallback;
53 
54  // Authorization callback type (for JWT enforced APIs)
55  typedef boost::function<bool (const std::string &uri, const fun::Json &claim)>
56  JwtAuthorizationCallback;
57 
58  /*
59  * Spawn a dedicated server instance on a remote server.
60  * After spwaning a instance, selected users will be redirected to the
61  * newly spawned instance.
62  * After getting response from the dedicated server, result will be
63  * passed to the `callback` function.
64  *
65  * You may send additional data in JSON format using `match_data` and
66  * `user_data.` `match_data` should contain per-match game data and
67  * `user_data` should have per-user game data.
68  *
69  * Also, you could pass arguments for a dedicated server using
70  * `dedicated_server_args' parameter. These arguments will be passed when
71  * dedicated server manager executes dedicated server excutable.
72  *
73  * Note: user_data should be given in the same order as `accounts` parameter.
74  */
75  static void Spawn(const fun::Uuid &match_id,
76  const fun::Json &match_data,
77  const std::vector<std::string> &dedicated_server_args,
78  const std::vector<std::string> &accounts,
79  const std::vector<fun::Json> &user_data,
80  const SendCallback &callback);
81 
82  // Additional parameter `region_preference` may given to prioritize certain
83  // EC2 regions.
84  static void Spawn(const fun::Uuid &match_id,
85  const fun::Json &match_data,
86  const std::vector<std::string> &dedicated_server_args,
87  const std::vector<std::string> &accounts,
88  const std::vector<fun::Json> &user_data,
89  const std::vector<std::string> &region_preference,
90  const SendCallback &callback);
91 
92  /*
93  * Cancels a spawn request corresponding to `match_id`.
94  * If the spawn request was successfully canceled, it returns true.
95  * Otherwise false.
96  * Also, you can get the parameters that was passed into Spawn() by
97  * passing non-null pointer arguments.
98  */
99  static bool Cancel(const fun::Uuid &match_id,
100  fun::Json *match_data = NULL,
101  std::vector<std::string> *dedicated_server_args = NULL,
102  std::vector<std::string> *accounts = NULL,
103  std::vector<fun::Json> *user_data = NULL);
104 
105  /*
106  * Sends users in `accounts' to a existing dedicated server instance.
107  * You may send additional per-user data in JSON format using user_data.
108  *
109  * After getting response from the dedicated server, result will be
110  * called back using `callback' function.
111  */
112  static void SendUsers(const fun::Uuid &match_id,
113  const fun::Json &match_data,
114  const std::vector<std::string> &accounts,
115  const std::vector<fun::Json> &user_data,
116  const SendCallback &callback);
117 
118  /*
119  * Registers handler function for match result.
120  *
121  * If the match result from the dedicated server instance received,
122  * register callback function `callback' would be called.
123  */
124  static void RegisterMatchResultCallback(const MatchResultCallback &callback);
125 
126  /*
127  * Registers handler function, which would be called when user enters/leaves
128  * the dedicated server.
129  */
130  static void RegisterUserEnteredCallback(const UserJoinedCallback &callback);
131  static void RegisterUserLeftCallback(const UserLeftCallback &callback);
132 
133  /*
134  * Controls desired # of reserved instance count.
135  * If set, iFunEngine will use the callback to determine how many reserved
136  * instance should be spawned.
137  *
138  * If not set, iFunEngine will use the `reserved_instances` value from
139  * the MANIFEST file(s).
140  */
141  static void RegisterConfigureReservedInstanceCountCallback(
142  const ConfigureReservedInstanceCountCallback &callback);
143 
144  /*
145  * Registers callback function. It would be called whenever a dedicated
146  * server instance sends any message to the iFunEngine game server.
147  */
148  static void RegisterCustomCallback(const CustomCallback &callback);
149 
150  /*
151  * Get state of game that is running on dedicated server
152  */
153  static void GetGameState(const fun::Uuid &match_id,
154  const GetGameStateCallback &callback);
155 
156  // Register callback function to authorize a JWT claims
157  static void RegisterJwtAuthorizationCallback(
158  const JwtAuthorizationCallback &callback);
159 };
160 
161 } // namespace fun
162 
163 #endif // INCLUDE_FUNAPI_SERVICE_DEDICATED_SERVER_MANAGER_H_
Definition: dedicated_server_manager.h:23
Definition: json.h:27