import java.util.List;
import java.util.Map;
+import org.eclipse.linuxtools.tmf.Tracer;
+
/**
* <b><u>TmfSignalHandler</u></b>
* <p>
// harm in letting anyone use this since it is not tied to anything but
// the signal data type.
static private Map<Object, Method[]> fListeners = new HashMap<Object, Method[]>();
+ static private Map<Object, Method[]> fVIPListeners = new HashMap<Object, Method[]>();
// If requested, add universal signal tracer
// TODO: Temporary solution: should be enabled/disabled dynamically
fListeners.put(listener, methods);
}
+ public static synchronized void registerVIP(Object listener) {
+ Method[] methods = getSignalHandlerMethods(listener);
+ if (methods.length > 0)
+ fVIPListeners.put(listener, methods);
+ }
+
public static synchronized void deregister(Object listener) {
- fListeners.remove(listener);
+ fVIPListeners.remove(listener);
+ fListeners.remove(listener);
}
/**
*
* @param signal the signal to dispatch
*/
- static int fSynchId = 0;
+ static int fSignalId = 0;
static public synchronized void dispatchSignal(TmfSignal signal) {
- fSynchId++;
- sendSignal(new TmfStartSynchSignal(fSynchId));
- signal.setReference(fSynchId);
+ fSignalId++;
+ sendSignal(new TmfStartSynchSignal(fSignalId));
+ signal.setReference(fSignalId);
sendSignal(signal);
- sendSignal(new TmfEndSynchSignal(fSynchId));
+ sendSignal(new TmfEndSynchSignal(fSignalId));
}
- static private void sendSignal(TmfSignal signal) {
-
- // Build the list of listener methods that are registered for this signal
- Class<?> signalClass = signal.getClass();
- Map<Object, List<Method>> listeners = new HashMap<Object, List<Method>>();
- listeners.clear();
- for (Map.Entry<Object, Method[]> entry : fListeners.entrySet()) {
- List<Method> matchingMethods = new ArrayList<Method>();
- for (Method method : entry.getValue()) {
- if (method.getParameterTypes()[0].isAssignableFrom(signalClass)) {
- matchingMethods.add(method);
- }
- }
- if (!matchingMethods.isEmpty()) {
- listeners.put(entry.getKey(), matchingMethods);
- }
- }
+ static private void sendSignal(TmfSignal signal) {
+ sendSignal(fVIPListeners, signal);
+ sendSignal(fListeners, signal);
+ }
- // Call the signal handlers
- for (Map.Entry<Object, List<Method>> entry : listeners.entrySet()) {
- for (Method method : entry.getValue()) {
- try {
- method.invoke(entry.getKey(), new Object[] { signal });
- } catch (IllegalArgumentException e) {
- // TODO Auto-generated catch block
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- } catch (InvocationTargetException e) {
- // TODO Auto-generated catch block
- }
- }
- }
- }
+ static private void sendSignal(Map<Object, Method[]> listeners, TmfSignal signal) {
+
+ if (Tracer.isSignalTraced()) Tracer.traceSignal(signal, "(start)"); //$NON-NLS-1$
+
+ // Build the list of listener methods that are registered for this signal
+ Class<?> signalClass = signal.getClass();
+ Map<Object, List<Method>> targets = new HashMap<Object, List<Method>>();
+ targets.clear();
+ for (Map.Entry<Object, Method[]> entry : listeners.entrySet()) {
+ List<Method> matchingMethods = new ArrayList<Method>();
+ for (Method method : entry.getValue()) {
+ if (method.getParameterTypes()[0].isAssignableFrom(signalClass)) {
+ matchingMethods.add(method);
+ }
+ }
+ if (!matchingMethods.isEmpty()) {
+ targets.put(entry.getKey(), matchingMethods);
+ }
+ }
+
+ // Call the signal handlers
+ for (Map.Entry<Object, List<Method>> entry : targets.entrySet()) {
+ for (Method method : entry.getValue()) {
+ try {
+ method.invoke(entry.getKey(), new Object[] { signal });
+ if (Tracer.isSignalTraced()) {
+ Object key = entry.getKey();
+ String hash = String.format("%1$08X", entry.getKey().hashCode()); //$NON-NLS-1$
+ String target = "[" + hash + "] " + key.getClass().getSimpleName() + ":" + method.getName(); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
+ Tracer.traceSignal(signal, target);
+ }
+ } catch (IllegalArgumentException e) {
+ } catch (IllegalAccessException e) {
+ } catch (InvocationTargetException e) {
+ }
+ }
+ }
+
+ if (Tracer.isSignalTraced()) Tracer.traceSignal(signal, "(end)"); //$NON-NLS-1$
+ }
-}
\ No newline at end of file
+}