22 package com.realtime.crossfire.jxclient.metaserver;
25 import java.io.IOException;
26 import java.util.regex.Pattern;
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
135 final String[] entries = FIELD_SEPARATOR_PATTERN.split(entry, -1);
136 if (entries.length != 11) {
152 updateSeconds = Integer.parseInt(entries[0]);
153 hostname = entries[1];
154 players = Integer.parseInt(entries[2]);
155 version = entries[3];
156 comment = entries[4];
157 bytesIn = Long.parseLong(entries[5]);
158 bytesOut = Long.parseLong(entries[6]);
159 uptimeSeconds = Integer.parseInt(entries[7]);
160 archBase = entries[8];
161 codeBase = entries[9];
162 mapBase = entries[10];
163 }
catch (
final NumberFormatException ignored) {
167 return new MetaserverEntry(updateSeconds, hostname, players, version, comment, bytesIn, bytesOut, uptimeSeconds, archBase, codeBase, mapBase);
177 @SuppressWarnings(
"IfStatementWithIdenticalBranches")
180 if (line.equals(
"END_SERVER_DATA")) {
182 if (hostname == null) {
183 System.err.println(
"Warning: metaserver response missing hostname field, skipping");
184 metaserverEntry = null;
186 metaserverEntry =
new MetaserverEntry(updateSeconds, hostname, players, version, comment, bytesIn, bytesOut, uptimeSeconds, archBase, mapBase, codeBase);
190 return metaserverEntry;
192 final String[] tmp = line.split(
"=", 2);
193 if (tmp.length == 2) {
194 final String key = tmp[0];
195 final String value = tmp[1];
209 if (comment.isEmpty()) {
253 final long now = (System.currentTimeMillis()+500)/1000;
255 updateSeconds = Math.max((
int)((uptime-now)/1000), 0);
262 System.err.println(
"Ignoring unknown key: "+key);
266 throw new IOException(
"syntax error: "+line);
269 if (line.equals(
"START_SERVER_DATA")) {
272 throw new IOException(
"syntax error: "+line);
305 return entry.getUpdateSeconds()+
"|"+
replace(entry.getHostname())+
"|"+entry.getPlayers()+
"|"+
replace(entry.getVersion())+
"|"+
replace(entry.getComment())+
"|"+entry.getBytesIn()+
"|"+entry.getBytesOut()+
"|"+entry.getUptimeSeconds()+
"|"+
replace(entry.getArchBase())+
"|"+
replace(entry.getCodeBase())+
"|"+
replace(entry.getMapBase());
314 private static String
replace(@NotNull
final String str) {
315 return str.replaceAll(
"[\\|\r\n]",
" ");
int updateSeconds
The "update seconds" value for the current server entry.
MetaserverEntryParser()
Creates a new instance.
String codeBase
The "code base" value for the current server entry.
int players
The "players" value for the current server entry.
long bytesIn
The "bytes in" value for the current server entry.
boolean inSection
Whether response parsing is withing a server entry.
String comment
The "comment" value for the current server entry.
static long parseLong(@NotNull final String string, final long defaultValue)
Converts a string into a long value.
static String replace(@NotNull final String str)
Replaces characters with may cause parsing issues.
Represents a response line from the metaserver.
static final String DEFAULT_BASE
The default for arch base, map base, and code base if none specified.
static final Pattern FIELD_SEPARATOR_PATTERN
The pattern to split a metaserver response line into fields.
static String format(@NotNull final MetaserverEntry entry)
Formats a metaserver entry that returns the metaserver entry when parse with parseEntry(CharSequence)...
static int parseInt(@NotNull final String string, final int defaultValue)
Converts a string into an int value.
int uptimeSeconds
The "uptime seconds" value for the current server entry.
String hostname
The "hostname" value for the current server entry.
MetaserverEntry parseLine(@NotNull final String line)
Parses a metaserver response line.
static final String UNKNOWN_VERSION
The default server version if none specified.
Utility class for parsing strings into numbers.
String archBase
The "archetype base" value for the current server entry.
Parser for response lines of metaserver response lines.
static MetaserverEntry parseEntry(@NotNull final CharSequence entry)
Parses a metaserver response line.
String mapBase
The "map base" value for the current server entry.
String version
The "server version" value for the current server entry.
long bytesOut
The "bytes out" value for the current server entry.
void clear()
Resets values for the current server entry.