Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jitsi.impl.neomedia;
import javax.media.*;
import org.jitsi.util.*;
/**
* A utility class that provides utility functions when working with processors.
*
* @author Emil Ivov
* @author Ken Larson
* @author Lubomir Marinov
*/
public class ProcessorUtility
implements ControllerListener
{
/**
* The <tt>Logger</tt> used by the <tt>ProcessorUtility</tt> class and its
* instances for logging output.
*/
private static final Logger logger
= Logger.getLogger(ProcessorUtility.class);
/**
* The <tt>Object</tt> used for syncing when waiting for a processor to
* enter a specific state.
*/
private final Object stateLock = new Object();
/**
* The indicator which determines whether the waiting of this instance on a
* processor for it to enter a specific state has failed.
*/
private boolean failed = false;
/**
* Initializes a new <tt>ProcessorUtility</tt> instance.
*/
public ProcessorUtility()
{
}
/**
* Gets the <tt>Object</tt> to use for syncing when waiting for a processor
* to enter a specific state.
*
* @return the <tt>Object</tt> to use for syncing when waiting for a
* processor to enter a specific state
*/
private Object getStateLock()
{
return stateLock;
}
/**
* Specifies whether the wait operation has failed or completed with
* success.
*
* @param failed <tt>true</tt> if waiting has failed; <tt>false</tt>,
* otherwise
*/
private void setFailed(boolean failed)
{
this.failed = failed;
}
/**
* This method is called when an event is generated by a
* <code>Controller</code> that this listener is registered with. We use
* the event to notify all waiting on our lock and record success or
* failure.
*
* @param ce The event generated.
*/
public void controllerUpdate(ControllerEvent ce)
{
// If there was an error during configure or
// realize, the processor will be closed
if (ce instanceof ControllerClosedEvent)
{
if (ce instanceof ControllerErrorEvent)
logger.warn("ControllerErrorEvent: " + ce);
else
if (logger.isDebugEnabled())
logger.debug("ControllerClosedEvent: " + ce);
setFailed(true);
// All controller events, send a notification
// to the waiting thread in waitForState method.
}
Object stateLock = getStateLock();
synchronized (stateLock)
{
stateLock.notifyAll();
}
}
/**
* Waits until <tt>processor</tt> enters state and returns a boolean
* indicating success or failure of the operation.
*
* @param processor Processor
* @param state one of the Processor.XXXed state vars
* @return <tt>true</tt> if the state has been reached; <tt>false</tt>,
* otherwise
*/
public synchronized boolean waitForState(Processor processor, int state)
{
processor.addControllerListener(this);
setFailed(false);
// Call the required method on the processor
if (state == Processor.Configured)
processor.configure();
else if (state == Processor.Realized)
processor.realize();
// Wait until we get an event that confirms the
// success of the method, or a failure event.
// See StateListener inner class
while ((processor.getState() < state) && !failed)
{
Object stateLock = getStateLock();
synchronized (stateLock)
{
try
{
stateLock.wait();
}
catch (InterruptedException ie)
{
logger
.warn(
"Failed while waiting on Processor "
+ processor
+ " for state "
+ state,
ie);
processor.removeControllerListener(this);
return false;
}
}
}
processor.removeControllerListener(this);
return !failed;
}
}