Author: bdonlan
Date: 2005-05-22 16:48:48 -0400 (Sun, 22 May 2005)
New Revision: 709
Added:
trunk/clients/Javer2/src/javer2/CMod.java
Modified:
trunk/clients/Javer2/nbproject/project.properties
trunk/clients/Javer2/src/haver/Client.java
trunk/clients/Javer2/src/haver/NonblockingOutputStream.java
Log:
* Added beginnings of a haver CMod
* Improved exception safety in CallbackDist
* Improved listening set thread safety
* Added sendPrivateMessage()
project.properties: Change main class to cmod
Modified: trunk/clients/Javer2/nbproject/project.properties
===================================================================
--- trunk/clients/Javer2/nbproject/project.properties 2005-05-22 18:43:24 UTC
(rev 708)
+++ trunk/clients/Javer2/nbproject/project.properties 2005-05-22 20:48:48 UTC
(rev 709)
@@ -36,7 +36,7 @@
javadoc.use=true
javadoc.version=false
javadoc.windowtitle=
-main.class=javer2.Main
+main.class=javer2.CMod
manifest.file=manifest.mf
platform.active=Java_HotSpot_TM__Client_VM_1.4.2_08-b03
run.classpath=\
Modified: trunk/clients/Javer2/src/haver/Client.java
===================================================================
--- trunk/clients/Javer2/src/haver/Client.java 2005-05-22 18:43:24 UTC (rev
708)
+++ trunk/clients/Javer2/src/haver/Client.java 2005-05-22 20:48:48 UTC (rev
709)
@@ -16,7 +16,7 @@
*/
public class Client {
- Set listening = Collections.synchronizedSet(new java.util.HashSet());
+ HashSet listening = new java.util.HashSet();
Callback dist;
Socket theSocket = null;
boolean connecting = false;
@@ -125,13 +125,13 @@
* Internal class for distributing callback events to all listeners
*/
protected class CallbackDist extends Callback {
- java.util.Set listeners;
+ java.util.HashSet listeners;
/**
*
* @param l Listener set
*/
- public CallbackDist(Set l) {
+ public CallbackDist(HashSet l) {
listeners = l;
}
@@ -141,7 +141,9 @@
*/
protected Iterator iterator() {
// Prevent concurrency issues
- return new HashSet(listeners).iterator();
+ synchronized (listeners) {
+ return ((HashSet)listeners.clone()).iterator();
+ }
}
public void onDisconnected(Client source, IOException e) throws
IOException {
@@ -156,7 +158,11 @@
Iterator i = iterator();
while (i.hasNext()) {
Callback c = (Callback)i.next();
- c.onDisconnected(source, e);
+ try {
+ c.onDisconnected(source, e);
+ } catch (Throwable t) {
+ t.printStackTrace();
+ }
}
}
@@ -165,7 +171,11 @@
Iterator i = iterator();
while (i.hasNext()) {
Callback c = (Callback)i.next();
- c.onConnectFailed(source, e);
+ try {
+ c.onConnectFailed(source, e);
+ } catch (Throwable t) {
+ t.printStackTrace();
+ }
}
}
@@ -176,7 +186,13 @@
Iterator i = iterator();
while (i.hasNext()) {
Callback c = (Callback)i.next();
- c.onConnect(source);
+ try {
+ c.onConnect(source);
+ } catch (IOException e) {
+ throw e;
+ } catch (Throwable e) {
+ e.printStackTrace();
+ }
}
}
@@ -184,7 +200,13 @@
Iterator i = iterator();
while (i.hasNext()) {
Callback c = (Callback)i.next();
- c.onOutgoingLine(source, args);
+ try {
+ c.onOutgoingLine(source, args);
+ } catch (IOException e) {
+ throw e;
+ } catch (Throwable e) {
+ e.printStackTrace();
+ }
}
}
@@ -192,7 +214,13 @@
Iterator i = iterator();
while (i.hasNext()) {
Callback c = (Callback)i.next();
- c.onIncomingLine(source, args);
+ try {
+ c.onIncomingLine(source, args);
+ } catch (IOException e) {
+ throw e;
+ } catch (Throwable e) {
+ e.printStackTrace();
+ }
}
}
@@ -200,7 +228,13 @@
Iterator i = iterator();
while (i.hasNext()) {
Callback c = (Callback)i.next();
- c.onNeedIdent(source);
+ try {
+ c.onNeedIdent(source);
+ } catch (IOException e) {
+ throw e;
+ } catch (Throwable e) {
+ e.printStackTrace();
+ }
}
}
@@ -208,7 +242,13 @@
Iterator i = iterator();
while (i.hasNext()) {
Callback c = (Callback)i.next();
- c.onAccept(source, name);
+ try {
+ c.onAccept(source, name);
+ } catch (IOException e) {
+ throw e;
+ } catch (Throwable e) {
+ e.printStackTrace();
+ }
}
}
@@ -216,7 +256,13 @@
Iterator i = iterator();
while (i.hasNext()) {
Callback c = (Callback)i.next();
- c.onReceivedList(source, channel, namespace, list);
+ try {
+ c.onReceivedList(source, channel, namespace, list);
+ } catch (IOException e) {
+ throw e;
+ } catch (Throwable e) {
+ e.printStackTrace();
+ }
}
}
@@ -224,7 +270,13 @@
Iterator i = iterator();
while (i.hasNext()) {
Callback c = (Callback)i.next();
- c.onPublicMessage(source, channel, from, type, args);
+ try {
+ c.onPublicMessage(source, channel, from, type, args);
+ } catch (IOException e) {
+ throw e;
+ } catch (Throwable e) {
+ e.printStackTrace();
+ }
}
}
@@ -232,7 +284,13 @@
Iterator i = iterator();
while (i.hasNext()) {
Callback c = (Callback)i.next();
- c.onPrivateMessage(source, from, type, args);
+ try {
+ c.onPrivateMessage(source, from, type, args);
+ } catch (IOException e) {
+ throw e;
+ } catch (Throwable e) {
+ e.printStackTrace();
+ }
}
}
@@ -244,7 +302,12 @@
* @param port Port number to connect to
*/
protected void run(String host, int port) {
- io.setName("haver.Client input thread");
+ synchronized (this) {
+ if (Thread.currentThread() != io) {
+ throw new IllegalStateException("Client.run called outside io
thread");
+ }
+ }
+ Thread.currentThread().setName("haver.Client input thread");
try {
Socket mySock = new Socket(host, port);
theSocket = mySock;
@@ -453,7 +516,9 @@
* @param c Callback class to add
*/
public void addNotify(Callback c) {
- listening.add(c);
+ synchronized (listening) {
+ listening.add(c);
+ }
}
/**
@@ -461,7 +526,9 @@
* @param c Callback to remove
*/
public void removeNotify(Callback c) {
- listening.remove(c);
+ synchronized (listening) {
+ listening.remove(c);
+ }
}
/**
@@ -479,14 +546,10 @@
m.invoke(this, args2);
} catch (InvocationTargetException e) {
Throwable thingy = e.getTargetException();
- if (thingy instanceof Error) {
- throw (Error)thingy;
- }
if (thingy instanceof IOException) {
throw (IOException) thingy;
}
- // XXX: need better exception here
- throw new IOException("Unknown TargetException:\n" +
thingy.toString());
+ thingy.printStackTrace();
}
catch (NoSuchMethodException e) {} // unhandled server event
catch (IllegalAccessException e) {
@@ -610,7 +673,7 @@
}
protected void handle_IN(String[] args) throws IOException {
- if (args.length < 5) {
+ if (args.length < 4) {
// XXX: exception?
System.err.println("Short IN message from haver server");
return;
@@ -624,16 +687,17 @@
dist.onPublicMessage(this, channel, from, type, margs);
}
- public void sendPublicMessage(String channel, String type, String[] margs)
{
+ public void sendPublicMessage(String channel, String type, String[] margs)
throws IOException {
String[] cmd = new String[margs.length + 3];
cmd[0] = "IN";
cmd[1] = channel;
cmd[2] = type;
System.arraycopy(margs, 0, cmd, 3, margs.length);
+ sendLine(cmd);
}
protected void handle_FROM(String[] args) throws IOException {
- if (args.length < 2) {
+ if (args.length < 3) {
// XXX: exception?
System.err.println("Short FROM message from haver server");
return;
@@ -645,4 +709,13 @@
dist.onPrivateMessage(this, user, type, margs);
}
+
+ public void sendPrivateMessage (String target, String type, String[]
margs) throws IOException {
+ String[] args = new String[margs.length + 3];
+ args[0] = "TO";
+ args[1] = target;
+ args[2] = type;
+ System.arraycopy(margs, 0, args, 3, margs.length);
+ sendLine(args);
+ }
}
Modified: trunk/clients/Javer2/src/haver/NonblockingOutputStream.java
===================================================================
--- trunk/clients/Javer2/src/haver/NonblockingOutputStream.java 2005-05-22
18:43:24 UTC (rev 708)
+++ trunk/clients/Javer2/src/haver/NonblockingOutputStream.java 2005-05-22
20:48:48 UTC (rev 709)
@@ -78,6 +78,7 @@
pending = new Vector();
active_len = pending_len = preflush_len = 0;
th = new Thread(this);
+ th.setDaemon(true);
th.start();
}
Added: trunk/clients/Javer2/src/javer2/CMod.java
===================================================================
--- trunk/clients/Javer2/src/javer2/CMod.java 2005-05-22 18:43:24 UTC (rev
708)
+++ trunk/clients/Javer2/src/javer2/CMod.java 2005-05-22 20:48:48 UTC (rev
709)
@@ -0,0 +1,49 @@
+/*
+ * CMod.java
+ *
+ * Created on May 22, 2005, 3:37 PM
+ */
+
+package javer2;
+import haver.*;
+import java.util.*;
+import java.text.*;
+
+/**
+ *
+ * @author bdonlan
+ */
+public class CMod extends Callback {
+ Client c = new Client();
+ Timer t = new Timer(true);
+
+ protected void ping() {
+ logMessage("ping!");
+ TimerTask ping = new TimerTask() {
+ public void run() {
+ ping();
+ }
+ };
+ t.schedule(ping, 10000);
+ }
+
+ /** Creates a new instance of CMod */
+ public CMod(String host, int port, String name, String channel) throws
Throwable {
+ c.addNotify(this);
+ c.syncConnect(host, port, name);
+ ping();
+ logMessage("connected");
+ }
+
+ protected static void logMessage(String s) {
+ Date now = new Date(System.currentTimeMillis());
+ DateFormat tf = DateFormat.getTimeInstance(DateFormat.MEDIUM);
+ String time = tf.format(now);
+ System.err.println("[" + time + "] " + s);
+ }
+
+ public static void main(String[] args) throws Throwable {
+ new CMod("localhost", 13456, "cmod", "lobby");
+ }
+
+}
Property changes on: trunk/clients/Javer2/src/javer2/CMod.java
___________________________________________________________________
Name: svn:eol-style
+ native