From cc3ef5cfbd9bcc00b78cda298c037a0e23d2ff72 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 3 Sep 2009 23:27:21 +0200 Subject: madc: Erlaube Angabe von Hostnamen --- src/madc.cpp | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/madc.cpp b/src/madc.cpp index 762aa03..1017912 100644 --- a/src/madc.cpp +++ b/src/madc.cpp @@ -34,6 +34,8 @@ #include "Client/Authenticators/ChallengeResponseAuthenticator.h" #include "Client/Authenticators/PasswordAuthenticator.h" +#include + #include #ifdef EDITLINE_FOUND @@ -48,11 +50,11 @@ using namespace Mad; static void usage(const std::string &cmd) { - std::cerr << "Usage: " << cmd << " address" << std::endl; + std::cerr << "Usage: " << cmd << " [host[:port]]" << std::endl; } int main(int argc, char *argv[]) { - if(argc != 2) { + if(argc > 2) { usage(argv[0]); std::exit(1); } @@ -62,13 +64,31 @@ int main(int argc, char *argv[]) { application.getConfigManager()->finish(); Common::ClientConnection *connection = new Common::ClientConnection(&application); + application.getRequestManager()->registerConnection(connection); try { - application.getRequestManager()->registerConnection(connection); + std::string server = "localhost", port = "6666"; + + if(argc == 2) { + server = argv[1]; + } - connection->connect(boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(argv[1]), 6666)); + static const boost::regex r("(.+):(\\d+)", boost::regex_constants::perl); + + boost::smatch match; + if(boost::regex_match(server, match, r)) { + server = match[1].str(); + port = match[2].str(); + } - std::cerr << "Connecting to " << argv[1] << "..." << std::flush; + boost::asio::ip::tcp::resolver resolver(application.getIOService()); + boost::asio::ip::tcp::resolver::query query(server, port); + boost::asio::ip::tcp::resolver::iterator entry = resolver.resolve(query); + + + connection->connect(entry->endpoint()); + + std::cerr << "Connecting to " << entry->host_name() << "..." << std::flush; connection->waitWhileConnecting(); @@ -121,13 +141,12 @@ int main(int argc, char *argv[]) { connection->waitWhileConnected(); application.getUserManager()->unregisterBackend(networkUserBackend); - - application.getRequestManager()->unregisterConnection(connection); } catch(Core::Exception &e) { application.logf(Core::Logger::LOG_CRITICAL, "Error: %s", e.what()); } + application.getRequestManager()->unregisterConnection(connection); delete connection; return 0; -- cgit v1.2.3