This repository was archived by the owner on Apr 30, 2020. It is now read-only.
forked from gigablast/open-source-search-engine
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathFxClient.h
87 lines (72 loc) · 2.73 KB
/
FxClient.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//
// Copyright (C) 2018 Privacore ApS - https://www.privacore.com
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// License TL;DR: If you change this file, you must publish your changes.
//
#ifndef FX_FXCLIENT_H
#define FX_FXCLIENT_H
#include <atomic>
#include <vector>
#include <map>
#include <memory>
#include "GbMutex.h"
#include "IOBuffer.h"
struct FxClientRequest {
FxClientRequest(void *context, int timeout_ms);
virtual ~FxClientRequest() = default;
void *m_context;
timespec m_deadline;
};
typedef std::shared_ptr<FxClientRequest> fxclient_request_ptr_t;
class FxClient {
protected:
FxClient();
virtual ~FxClient() = default;
bool initialize(const char *servicename, const char *threadname, const char *hostname, int port, unsigned max_outstanding, bool log_trace);
void reinitializeSettings(const char *hostname, int port, unsigned max_outstanding, bool log_trace);
bool sendRequest(fxclient_request_ptr_t request);
void finalize();
virtual void convertRequestToWireFormat(IOBuffer *out_buffer, uint32_t seq, fxclient_request_ptr_t base_request) = 0;
virtual void processResponse(fxclient_request_ptr_t base_request, char *response) = 0;
virtual void errorCallback(fxclient_request_ptr_t base_request) = 0;
bool communicationWorks() const {
return m_communication_works;
}
private:
static void* communicationThread(void *);
static int calculateEarliestTimeout(const std::map<uint32_t, fxclient_request_ptr_t> &outstanding_requests);
void drainWakeupPipe();
void waitForTimeToConnect();
int runConnectLoop();
void runCommunicationLoop(int fd);
void processInBuffer(IOBuffer *in_buffer, std::map<uint32_t, fxclient_request_ptr_t> *outstanding_requests);
void finishQueuedRequests();
std::vector<fxclient_request_ptr_t> m_queued_requests;
GbMutex m_queued_requests_mtx;
pthread_t m_tid;
bool m_stop;
int m_wakeup_fd[2];
time_t m_next_connect_attempt;
std::atomic<bool> m_communication_works;
std::atomic<unsigned> m_outstanding_request_count;
const char *m_servicename;
const char *m_threadname;
const char *m_hostname;
int m_port;
unsigned m_max_outstanding;
bool m_log_trace;
};
#endif //FX_FXCLIENT_H