Pioneer
Loading...
Searching...
No Matches
ServerAgent.h
Go to the documentation of this file.
1// Copyright © 2008-2023 Pioneer Developers. See AUTHORS.txt for details
2// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3
4#ifdef ENABLE_SERVER_AGENT
5#ifndef SERVERAGENT_H
6#define SERVERAGENT_H
7
8#include "libs.h"
9#include <curl/curl.h>
10#include <json/json.h>
11#include <map>
12#include <queue>
13
14class ServerAgent {
15public:
16 virtual ~ServerAgent() {}
17
18 typedef sigc::slot<void, const Json &, void *> SuccessCallback;
19 typedef sigc::slot<void, const std::string &, void *> FailCallback;
20
21 virtual void Call(const std::string &method, const Json &data, SuccessCallback onSuccess, FailCallback onFail, void *userdata) = 0;
22
23 virtual void ProcessResponses() = 0;
24
25protected:
26 static void IgnoreSuccessCallback(const Json &data) {}
27 static void IgnoreFailCallback(const std::string &error) {}
28};
29
30class NullServerAgent : public ServerAgent {
31public:
32 virtual void Call(const std::string &method, const Json &data, ServerAgent::SuccessCallback onSuccess = sigc::ptr_fun(&ServerAgent::IgnoreSuccessCallback), ServerAgent::FailCallback onFail = sigc::ptr_fun(&ServerAgent::IgnoreFailCallback), void *userdata = 0);
33
34 virtual void ProcessResponses();
35
36private:
37 struct Response {
38 Response(FailCallback _onFail, void *_userdata) :
39 onFail(_onFail),
40 userdata(_userdata)
41 {}
42
43 FailCallback onFail;
44 void *userdata;
45 };
46
47 std::queue<Response> m_queue;
48};
49
50class HTTPServerAgent : public ServerAgent {
51public:
52 HTTPServerAgent(const std::string &endpoint);
53 virtual ~HTTPServerAgent();
54
55 virtual void Call(const std::string &method, const Json &data, SuccessCallback onSuccess = sigc::ptr_fun(&ServerAgent::IgnoreSuccessCallback), FailCallback onFail = sigc::ptr_fun(&ServerAgent::IgnoreFailCallback), void *userdata = 0);
56
57 virtual void ProcessResponses();
58
59private:
60 struct Request {
61 Request(const std::string &_method, const Json &_data, SuccessCallback _onSuccess, FailCallback _onFail, void *_userdata) :
62 method(_method),
63 data(_data),
64 onSuccess(_onSuccess),
65 onFail(_onFail),
66 userdata(_userdata) {}
67
68 const std::string method;
69 const Json data;
70
71 std::string buffer;
72
73 SuccessCallback onSuccess;
74 FailCallback onFail;
75
76 void *userdata;
77 };
78
79 struct Response {
80 Response(SuccessCallback _onSuccess, FailCallback _onFail, void *_userdata) :
81 success(false),
82 onSuccess(_onSuccess),
83 onFail(_onFail),
84 userdata(_userdata) {}
85
86 bool success;
87
88 std::string buffer;
89
90 SuccessCallback onSuccess;
91 Json data;
92
93 FailCallback onFail;
94
95 void *userdata;
96 };
97
98 static int ThreadEntry(void *data);
99 void ThreadMain();
100
101 static const std::string &UserAgent();
102
103 static size_t FillRequestBuffer(char *ptr, size_t size, size_t nmemb, void *userdata);
104 static size_t FillResponseBuffer(char *ptr, size_t size, size_t nmemb, void *userdata);
105
106 static bool s_initialised;
107
108 const std::string m_endpoint;
109
110 SDL_Thread *m_thread;
111
112 CURL *m_curl;
113 curl_slist *m_curlHeaders;
114
115 std::queue<Request> m_requestQueue;
116 SDL_mutex *m_requestQueueLock;
117 SDL_cond *m_requestQueueCond;
118
119 std::queue<Response> m_responseQueue;
120 SDL_mutex *m_responseQueueLock;
121};
122
123#endif
124#endif // ENABLE_SERVER_AGENT
nlohmann::json Json
Definition Json.h:8
Response
Definition InputBindings.h:15