1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
#include <Eris/Metaserver.h>
#include <Eris/Log.h>
#include <Eris/PollDefault.h>
#include <Eris/ServerInfo.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <sigc++/functors/ptr_fun.h>
using std::endl;
using std::cout;
using std::cerr;
bool queryDone = false,
failure = false,
exactTime = false;
void erisLog(Eris::LogLevel, const std::string& msg)
{
cerr << "ERIS: " << msg << endl;
}
void gotServerList(int count)
{
cerr << "metaserver knows about " << count << " servers." << endl;
}
void gotServer(const Eris::ServerInfo& info)
{
cerr << "got info for server: " << info.getServername() << '/'
<< info.getHostname() << endl;
}
void queriesDone()
{
cerr << "query complete" << endl;
queryDone = true;
}
void queryFailed(const std::string& msg)
{
cerr << "got query failure: " << msg << endl;
failure = true;
}
std::string timeFormat(double time)
{
static const std::string times[] = { "secs", "mins", "hours", "days", "weeks" };
// int precs[] = { 0, 1, 1, 2, 2, 2 };
// int divi = 0;
std::stringstream result;
std::string timestring;
// double orig = time;
if(exactTime == false)
{
static const int divs[] = { 60, 60, 24, 7, 0 };
#if 0
while((divs[divi] > 0) && (time > divs[divi]))
{
std::stringstream division;
division << (int)time % divs[divi] << " "
<< times[divi];
if (!timestring.empty()) {
timestring = ", " + timestring;
}
timestring = division.str() + timestring;
time /= divs[divi++];
}
#else
if(exactTime == false) {
for (int i = 0; i < 5; ++i) {
std::stringstream division;
int interval;
if (i == 4) {
interval = (int)time;
} else {
interval = (int)time % divs[i];
}
if (interval != 0) {
division << interval << " " << times[i];
if (!timestring.empty()) {
timestring = ", " + timestring;
}
timestring = division.str() + timestring;
}
time /= divs[i];
if (time < 1) {
break;
}
}
}
#endif
}
// result << std::fixed << std::setprecision(precs[divi]) << orig << "," << time << ' ' << times[divi];
return timestring;
}
void dumpToScreen(const Eris::Meta& meta)
{
for (unsigned int S=0; S < meta.getGameServerCount(); ++S)
{
const Eris::ServerInfo& sv = meta.getInfoForServer(S);
cout << S << ": " << sv.getServername() << '/'<< sv.getHostname() << endl;
switch (sv.getStatus())
{
case Eris::ServerInfo::VALID:
cout << "\tserver: " << sv.getServer() << " " << sv.getVersion() << " (builddate " << sv.getBuildDate() << ")" << endl;
cout << "\truleset:" << sv.getRuleset() << endl;
cout << "\tuptime:" << timeFormat(sv.getUptime()) << endl;
cout << "\tping:" << sv.getPing() << endl;
cout << "\tconnected clients:" << sv.getNumClients() << endl;
break;
case Eris::ServerInfo::TIMEOUT:
cout << "Timed out." << endl;
break;
case Eris::ServerInfo::QUERYING:
cout << "Something is broken, all queries should be done" << endl;
break;
default:
cout << "Query failed" << endl;
}
} // of server iteration
}
void dumpToXML(const Eris::Meta & meta)
{
cout << "<metaquery>" << endl;
for(unsigned int S=0; S < meta.getGameServerCount(); ++S)
{
const Eris::ServerInfo& sv = meta.getInfoForServer(S);
cout << "<server status=\"";
switch (sv.getStatus())
{
case Eris::ServerInfo::VALID: cout << "valid"; break;
case Eris::ServerInfo::TIMEOUT: cout << "timeout"; break;
case Eris::ServerInfo::QUERYING: cout << "querying"; break;
default: cout << "failed";
}
cout << "\">" << endl;
cout << "<address>" << sv.getHostname() << "</address>" << endl;
if(sv.getStatus() == Eris::ServerInfo::VALID)
{
cout << "<status>valid</status>" << endl;
cout << "<name>" << sv.getServername() << "</name>" << endl;
cout << "<servertype>" << sv.getServer() << "</servertype>" << endl;
cout << "<ruleset>" << sv.getRuleset() << "</ruleset>" << endl;
cout << "<uptime>" << timeFormat(sv.getUptime()) << "</uptime>" << endl;
cout << "<ping>" << sv.getPing() << "</ping>" << endl;
cout << "<clients>" << sv.getNumClients() << "</clients>" << endl;
cout << "<builddate>" << sv.getBuildDate() << "</builddate>" << endl;
cout << "<version>" << sv.getVersion() << "</version>" << endl;
}
cout << "</server>" << endl;
} // of server iteration
cout << "</metaquery>" << endl;
}
void dumpToHTML(const Eris::Meta& meta)
{
cout << "<div class=\"metaserver\">" << endl;
cout << " <dl>" << endl;
for (unsigned int S=0; S < meta.getGameServerCount(); ++S)
{
const Eris::ServerInfo& sv = meta.getInfoForServer(S);
cout << " <dt>" << sv.getHostname() << " :: " << sv.getServername() << "</dt>" << endl;
cout << " <dd>" << endl;
switch (sv.getStatus())
{
case Eris::ServerInfo::VALID:
cout << "Server: " << sv.getServer() << " " << sv.getVersion() << " (builddate " << sv.getBuildDate() << ")<br/>" << endl;
cout << "Ruleset: " << sv.getRuleset() << "<br/>" << endl;
cout << "Up: " << timeFormat(sv.getUptime()) << " (" << sv.getPing() << " ping)<br/>" << endl;
cout << "Clients: " << sv.getNumClients() << endl;
break;
case Eris::ServerInfo::TIMEOUT:
cout << "Timed out." << endl;
break;
case Eris::ServerInfo::QUERYING:
cout << "Something is broken, all queries should be done" << endl;
break;
default:
cout << "Query failed" << endl;
}
cout << " </dd>" << endl;
} // of server iteration
cout << " </dl>" << endl;
cout << "</div>" << endl;
}
int main(int argc, char* argv[])
{
std::string metaServer = "metaserver.worldforge.org";
std::vector< std::string > args(argv, argv + argc);
void (* dumper)(const Eris::Meta &) = dumpToScreen;
Eris::setLogLevel(Eris::LOG_DEBUG);
Eris::Logged.connect(sigc::ptr_fun(&erisLog));
if(args.size() > 1)
{
if(args[1].substr(0, 2) != "--")
{
metaServer = argv[1];
}
if(find(args.begin(), args.end(), "--html") != args.end())
{
dumper = dumpToHTML;
}
else if(find(args.begin(), args.end(), "--xml") != args.end())
{
dumper = dumpToXML;
}
if(find(args.begin(), args.end(), "--exact") != args.end())
{
exactTime = true;
}
}
// maximum of 5 simultaneous queries
Eris::Meta meta(metaServer, 5);
meta.CompletedServerList.connect(sigc::ptr_fun(&gotServerList));
meta.AllQueriesDone.connect(sigc::ptr_fun(&queriesDone));
meta.ReceivedServerInfo.connect(sigc::ptr_fun(&gotServer));
meta.Failure.connect(sigc::ptr_fun(&queryFailed));
cerr << "querying " << metaServer << endl;
meta.refresh();
while (!queryDone && !failure)
{
Eris::PollDefault::poll(10);
}
if (failure) {
cerr << "querying meta server at " << metaServer << " failed" << endl;
return EXIT_FAILURE;
}
cerr << "final list contains " << meta.getGameServerCount() << " servers." << endl;
dumper(meta);
return EXIT_SUCCESS;
}
|