001: /*
002: * TestEvent.java --
003: *
004: * This file contains classes needed to test the TclEvent and
005: * Notifier classes.
006: *
007: * Copyright (c) 1998 by Sun Microsystems, Inc.
008: *
009: * See the file "license.terms" for information on usage and redistribution
010: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
011: *
012: * RCS: @(#) $Id: TestEvent.java,v 1.1 1999/05/10 04:08:59 dejong Exp $
013: */
014:
015: package tcl.lang;
016:
017: import java.util.*;
018:
019: class TestEvent extends TclEvent {
020: /*
021: * Set variables inside this interp.
022: */
023:
024: Interp interp;
025:
026: String script;
027:
028: public TestEvent(Interp i, String s) {
029: interp = i;
030: script = s;
031: }
032:
033: /*
034: * Use the testEvtReply global varable to control what this method should
035: * return. It should be 1 most of the time -- if you want the event
036: * to be removed from the event queue after it's processed.
037: */
038:
039: public int processEvent(int flags) // Same as flags passed to Notifier.doOneEvent.
040: {
041: try {
042: interp.eval(script);
043:
044: int k = TclInteger.get(interp, interp.getVar(
045: "testEvtReply", TCL.GLOBAL_ONLY));
046: return k;
047: } catch (TclException e) {
048: System.out.println("TclException caught");
049: e.printStackTrace();
050:
051: return 1;
052: }
053: }
054:
055: } // end TestEvent
056:
057: /*
058: * Used by TclEvent.test.
059: */
060:
061: class TestEventThread1 extends Thread {
062:
063: Interp interp;
064: String script;
065:
066: public TestEventThread1(Interp i, String s) {
067: interp = i;
068: script = s;
069: }
070:
071: public void run() {
072: TestEvent evt = new TestEvent(interp, script);
073: synchronized (this ) {
074: try {
075: wait(1000);
076: } catch (InterruptedException e) {
077: }
078: }
079: interp.getNotifier().queueEvent(evt, TCL.QUEUE_TAIL);
080: evt.sync();
081: }
082:
083: } // end TestEventThread1
084:
085: /*
086: * This class tests how well the Notifier can handle events sent from
087: * different threads.
088: *
089: */
090:
091: class TestEventThread2 extends Thread {
092:
093: Interp interp;
094:
095: Vector scripts = new Vector();
096: Vector waitTime = new Vector();
097:
098: public TestEventThread2(Interp i) {
099: interp = i;
100:
101: }
102:
103: /*
104: * This method should be called in the primary thread before the thread runs.
105: */
106:
107: public void addEvent(String script, int wtime) {
108: scripts.addElement(script);
109: waitTime.addElement(new Integer(wtime));
110: }
111:
112: /*
113: * When the thread runs, it sends all the scripts to be evaluated by
114: * the interpreter in the primary Thread. If the <wtime> corresponding
115: * to the script is -1, it will sync() on the event. Otherwise, it
116: * will sleep for <wtime> milliseconds before queueing the next event.
117: */
118:
119: public void run() {
120: for (int i = 0; i < scripts.size(); i++) {
121: int wtime = ((Integer) waitTime.elementAt(i)).intValue();
122: TestEvent evt = new TestEvent(interp, (String) scripts
123: .elementAt(i));
124: interp.getNotifier().queueEvent(evt, TCL.QUEUE_TAIL);
125: if (wtime == -1) {
126: evt.sync();
127: } else {
128: try {
129: sleep((long) wtime);
130: } catch (InterruptedException e) {
131: /*
132: * do nothing.
133: */
134: }
135: }
136: }
137: }
138:
139: } // end TestEventThread2
140:
141: class TestEventDeleter implements EventDeleter {
142:
143: Interp interp;
144:
145: public TestEventDeleter(Interp i) {
146: interp = i;
147: }
148:
149: String myScript;
150:
151: public void delete(String s) {
152: myScript = s;
153:
154: interp.getNotifier().deleteEvents(this );
155: }
156:
157: public int deleteEvent(TclEvent evt) {
158: if (evt instanceof TestEvent) {
159: if (((TestEvent) evt).script.equals(myScript)) {
160: return 1;
161: }
162: }
163:
164: return 0;
165: }
166:
167: } // end TestEventDeleter
|