Author: bdonlan
Date: 2005-05-23 21:55:15 -0400 (Mon, 23 May 2005)
New Revision: 724
Modified:
trunk/clients/Javer2/src/org/haverdev/haver/server/ChannelBase.java
trunk/clients/Javer2/src/org/haverdev/haver/server/SimplePropagatedException.java
trunk/clients/Javer2/src/org/haverdev/haver/server/UserConnection.java
Log:
Lots of improvements...
Modified: trunk/clients/Javer2/src/org/haverdev/haver/server/ChannelBase.java
===================================================================
--- trunk/clients/Javer2/src/org/haverdev/haver/server/ChannelBase.java
2005-05-24 01:44:00 UTC (rev 723)
+++ trunk/clients/Javer2/src/org/haverdev/haver/server/ChannelBase.java
2005-05-24 01:55:15 UTC (rev 724)
@@ -22,10 +22,10 @@
HashMap subset = (HashMap)namespaces.get(namespace.toLowerCase());
if (subset == null)
return emptyArray;
- Object[] objs = subset.keySet().toArray();
+ Object[] objs = subset.values().toArray();
String[] strs = new String[objs.length];
for (int i = 0; i < objs.length; i++)
- strs[i] = (String)objs[i];
+ strs[i] = ((Entity)objs[i]).getName();
return strs;
}
Modified:
trunk/clients/Javer2/src/org/haverdev/haver/server/SimplePropagatedException.java
===================================================================
---
trunk/clients/Javer2/src/org/haverdev/haver/server/SimplePropagatedException.java
2005-05-24 01:44:00 UTC (rev 723)
+++
trunk/clients/Javer2/src/org/haverdev/haver/server/SimplePropagatedException.java
2005-05-24 01:55:15 UTC (rev 724)
@@ -1,15 +1,21 @@
package org.haverdev.haver.server;
-public class SimplePropagatedException extends PropagatedException {
- String cmd, code;
+public abstract class SimplePropagatedException extends PropagatedException {
+ String cmd, code, detail;
public SimplePropagatedException(String cmd, String code, String detail) {
- super(detail);
+ super(detail == null ? "Error " + code + " in command " + cmd :
detail);
this.cmd = cmd;
this.code = code;
+ this.detail = detail;
}
public String[] clientReport() {
- String[] fail = { "FAIL", cmd, code, this.getMessage() };
- return fail;
+ if (detail != null) {
+ String[] fail = { "FAIL", cmd, code, this.getMessage() };
+ return fail;
+ } else {
+ String[] fail = { "FAIL", cmd, code };
+ return fail;
+ }
}
}
Modified: trunk/clients/Javer2/src/org/haverdev/haver/server/UserConnection.java
===================================================================
--- trunk/clients/Javer2/src/org/haverdev/haver/server/UserConnection.java
2005-05-24 01:44:00 UTC (rev 723)
+++ trunk/clients/Javer2/src/org/haverdev/haver/server/UserConnection.java
2005-05-24 01:55:15 UTC (rev 724)
@@ -163,7 +163,7 @@
while (!sock.isClosed()) {
String line = reader.readLine();
if (line == null) {
- throw new IOException("Disconnected.");
+ cutConnection("closed", null);
}
System.err.println("C: " + line);
try {
@@ -181,7 +181,7 @@
}
}
- protected synchronized void cutConnection(String reason, String detail) {
+ protected synchronized void cutConnection() {
try { writer.close(); } catch (Throwable t) { }
try {
Thread.sleep(1000); // Hopefully flush the writer
@@ -190,13 +190,23 @@
try { sock.close(); } catch (Throwable t) { }
writer = null;
pingTimer.cancel();
+ }
+
+ protected synchronized void cutConnection(String reason, String detail) {
+ String[] sayonara = new String[detail == null ? 2 : 3];
+ sayonara[0] = "BYE";
+ sayonara[1] = reason;
+ if (detail != null)
+ sayonara[2] = detail;
+ sendLine(sayonara);
+ cutConnection();
if (this.e != null)
this.e.quit(reason, detail);
}
protected synchronized void ioExcept(IOException e) {
e.printStackTrace();
- cutConnection("discon", e.getMessage());
+ cutConnection("error", e.getMessage());
}
public synchronized void sendLine(String[] args) {
@@ -222,12 +232,12 @@
}
public void notifyPart(String channel, Entity what) {
- String[] msg = {"PART", what.getName(), channel};
+ String[] msg = {"PART", channel, what.getName()};
sendLine(msg);
}
public void notifyJoin(String channel, Entity what) {
- String[] msg = {"JOIN", what.getName(), channel};
+ String[] msg = {"JOIN", channel, what.getName()};
sendLine(msg);
}
@@ -257,8 +267,13 @@
}
public void notifyQuit(String who, String type, String detail) {
- String[] msg = { "QUIT", who, type, detail };
- sendLine(msg);
+ if (detail != null) {
+ String[] msg = { "QUIT", who, type, detail };
+ sendLine(msg);
+ } else {
+ String[] msg = { "QUIT", who, type };
+ sendLine(msg);
+ }
}
public boolean equals(Object obj) {
@@ -351,8 +366,7 @@
try {
super.processCommand(cmd);
} catch (PropagatedException e) {
- e.printStackTrace();
- ioExcept(new IOException("Client didn't greet me. I feel
unloved. :("));
+ cutConnection();
}
}
}
@@ -375,8 +389,10 @@
String who = args[1];
User them = (User)Lobby.theLobby.lookup("user", who);
if (them == null)
- throw new SimplePropagatedException("TO", "notfound.user",
"User " + who + " does not exist or is not logged in");
+ throw new UserNotFound("TO", who);
String margs[] = new String[args.length - 2];
+ if (margs.length == 0)
+ throw new MissingMessageType("TO");
System.arraycopy(args, 2, margs, 0, margs.length);
them.sendPrivateMessage(e.getName(), margs);
}
@@ -386,7 +402,7 @@
String filter = args[2];
Channel theChannel = (Channel)Lobby.theLobby.lookup("channel",
channel);
if (theChannel == null)
- throw new SimplePropagatedException("LIST", "unknown.channel",
channel);
+ throw new ChannelNotFound("LIST", channel);
String[] members = theChannel.getNames(filter);
String[] response = new String[members.length + 3];
System.arraycopy(args, 0, response, 0, 3);
@@ -399,7 +415,7 @@
Channel chan_e = (Channel)Lobby.theLobby.lookup("channel",
channel);
// Special case!
if (chan_e == Lobby.theLobby)
- throw new SimplePropagatedException("JOIN", "forbidden",
channel);
+ throw new Forbidden("PART", "&lobby");
e.join(chan_e);
}
@@ -408,7 +424,7 @@
Channel chan_e = (Channel)Lobby.theLobby.lookup("channel",
channel);
// Special case!
if (chan_e == Lobby.theLobby)
- throw new SimplePropagatedException("PART", "forbidden",
channel);
+ throw new Forbidden("PART", "&lobby");
e.part(chan_e);
}
@@ -416,6 +432,8 @@
String channel = args[1]; // TODO: check presence
Channel chan_e = (Channel)Lobby.theLobby.lookup("channel",
channel);
String[] margs = new String[args.length - 2];
+ if (margs.length == 0)
+ throw new MissingMessageType("IN");
System.arraycopy(args, 2, margs, 0, margs.length);
chan_e.distributePublicMessage(e, margs);
}
@@ -430,9 +448,7 @@
}
public void handle_BYE(String[] args) {
- String detail = args[1];
- String[] sayonara = { "BYE", "active", detail };
- sendLine(sayonara);
+ String detail = args.length > 1 ? args[1] : null;
cutConnection("active", detail);
}