154 std::map<size_t, partial> partial_nodesets;
157 if (listen(
sock, 1024) < 0)
160 sockaddr_in client_address;
161 socklen_t size =
sizeof(client_address);
162 socket_t f = accept(
sock, (sockaddr*)&client_address, &size);
164 if (f == INVALID_SOCKET)
173 char dotted_quad[INET_ADDRSTRLEN];
174 if (NULL == inet_ntop(AF_INET, &(client_address.sin_addr), dotted_quad, INET_ADDRSTRLEN))
177 char hostname[NI_MAXHOST];
178 char servInfo[NI_MAXSERV];
179 if (getnameinfo((sockaddr*)&client_address,
sizeof(sockaddr), hostname, NI_MAXHOST, servInfo, NI_MAXSERV, 0))
183 std::cerr <<
"inbound connection from " << dotted_quad <<
"(" << hostname <<
':' << ntohs(
m_port)
184 <<
") serv=" << servInfo << std::endl;
187 if (recv(f, (
char*)&nonce,
sizeof(nonce), 0) !=
sizeof(nonce))
189 THROW(dotted_quad <<
"(" << hostname <<
':' << ntohs(
m_port) <<
"): nonce read failed, exiting");
194 std::cerr << dotted_quad <<
"(" << hostname <<
':' << ntohs(
m_port) <<
"): nonce=" << nonce << std::endl;
197 if (recv(f, (
char*)&total,
sizeof(total), 0) !=
sizeof(total))
199 THROW(dotted_quad <<
"(" << hostname <<
':' << ntohs(
m_port) <<
"): total node count read failed, exiting");
204 std::cerr << dotted_quad <<
"(" << hostname <<
':' << ntohs(
m_port) <<
"): total=" << total << std::endl;
207 if (recv(f, (
char*)&
id,
sizeof(
id), 0) !=
sizeof(
id))
209 THROW(dotted_quad <<
"(" << hostname <<
':' << ntohs(
m_port) <<
"): node id read failed, exiting");
214 std::cerr << dotted_quad <<
"(" << hostname <<
':' << ntohs(
m_port) <<
"): node id=" <<
id << std::endl;
221 std::cout << dotted_quad <<
"(" << hostname <<
':' << ntohs(
m_port) <<
"): invalid id=" <<
id 222 <<
" >= " << total <<
" !" << std::endl;
227 if (partial_nodesets.find(nonce) == partial_nodesets.end())
230 for (
size_t i = 0; i < total; i++) partial_nodeset.
nodes[i].
client_ip = (uint32_t)-1;
231 partial_nodeset.
filled = 0;
235 partial_nodeset = partial_nodesets[nonce];
236 partial_nodesets.erase(nonce);
249 if (partial_nodeset.
filled != total)
251 partial_nodesets[nonce] = partial_nodeset;
252 for (
size_t i = 0; i < total; i++)
257 std::cout <<
"nonce " << nonce <<
" still waiting for " << (total - partial_nodeset.
filled)
258 <<
" nodes out of " << total <<
" for example node " << i << std::endl;
268 int* parent = (
int*)calloc(total,
sizeof(
int));
269 uint16_t* kid_count = (uint16_t*)calloc(total,
sizeof(uint16_t));
271 int root =
build_tree(parent, kid_count, total, 0);
274 for (
size_t i = 0; i < total; i++)
279 uint16_t* client_ports = (uint16_t*)calloc(total,
sizeof(uint16_t));
281 for (
size_t i = 0; i < total; i++)
284 if (recv(partial_nodeset.
nodes[i].
socket, (
char*)&(client_ports[i]),
sizeof(client_ports[i]), 0) <
285 (int)
sizeof(client_ports[i]))
288 std::cerr <<
" Port read failed for node " << i <<
" read " << done << std::endl;
291 for (
size_t i = 0; i < total; i++)
297 fail_send(partial_nodeset.
nodes[i].
socket, &client_ports[parent[i]],
sizeof(client_ports[parent[i]]));
302 uint32_t bogus2 = -1;
309 free(partial_nodeset.
nodes);
int build_tree(int *parent, uint16_t *kid_count, size_t source_count, int offset)
void fail_send(const socket_t fd, const void *buf, const int count)
static int socket_sort(const void *s1, const void *s2)