[Cherbot-commit] SF.net SVN: cherbot: [109] trunk/src/net/sf/cherbot
Status: Alpha
Brought to you by:
christianhujer
|
From: <chr...@us...> - 2007-06-30 19:17:45
|
Revision: 109
http://svn.sourceforge.net/cherbot/?rev=109&view=rev
Author: christianhujer
Date: 2007-06-30 12:17:43 -0700 (Sat, 30 Jun 2007)
Log Message:
-----------
Extracted common code of CrossfireConnection and DaimoninConnection into a superclass.
Modified Paths:
--------------
trunk/src/net/sf/cherbot/CrossfireConnection.java
trunk/src/net/sf/cherbot/DaimoninConnection.java
Added Paths:
-----------
trunk/src/net/sf/cherbot/AbstractCFConnection.java
Added: trunk/src/net/sf/cherbot/AbstractCFConnection.java
===================================================================
--- trunk/src/net/sf/cherbot/AbstractCFConnection.java (rev 0)
+++ trunk/src/net/sf/cherbot/AbstractCFConnection.java 2007-06-30 19:17:43 UTC (rev 109)
@@ -0,0 +1,126 @@
+/*
+ * 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;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.EOFException;
+import java.io.IOException;
+import java.io.BufferedOutputStream;
+import java.net.Socket;
+import org.jetbrains.annotations.Nullable;
+import org.jetbrains.annotations.NotNull;
+import net.sf.japi.io.args.Option;
+import net.sf.japi.io.args.OptionType;
+
+/**
+ * Common base class for Connections that use Crossfire-style protocols such as Angelion or Daimonin and of course Crossfire itself.
+ * @author <a href="mailto:ch...@ri...">Christian Hujer</a>
+ */
+public abstract class AbstractCFConnection extends AbstractConnection {
+
+ /** The username to authenticate with. */
+ @NotNull protected String username;
+
+ /** The password to authenticate with. */
+ @NotNull protected String password;
+
+ /** The PrintStream to write to. */
+ @Nullable protected OutputStream out;
+
+ /** The BufferedReader to read from. */
+ @Nullable protected InputStream in;
+
+ /** Creates an AbstractCFConnection. */
+ protected AbstractCFConnection() {
+ }
+
+ /** Reads and discards a single message.
+ * @throws IOException In case of I/O problems.
+ */
+ protected void readToNull() throws IOException {
+ receivePacket();
+ }
+
+ /** Reads the next data packet.
+ * @throws IOException In case of I/O problems.
+ * @return The data of the next data packet.
+ */
+ @NotNull protected byte[] receivePacket() throws IOException {
+ final InputStream in = this.in;
+ assert in != null;
+ final int h1 = in.read();
+ final int length = h1 != -1 && (h1 & 0x80) == 0x80 ? h1 << 16 | in.read() << 8 | in.read() : h1 << 8 | in.read();
+ if (length == -1) {
+ throw new EOFException();
+ }
+ final byte[] data = new byte[length];
+ for (int read = 0; read < length;) {
+ final int bytesRead = in.read(data, read, length - read);
+ if (bytesRead == -1) {
+ throw new EOFException();
+ }
+ read += bytesRead;
+ }
+ return data;
+ }
+
+ /** Sends a simple text message.
+ * @param msg Message to send
+ * @throws IOException In case of I/O problems.
+ */
+ protected void textMsg(@NotNull final String msg) throws IOException {
+ final OutputStream out = this.out;
+ assert out != null;
+ final byte[] data = msg.getBytes("utf-8");
+ out.write(0xFF & data.length >> 8);
+ out.write(0xFF & data.length);
+ out.write(data);
+ out.flush();
+ }
+
+ /** {@inheritDoc} */
+ public void close() throws IOException {
+ try {
+ super.close();
+ } finally {
+ in = null;
+ out = null;
+ }
+ }
+
+ /** {@inheritDoc} */
+ @Override public void setSocket(@NotNull final Socket socket) throws IOException {
+ super.setSocket(socket);
+ out = new BufferedOutputStream(socket.getOutputStream());
+ in = socket.getInputStream();
+ }
+
+ /** Sets the username for connecting to this CF-Style server.
+ * @param username Username for connecting to this CF-Style server.
+ */
+ @Option(type = OptionType.REQUIRED, value = {"username"})
+ public void setUsername(@NotNull final String username) {
+ this.username = username;
+ }
+
+ /** Sets the password for connecting to this CF-Style server.
+ * @param password Password for connecting to this CF-Style server.
+ */
+ @Option(type = OptionType.REQUIRED, value = {"password"})
+ public void setPassword(@NotNull final String password) {
+ this.password = password;
+ }
+
+ /** {@inheritDoc} */
+ // Overridden because for CF-Style servers the host is optional.
+ @Option({"host"})
+ public void setHost(@NotNull final String host) {
+ super.setHost(host);
+ }
+
+} // class AbstractCFConnection
Property changes on: trunk/src/net/sf/cherbot/AbstractCFConnection.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ LF
Modified: trunk/src/net/sf/cherbot/CrossfireConnection.java
===================================================================
--- trunk/src/net/sf/cherbot/CrossfireConnection.java 2007-06-30 18:32:48 UTC (rev 108)
+++ trunk/src/net/sf/cherbot/CrossfireConnection.java 2007-06-30 19:17:43 UTC (rev 109)
@@ -6,23 +6,16 @@
package net.sf.cherbot;
-import java.io.BufferedOutputStream;
-import java.io.EOFException;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
import java.net.Socket;
import java.util.List;
import net.sf.japi.io.args.ArgParser;
-import net.sf.japi.io.args.Option;
-import net.sf.japi.io.args.OptionType;
import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
/** A CrossfireConnection represents a connection to a Crossfire server.
* @author <a href="mailto:ch...@ri...">Christian Hujer</a>
*/
-public class CrossfireConnection extends AbstractConnection {
+public class CrossfireConnection extends AbstractCFConnection {
/** Default server host. */
//@NotNull public static final String DEFAULT_CROSSFIRE_HOST = "crossfire.metalforge.net";
@@ -31,18 +24,6 @@
/** Default server port. */
public static final int DEFAULT_CROSSFIRE_PORT = 13327;
- /** The username to authenticate with. */
- @NotNull private String username;
-
- /** The password to authenticate with. */
- @NotNull private String password;
-
- /** The PrintStream to write to. */
- @Nullable private OutputStream out;
-
- /** The BufferedReader to read from. */
- @Nullable private InputStream in;
-
/** Creates a CrossfireConnection. */
public CrossfireConnection() {
setHost(DEFAULT_CROSSFIRE_HOST);
@@ -72,68 +53,7 @@
return socket;
}
- /** Reads and discards a single message.
- * @throws IOException In case of I/O problems.
- */
- private void readToNull() throws IOException {
- receivePacket();
- }
-
- /** Reads the next data packet.
- * @throws IOException In case of I/O problems.
- * @return The data of the next data packet.
- */
- private byte[] receivePacket() throws IOException {
- final InputStream in = this.in;
- assert in != null;
- final int h1 = in.read();
- final int length = h1 != -1 && (h1 & 0x80) == 0x80 ? h1 << 16 | in.read() << 8 | in.read() : h1 << 8 | in.read();
- if (length == -1) {
- throw new EOFException();
- }
- final byte[] data = new byte[length];
- for (int read = 0; read < length;) {
- final int bytesRead = in.read(data, read, length - read);
- if (bytesRead == -1) {
- throw new EOFException();
- }
- read += bytesRead;
- }
- return data;
- }
-
- /** Sends a simple text message.
- * @param msg Message to send
- * @throws IOException In case of I/O problems.
- */
- private void textMsg(@NotNull final String msg) throws IOException {
- final OutputStream out = this.out;
- assert out != null;
- final byte[] data = msg.getBytes("utf-8");
- out.write(0xFF & data.length >> 8);
- out.write(0xFF & data.length);
- out.write(data);
- out.flush();
- }
-
/** {@inheritDoc} */
- public void close() throws IOException {
- try {
- super.close();
- } finally {
- in = null;
- out = null;
- }
- }
-
- /** {@inheritDoc} */
- @Override public void setSocket(@NotNull final Socket socket) throws IOException {
- super.setSocket(socket);
- out = new BufferedOutputStream(socket.getOutputStream());
- in = socket.getInputStream();
- }
-
- /** {@inheritDoc} */
public int run(@NotNull final List<String> args) throws Exception {
connect();
for (int i = 0; i < 10000; i++) {
@@ -143,27 +63,4 @@
return 0;
}
- /** Sets the username for connecting to this Crossfire server.
- * @param username Username for connecting to this Crossfire server.
- */
- @Option(type = OptionType.REQUIRED, value = {"username"})
- public void setUsername(@NotNull final String username) {
- this.username = username;
- }
-
- /** Sets the password for connecting to this Crossfire server.
- * @param password Password for connecting to this Crossfire server.
- */
- @Option(type = OptionType.REQUIRED, value = {"password"})
- public void setPassword(@NotNull final String password) {
- this.password = password;
- }
-
- /** {@inheritDoc} */
- // Overridden because for Crossfire the host is optional.
- @Option({"host"})
- public void setHost(@NotNull final String host) {
- super.setHost(host);
- }
-
} // class CrossfireConnection
Modified: trunk/src/net/sf/cherbot/DaimoninConnection.java
===================================================================
--- trunk/src/net/sf/cherbot/DaimoninConnection.java 2007-06-30 18:32:48 UTC (rev 108)
+++ trunk/src/net/sf/cherbot/DaimoninConnection.java 2007-06-30 19:17:43 UTC (rev 109)
@@ -6,23 +6,16 @@
package net.sf.cherbot;
-import java.io.BufferedOutputStream;
-import java.io.EOFException;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
import java.net.Socket;
import java.util.List;
import net.sf.japi.io.args.ArgParser;
-import net.sf.japi.io.args.Option;
-import net.sf.japi.io.args.OptionType;
import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
/** A DaimoninConnection represents a connection to a Daimonin server.
* @author <a href="mailto:ch...@ri...">Christian Hujer</a>
*/
-public class DaimoninConnection extends AbstractConnection {
+public class DaimoninConnection extends AbstractCFConnection {
/** Default server host. */
//@NotNull public static final String DEFAULT_DAIMONIN_HOST = "daimonin.game-server.cc";
@@ -31,18 +24,6 @@
/** Default server port. */
public static final int DEFAULT_DAIMONIN_PORT = 13327;
- /** The username to authenticate with. */
- @NotNull private String username;
-
- /** The password to authenticate with. */
- @NotNull private String password;
-
- /** The PrintStream to write to. */
- @Nullable private OutputStream out;
-
- /** The BufferedReader to read from. */
- @Nullable private InputStream in;
-
/** Creates a DaimoninConnection. */
public DaimoninConnection() {
setHost(DEFAULT_DAIMONIN_HOST);
@@ -59,9 +40,6 @@
/** {@inheritDoc} */
@NotNull @Override public Socket connect() throws IOException {
final Socket socket = super.connect();
- this.out = new BufferedOutputStream(socket.getOutputStream());
- this.in = socket.getInputStream();
-
readToNull(); // "991023 991023 Daimonin Server"
textMsg("version 991023 991023 Daimonin SDL Client"); // we have to cheat or the server won't let us in.
textMsg("setup bot 1"); // We are a bot
@@ -75,61 +53,7 @@
return socket;
}
- /** Reads and discards a single message.
- * @throws IOException In case of I/O problems.
- */
- private void readToNull() throws IOException {
- receivePacket();
- }
-
- /** Reads the next data packet.
- * @throws IOException In case of I/O problems.
- * @return The data of the next data packet.
- */
- private byte[] receivePacket() throws IOException {
- final InputStream in = this.in;
- assert in != null;
- final int h1 = in.read();
- final int length = h1 != -1 && (h1 & 0x80) == 0x80 ? h1 << 16 | in.read() << 8 | in.read() : h1 << 8 | in.read();
- if (length == -1) {
- throw new EOFException();
- }
- final byte[] data = new byte[length];
- for (int read = 0; read < length;) {
- final int bytesRead = in.read(data, read, length - read);
- if (bytesRead == -1) {
- throw new EOFException();
- }
- read += bytesRead;
- }
- return data;
- }
-
- /** Sends a simple text message.
- * @param msg Message to send
- * @throws IOException In case of I/O problems.
- */
- private void textMsg(@NotNull final String msg) throws IOException {
- final OutputStream out = this.out;
- assert out != null;
- final byte[] data = msg.getBytes("utf-8");
- out.write(0xFF & data.length >> 8);
- out.write(0xFF & data.length);
- out.write(data);
- out.flush();
- }
-
/** {@inheritDoc} */
- public void close() throws IOException {
- try {
- super.close();
- } finally {
- in = null;
- out = null;
- }
- }
-
- /** {@inheritDoc} */
public int run(@NotNull final List<String> args) throws Exception {
connect();
Thread.sleep(100000);
@@ -137,27 +61,4 @@
return 0;
}
- /** Sets the username for connecting to this Daimonin server.
- * @param username Username for connecting to this Daimonin server.
- */
- @Option(type = OptionType.REQUIRED, value = {"username"})
- public void setUsername(@NotNull final String username) {
- this.username = username;
- }
-
- /** Sets the password for connecting to this Daimonin server.
- * @param password Password for connecting to this Daimonin server.
- */
- @Option(type = OptionType.REQUIRED, value = {"password"})
- public void setPassword(@NotNull final String password) {
- this.password = password;
- }
-
- /** {@inheritDoc} */
- // Overridden because for Daimonin the host is optional.
- @Option({"host"})
- public void setHost(@NotNull final String host) {
- super.setHost(host);
- }
-
} // class DaimoninConnection
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|