[Cherbot-commit] SF.net SVN: cherbot: [51] trunk/src
Status: Alpha
Brought to you by:
christianhujer
|
From: <chr...@us...> - 2007-06-09 17:14:37
|
Revision: 51
http://svn.sourceforge.net/cherbot/?rev=51&view=rev
Author: christianhujer
Date: 2007-06-09 10:14:35 -0700 (Sat, 09 Jun 2007)
Log Message:
-----------
Added MetaServer stuff.
Added Paths:
-----------
trunk/src/net/sf/cherbot/metaserver/
trunk/src/net/sf/cherbot/metaserver/MetaServerEntry.java
trunk/src/net/sf/cherbot/metaserver/MetaServerInfo.java
trunk/src/test/net/sf/cherbot/metaserver/
trunk/src/test/net/sf/cherbot/metaserver/MetaServerEntryTest.java
trunk/src/test/net/sf/cherbot/metaserver/MetaServerInfoTest.java
Added: trunk/src/net/sf/cherbot/metaserver/MetaServerEntry.java
===================================================================
--- trunk/src/net/sf/cherbot/metaserver/MetaServerEntry.java (rev 0)
+++ trunk/src/net/sf/cherbot/metaserver/MetaServerEntry.java 2007-06-09 17:14:35 UTC (rev 51)
@@ -0,0 +1,162 @@
+/*
+ * Copyright © 2007, Christian Hujer and the CherBot developers. All Rights Reserved.
+ * License: GNU General Public License v2.0 or newer.
+ * See file COPYING in the root directory of this project.
+ */
+
+package net.sf.cherbot.metaserver;
+
+import org.jetbrains.annotations.NotNull;
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
+
+/**
+ * Created by IntelliJ IDEA.
+ * @author <a href="mailto:ch...@ri...">Christian Hujer</a>
+ */
+public class MetaServerEntry {
+
+ /** The pattern for parsing an entryString from the metaserver. */
+ @NotNull private static final Pattern entryPattern = Pattern.compile("(.*?)\\|(.*?)\\|(.*?)\\|(.*?)\\|(.*?)\\|(.*?)\\|(.*?)\\|(.*?)\\|(.*?)");
+
+ /** The ip address of this entry. */
+ @NotNull private final String ipaddress;
+
+ /** Unknown1. */
+ @NotNull private final String unknown1;
+
+ /** The host name of this entry. */
+ @NotNull private final String hostname;
+
+ /** The number of currently logged in players. */
+ private final int playerCount;
+
+ /** The version string of this entry's server. */
+ @NotNull private final String version;
+
+ /** The description of this entry. */
+ @NotNull private final String description;
+
+ /** Unknown2. */
+ @NotNull private final String unknown2;
+
+ /** Unknown3. */
+ @NotNull private final String unknown3;
+
+ /** Unknown3. */
+ @NotNull private final String unknown4;
+
+ /** Create a MetaServerEntry.
+ * @param ipaddress The ip address of this entry.
+ * @param unknown1 Unknown1.
+ * @param hostname The host name of this entry.
+ * @param playerCount The number of currently logged in players.
+ * @param version The version string of this entry's server.
+ * @param description The description of this entry.
+ * @param unknown2 Unknown2.
+ * @param unknown3 Unknown3.
+ * @param unknown4 Unknown4.
+ */
+ public MetaServerEntry(final String ipaddress, final String unknown1, final String hostname, final int playerCount, final String version, final String description, final String unknown2, final String unknown3, final String unknown4) {
+ this.ipaddress = ipaddress;
+ this.unknown1 = unknown1;
+ this.hostname = hostname;
+ this.playerCount = playerCount;
+ this.version = version;
+ this.description = description;
+ this.unknown2 = unknown2;
+ this.unknown3 = unknown3;
+ this.unknown4 = unknown4;
+ }
+
+ /** Creates a MetaServerEntry by parsing a single entry String as returned by the metaserer.
+ * @param entryString String of the metaserver entry.
+ * @return MetaServerEntry created by parsing <var>entryString</var>.
+ */
+ @NotNull public static MetaServerEntry parse(@NotNull final String entryString) {
+ final Matcher matcher = entryPattern.matcher(entryString);
+ if (matcher.matches()) {
+ return new MetaServerEntry(
+ matcher.group(1),
+ matcher.group(2),
+ matcher.group(3),
+ Integer.parseInt(matcher.group(4)),
+ matcher.group(5),
+ matcher.group(6),
+ matcher.group(7),
+ matcher.group(8),
+ matcher.group(9)
+ );
+ }
+ throw new IllegalArgumentException("supplied String is not a MetaServer string.");
+ }
+
+ /** Returns the ip address of this entry.
+ * @return The ip address of this entry.
+ */
+ @NotNull public String getIpaddress() {
+ return ipaddress;
+ }
+
+ /** Returns the Unkown1.
+ * @return The Unknown1.
+ */
+ @NotNull public String getUnknown1() {
+ return unknown1;
+ }
+
+ /** Returns the host name of this entry.
+ * @return The host name of this entry.
+ */
+ @NotNull public String getHostname() {
+ return hostname;
+ }
+
+ /** Returns the number of currently logged in players.
+ * @return The number of currently logged in players.
+ */
+ public int getPlayerCount() {
+ return playerCount;
+ }
+
+ /** Returns the version string of this entry's server.
+ * @return The version string of this entry's server.
+ */
+ @NotNull public String getVersion() {
+ return version;
+ }
+
+ /** Returns the description of this entry.
+ * @return The description of this entry.
+ */
+ @NotNull public String getDescription() {
+ return description;
+ }
+
+ /** Returns the Unkown2.
+ * @return The Unknown2.
+ */
+ @NotNull public String getUnknown2() {
+ return unknown2;
+ }
+
+ /** Returns the Unkown3.
+ * @return The Unknown3.
+ */
+ @NotNull public String getUnknown3() {
+ return unknown3;
+ }
+
+ /** Returns the Unkown4.
+ * @return The Unknown4.
+ */
+ @NotNull public String getUnknown4() {
+ return unknown4;
+ }
+
+ /** {@inheritDoc} */
+ @NotNull public String toString() {
+ return hostname + " (" + version + ") " + playerCount + " players. (" + description + ")";
+ }
+
+} // class MetaServerEntry
Property changes on: trunk/src/net/sf/cherbot/metaserver/MetaServerEntry.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ LF
Added: trunk/src/net/sf/cherbot/metaserver/MetaServerInfo.java
===================================================================
--- trunk/src/net/sf/cherbot/metaserver/MetaServerInfo.java (rev 0)
+++ trunk/src/net/sf/cherbot/metaserver/MetaServerInfo.java 2007-06-09 17:14:35 UTC (rev 51)
@@ -0,0 +1,139 @@
+/*
+ * Copyright © 2007, Christian Hujer and the CherBot developers. All Rights Reserved.
+ * License: GNU General Public License v2.0 or newer.
+ * See file COPYING in the root directory of this project.
+ */
+
+package net.sf.cherbot.metaserver;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ArrayList;
+import java.net.Socket;
+import org.jetbrains.annotations.NotNull;
+import net.sf.cherbot.Updatable;
+import net.sf.japi.io.args.BasicCommand;
+import net.sf.japi.io.args.Option;
+
+/** Represents the information stored in a MetaServer.
+ * @author <a href="mailto:ch...@ri...">Christian Hujer</a>
+ */
+public class MetaServerInfo extends BasicCommand implements Iterable<MetaServerEntry>, Updatable {
+
+ /** The entries of this MetaServerInfo. */
+ @NotNull private List<MetaServerEntry> metaServerEntries = new ArrayList<MetaServerEntry>();
+
+ /** The address (ip address or hostname) of the meta server to query. */
+ @NotNull private String host;
+
+ /** The port of the meta server to query. */
+ private int port;
+
+ /** Creates a MetaServerInfo.
+ * This constructor creates a dummy meta server with no functional host and port.
+ * It cannot be used for querying and exists for testing purposes only.
+ */
+ public MetaServerInfo() {
+ this("dummy", 0);
+ }
+
+ /** Creates a MetaServerInfo.
+ * The MetaServerInfo is initially empty.
+ * Invoke the query() method to update information.
+ * @param host address (ip address or hostname) of the meta server to query.
+ * @param port port of the meta server to query
+ */
+ public MetaServerInfo(@NotNull final String host, final int port) {
+ this.host = host;
+ this.port = port;
+ }
+
+ /** Parse meta server information from the specified BufferedReader.
+ * @param in BufferedReader to parse information from.
+ * @throws IOException In case of I/O problems while reading from <var>in</var>.
+ */
+ public void parse(final BufferedReader in) throws IOException {
+ final List<MetaServerEntry> metaServerEntries = new ArrayList<MetaServerEntry>();
+ for (String line; (line = in.readLine()) != null;) {
+ metaServerEntries.add(MetaServerEntry.parse(line));
+ }
+ this.metaServerEntries = metaServerEntries;
+ }
+
+ /** Returns the MetaServerEntry with the specified index.
+ * @param index Index of the MetaServerEntry to return.
+ * @return MetaServerEntry with the specified index.
+ */
+ public MetaServerEntry get(final int index) {
+ return metaServerEntries.get(index);
+ }
+
+ /** Returns the number of MetaServerEntries for this MetaServerInfo.
+ * @return Number of MetaServerEntries.
+ */
+ public int size() {
+ return metaServerEntries.size();
+ }
+
+ /** {@inheritDoc} */
+ @NotNull public Iterator<MetaServerEntry> iterator() {
+ return metaServerEntries.iterator();
+ }
+
+ /** Sets the host of the meta server to query.
+ * @param host The host of the meta server to query.
+ */
+ @Option({"h", "host"})
+ public void setHost(@NotNull final String host) {
+ this.host = host;
+ }
+
+ /** Sets the port of the meta server to query.
+ * @param port The port of the meta server to query.
+ */
+ @Option({"p", "port"})
+ public void setPort(final int port) {
+ this.port = port;
+ }
+
+ /** Returns the host of the meta server to query.
+ * @return The host of the meta server to query.
+ */
+ @NotNull public String getHost() {
+ return host;
+ }
+
+ /** Returns the port of the meta server to query.
+ * @return The port of the meta server to query.
+ */
+ public int getPort() {
+ return port;
+ }
+
+ /** {@inheritDoc} */
+ public void update() throws Exception {
+ final Socket s = new Socket(host, port);
+ try {
+ final BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
+ parse(in);
+ } finally {
+ s.close();
+ }
+ }
+
+ /** {@inheritDoc} */
+ public int run(@NotNull final List<String> args) throws Exception {
+ if (args.size() > 0) {
+ throw new Exception("Wrong usage. Try --help.");
+ }
+ update();
+ for (final MetaServerEntry entry : this) {
+ System.out.println(entry);
+ }
+ return 0;
+ }
+
+} // class MetaServerInfo
Property changes on: trunk/src/net/sf/cherbot/metaserver/MetaServerInfo.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ LF
Added: trunk/src/test/net/sf/cherbot/metaserver/MetaServerEntryTest.java
===================================================================
--- trunk/src/test/net/sf/cherbot/metaserver/MetaServerEntryTest.java (rev 0)
+++ trunk/src/test/net/sf/cherbot/metaserver/MetaServerEntryTest.java 2007-06-09 17:14:35 UTC (rev 51)
@@ -0,0 +1,46 @@
+/*
+ * Copyright © 2007, Christian Hujer and the CherBot developers. All Rights Reserved.
+ * License: GNU General Public License v2.0 or newer.
+ * See file COPYING in the root directory of this project.
+ */
+
+package test.net.sf.cherbot.metaserver;
+
+import org.junit.Test;
+import org.junit.Assert;
+import net.sf.cherbot.metaserver.MetaServerEntry;
+
+/** Unit-Test for {@link MetaServerEntry}.
+ * @author <a href="mailto:ch...@ri...">Christian Hujer</a>
+ */
+public class MetaServerEntryTest {
+
+ /** Tests the constructor {@link net.sf.cherbot.metaserver.MetaServerEntry#MetaServerEntry(String, String, String, int, String, String, String, String, String)} and all related getters. */
+ @Test public void testMetaServerEntry() {
+ final MetaServerEntry testling = new MetaServerEntry("192.168.0.1", "177", "localhost", 20, "0.9.7", "My own Daimonin server.", "02", "03", "04");
+ Assert.assertEquals("ipaddress must be stored.", "192.168.0.1", testling.getIpaddress());
+ Assert.assertEquals("unknown1 must be stored.", "177", testling.getUnknown1());
+ Assert.assertEquals("hostname must be stored.", "localhost", testling.getHostname());
+ Assert.assertEquals("playercount must be stored.", 20, testling.getPlayerCount());
+ Assert.assertEquals("version must be stored.", "0.9.7", testling.getVersion());
+ Assert.assertEquals("description must be stored.", "My own Daimonin server.", testling.getDescription());
+ Assert.assertEquals("unknown2 must be stored.", "02", testling.getUnknown2());
+ Assert.assertEquals("unknown3 must be stored.", "03", testling.getUnknown3());
+ Assert.assertEquals("unknown4 must be stored.", "04", testling.getUnknown4());
+ }
+
+ /** Tests the method {@link MetaServerEntry#parse(String)}. */
+ @Test public void testParse() {
+ final MetaServerEntry testling = MetaServerEntry.parse("192.168.0.1|177|localhost|20|0.9.7|My own Daimonin server.|02|03|04");
+ Assert.assertEquals("ipaddress must be stored.", "192.168.0.1", testling.getIpaddress());
+ Assert.assertEquals("unknown1 must be stored.", "177", testling.getUnknown1());
+ Assert.assertEquals("hostname must be stored.", "localhost", testling.getHostname());
+ Assert.assertEquals("playercount must be stored.", 20, testling.getPlayerCount());
+ Assert.assertEquals("version must be stored.", "0.9.7", testling.getVersion());
+ Assert.assertEquals("description must be stored.", "My own Daimonin server.", testling.getDescription());
+ Assert.assertEquals("unknown2 must be stored.", "02", testling.getUnknown2());
+ Assert.assertEquals("unknown3 must be stored.", "03", testling.getUnknown3());
+ Assert.assertEquals("unknown4 must be stored.", "04", testling.getUnknown4());
+ }
+
+} // class MetaServerEntryTest
Property changes on: trunk/src/test/net/sf/cherbot/metaserver/MetaServerEntryTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ LF
Added: trunk/src/test/net/sf/cherbot/metaserver/MetaServerInfoTest.java
===================================================================
--- trunk/src/test/net/sf/cherbot/metaserver/MetaServerInfoTest.java (rev 0)
+++ trunk/src/test/net/sf/cherbot/metaserver/MetaServerInfoTest.java 2007-06-09 17:14:35 UTC (rev 51)
@@ -0,0 +1,76 @@
+/*
+ * Copyright © 2007, Christian Hujer and the CherBot developers. All Rights Reserved.
+ * License: GNU General Public License v2.0 or newer.
+ * See file COPYING in the root directory of this project.
+ */
+
+package test.net.sf.cherbot.metaserver;
+
+import net.sf.cherbot.metaserver.MetaServerInfo;
+import org.junit.Test;
+import org.junit.Assert;
+import java.io.IOException;
+import java.io.BufferedReader;
+import java.io.StringReader;
+
+/** Unit-Test for {@link MetaServerInfo}.
+ * @author <a href="mailto:ch...@ri...">Christian Hujer</a>
+ */
+public class MetaServerInfoTest {
+
+ /** Tests whether the constructor {@link net.sf.cherbot.metaserver.MetaServerInfo#MetaServerInfo(String, int)} and the corresponding getters work. */
+ @Test public void testMetaServerInfo() {
+ final MetaServerInfo testling = new MetaServerInfo("localhost", 1234);
+ Assert.assertEquals("host must be stored.", "localhost", testling.getHost());
+ Assert.assertEquals("port must be stored.", 1234, testling.getPort());
+ }
+
+ /** Tests whether parsing from a BufferedReader works.
+ * @throws IOException in case of I/O problems (unexpected).
+ */
+ @Test public void testParse() throws IOException {
+ final String metaServerString = "192.168.0.1|177|riedquat|20|0.9.7|My own Daimonin server.|02|03|04\n" +
+ "192.168.0.2|177|lave|20|0.9.7|My other Daimonin server.|02|03|04\n";
+ final MetaServerInfo testling = new MetaServerInfo();
+ testling.parse(new BufferedReader(new StringReader(metaServerString)));
+ Assert.assertEquals("MetaServerInfo now must contain 2 entries.", 2, testling.size());
+ Assert.assertEquals("ip address of first entry must be 192.168.0.1.", "192.168.0.1", testling.get(0).getIpaddress());
+ Assert.assertEquals("ip address of second entry must be 192.168.0.2.", "192.168.0.2", testling.get(1).getIpaddress());
+ }
+
+ /** Tests whether parsing from a BufferedReader deletes previous entries.
+ * @throws IOException in case of I/O problems (unexpected).
+ */
+ @Test public void testParseDeletes() throws IOException {
+ final String metaServerString = "192.168.0.1|177|riedquat|20|0.9.7|My own Daimonin server.|02|03|04\n" +
+ "192.168.0.2|177|lave|20|0.9.7|My other Daimonin server.|02|03|04\n";
+ final MetaServerInfo testling = new MetaServerInfo();
+ testling.parse(new BufferedReader(new StringReader(metaServerString)));
+ Assert.assertEquals("MetaServerInfo now must contain 2 entries.", 2, testling.size());
+ testling.parse(new BufferedReader(new StringReader(metaServerString)));
+ Assert.assertEquals("Parsing a second time must delete previous entries, so size again is 2.", 2, testling.size());
+ }
+
+ /** Tests that {@link net.sf.cherbot.metaserver.MetaServerInfo#update()} doesn't violate the contract of {@link net.sf.cherbot.Updatable#update()}.
+ * @throws IOException in case of I/O problems (unexpected).
+ */
+ @Test public void testUpdate() throws IOException {
+ final String metaServerString = "192.168.0.1|177|riedquat|20|0.9.7|My own Daimonin server.|02|03|04\n" +
+ "192.168.0.2|177|lave|20|0.9.7|My other Daimonin server.|02|03|04\n";
+ final MetaServerInfo testling = new MetaServerInfo();
+ testling.parse(new BufferedReader(new StringReader(metaServerString)));
+ Assert.assertEquals("MetaServerInfo now must contain 2 entries.", 2, testling.size());
+ Assert.assertEquals("ip address of first entry must be 192.168.0.1.", "192.168.0.1", testling.get(0).getIpaddress());
+ Assert.assertEquals("ip address of second entry must be 192.168.0.2.", "192.168.0.2", testling.get(1).getIpaddress());
+ try {
+ testling.update();
+ Assert.fail("Exception expected but not thrown.");
+ } catch (final Exception ignore) {
+ // expected
+ }
+ // Verify that the failed update() didn't destroy information.
+ Assert.assertEquals("MetaServerInfo now still must contain 2 entries.", 2, testling.size());
+ Assert.assertEquals("ip address of first entry still must be 192.168.0.1.", "192.168.0.1", testling.get(0).getIpaddress());
+ Assert.assertEquals("ip address of second entry still must be 192.168.0.2.", "192.168.0.2", testling.get(1).getIpaddress());
+ }
+} // class MetaServerInfoTest
Property changes on: trunk/src/test/net/sf/cherbot/metaserver/MetaServerInfoTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ LF
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|