From 9528dcbf1a567c4fe0a2ecab7dbc7fc6f4a2d6ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sat, 11 Dec 2021 21:51:36 +0100 Subject: Test: use numeric index for peer identification --- src/main.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 13 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index b061747..690f7fb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -64,19 +64,40 @@ void printLine(const string & line) Storage st(getErebosDir()); optional> h; optional server; -map> attachAnswer; -Peer getPeer(const string & name) +struct TestPeer { - auto & plist = server->peerList(); - for (size_t i = 0; i < plist.size(); i++) - if (plist.at(i).name() == name) - return plist.at(i); + Peer peer; + size_t id; + bool deleted = false; + promise attachAnswer {}; +}; +vector testPeers; + +TestPeer & getPeer(const string & name) +{ + try { + return testPeers.at(std::stoi(name) - 1); + } + catch (const std::invalid_argument &) {} + + for (auto & p : testPeers) + if (p.peer.name() == name) + return p; + ostringstream ss; ss << "peer '" << name << "' not found"; throw invalid_argument(ss.str().c_str()); } +TestPeer & getPeer(const Peer & peer) +{ + for (auto & p : testPeers) + if (p.peer == peer) + return p; + throw invalid_argument("peer not found"); +} + struct Command { string name; @@ -111,7 +132,7 @@ void printAttachResult(Peer peer, future && success) { bool s = success.get(); ostringstream ss; - ss << "attach-result " << peer.name() << " " << s; + ss << "attach-result " << getPeer(peer).id << " " << s; printLine(ss.str()); } @@ -121,10 +142,10 @@ future confirmAttach(const Peer & peer, string confirm, future && su promise promise; auto input = promise.get_future(); - attachAnswer[peer] = move(promise); + getPeer(peer).attachAnswer = move(promise); ostringstream ss; - ss << "attach-confirm " << peer.name() << " " << confirm; + ss << "attach-confirm " << getPeer(peer).id << " " << confirm; printLine(ss.str()); return input; } @@ -143,9 +164,19 @@ void startServer(const vector &) server.emplace(*h, move(services)); server->peerList().onUpdate([](size_t idx, const Peer * peer) { + size_t i = 0; + while (idx > 0 && i < testPeers.size() && testPeers[i].deleted) { + if (!testPeers[i].deleted) + idx--; + i++; + } + ostringstream ss; - ss << "peer " << idx + 1; + ss << "peer " << i + 1; if (peer) { + if (i >= testPeers.size()) + testPeers.push_back(TestPeer { .peer = *peer, .id = i + 1 }); + if (peer->identity()) { ss << " id"; for (auto idt = peer->identity(); idt; idt = idt->owner()) @@ -155,6 +186,7 @@ void startServer(const vector &) ss << " addr " << inet_ntoa(paddr.sin_addr) << " " << ntohs(paddr.sin_port); } } else { + testPeers[i].deleted = true; ss << " deleted"; } printLine(ss.str()); @@ -216,13 +248,12 @@ void updateSharedIdentity(const vector & params) void attach(const vector & params) { - server->svc().attachTo(getPeer(params.at(0))); + server->svc().attachTo(getPeer(params.at(0)).peer); } void attachAccept(const vector & params) { - attachAnswer.extract(getPeer(params[0])) - .mapped().set_value(params[1] == "1"); + getPeer(params.at(0)).attachAnswer.set_value(params[1] == "1"); } vector commands = { -- cgit v1.2.3