forked from ClickHouse/clickhouse-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathendpoint.h
More file actions
131 lines (114 loc) · 3.42 KB
/
endpoint.h
File metadata and controls
131 lines (114 loc) · 3.42 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#pragma once
#include "../exceptions.h"
#include <optional>
#include <queue>
#include <iostream>
namespace clickhouse {
class NetworkAddress;
/// List of hostnames with service ports
struct Endpoint {
std::string host;
std::optional<unsigned int> port = std::nullopt;
};
class EndpointConnector {
public:
std::vector<Endpoint> endpoints;
explicit EndpointConnector(std::vector<Endpoint> endpoints) {
std::cout << "connector constructor" << endpoints.size() << std::endl;
this->endpoints = endpoints;
}
struct Iterator {
Iterator() {
endpoints_ = nullptr;
finished_ = true;
}
Iterator(const std::vector<Endpoint> *endpoints, const std::vector<Endpoint>::const_iterator & start_with, bool finished = false) {
if (finished) {
finished_ = true;
}
it_ = start_with;
start_with_ = start_with;
endpoints_ = endpoints;
}
Iterator& operator++() {
++it_;
if (it_ == endpoints_->end()) {
it_ = endpoints_->begin();
}
if (it_ == start_with_) {
finished_ = true;
}
return *this;
}
bool operator!=(const Iterator& other) {
std::cout << "compare start" << std::endl;
if (finished_ && other.finished_) {
return false;
}
std::cout << "first" << std::endl;
if (finished_ != other.finished_) {
return true;
}
std::cout << "second" << std::endl;
if (other.it_ != it_) {
return true;
}
std::cout << "third" << std::endl;
return false;
}
const Endpoint& operator*() const {
std::cout << "dereference" << std::endl;
return *it_;
}
std::vector<Endpoint>::const_iterator getInsideIterator() {
return it_;
}
private:
bool finished_ = false;
std::vector<Endpoint>::const_iterator start_with_;
std::vector<Endpoint>::const_iterator it_;
const std::vector<Endpoint> *endpoints_;
};
Iterator begin() const {
return Iterator(&endpoints, endpoints.begin());
}
Iterator end() const {
return Iterator(&endpoints, endpoints.begin(), true);
}
bool isConnected();
void setCurrentEndpoint(const Iterator& iter) const {
current_endpoint_ = iter;
}
Endpoint getCurrentEndpoint();
void setNetworkAddress(std::shared_ptr<NetworkAddress> addr) const {
addr_ = addr;
}
std::shared_ptr<NetworkAddress> getNetworkAddress() {
return addr_;
}
enum ReconnectType {
ONLY_CURRENT,
ALL
};
ReconnectType getReconnectType() {
return reconnectType_;
}
void setReconnectType(ReconnectType reconnectType) {
if (reconnectType == ONLY_CURRENT) {
begin_ = current_endpoint_;
end_ = current_endpoint_;
++end_;
} else if (reconnectType == ALL) {
begin_ = current_endpoint_;
end_ = Iterator(&endpoints, current_endpoint_.getInsideIterator(), true);
} else {
throw AssertionError("There is no such Reconnect Type: " + std::to_string(reconnectType));
}
}
ReconnectType reconnectType_;
Iterator begin_;
Iterator end_;
mutable Iterator current_endpoint_;
mutable std::shared_ptr<NetworkAddress> addr_;
};
}