~seh999/jcog/proto3

« back to all changes in this revision

Viewing changes to spacetime/src.tool/opencog/spacetime/toolsystem/ToolEventQueue.java

  • Committer: SeH
  • Date: 2009-09-19 22:59:48 UTC
  • Revision ID: seh999@gmail.com-20090919225948-q3ab80xa57i74mm6
start of major jReality refactoring

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 *
3
 
 * This file is part of jReality. jReality is open source software, made
4
 
 * available under a BSD license:
5
 
 *
6
 
 * Copyright (c) 2003-2006, jReality Group: Charles Gunn, Tim Hoffmann, Markus
7
 
 * Schmies, Steffen Weissmann.
8
 
 *
9
 
 * All rights reserved.
10
 
 *
11
 
 * Redistribution and use in source and binary forms, with or without
12
 
 * modification, are permitted provided that the following conditions are met:
13
 
 *
14
 
 * - Redistributions of source code must retain the above copyright notice, this
15
 
 *   list of conditions and the following disclaimer.
16
 
 *
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.
20
 
 *
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
24
 
 *   permission.
25
 
 *
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.
37
 
 *
38
 
 */
39
 
 
40
 
 
41
 
package opencog.spacetime.toolsystem;
42
 
 
43
 
import java.util.LinkedList;
44
 
import java.util.ListIterator;
45
 
import java.util.logging.Level;
46
 
 
47
 
import opencog.spacetime.scene.tool.InputSlot;
48
 
import opencog.spacetime.util.LoggingSystem;
49
 
 
50
 
 
51
 
/**
52
 
 * 
53
 
 * TODO: check sync!
54
 
 * 
55
 
 * @author weissman
56
 
 *
57
 
 **/
58
 
public class ToolEventQueue {
59
 
    
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;
64
 
    
65
 
    private Runnable eventThread = new Runnable() {
66
 
        public void run() {
67
 
            ToolEvent event;
68
 
            boolean isRunning;
69
 
            loop: while (true) {
70
 
                synchronized(mutex) {
71
 
                    while (queue.isEmpty()) {
72
 
                        try {
73
 
                                if (running) mutex.wait();
74
 
                            else break loop;
75
 
                        } catch (InterruptedException e) {
76
 
                            throw new Error();
77
 
                        }
78
 
                    }
79
 
                    event = (ToolEvent) queue.removeFirst();
80
 
                    isRunning = running;
81
 
                }
82
 
                if (isRunning) try {
83
 
                        receiver.processToolEvent(event);
84
 
                } catch (Exception e) {
85
 
                        e.printStackTrace();
86
 
                }
87
 
                else break;
88
 
            }
89
 
            System.out.println("TEQ shut down.");
90
 
        }    
91
 
    };
92
 
    
93
 
    public ToolEventQueue(ToolEventReceiver receiver) {
94
 
        this.receiver = receiver;
95
 
    }
96
 
 
97
 
    private volatile boolean started = false;
98
 
    private Thread thread = new Thread(eventThread);
99
 
    {
100
 
      thread.setName("jReality ToolSystem EventQueue");
101
 
    }
102
 
    public void start() {
103
 
      if (started) throw new IllegalStateException("already started");
104
 
      started = true;
105
 
      thread.start();
106
 
    }
107
 
    
108
 
    /**
109
 
     * places the given event into the queue
110
 
     * if queue was started already
111
 
     * 
112
 
     * @param event the event to post
113
 
     * @return true if the event was added false if not
114
 
     */
115
 
    public boolean addEvent(ToolEvent event) {
116
 
      if (!started) return false;
117
 
      placeEvent(event);
118
 
      return true;
119
 
    }
120
 
    
121
 
    /**
122
 
     * returns wether the event was added or if it replaced another event
123
 
     * @param event
124
 
     * @param senderWaits
125
 
     * @return true if the event was added to the queue, false if replaced an
126
 
     * already scheduled event
127
 
     */
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);
137
 
                return false;
138
 
            }
139
 
        }
140
 
        queue.addLast(event);
141
 
//        System.out.println(queue);
142
 
        mutex.notify();
143
 
      }
144
 
      return true;
145
 
    }
146
 
    
147
 
    public void dispose() {
148
 
        synchronized (mutex) {
149
 
                running = false;
150
 
            mutex.notifyAll();
151
 
            queue.clear();
152
 
        }
153
 
    }
154
 
 
155
 
    public Thread getThread() {
156
 
      return thread;
157
 
    }
158
 
}
 
 
b'\\ No newline at end of file'