3
* This file is part of jReality. jReality is open source software, made
4
* available under a BSD license:
6
* Copyright (c) 2003-2006, jReality Group: Charles Gunn, Tim Hoffmann, Markus
7
* Schmies, Steffen Weissmann.
11
* Redistribution and use in source and binary forms, with or without
12
* modification, are permitted provided that the following conditions are met:
14
* - Redistributions of source code must retain the above copyright notice, this
15
* list of conditions and the following disclaimer.
17
* - Redistributions in binary form must reproduce the above copyright notice,
18
* this list of conditions and the following disclaimer in the documentation
19
* and/or other materials provided with the distribution.
21
* - Neither the name of jReality nor the names of its contributors nor the
22
* names of their associated organizations may be used to endorse or promote
23
* products derived from this software without specific prior written
26
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36
* POSSIBILITY OF SUCH DAMAGE.
41
package opencog.spacetime.toolsystem;
43
import java.util.LinkedList;
44
import java.util.ListIterator;
45
import java.util.logging.Level;
47
import opencog.spacetime.scene.tool.InputSlot;
48
import opencog.spacetime.util.LoggingSystem;
58
public class ToolEventQueue {
60
private ToolEventReceiver receiver;
61
private LinkedList<ToolEvent> queue = new LinkedList<ToolEvent>();
62
private final Object mutex = new Object();
63
protected volatile boolean running = true;
65
private Runnable eventThread = new Runnable() {
71
while (queue.isEmpty()) {
73
if (running) mutex.wait();
75
} catch (InterruptedException e) {
79
event = (ToolEvent) queue.removeFirst();
83
receiver.processToolEvent(event);
84
} catch (Exception e) {
89
System.out.println("TEQ shut down.");
93
public ToolEventQueue(ToolEventReceiver receiver) {
94
this.receiver = receiver;
97
private volatile boolean started = false;
98
private Thread thread = new Thread(eventThread);
100
thread.setName("jReality ToolSystem EventQueue");
102
public void start() {
103
if (started) throw new IllegalStateException("already started");
109
* places the given event into the queue
110
* if queue was started already
112
* @param event the event to post
113
* @return true if the event was added false if not
115
public boolean addEvent(ToolEvent event) {
116
if (!started) return false;
122
* returns wether the event was added or if it replaced another event
125
* @return true if the event was added to the queue, false if replaced an
126
* already scheduled event
128
private boolean placeEvent(ToolEvent event) {
129
synchronized(mutex) {
130
if (!running) return false;
131
// we replace the last possible event
132
for (ListIterator i = queue.listIterator(queue.size()); i.hasPrevious(); ) {
133
ToolEvent e = (ToolEvent) i.previous();
134
if (event.canReplace(e)) {
135
LoggingSystem.getLogger(this).log(e.getInputSlot() == InputSlot.getDevice("SystemTime") ? Level.FINEST:Level.FINER, "replacing ToolEvent {0} with {1}", new Object[]{e, event});
136
e.replaceWith(event);
140
queue.addLast(event);
141
// System.out.println(queue);
147
public void dispose() {
148
synchronized (mutex) {
155
public Thread getThread() {
b'\\ No newline at end of file'