Skip to content

Commit

Permalink
Add usage
Browse files Browse the repository at this point in the history
  • Loading branch information
resetius committed Dec 2, 2023
1 parent e73e62b commit 367ad1f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
11 changes: 10 additions & 1 deletion client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ TSimpleTask Client(Poller& poller, TAddress addr) {
co_return;
}

void usage(const char* prog) {
std::cerr << prog << " --node ip:port:id\n";
exit(0);
}

int main(int argc, char** argv) {
signal(SIGPIPE, SIG_IGN);
std::vector<THost> hosts;
Expand All @@ -71,9 +76,13 @@ int main(int argc, char** argv) {
// address:port:id
hosts.push_back(THost{argv[++i]});
} else if (!strcmp(argv[i], "--help")) {

usage(argv[0]);
}
}

if (hosts.empty() || !hosts[0]) {
std::cerr << "At least one node must be set\n"; return 1;
}
using TPoller = NNet::TDefaultPoller;
std::shared_ptr<ITimeSource> timeSource = std::make_shared<TTimeSource>();
NNet::TLoop<TPoller> loop;
Expand Down
14 changes: 13 additions & 1 deletion server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
#include <raft.h>
#include <server.h>

void usage(const char* prog) {
std::cerr << prog << " --id myid --node ip:port:id [--node ip:port:id ...]" << "\n";
exit(0);
}

int main(int argc, char** argv) {
signal(SIGPIPE, SIG_IGN);
std::vector<THost> hosts;
Expand All @@ -17,7 +22,7 @@ int main(int argc, char** argv) {
} else if (!strcmp(argv[i], "--id") && i < argc - 1) {
id = atoi(argv[++i]);
} else if (!strcmp(argv[i], "--help")) {

usage(argv[0]);
}
}

Expand All @@ -27,6 +32,9 @@ int main(int argc, char** argv) {
NNet::TLoop<TPoller> loop;

for (auto& host : hosts) {
if (!host) {
std::cerr << "Empty host\n"; return 1;
}
if (host.Id == id) {
myHost = host;
} else {
Expand All @@ -38,6 +46,10 @@ int main(int argc, char** argv) {
}
}

if (!myHost) {
std::cerr << "Host not found\n"; return 1;
}

auto raft = std::make_shared<TRaft>(myHost.Id, nodes);
TRaftServer server(loop.Poller(), NNet::TAddress{myHost.Address, myHost.Port}, raft, nodes, timeSource);
server.Serve();
Expand Down
4 changes: 4 additions & 0 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ struct THost {

THost() { }

operator bool() const {
return !!Id;
}

THost(const std::string& str) {
std::string_view s(str);
auto p = s.find(':');
Expand Down

0 comments on commit 367ad1f

Please sign in to comment.