WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
RpcServer.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2015. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef NT_RPCSERVER_H_
9 #define NT_RPCSERVER_H_
10 
11 #include <atomic>
12 #include <condition_variable>
13 #include <mutex>
14 #include <queue>
15 #include <utility>
16 
17 #include "llvm/DenseMap.h"
18 #include "atomic_static.h"
19 #include "Message.h"
20 #include "ntcore_cpp.h"
21 #include "SafeThread.h"
22 
23 namespace nt {
24 
25 class RpcServer {
26  friend class RpcServerTest;
27  public:
28  static RpcServer& GetInstance() {
29  ATOMIC_STATIC(RpcServer, instance);
30  return instance;
31  }
32  ~RpcServer();
33 
34  typedef std::function<void(std::shared_ptr<Message>)> SendMsgFunc;
35 
36  void Start();
37  void Stop();
38 
39  void SetOnStart(std::function<void()> on_start) { m_on_start = on_start; }
40  void SetOnExit(std::function<void()> on_exit) { m_on_exit = on_exit; }
41 
42  void ProcessRpc(StringRef name, std::shared_ptr<Message> msg,
43  RpcCallback func, unsigned int conn_id,
44  SendMsgFunc send_response);
45 
46  bool PollRpc(bool blocking, RpcCallInfo* call_info);
47  void PostRpcResponse(unsigned int rpc_id, unsigned int call_uid,
48  llvm::StringRef result);
49 
50  private:
51  RpcServer();
52 
53  class Thread;
55 
56  struct RpcCall {
57  RpcCall(StringRef name_, std::shared_ptr<Message> msg_, RpcCallback func_,
58  unsigned int conn_id_, SendMsgFunc send_response_)
59  : name(name_),
60  msg(msg_),
61  func(func_),
62  conn_id(conn_id_),
63  send_response(send_response_) {}
64 
65  std::string name;
66  std::shared_ptr<Message> msg;
67  RpcCallback func;
68  unsigned int conn_id;
69  SendMsgFunc send_response;
70  };
71 
72  std::mutex m_mutex;
73 
74  std::queue<RpcCall> m_poll_queue;
76  m_response_map;
77 
78  std::condition_variable m_poll_cond;
79 
80  std::atomic_bool m_terminating;
81 
82  std::function<void()> m_on_start;
83  std::function<void()> m_on_exit;
84 
85  ATOMIC_STATIC_DECL(RpcServer)
86 };
87 
88 } // namespace nt
89 
90 #endif // NT_RPCSERVER_H_
NetworkTables RPC Call Data.
Definition: ntcore_cpp.h:79
Definition: DenseMap.h:539
Definition: RpcServer.cpp:18
Definition: RpcServer.h:25
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:39