Author: bdonlan
Date: 2005-05-22 23:10:36 -0400 (Sun, 22 May 2005)
New Revision: 711
Modified:
trunk/clients/Javer2/src/haver/Callback.java
trunk/clients/Javer2/src/haver/Client.java
trunk/clients/Javer2/src/javer2/CMod.java
Log:
* Add join/part/incoming quit support
* Fix missing synchronized tags on SyncMonitor functions
* Make SyncMonitor.block() loop until it's really done
* Make cmod join its initial channel
Modified: trunk/clients/Javer2/src/haver/Callback.java
===================================================================
--- trunk/clients/Javer2/src/haver/Callback.java 2005-05-23 02:54:29 UTC
(rev 710)
+++ trunk/clients/Javer2/src/haver/Callback.java 2005-05-23 03:10:36 UTC
(rev 711)
@@ -75,4 +75,10 @@
public void onPublicMessage(Client source, String channel, String from,
String type, String[] args) throws IOException {}
public void onPrivateMessage(Client source, String from, String type,
String[] args) throws IOException {}
+
+ public void onJoin(Client source, String channel, String who) throws
IOException {}
+
+ public void onPart(Client source, String channel, String who) throws
IOException {}
+
+ public void onQuit(Client source, String who, String type, String detail)
throws IOException {}
}
Modified: trunk/clients/Javer2/src/haver/Client.java
===================================================================
--- trunk/clients/Javer2/src/haver/Client.java 2005-05-23 02:54:29 UTC (rev
710)
+++ trunk/clients/Javer2/src/haver/Client.java 2005-05-23 03:10:36 UTC (rev
711)
@@ -25,6 +25,7 @@
PrintWriter writer = null;
BufferedReader reader = null;
NonblockingOutputStream s;
+ String name = null;
static final String[] greeting = {"HAVER", "haver.Client/0.00"};
@@ -293,6 +294,48 @@
}
}
}
+
+ public void onQuit(Client source, String who, String type, String
detail) throws IOException {
+ Iterator i = iterator();
+ while (i.hasNext()) {
+ Callback c = (Callback)i.next();
+ try {
+ c.onQuit(source, who, type, detail);
+ } catch (IOException e) {
+ throw e;
+ } catch (Throwable e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ public void onPart(Client source, String channel, String who) throws
IOException {
+ Iterator i = iterator();
+ while (i.hasNext()) {
+ Callback c = (Callback)i.next();
+ try {
+ c.onPart(source, channel, who);
+ } catch (IOException e) {
+ throw e;
+ } catch (Throwable e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ public void onJoin(Client source, String channel, String who) throws
IOException {
+ Iterator i = iterator();
+ while (i.hasNext()) {
+ Callback c = (Callback)i.next();
+ try {
+ c.onJoin(source, channel, who);
+ } catch (IOException e) {
+ throw e;
+ } catch (Throwable e) {
+ e.printStackTrace();
+ }
+ }
+ }
}
@@ -385,6 +428,7 @@
if (io != null && io.isAlive()) {
throw new IllegalStateException("Already connecting");
}
+ name = null;
theSocket = null;
this.host = host;
this.port = port;
@@ -412,7 +456,7 @@
/**
* Signal completion of the synchronous event
*/
- protected void done() {
+ protected synchronized void done() {
flag.ok = false;
notifyAll();
}
@@ -430,23 +474,25 @@
* Wait until the synchronous event completes
* @throws java.io.IOException
*/
- public void block() throws IOException {
- if (Thread.currentThread() == io) {
- if (flag != null) {
- ioLoop(flag);
- if (flag.ok) {
- throw new IOException("Unexpectedly disconnected");
+ public synchronized void block() throws IOException {
+ while (flag.ok) {
+ if (Thread.currentThread() == io) {
+ if (flag != null) {
+ ioLoop(flag);
+ if (flag.ok) {
+ throw new IOException("Unexpectedly disconnected");
+ }
+ } else {
+ removeNotify(this);
+ throw new IllegalStateException("Blocking in IO thread
without flag");
}
} else {
- removeNotify(this);
- throw new IllegalStateException("Blocking in IO thread
without flag");
+ try {
+ wait();
+ } catch (InterruptedException e) {
+ // return, hopefully there's an abort flag elsewhere
+ }
}
- } else {
- try {
- wait();
- } catch (InterruptedException e) {
- // return, hopefully there's an abort flag elsewhere
- }
}
}
}
@@ -583,6 +629,7 @@
* @throws java.io.IOException
*/
protected void handle_HELLO(String[] args) throws IOException {
+ name = args[1];
dist.onAccept(this, args[1]);
}
@@ -728,4 +775,76 @@
String[] args = {arg};
sendPrivateMessage(target, type, args);
}
+
+ public void handle_JOIN(String[] args) throws IOException {
+ String channel = args[1];
+ String user = args[2];
+ dist.onJoin(this, channel, user);
+ }
+
+ public void handle_PART(String[] args) throws IOException {
+ String channel = args[1];
+ String user = args[2];
+ dist.onPart(this, channel, user);
+ }
+
+ public void handle_QUIT(String[] args) throws IOException {
+ String who = args[1];
+ String type = args[2];
+ String detail = args[3];
+ dist.onQuit(this, who, type, detail);
+ }
+
+ public void sendJoin(String channel) throws IOException {
+ String[] cmd = {"JOIN", channel};
+ sendLine(cmd);
+ }
+
+ protected class JoinMonitor extends SyncMonitor {
+ String channel;
+
+ public JoinMonitor (String channel) {
+ this.channel = channel;
+ }
+
+ public void onJoin(Client from, String channel, String who) {
+ if (channel.equals(this.channel) && who.equals(name)) {
+ this.done();
+ }
+ }
+ }
+
+ public void syncJoin(String channel) throws IOException {
+ JoinMonitor m = new JoinMonitor(channel);
+ addNotify(m);
+ sendJoin(channel);
+ m.block();
+ }
+
+ public void sendPart(String channel) throws IOException {
+ String[] cmd = {"PART", channel};
+ sendLine(cmd);
+ }
+
+ protected class PartMonitor extends SyncMonitor {
+ String channel;
+
+ public PartMonitor (String channel) {
+ this.channel = channel;
+ }
+
+ public void onPart(Client from, String channel, String who) {
+ if (channel.equals(this.channel) && who.equals(name)) {
+ this.done();
+ }
+ }
+ }
+
+ public void syncPart(String channel) throws IOException {
+ PartMonitor m = new PartMonitor(channel);
+ addNotify(m);
+ sendPart(channel);
+ m.block();
+ }
+
}
Modified: trunk/clients/Javer2/src/javer2/CMod.java
===================================================================
--- trunk/clients/Javer2/src/javer2/CMod.java 2005-05-23 02:54:29 UTC (rev
710)
+++ trunk/clients/Javer2/src/javer2/CMod.java 2005-05-23 03:10:36 UTC (rev
711)
@@ -247,4 +247,8 @@
c.sendPublicMessage(channel, "say", "Game Over. Thanks for playing!");
reset();
}
+
+ public void onAccept(Client source, String name) throws IOException {
+ c.syncJoin(channel);
+ }
}