[Cherbot-commit] SF.net SVN: cherbot: [76] trunk/src/net/sf/cherbot
Status: Alpha
Brought to you by:
christianhujer
|
From: <chr...@us...> - 2007-06-17 17:09:16
|
Revision: 76
http://svn.sourceforge.net/cherbot/?rev=76&view=rev
Author: christianhujer
Date: 2007-06-17 10:09:14 -0700 (Sun, 17 Jun 2007)
Log Message:
-----------
Moved common code from DaimoninConnection and IRCConnection into their superclass AbstractConnection.
Modified Paths:
--------------
trunk/src/net/sf/cherbot/AbstractConnection.java
trunk/src/net/sf/cherbot/DaimoninConnection.java
trunk/src/net/sf/cherbot/IRCConnection.java
Modified: trunk/src/net/sf/cherbot/AbstractConnection.java
===================================================================
--- trunk/src/net/sf/cherbot/AbstractConnection.java 2007-06-17 14:37:59 UTC (rev 75)
+++ trunk/src/net/sf/cherbot/AbstractConnection.java 2007-06-17 17:09:14 UTC (rev 76)
@@ -6,9 +6,99 @@
package net.sf.cherbot;
+import net.sf.japi.io.args.BasicCommand;
+import net.sf.japi.io.args.Option;
+import net.sf.japi.io.args.OptionType;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import java.net.Socket;
+import java.io.IOException;
+import java.io.Closeable;
+import java.io.Serializable;
+
/** An AbstractConnection represents the connection to a server and has one or more channels.
* @author <a href="mailto:ch...@ri...">Christian Hujer</a>
*/
-public abstract class AbstractConnection {
+public abstract class AbstractConnection extends BasicCommand implements Closeable, Serializable {
+ /** The host to connect to.
+ * @serial include
+ */
+ @Nullable private String host;
+
+ /** The port to connect to.
+ * @serial include
+ */
+ private int port;
+
+ /** The socket for the connection. */
+ @Nullable private transient Socket socket;
+
+ /** Sets the host of the IRC server to connect to.
+ * @param host Host of the IRC server.
+ */
+ @Option(type = OptionType.REQUIRED, value = {"host"})
+ public void setHost(@NotNull final String host) {
+ this.host = host;
+ }
+
+ /** Returns the host to connect to.
+ * @return The host to connect to.
+ */
+ @Nullable public String getHost() {
+ return host;
+ }
+
+ /** Sets the port of the IRC server to connect to.
+ * @param port Port of the IRC server.
+ */
+ @Option({"port"})
+ public void setPort(@NotNull final Integer port) {
+ this.port = port;
+ }
+
+ /** Returns the port to connect to.
+ * @return The port to connect to.
+ */
+ public int getPort() {
+ return port;
+ }
+
+ /** Establishes the connection.
+ * @return The socket created for this connection.
+ * @throws IOException in case of connection problems.
+ */
+ @NotNull public Socket connect() throws IOException {
+ final Socket socket = new Socket(host, port);
+ setSocket(socket);
+ return socket;
+ }
+
+ /** {@inheritDoc} */
+ public void close() throws IOException {
+ try {
+ final Socket socket = this.socket;
+ if (socket != null) {
+ socket.close();
+ }
+ } finally {
+ socket = null;
+ }
+ }
+
+ /** Sets the socket for the connection.
+ * Use this method if you want to create a connection on an existing socket instead of letting this Connection automatically create its own socket.
+ * @param socket Socket for the connection.
+ */
+ public void setSocket(@NotNull final Socket socket) {
+ this.socket = socket;
+ }
+
+ /** Returns the socket for the connection.
+ * @return The socket for the connection.
+ */
+ @Nullable public Socket getSocket() {
+ return socket;
+ }
+
} // class AbstractConnection
Modified: trunk/src/net/sf/cherbot/DaimoninConnection.java
===================================================================
--- trunk/src/net/sf/cherbot/DaimoninConnection.java 2007-06-17 14:37:59 UTC (rev 75)
+++ trunk/src/net/sf/cherbot/DaimoninConnection.java 2007-06-17 17:09:14 UTC (rev 76)
@@ -6,15 +6,14 @@
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.io.BufferedOutputStream;
-import java.io.EOFException;
import java.net.Socket;
import java.util.List;
import net.sf.japi.io.args.ArgParser;
-import net.sf.japi.io.args.BasicCommand;
import net.sf.japi.io.args.Option;
import net.sf.japi.io.args.OptionType;
import org.jetbrains.annotations.NotNull;
@@ -23,7 +22,7 @@
/** A DaimoninConnection represents a connection to a Daimonin server.
* @author <a href="mailto:ch...@ri...">Christian Hujer</a>
*/
-public class DaimoninConnection extends BasicCommand {
+public class DaimoninConnection extends AbstractConnection {
/** Default server host. */
//@NotNull public static final String DEFAULT_DAIMONIN_HOST = "daimonin.game-server.cc";
@@ -32,21 +31,12 @@
/** Default server port. */
public static final int DEFAULT_DAIMONIN_PORT = 13327;
- /** The host to connect to. */
- @NotNull private String host = DEFAULT_DAIMONIN_HOST;
-
- /** The port to connect to. */
- private int port = DEFAULT_DAIMONIN_PORT;
-
/** The username to authenticate with. */
@NotNull private String username;
/** The password to authenticate with. */
@NotNull private String password;
- /** The Socket for communication. */
- @Nullable private Socket socket;
-
/** The PrintStream to write to. */
@Nullable private OutputStream out;
@@ -55,6 +45,8 @@
/** Creates a DaimoninConnection. */
public DaimoninConnection() {
+ setHost(DEFAULT_DAIMONIN_HOST);
+ setPort(DEFAULT_DAIMONIN_PORT);
}
/** Main program.
@@ -67,13 +59,10 @@
/** Establishes a connection.
* @throws IOException In case of connection problems.
*/
- private void connect() throws IOException {
- final Socket socket = new Socket(host, port);
- this.socket = socket;
- final OutputStream out = new BufferedOutputStream(socket.getOutputStream());
- this.out = out;
- final InputStream in = socket.getInputStream();
- this.in = in;
+ @NotNull public Socket connect() throws IOException {
+ final Socket socket = super.connect();
+ this.out = new BufferedOutputStream(socket.getOutputStream());
+ this.in = socket.getInputStream();
// read server version
readToNull();
@@ -103,6 +92,7 @@
// write 0x00 0x12 "reply LFoobuzzer49" // L not part of name
// read 0x00 0x06 0x18 "4 QP0"
// write 0x00 0x0E "reply xxxxxxxx"
+ return socket;
}
/** Reads and discards a single message.
@@ -149,17 +139,11 @@
out.flush();
}
- /** Closes a connection.
- * @throws IOException In case of connection problems.
- */
- private void disconnect() throws IOException {
+ /** {@inheritDoc} */
+ public void close() throws IOException {
try {
- final Socket socket = this.socket;
- if (socket != null) {
- socket.close();
- }
+ super.close();
} finally {
- socket = null;
in = null;
out = null;
}
@@ -169,7 +153,7 @@
public int run(@NotNull final List<String> args) throws Exception {
connect();
Thread.sleep(10000);
- disconnect();
+ close();
return 0;
}
@@ -189,20 +173,11 @@
this.password = password;
}
- /** Sets the host to connect to.
- * @param host Host to connect to
- */
+ /** {@inheritDoc} */
+ // Overridden because for Daimonin the host is optional.
@Option({"host"})
public void setHost(@NotNull final String host) {
- this.host = host;
+ super.setHost(host);
}
- /** Sets the port to connect to.
- * @param port Port to connect to
- */
- @Option({"port"})
- public void setPort(@NotNull final Integer port) {
- this.port = port;
- }
-
} // class DaimoninConnection
Modified: trunk/src/net/sf/cherbot/IRCConnection.java
===================================================================
--- trunk/src/net/sf/cherbot/IRCConnection.java 2007-06-17 14:37:59 UTC (rev 75)
+++ trunk/src/net/sf/cherbot/IRCConnection.java 2007-06-17 17:09:14 UTC (rev 76)
@@ -15,17 +15,15 @@
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import net.sf.japi.io.args.BasicCommand;
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;
/** An IRCConnection represents a connection to an IRC server.
* @author <a href="mailto:ch...@ri...">Christian Hujer</a>
*/
-public class IRCConnection extends BasicCommand {
+public class IRCConnection extends AbstractConnection {
/** The default IRC port. */
public static final int DEFAULT_IRC_PORT = 6667;
@@ -46,15 +44,6 @@
/** The password to authenticate with NickServ or <code>null</code> if no authentication is desired. */
@Nullable private String nickPassword;
- /** The IRC server host to connect to. */
- @NotNull private String host;
-
- /** The IRC server port to connect to. */
- private int port = DEFAULT_IRC_PORT;
-
- /** The Socket for communication. */
- @Nullable private Socket socket;
-
/** The PrintStream to write to. */
@Nullable private PrintWriter out;
@@ -63,6 +52,7 @@
/** Creates an IRCConnection. */
public IRCConnection() {
+ setPort(DEFAULT_IRC_PORT);
}
/** Main program.
@@ -77,9 +67,9 @@
/** Establishes a connection.
* @throws IOException In case of connection problems.
*/
- private void connect() throws IOException {
- final Socket socket = new Socket(host, port);
- this.socket = socket;
+ @Override @NotNull public Socket connect() throws IOException {
+ super.connect();
+ final Socket socket = super.connect();
final PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "utf-8"));
this.out = out;
this.in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf-8"));
@@ -89,19 +79,14 @@
out.println("PRIVMSG NICKSERV :IDENTIFY " + nickPassword);
}
out.flush();
+ return socket;
}
- /** Closes a connection.
- * @throws IOException In case of connection problems.
- */
- private void disconnect() throws IOException {
+ /** {@inheritDoc} */
+ @Override public void close() throws IOException {
try {
- final Socket socket = this.socket;
- if (socket != null) {
- socket.close();
- }
+ super.close();
} finally {
- socket = null;
in = null;
out = null;
}
@@ -139,7 +124,7 @@
}
return 0;
} finally {
- disconnect();
+ close();
}
}
@@ -178,20 +163,4 @@
this.nickPassword = nickPassword;
}
- /** Sets the host of the IRC server to connect to.
- * @param host Host of the IRC server.
- */
- @Option(type = OptionType.REQUIRED, value = {"host"})
- public void setHost(@NotNull final String host) {
- this.host = host;
- }
-
- /** Sets the port of the IRC server to connect to.
- * @param port Port of the IRC server.
- */
- @Option({"port"})
- public void setPort(@NotNull final Integer port) {
- this.port = port;
- }
-
} // class IRCConnection
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|