01: package org.lucane.common.net;
02:
03: import java.util.*;
04:
05: import org.lucane.common.Logging;
06:
07: /**
08: * A thread that can listen on an ObjectConnection and notify listeners
09: */
10: class ObjectListeningThread extends Thread {
11: //-- attributes
12: private ObjectConnection connection;
13: private ArrayList listeners;
14: private boolean end;
15:
16: /**
17: * Constructor
18: *
19: * @param oc the ObjectConnection to listen
20: */
21: public ObjectListeningThread(ObjectConnection oc) {
22: this .connection = oc;
23: this .listeners = new ArrayList();
24: this .end = false;
25: }
26:
27: /**
28: * Add a listener
29: *
30: * @param ol the listener
31: */
32: public void addObjectListener(ObjectListener ol) {
33: this .listeners.add(ol);
34: }
35:
36: /**
37: * Stop the thread
38: */
39: public void close() {
40: this .end = true;
41: }
42:
43: /**
44: * Thread excecution
45: * Read messages and notify listeners
46: */
47: public void run() {
48: while (!end) {
49: if (this .connection.readyToRead()) {
50: try {
51: Object o = this .connection.read();
52: Iterator i = listeners.iterator();
53: while (i.hasNext()) {
54: ObjectListener ol = (ObjectListener) i.next();
55: ol.objectRead(o);
56: }
57: } catch (Exception e) {
58: Logging.getLogger().info(e.toString());
59: }
60: } else {
61: try {
62: Thread.sleep(50);
63: } catch (Exception e) {
64: }
65: }
66: }
67: }
68: }
|