summaryrefslogtreecommitdiffstats
path: root/src/Client/CommandParser.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Client/CommandParser.cpp')
-rw-r--r--src/Client/CommandParser.cpp165
1 files changed, 12 insertions, 153 deletions
diff --git a/src/Client/CommandParser.cpp b/src/Client/CommandParser.cpp
index 74b5710..1a5a96c 100644
--- a/src/Client/CommandParser.cpp
+++ b/src/Client/CommandParser.cpp
@@ -65,58 +65,6 @@ void CommandParser::printUsage(const std::string& command) {
Common::Logger::logf("Usage: %s\n", cmd->cmdline);
}
-void CommandParser::printHostStatus(const Net::Packets::HostStatusPacket &packet) {
- if(packet.getUptime()) {
- unsigned long days = packet.getUptime()/86400;
- unsigned long hours = (packet.getUptime()%86400)/3600;
- unsigned long minutes = (packet.getUptime()%3600)/60;
-
- std::printf("\tUptime:\t\t");
-
- if(days) std::printf("%lu days ", days);
-
- std::printf("%lu:%02lu", hours, minutes);
-
- std::printf(" (load average: %.2f %.2f %.2f, %lu processes)", packet.getLoadAverage1(), packet.getLoadAverage5(), packet.getLoadAverage15(), (unsigned long)packet.getProcessNumber());
-
- std::printf("\n\n");
- }
-
- if(packet.getTotalMem() && packet.getFreeMem()) {
- const std::string units[] = {
- "kB", "MB", "GB", "TB", ""
- };
-
- unsigned unit = 0;
- float totalMem = packet.getTotalMem(), usedMem = packet.getTotalMem()-packet.getFreeMem();
-
- while(totalMem >= 1024 && !units[unit+1].empty()) {
- ++unit;
- totalMem /= 1024;
- usedMem /= 1024;
- }
-
- std::printf("\tMemory usage:\t%.*f/%.*f %s", (usedMem < 10) ? 2 : 1, usedMem, (totalMem < 10) ? 2 : 1, totalMem, units[unit].c_str());
- std::printf(" (%.1f%%)\n", usedMem*100.0f/totalMem);
-
- if(packet.getTotalSwap() && packet.getFreeSwap()) {
- unit = 0;
- totalMem = packet.getTotalSwap(); usedMem = packet.getTotalSwap()-packet.getFreeSwap();
-
- while(totalMem >= 1024 && !units[unit+1].empty()) {
- ++unit;
- totalMem /= 1024;
- usedMem /= 1024;
- }
-
- std::printf("\tSwap usage:\t%.*f/%.*f %s", (usedMem < 10) ? 2 : 1, usedMem, (totalMem < 10) ? 2 : 1, totalMem, units[unit].c_str());
- std::printf(" (%.1f%%)\n", usedMem*100.0f/totalMem);
- }
-
- std::printf("\n");
- }
-}
-
void CommandParser::helpCommand(const std::vector<std::string> &args) {
if(args.size() == 1) {
std::cout << "Available commands:" << std::endl << std::endl;
@@ -151,7 +99,7 @@ void CommandParser::listHostsCommand(const std::vector<std::string> &args) {
Common::RequestManager::getRequestManager()->sendRequest(connection,
std::auto_ptr<Common::RequestBase>(
new Requests::DaemonListRequest(
- sigc::bind(sigc::mem_fun(this, &CommandParser::daemonListRequestFinished), false)
+ sigc::bind(sigc::mem_fun(commandManager, &CommandManager::daemonListRequestFinished), false)
)
)
);
@@ -165,7 +113,7 @@ void CommandParser::listHostsCommand(const std::vector<std::string> &args) {
Common::RequestManager::getRequestManager()->sendRequest(connection,
std::auto_ptr<Common::RequestBase>(
new Requests::DaemonListRequest(
- sigc::bind(sigc::mem_fun(this, &CommandParser::daemonListRequestFinished), true)
+ sigc::bind(sigc::mem_fun(commandManager, &CommandManager::daemonListRequestFinished), true)
)
)
);
@@ -176,7 +124,7 @@ void CommandParser::listHostsCommand(const std::vector<std::string> &args) {
return;
}
- activeRequests++;
+ commandManager.activeRequests++;
}
void CommandParser::rebootCommand(const std::vector<std::string> &args) {
@@ -192,10 +140,10 @@ void CommandParser::rebootCommand(const std::vector<std::string> &args) {
}
Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(
- new Requests::DaemonCommandRequest(args[1], true, sigc::mem_fun(this, &CommandParser::daemonCommandRequestFinished))
+ new Requests::DaemonCommandRequest(args[1], true, sigc::mem_fun(commandManager, &CommandManager::daemonCommandRequestFinished))
));
- activeRequests++;
+ commandManager.activeRequests++;
}
void CommandParser::shutdownCommand(const std::vector<std::string> &args) {
@@ -211,119 +159,30 @@ void CommandParser::shutdownCommand(const std::vector<std::string> &args) {
}
Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(
- new Requests::DaemonCommandRequest(args[1], false, sigc::mem_fun(this, &CommandParser::daemonCommandRequestFinished))
+ new Requests::DaemonCommandRequest(args[1], false, sigc::mem_fun(commandManager, &CommandManager::daemonCommandRequestFinished))
));
- activeRequests++;
+ commandManager.activeRequests++;
}
void CommandParser::statusCommand(const std::vector<std::string> &args) {
if(args.size() == 1)
- Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Requests::CoreStatusRequest(sigc::mem_fun(this, &CommandParser::coreStatusRequestFinished))));
+ Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Requests::CoreStatusRequest(sigc::mem_fun(commandManager, &CommandManager::coreStatusRequestFinished))));
else if(args.size() == 2)
- Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Requests::DaemonStatusRequest(args[1], sigc::mem_fun(this, &CommandParser::daemonStatusRequestFinished))));
+ Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Requests::DaemonStatusRequest(args[1], sigc::mem_fun(commandManager, &CommandManager::daemonStatusRequestFinished))));
else {
Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str());
printUsage("status");
return;
}
- activeRequests++;
+ commandManager.activeRequests++;
}
void CommandParser::exitCommand(const std::vector<std::string>&) {
- activeRequests++;
-
- Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Common::Requests::DisconnectRequest(sigc::mem_fun(this, &CommandParser::disconnectRequestFinished))));
-}
-
-void CommandParser::coreStatusRequestFinished(const Common::Request<Net::Packets::HostStatusPacket> &request) {
- try {
- const Net::Packets::HostStatusPacket &packet = request.getResult();
- std::cout << "Server status:" << std::endl;
- printHostStatus(packet);
- }
- catch(Common::Exception &exception) {
- Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str());
- }
-
- requestFinished();
-}
-
-void CommandParser::daemonCommandRequestFinished(const Common::Request<> &request) {
- try {
- request.getResult();
- }
- catch(Common::Exception &exception) {
- Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str());
- }
-
- requestFinished();
-}
-
-void CommandParser::daemonListRequestFinished(const Common::Request<Net::Packets::HostListPacket> &request, bool all) {
- try {
- const std::vector<Common::HostInfo>& hosts = request.getResult().getHostInfo();
-
- if(hosts.empty()) {
- std::cout << "The host list is empty." << std::endl << std::endl;
- }
- else {
- bool output = false;
-
- for(std::vector<Common::HostInfo>::const_iterator host = hosts.begin(); host != hosts.end(); ++host) {
- if(host->getStatus() == Common::HostInfo::INACTIVE && !all)
- continue;
-
- if(!output) {
- std::cout << (all ? "Host list:" : "Active hosts:") << std::endl;
- output = true;
- }
-
- std::cout << " " << host->getName();
-
- if(all)
- std::cout << " (" << (host->getStatus() == Common::HostInfo::RUNNING ? "running" : "inactive") << ")";
-
- std::cout << std::endl;
- }
-
- if(!output)
- std::cout << "No active hosts." << std::endl;
-
- std::cout << std::endl;
- }
- }
- catch(Common::Exception &exception) {
- Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str());
- }
-
- requestFinished();
-}
-
-void CommandParser::daemonStatusRequestFinished(const Common::Request<Net::Packets::HostStatusPacket> &request) {
- try {
- const Net::Packets::HostStatusPacket &packet = request.getResult();
- std::cout << "Host status:" << std::endl;
- printHostStatus(packet);
- }
- catch(Common::Exception &exception) {
- Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str());
- }
-
- requestFinished();
-}
-
-void CommandParser::disconnectRequestFinished(const Common::Request<> &request) {
- try {
- request.getResult();
- disconnect = true;
- }
- catch(Common::Exception &exception) {
- Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str());
- }
+ commandManager.activeRequests++;
- requestFinished();
+ Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Common::Requests::DisconnectRequest(sigc::mem_fun(commandManager, &CommandManager::disconnectRequestFinished))));
}
bool CommandParser::split(const std::string &str, std::vector<std::string> &ret) {