2012-08-16 21:07:56 +00:00
|
|
|
/*
|
|
|
|
* ET: Legacy
|
|
|
|
* Copyright (C) 2012 Jan Simek <mail@etlegacy.com>
|
|
|
|
*
|
|
|
|
* This file is part of ET: Legacy - http://www.etlegacy.com
|
|
|
|
*
|
|
|
|
* ET: Legacy is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* ET: Legacy is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with ET: Legacy. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <boost/asio.hpp>
|
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
|
|
|
|
#include "connection.h"
|
2012-09-02 22:11:28 +00:00
|
|
|
#include "etparser.h"
|
2012-08-16 21:07:56 +00:00
|
|
|
|
|
|
|
using boost::asio::ip::udp;
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2012-09-02 22:11:28 +00:00
|
|
|
/*
|
2012-08-16 21:07:56 +00:00
|
|
|
* Program options
|
|
|
|
*/
|
|
|
|
boost::program_options::options_description desc("[OPTIONS]");
|
|
|
|
desc.add_options()
|
|
|
|
("help,h", "show this message")
|
|
|
|
("server,s", boost::program_options::value<std::string>(),
|
|
|
|
"server to query")
|
|
|
|
("port,p",
|
2012-10-14 00:21:36 +00:00
|
|
|
boost::program_options::value<unsigned short>()->default_value(27960),
|
2012-08-16 21:07:56 +00:00
|
|
|
"port on the server")
|
|
|
|
("message,m",
|
|
|
|
boost::program_options::value<std::string>()->default_value("getstatus"),
|
|
|
|
"message to be sent")
|
2012-09-02 22:11:28 +00:00
|
|
|
("timeout,t",
|
|
|
|
boost::program_options::value<float>()->default_value(1.5),
|
|
|
|
"seconds to wait for the server to respond")
|
2012-08-16 21:07:56 +00:00
|
|
|
("raw,r", "don't parse the server response")
|
|
|
|
;
|
|
|
|
boost::program_options::variables_map var_map;
|
|
|
|
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), var_map);
|
|
|
|
boost::program_options::notify(var_map);
|
|
|
|
|
|
|
|
if (var_map.count("help"))
|
|
|
|
{
|
|
|
|
std::cout << desc << std::endl;
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
else if (!var_map.count("server"))
|
|
|
|
{
|
|
|
|
std::cerr << "Usage: " << argv[0] << " -s <server>" << std::endl;
|
|
|
|
std::cerr << "Append -h or --help to see all program options." << std::endl;
|
2012-09-14 11:18:17 +00:00
|
|
|
return EXIT_FAILURE;
|
2012-08-16 21:07:56 +00:00
|
|
|
}
|
|
|
|
|
2012-09-02 22:11:28 +00:00
|
|
|
/*
|
2012-08-16 21:07:56 +00:00
|
|
|
* Send the request
|
|
|
|
*/
|
2012-09-02 22:11:28 +00:00
|
|
|
boost::asio::io_service io_service;
|
|
|
|
Connection client(io_service, var_map["server"].as<std::string>(),
|
2012-10-14 00:21:36 +00:00
|
|
|
var_map["port"].as<unsigned short>(),
|
2012-09-02 22:11:28 +00:00
|
|
|
var_map["message"].as<std::string>(),
|
|
|
|
var_map["timeout"].as<float>());
|
|
|
|
io_service.run();
|
2012-08-16 21:07:56 +00:00
|
|
|
|
2012-09-02 22:11:28 +00:00
|
|
|
/*
|
|
|
|
* Parse the response
|
|
|
|
*/
|
|
|
|
if (var_map.count("raw"))
|
2012-08-16 21:07:56 +00:00
|
|
|
{
|
2012-09-14 11:18:17 +00:00
|
|
|
for (int i = 0; i < client.get_response().size(); i++)
|
|
|
|
{
|
|
|
|
std::cout << "Response #" << i << ": " << client.get_response()[i] << std::endl << std::endl;
|
|
|
|
}
|
2012-08-16 21:07:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-09-14 11:18:17 +00:00
|
|
|
ETParser parser(client.get_response());
|
2012-08-16 21:07:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
|
|
|
std::cerr << "Exception caught: " << e.what() << std::endl;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
std::cerr << "Exception of unknown type!" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|