jdk/src/share/classes/com/sun/media/sound/EventDispatcher.java
author amenkov
Wed, 06 Apr 2011 15:12:33 +0400
changeset 9215 cab45ca6ab44
parent 5506 202f599c92aa
child 18215 b2afd66ce6db
permissions -rw-r--r--
6992523: FindBugs scan - Malicious code vulnerability Warnings in com.sun.media.sound.* Reviewed-by: alexp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.media.sound;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.EventObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.sound.sampled.Clip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.sound.sampled.Line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.sound.sampled.LineEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.sound.sampled.LineListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.sound.midi.MetaMessage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.sound.midi.ShortMessage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.sound.midi.MetaEventListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.sound.midi.ControllerEventListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * EventDispatcher.  Used by various classes in the Java Sound implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * to send events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author David Rivas
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author Kara Kytle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author Florian Bomers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
class EventDispatcher implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * time of inactivity until the auto closing clips
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * are closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static final int AUTO_CLOSE_TIME = 5000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * List of events
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private ArrayList eventQueue = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Thread object for this EventDispatcher instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private Thread thread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * support for auto-closing Clips
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private ArrayList<ClipInfo> autoClosingClips = new ArrayList<ClipInfo>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * support for monitoring data lines
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private ArrayList<LineMonitor> lineMonitors = new ArrayList<LineMonitor>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Approximate interval between calls to LineMonitor.checkLine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    static final int LINE_MONITOR_TIME = 400;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * This start() method starts an event thread if one is not already active.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    synchronized void start() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if(thread == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            thread = JSSecurityManager.createThread(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                                                    "Java Sound Event Dispatcher",   // name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                                    true,  // daemon
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                                    -1,    // priority
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                                                    true); // doStart
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Invoked when there is at least one event in the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Implement this as a callback to process one event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    protected void processEvent(EventInfo eventInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        int count = eventInfo.getListenerCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        // process an LineEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (eventInfo.getEvent() instanceof LineEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            LineEvent event = (LineEvent) eventInfo.getEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            if (Printer.debug) Printer.debug("Sending "+event+" to "+count+" listeners");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    ((LineListener) eventInfo.getListener(i)).update(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    if (Printer.err) t.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // process a MetaMessage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (eventInfo.getEvent() instanceof MetaMessage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            MetaMessage event = (MetaMessage)eventInfo.getEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    ((MetaEventListener) eventInfo.getListener(i)).meta(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    if (Printer.err) t.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        // process a Controller or Mode Event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (eventInfo.getEvent() instanceof ShortMessage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            ShortMessage event = (ShortMessage)eventInfo.getEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            int status = event.getStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            // Controller and Mode events have status byte 0xBc, where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            // c is the channel they are sent on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            if ((status & 0xF0) == 0xB0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        ((ControllerEventListener) eventInfo.getListener(i)).controlChange(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        if (Printer.err) t.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        Printer.err("Unknown event type: " + eventInfo.getEvent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Wait until there is something in the event queue to process.  Then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * dispatch the event to the listeners.The entire method does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * need to be synchronized since this includes taking the event out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * from the queue and processing the event. We only need to provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * exclusive access over the code where an event is removed from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    protected void dispatchEvents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        EventInfo eventInfo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            // Wait till there is an event in the event queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                if (eventQueue.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    if (autoClosingClips.size() > 0 || lineMonitors.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        int waitTime = AUTO_CLOSE_TIME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        if (lineMonitors.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                            waitTime = LINE_MONITOR_TIME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                        wait(waitTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                        wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            if (eventQueue.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                // Remove the event from the queue and dispatch it to the listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                eventInfo = (EventInfo) eventQueue.remove(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        } // end of synchronized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (eventInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            processEvent(eventInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            if (autoClosingClips.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                closeAutoClosingClips();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if (lineMonitors.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                monitorLines();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Queue the given event in the event queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    private synchronized void postEvent(EventInfo eventInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        eventQueue.add(eventInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * A loop to dispatch events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                dispatchEvents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                if (Printer.err) t.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Send audio and MIDI events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    void sendAudioEvents(Object event, List listeners) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if ((listeners == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            || (listeners.size() == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            // nothing to do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        EventInfo eventInfo = new EventInfo(event, listeners);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        postEvent(eventInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * go through the list of registered auto-closing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Clip instances and close them, if appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * This method is called in regular intervals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    private void closeAutoClosingClips() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        synchronized(autoClosingClips) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            if (Printer.debug)Printer.debug("> EventDispatcher.closeAutoClosingClips ("+autoClosingClips.size()+" clips)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            long currTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            for (int i = autoClosingClips.size()-1; i >= 0 ; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                ClipInfo info = autoClosingClips.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                if (info.isExpired(currTime)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    AutoClosingClip clip = info.getClip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    // sanity check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    if (!clip.isOpen() || !clip.isAutoClosing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        if (Printer.debug)Printer.debug("EventDispatcher: removing clip "+clip+"  isOpen:"+clip.isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                        autoClosingClips.remove(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    else if (!clip.isRunning() && !clip.isActive() && clip.isAutoClosing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                        if (Printer.debug)Printer.debug("EventDispatcher: closing clip "+clip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                        clip.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                        if (Printer.debug)Printer.debug("Doing nothing with clip "+clip+":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                        if (Printer.debug)Printer.debug("  open="+clip.isOpen()+", autoclosing="+clip.isAutoClosing());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                        if (Printer.debug)Printer.debug("  isRunning="+clip.isRunning()+", isActive="+clip.isActive());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    if (Printer.debug)Printer.debug("EventDispatcher: clip "+info.getClip()+" not yet expired");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if (Printer.debug)Printer.debug("< EventDispatcher.closeAutoClosingClips ("+autoClosingClips.size()+" clips)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    private int getAutoClosingClipIndex(AutoClosingClip clip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        synchronized(autoClosingClips) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            for (int i = autoClosingClips.size()-1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                if (clip.equals(autoClosingClips.get(i).getClip())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * called from auto-closing clips when one of their open() method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    void autoClosingClipOpened(AutoClosingClip clip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if (Printer.debug)Printer.debug("> EventDispatcher.autoClosingClipOpened ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        synchronized(autoClosingClips) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            index = getAutoClosingClipIndex(clip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            if (index == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                if (Printer.debug)Printer.debug("EventDispatcher: adding auto-closing clip "+clip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                autoClosingClips.add(new ClipInfo(clip));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        if (index == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                // this is only for the case that the first clip is set to autoclosing,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                // and it is already open, and nothing is done with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                // EventDispatcher.process() method would block in wait() and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                // never close this first clip, keeping the device open.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (Printer.debug)Printer.debug("< EventDispatcher.autoClosingClipOpened finished("+autoClosingClips.size()+" clips)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * called from auto-closing clips when their closed() method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    void autoClosingClipClosed(AutoClosingClip clip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        // nothing to do -- is removed from arraylist above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    // ////////////////////////// Line Monitoring Support /////////////////// //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * go through the list of registered line monitors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * and call their checkLine method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * This method is called in regular intervals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    private void monitorLines() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        synchronized(lineMonitors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            if (Printer.debug)Printer.debug("> EventDispatcher.monitorLines ("+lineMonitors.size()+" monitors)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            for (int i = 0; i < lineMonitors.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                lineMonitors.get(i).checkLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if (Printer.debug)Printer.debug("< EventDispatcher.monitorLines("+lineMonitors.size()+" monitors)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * Add this LineMonitor instance to the list of monitors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    void addLineMonitor(LineMonitor lm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        if (Printer.trace)Printer.trace("> EventDispatcher.addLineMonitor("+lm+")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        synchronized(lineMonitors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            if (lineMonitors.indexOf(lm) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                if (Printer.trace)Printer.trace("< EventDispatcher.addLineMonitor finished -- this monitor already exists!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            if (Printer.debug)Printer.debug("EventDispatcher: adding line monitor "+lm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            lineMonitors.add(lm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            // need to interrupt the infinite wait()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        if (Printer.debug)Printer.debug("< EventDispatcher.addLineMonitor finished -- now ("+lineMonitors.size()+" monitors)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * Remove this LineMonitor instance from the list of monitors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    void removeLineMonitor(LineMonitor lm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if (Printer.trace)Printer.trace("> EventDispatcher.removeLineMonitor("+lm+")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        synchronized(lineMonitors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            if (lineMonitors.indexOf(lm) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                if (Printer.trace)Printer.trace("< EventDispatcher.removeLineMonitor finished -- this monitor does not exist!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            if (Printer.debug)Printer.debug("EventDispatcher: removing line monitor "+lm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            lineMonitors.remove(lm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (Printer.debug)Printer.debug("< EventDispatcher.removeLineMonitor finished -- now ("+lineMonitors.size()+" monitors)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    // /////////////////////////////////// INNER CLASSES ////////////////////////////////////////// //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * Container for an event and a set of listeners to deliver it to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    private class EventInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        private Object event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        private Object[] listeners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
         * Create a new instance of this event Info class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
         * @param event the event to be dispatched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
         * @param listeners listener list; will be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        EventInfo(Object event, List listeners) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            this.event = event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            this.listeners = listeners.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        Object getEvent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            return event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        int getListenerCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            return listeners.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        Object getListener(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            return listeners[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    } // class EventInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * Container for a clip with its expiration time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    private class ClipInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        private AutoClosingClip clip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        private long expiration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
         * Create a new instance of this clip Info class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        ClipInfo(AutoClosingClip clip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            this.clip = clip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            this.expiration = System.currentTimeMillis() + AUTO_CLOSE_TIME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        AutoClosingClip getClip() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            return clip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        boolean isExpired(long currTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            return currTime > expiration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    } // class ClipInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * Interface that a class that wants to get regular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * line monitor events implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    interface LineMonitor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
         * Called by event dispatcher in regular intervals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        public void checkLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
} // class EventDispatcher