| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2009 Ericsson |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made available under the terms of the Eclipse Public License v1.0 which |
| 6 | * accompanies this distribution, and is available at |
| 7 | * http://www.eclipse.org/legal/epl-v10.html |
| 8 | * |
| 9 | * Contributors: |
| 10 | * Alvaro Sanchez-Leon (alvsan09@gmail.com) - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | package org.eclipse.linuxtools.lttng.state; |
| 13 | |
| 14 | import java.util.Iterator; |
| 15 | import java.util.Set; |
| 16 | import java.util.Vector; |
| 17 | |
| 18 | import org.eclipse.linuxtools.lttng.TraceDebug; |
| 19 | import org.eclipse.linuxtools.lttng.event.LttngEvent; |
| 20 | import org.eclipse.linuxtools.lttng.state.evProcessor.AbsEventProcessorFactory; |
| 21 | import org.eclipse.linuxtools.lttng.state.evProcessor.EventProcessorProxy; |
| 22 | import org.eclipse.linuxtools.lttng.state.evProcessor.IEventProcessing; |
| 23 | import org.eclipse.linuxtools.lttng.state.model.ILttngStateInputRef; |
| 24 | import org.eclipse.linuxtools.lttng.state.model.LttngTraceState; |
| 25 | import org.eclipse.linuxtools.tmf.event.TmfEvent; |
| 26 | import org.eclipse.linuxtools.tmf.trace.TmfTrace; |
| 27 | |
| 28 | /** |
| 29 | * @author Alvaro |
| 30 | * |
| 31 | */ |
| 32 | public class StateStacksHandler { |
| 33 | // ======================================================================== |
| 34 | // Table data |
| 35 | // ======================================================================= |
| 36 | protected LttngTraceState traceStateModel = null; |
| 37 | protected Set<String> eventsNotHandled = null; |
| 38 | protected Vector<IEventProcessing> listeners = new Vector<IEventProcessing>(); |
| 39 | |
| 40 | // ======================================================================== |
| 41 | // Constructors |
| 42 | // ======================================================================== |
| 43 | public StateStacksHandler(LttngTraceState model) { |
| 44 | // It's assumed to have one instance of this class per Trace |
| 45 | this.traceStateModel = model; |
| 46 | } |
| 47 | |
| 48 | // ======================================================================== |
| 49 | // Methods |
| 50 | // ======================================================================= |
| 51 | /** |
| 52 | * Initialised by manager, any time a JniTrace selection is updated |
| 53 | * |
| 54 | * @param trace |
| 55 | * @param log |
| 56 | * |
| 57 | */ |
| 58 | void init(TmfTrace log) throws LttngStateException { |
| 59 | if (log == null) { |
| 60 | throw new LttngStateException("No TmfTrace object available!"); |
| 61 | } |
| 62 | |
| 63 | // this.trace = trace; |
| 64 | ILttngStateInputRef ref = new LttngStateInputRef(log); |
| 65 | this.traceStateModel.init(ref); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | protected void processEvent(TmfEvent tmfEvent) /* throws LttngStateException */{ |
| 70 | if (tmfEvent == null) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | if (!(tmfEvent instanceof LttngEvent)) { |
| 75 | TraceDebug |
| 76 | .debug("The event received is not an instance of LttngEvent and can not be processed"); |
| 77 | } |
| 78 | |
| 79 | LttngEvent trcEvent = (LttngEvent) tmfEvent; |
| 80 | // LttngEventField[] fields = ((LttngEventContent)trcEvent.getContent()).getFields(); |
| 81 | |
| 82 | if (trcEvent != null) { |
| 83 | String inEventName = trcEvent.getMarkerName(); |
| 84 | // String inChannel = trcEvent.getChannelName(); |
| 85 | // TraceDebug.debug("Event: " + inEventName); |
| 86 | |
| 87 | // Check if the received event is a transition state event |
| 88 | // TODO: Remove temporarily to allow other events to go to the |
| 89 | // statistics view. |
| 90 | // Needs restructuring. |
| 91 | // Events eventStruct = StateStrings.getInstance() |
| 92 | // .getStateTransEventMap().get(inEventName); |
| 93 | // if (eventStruct != null) { |
| 94 | // String expectedChannel = eventStruct.getParent().getInName(); |
| 95 | // check that received channel is the expected channel in the |
| 96 | // structure |
| 97 | // if (inChannel.equals(expectedChannel)) { |
| 98 | // Notify the before Handlers |
| 99 | Set<AbsEventProcessorFactory> handlerRegister = EventProcessorProxy |
| 100 | .getInstance().getProcessingFactories(); |
| 101 | |
| 102 | // Notify the state BEFORE update handlers |
| 103 | for (Iterator<AbsEventProcessorFactory> iterator = handlerRegister |
| 104 | .iterator(); iterator.hasNext();) { |
| 105 | AbsEventProcessorFactory handlerRegistry = (AbsEventProcessorFactory) iterator |
| 106 | .next(); |
| 107 | IEventProcessing handler = handlerRegistry |
| 108 | .getBeforeProcessor(inEventName); |
| 109 | if (handler != null) { |
| 110 | // process State Update |
| 111 | handler.process(trcEvent, traceStateModel); |
| 112 | } |
| 113 | |
| 114 | } |
| 115 | |
| 116 | // Notify the STATE UPDATE handlers |
| 117 | // Only one state update expected |
| 118 | for (Iterator<AbsEventProcessorFactory> iterator = handlerRegister |
| 119 | .iterator(); iterator.hasNext();) { |
| 120 | AbsEventProcessorFactory handlerRegistry = (AbsEventProcessorFactory) iterator |
| 121 | .next(); |
| 122 | IEventProcessing handler = handlerRegistry |
| 123 | .getStateUpdaterProcessor(inEventName); |
| 124 | if (handler != null) { |
| 125 | // process State Update |
| 126 | handler.process(trcEvent, traceStateModel); |
| 127 | } |
| 128 | |
| 129 | } |
| 130 | |
| 131 | // Notify the AFTER update handlers |
| 132 | for (Iterator<AbsEventProcessorFactory> iterator = handlerRegister |
| 133 | .iterator(); iterator.hasNext();) { |
| 134 | AbsEventProcessorFactory handlerRegistry = (AbsEventProcessorFactory) iterator |
| 135 | .next(); |
| 136 | IEventProcessing handler = handlerRegistry |
| 137 | .getAfterProcessor(inEventName); |
| 138 | if (handler != null) { |
| 139 | // process State Update |
| 140 | handler.process(trcEvent, traceStateModel); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // } else { |
| 145 | // StringBuilder sb = new StringBuilder( |
| 146 | // "Unexpected channel received for: " + inEventName |
| 147 | // + ", channel rec: " + inChannel |
| 148 | // + " chanel expected: " + expectedChannel); |
| 149 | // TraceDebug.debug(sb.toString()); |
| 150 | // } |
| 151 | // } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Used for troubleshooting when debug mode is on |
| 157 | * |
| 158 | * @return |
| 159 | */ |
| 160 | Set<String> getEventsNotHandled() { |
| 161 | return eventsNotHandled; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Needed for checkpoint |
| 166 | * |
| 167 | * @param LttngTraceState |
| 168 | */ |
| 169 | public LttngTraceState getTraceStateModel() { |
| 170 | return traceStateModel; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Needed for checkpoint |
| 175 | * |
| 176 | * @param LttngTraceState |
| 177 | */ |
| 178 | public void setTraceStateModel(LttngTraceState newTraceState) { |
| 179 | traceStateModel = newTraceState; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Needed for verification purposes |
| 184 | * |
| 185 | * @param listener |
| 186 | */ |
| 187 | void registerListener(IEventProcessing listener) { |
| 188 | this.listeners.add(listener); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Needed for verification purposes |
| 193 | * |
| 194 | * @param listener |
| 195 | */ |
| 196 | void deregisterListener(IEventProcessing listener) { |
| 197 | this.listeners.remove(listener); |
| 198 | } |
| 199 | } |