[Cherbot-commit] SF.net SVN: cherbot: [90] trunk/src/net/sf/cherbot/CrossfireConnection.java
Status: Alpha
Brought to you by:
christianhujer
|
From: <chr...@us...> - 2007-06-20 22:17:43
|
Revision: 90
http://svn.sourceforge.net/cherbot/?rev=90&view=rev
Author: christianhujer
Date: 2007-06-20 15:17:41 -0700 (Wed, 20 Jun 2007)
Log Message:
-----------
Added connection for crossfire (tested).
Added Paths:
-----------
trunk/src/net/sf/cherbot/CrossfireConnection.java
Added: trunk/src/net/sf/cherbot/CrossfireConnection.java
===================================================================
--- trunk/src/net/sf/cherbot/CrossfireConnection.java (rev 0)
+++ trunk/src/net/sf/cherbot/CrossfireConnection.java 2007-06-20 22:17:41 UTC (rev 90)
@@ -0,0 +1,169 @@
+/*
+ * 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.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 {
+
+ /** Default server host. */
+ //@NotNull public static final String DEFAULT_CROSSFIRE_HOST = "crossfire.metalforge.net";
+ @NotNull public static final String DEFAULT_CROSSFIRE_HOST = "127.0.0.1";
+
+ /** 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);
+ setPort(DEFAULT_CROSSFIRE_PORT);
+ }
+
+ /** Main program.
+ * @param args Command line arguments.
+ */
+ public static void main(final String... args) {
+ ArgParser.simpleParseAndRun(new CrossfireConnection(), args);
+ }
+
+ /** {@inheritDoc} */
+ @NotNull @Override public Socket connect() throws IOException {
+ final Socket socket = super.connect();
+ readToNull();
+ textMsg("version 1023 1023 CherBot"); // we have to cheat or the server won't let us in.
+ textMsg("setup bot 1");
+ readToNull();
+ readToNull();
+ textMsg("addme");
+ readToNull();
+ textMsg("reply " + username);
+ readToNull();
+ textMsg("reply " + password);
+ 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++) {
+ readToNull();
+ }
+ close();
+ 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
Property changes on: trunk/src/net/sf/cherbot/CrossfireConnection.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.
|