forked from liskin/xchat-bttrw
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cluster.cc
197 lines (171 loc) · 5.53 KB
/
cluster.cc
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <iostream>
#include "xchat.h"
#include "TomiTCP/http.h"
using namespace net;
namespace xchat {
/**
* Helper function to add hostname if server exists and to add server if
* not.
*/
static void addserver(vector<server> &servers, const sockaddr_uni &host,
string hostname, server_type type)
{
for (vector<server>::iterator i = servers.begin(); i != servers.end(); i++) {
if (i->host == host) {
i->types[type] = hostname;
return;
}
}
server s;
s.host = host;
s.types[type] = hostname;
servers.push_back(s);
}
/**
* Get the list of available servers from DNS.
* If not possible (in proxy environments for example), add
* "www.xchat.cz" (clustering and failure tracking won't work).
*/
void XChat::makeservers()
{
string hostname;
{
vector<sockaddr_uni> sockaddrs;
hostname = "www.xchat.cz";
try {
resolve(hostname, "", sockaddrs);
} catch (...) { }
for (vector<sockaddr_uni>::iterator i = sockaddrs.begin();
i != sockaddrs.end(); i++)
addserver(servers, *i, hostname, SERVER_MODCHAT);
}
{
vector<sockaddr_uni> sockaddrs;
hostname = "scripts.xchat.cz";
try {
resolve(hostname, "", sockaddrs);
} catch (...) { }
for (vector<sockaddr_uni>::iterator i = sockaddrs.begin();
i != sockaddrs.end(); i++)
addserver(servers, *i, hostname, SERVER_SCRIPTS);
}
if (servers.empty()) {
server s;
s.host.sa.sa_family = 0;
s.types[SERVER_MODCHAT] = "www.xchat.cz";
s.types[SERVER_SCRIPTS] = "scripts.xchat.cz";
servers.push_back(s);
}
}
/**
* Last server broke, count it.
* \return A string representation of the server to let the user know.
*/
string XChat::lastsrv_broke()
{
if (lastsrv >= 0 && servers.size() > 0) {
servers[lastsrv].last_break = time(0);
servers[lastsrv].total_break_count++;
servers[lastsrv].break_count++;
if (servers[lastsrv].break_count >= tries_to_rest) {
auto_ptr<EvError> e(new EvError);
e->s = "Server " + tomi_ntop(servers[lastsrv].host) + " is having a rest.";
recvq_push((auto_ptr<Event>) e);
}
return tomi_ntop(servers[lastsrv].host);
}
return "";
}
/**
* Get a random working server for a given server type. Unrest servers
* when the time comes or when no server is available.
* \param type Server type.
* \return Index in the #servers array.
*/
int XChat::makesrv(server_type type)
{
vector<int> good;
for (vector<server>::iterator i = servers.begin(); i != servers.end(); i++) {
if (!i->supports(type))
continue;
if (i->break_count >= tries_to_rest &&
(i->last_break + rest_duration) < time(0)) {
auto_ptr<EvError> e(new EvError);
e->s = "Server " + tomi_ntop(i->host)
+ " is no longer having a rest.";
recvq_push((auto_ptr<Event>) e);
i->break_count = 0;
good.push_back(i - servers.begin());
} else if (i->break_count < tries_to_rest &&
(i->last_break + nextchance_interval) < time(0)) {
good.push_back(i - servers.begin());
}
}
if (good.size())
return (lastsrv = good[rand() % good.size()]);
else {
auto_ptr<EvNeedRelogin> e(new EvNeedRelogin);
e->s = "All servers considered bad, cleaning.";
recvq_push((auto_ptr<Event>) e);
for (vector<server>::iterator i = servers.begin(); i != servers.end(); i++) {
if (i->supports(type)) {
i->break_count = 0;
i->last_break = 0;
good.push_back(i - servers.begin());
}
}
return (lastsrv = good[rand() % good.size()]);
}
}
/**
* Construct a path using a given path and type.
* \param path The path.
* \param type Type, see path_type.
*/
string XChat::makepath(const string& path, path_type type)
{
if (type == PATH_PLAIN)
return "/" + path;
else if (type == PATH_AUTH)
return "/~$" + uid + "~" + sid + "/" + path;
else if (type == PATH_STATIC)
return "https://" + fallback_server + "/~$" + uid + "~" + sid + "/" + path;
else
throw invalid_argument("Unknown path_type");
}
/**
* Do a GET request...
*/
int XChat::request_GET(XChatAPI &s, server_type st, const string& path,
path_type pt)
{
int si = makesrv(st);
server &ss = servers[si];
string url = "https://" + ss.types[st] + makepath(path, pt);
long r = s.GET(url, &cookies);
s.parseresponse(&cookies);
return (int)r;
}
/**
* Do a POST request...
*/
int XChat::request_POST(XChatAPI &s, server_type st, const string& path,
path_type pt, const string &data, const string &type)
{
int si = makesrv(st);
server &ss = servers[si];
string url = "https://" + ss.types[st] + makepath(path, pt);
if(type != "")
{
long r = s.CB(url, data, type, &cookies);
s.parseresponse(&cookies);
return (int)r;
}
else
{
long r = s.POST(url, data, &cookies);
s.parseresponse(&cookies);
return (int)r;
}
}
}