Commit 25ab36e0 authored by aligungr's avatar aligungr

CLI executable improvement

parent 5bc0ea58
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
#include <iostream> #include <iostream>
#include <set> #include <set>
#include <string> #include <string>
#include <thread>
#include <unordered_map> #include <unordered_map>
#include <utils/common.hpp> #include <utils/common.hpp>
#include <utils/constants.hpp> #include <utils/constants.hpp>
...@@ -161,18 +160,20 @@ static void ReadOptions(int argc, char **argv) ...@@ -161,18 +160,20 @@ static void ReadOptions(int argc, char **argv)
} }
} }
static void HandleMessage(const app::CliMessage &msg, bool isOneShot) static bool HandleMessage(const app::CliMessage &msg, bool isOneShot)
{ {
if (msg.type == app::CliMessage::Type::ERROR) if (msg.type == app::CliMessage::Type::ERROR)
{ {
std::cerr << "ERROR: " << msg.value << std::endl; std::cerr << "ERROR: " << msg.value << std::endl;
if (isOneShot) if (isOneShot)
exit(1); exit(1);
return true;
} }
if (msg.type == app::CliMessage::Type::ECHO) if (msg.type == app::CliMessage::Type::ECHO)
{ {
std::cout << msg.value << std::endl; std::cout << msg.value << std::endl;
return true;
} }
if (msg.type == app::CliMessage::Type::RESULT) if (msg.type == app::CliMessage::Type::RESULT)
...@@ -180,7 +181,10 @@ static void HandleMessage(const app::CliMessage &msg, bool isOneShot) ...@@ -180,7 +181,10 @@ static void HandleMessage(const app::CliMessage &msg, bool isOneShot)
std::cout << msg.value << std::endl; std::cout << msg.value << std::endl;
if (isOneShot) if (isOneShot)
exit(0); exit(0);
return true;
} }
return false;
} }
[[noreturn]] static void SendCommand(uint16_t port) [[noreturn]] static void SendCommand(uint16_t port)
...@@ -189,20 +193,21 @@ static void HandleMessage(const app::CliMessage &msg, bool isOneShot) ...@@ -189,20 +193,21 @@ static void HandleMessage(const app::CliMessage &msg, bool isOneShot)
if (g_options.directCmd.empty()) if (g_options.directCmd.empty())
{ {
std::thread receiver{[&server]() {
while (true)
HandleMessage(server.receiveMessage(), false);
}};
while (true) while (true)
{ {
std::string line{}; std::string line{};
std::cout << ">> ";
std::getline(std::cin, line); std::getline(std::cin, line);
if (!std::cin) if (!std::cin)
exit(0); exit(0);
server.sendMessage( server.sendMessage(
app::CliMessage::Command(InetAddress{cons::CMD_SERVER_IP, port}, line, g_options.nodeName)); app::CliMessage::Command(InetAddress{cons::CMD_SERVER_IP, port}, line, g_options.nodeName));
while (!HandleMessage(server.receiveMessage(), false))
{
// empty
}
} }
} }
else else
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment