iFun Engine API  1.0.0-b2936
Great Technology for Great Games
 All Classes Files Functions Variables Typedefs Macros
rpc.h
Go to the documentation of this file.
1 // Copyright (C) 2013-2017 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_DISTRIBUTION_RPC_H_
10 #define INCLUDE_FUNAPI_DISTRIBUTION_RPC_H_
11 
12 #include <boost/asio.hpp>
13 #include <boost/enable_shared_from_this.hpp>
14 #include <boost/function.hpp>
15 #include <boost/noncopyable.hpp>
16 #include <funapi/distribution/fun_rpc_message.pb.h>
18 #include <funapi/types.h>
19 
20 #include <map>
21 #include <set>
22 
23 
24 namespace fun {
25 
27 class Rpc : public boost::enable_shared_from_this<Rpc>,
28  private boost::noncopyable {
29  public:
30  typedef Uuid Xid;
31  typedef Uuid PeerId;
32  typedef boost::asio::ip::tcp::endpoint PeerEndpoint;
33  typedef std::map<PeerId, PeerEndpoint> PeerMap;
34  typedef string Tag;
35  typedef std::set<Tag> Tags;
36 
38  typedef boost::function<void(const Ptr<FunRpcMessage> & /*reply*/)> ReadyBack ASSERT_NO_ROLLBACK;
39 
41  typedef boost::function<void(const PeerId & /*new server*/)> ConnectHandler;
42 
44  typedef boost::function<void(const PeerId & /*server*/)> DisconnectHandler;
45 
47  typedef boost::function<
48  void(const PeerId & /*sender*/, const Xid & /*xid*/,
49  const Ptr<const FunRpcMessage> & /*request*/,
50  const ReadyBack & /*finisher*/)> Handler;
51 
52  typedef boost::function<
53  void(const PeerId & /*sender*/, const Xid & /*xid*/,
54  const Ptr<const FunRpcMessage> & /*request*/)> VoidReplyHandler;
55 
57  typedef boost::function<
58  void(const PeerId & /*sender*/, const Xid & /*xid*/,
59  const Ptr<const FunRpcMessage> & /*reply*/)> Callback;
60 
61  static const Xid kNullXid;
62  static const PeerId kNullPeerId;
63 
66  static void AddTag(const Tag &tag);
67 
70  static void AddTags(const Tags &tags);
71 
74  static void RemoveTag(const Tag &tag);
75 
78  static void RemoveTags(const Tags &tags);
79 
83  static bool HasTag(const Tag &tag);
84 
86  static Json GetStatus();
87 
90  static void SetStatus(const Json &status);
91 
94  static PeerId GetSelfId();
95 
98  static PeerEndpoint GetSelfEndpoint();
99 
104  static size_t GetPeers(PeerMap *ret, bool include_self = false);
105 
111  static size_t GetPeersWithTag(PeerMap *ret, const Tag &tag,
112  bool include_self = false);
113 
118  static size_t GetPeerTags(Tags *ret, const PeerId &peer);
119 
126  static std::string GetPeerExternalHostname(const PeerId &peer);
127 
133  static boost::asio::ip::address GetPeerExternalIp(const PeerId &peer);
134 
140  static HardwareInfo::ProtocolPortMap GetPeerExternalPorts(const PeerId &peer);
141 
144  static Json GetPeerStatus(const PeerId &peer);
145 
149  static bool IsPeerConnected(const PeerId &peer);
150 
152  static void RegisterConnectHandler(const ConnectHandler &h);
153 
155  static void RegisterDisconnectHandler(const DisconnectHandler &h);
156 
161  static void RegisterHandler(const string &type, const Handler &h);
162 
167  static void Call(const PeerId &peer, const Ptr<FunRpcMessage> &msg,
168  const Callback &callback) ASSERT_NO_ROLLBACK;
169 
174  static void RegisterVoidReplyHandler(const string &type,
175  const VoidReplyHandler &h);
176 
180  static void Call(
181  const PeerId &peer,
182  const Ptr<FunRpcMessage> &msg) ASSERT_NO_ROLLBACK;
183 };
184 
185 } // namespace fun
186 
187 #endif // INCLUDE_FUNAPI_DISTRIBUTION_RPC_H_
static Json GetStatus()
Gets the server status.
static void RemoveTags(const Tags &tags)
Untags a local RPC server.
static void RegisterVoidReplyHandler(const string &type, const VoidReplyHandler &h)
Registers a handler for a particular RPC message type.
static bool IsPeerConnected(const PeerId &peer)
Checks if the peer is connected.
static size_t GetPeers(PeerMap *ret, bool include_self=false)
Returns information on other RPC peers.
boost::function< void(const Ptr< FunRpcMessage > &)> ReadyBack ASSERT_NO_ROLLBACK
Functor to signal that a RPC server finished processing.
Definition: rpc.h:38
static Json GetPeerStatus(const PeerId &peer)
Returns a status of the peer.
static void RegisterHandler(const string &type, const Handler &h)
Registers a handler for a particular RPC message type.
static PeerEndpoint GetSelfEndpoint()
Returns an endpoint information to which RPC listens.
static boost::asio::ip::address GetPeerExternalIp(const PeerId &peer)
Returns a public IP of the peer.
boost::function< void(const PeerId &)> DisconnectHandler
Functor to be invoked when a server disconnected.
Definition: rpc.h:44
static void Call(const PeerId &peer, const Ptr< FunRpcMessage > &msg, const Callback &callback) ASSERT_NO_ROLLBACK
Sends a RPC requst to the given RPC server.
static PeerId GetSelfId()
Returns an identifier to distinguish from RPC servers.
static size_t GetPeerTags(Tags *ret, const PeerId &peer)
Returns a set of tags associated with the given peer.
static HardwareInfo::ProtocolPortMap GetPeerExternalPorts(const PeerId &peer)
Returns a public ports of the peer.
static void SetStatus(const Json &status)
Sets the server status and broadcasts using RPC.
static void AddTags(const Tags &tags)
Tags a local RPC server.
static void AddTag(const Tag &tag)
Tags a local RPC server.
boost::function< void(const PeerId &, const Xid &, const Ptr< const FunRpcMessage > &, const ReadyBack &)> Handler
Functor to be invoked when a RPC server receives RPC messages.
Definition: rpc.h:50
static void RegisterConnectHandler(const ConnectHandler &h)
Registers a handler for notifying a new server connection.
boost::function< void(const PeerId &, const Xid &, const Ptr< const FunRpcMessage > &)> Callback
Functor to be invoked when a RPC client receives back a reply.
Definition: rpc.h:59
Definition: json.h:27
static std::string GetPeerExternalHostname(const PeerId &peer)
Returns a hostname of the peer.
static void RemoveTag(const Tag &tag)
Untags a local RPC server.
Funapi RPC interface.
Definition: rpc.h:27
boost::function< void(const PeerId &)> ConnectHandler
Functor to be invoked when a new server connected.
Definition: rpc.h:41
static void RegisterDisconnectHandler(const DisconnectHandler &h)
Registers a handler for notifying a server disconnection.
static bool HasTag(const Tag &tag)
Checks if a local RPC server has the tag.
static size_t GetPeersWithTag(PeerMap *ret, const Tag &tag, bool include_self=false)
Returns a list of RPC peers with the given tag.