2022-09-05 13:40:50 +00:00
|
|
|
|
/*
|
|
|
|
|
* main.cpp
|
|
|
|
|
*
|
|
|
|
|
* Created on: 5 сент. 2022 г.
|
|
|
|
|
* Author: alexander
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <ap.hpp>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
#include <server.hpp>
|
2022-09-07 12:43:34 +00:00
|
|
|
|
#include <client.hpp>
|
|
|
|
|
|
|
|
|
|
void print(std::string buffer, zh::ServerTCP &s)
|
|
|
|
|
{
|
|
|
|
|
std::cout << buffer << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void stop(std::string buffer, zh::ServerTCP &s)
|
|
|
|
|
{
|
|
|
|
|
s.stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void disconnect(std::string buffer, zh::ServerTCP &s)
|
|
|
|
|
{
|
|
|
|
|
s.disconnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void server(const int port)
|
|
|
|
|
{
|
|
|
|
|
zh::ServerTCP tcpServer(port);
|
|
|
|
|
|
|
|
|
|
tcpServer.registerHook("", print);
|
|
|
|
|
tcpServer.registerHook("disconnect", disconnect);
|
|
|
|
|
tcpServer.registerHook("stop", stop);
|
|
|
|
|
|
|
|
|
|
tcpServer.bind();
|
|
|
|
|
tcpServer.listen();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void client(const int port, const std::string &host)
|
|
|
|
|
{
|
|
|
|
|
zh::ClientTCP tcpClient(host, port);
|
|
|
|
|
tcpClient.connect();
|
|
|
|
|
}
|
2022-09-05 13:40:50 +00:00
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
|
{
|
2022-09-07 13:43:05 +00:00
|
|
|
|
ap::Hub hub({
|
|
|
|
|
{ "port", 'p', ap::REQUIRED },
|
|
|
|
|
{ "host", 'h', ap::OPTIONAL },
|
|
|
|
|
{ "server", 's', ap::NO },
|
|
|
|
|
{ "client", 'c', ap::NO }
|
|
|
|
|
});
|
2022-09-05 13:40:50 +00:00
|
|
|
|
hub.readArguments(argc, argv);
|
|
|
|
|
|
|
|
|
|
auto optionPort = hub.getOption('p');
|
|
|
|
|
if (!optionPort.isSet())
|
|
|
|
|
{
|
|
|
|
|
std::cerr << "Порт не был установлен!" << std::endl;
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int port = std::stoi(optionPort.getValues()[0]);
|
|
|
|
|
|
2022-09-07 13:43:05 +00:00
|
|
|
|
auto optionClient = hub.getOption('c');
|
|
|
|
|
auto optionServer = hub.getOption('s');
|
|
|
|
|
|
|
|
|
|
if (optionClient.isSet())
|
|
|
|
|
{
|
|
|
|
|
auto optionHost = hub.getOption('h');
|
|
|
|
|
if (!optionHost.isSet())
|
|
|
|
|
{
|
|
|
|
|
std::cerr << "Адрес не был установлен!" << std::endl;
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string host = optionHost.getValues()[0];
|
|
|
|
|
|
|
|
|
|
client(port, host);
|
|
|
|
|
}
|
|
|
|
|
else if (optionServer.isSet())
|
|
|
|
|
server(port);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
std::cerr << "Необходимо установить режим запуска:" << std::endl
|
|
|
|
|
<< "\t-c\t--client\tЗапуск в режиме клиента" << std::endl
|
|
|
|
|
<< "\t-s\t--server\tЗапуск в режиме сервера" << std::endl
|
|
|
|
|
<< "\t-h\t--host\t\tУказать IP-адрес сервера" << std::endl
|
|
|
|
|
<< "\t-p\t--port\t\tУказать порт" << std::endl;
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
2022-09-05 13:40:50 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|