tmf: Make TransientState really re-entrant
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / stubs / org / eclipse / linuxtools / tmf / tests / stubs / component / TmfSyntheticEventProviderStub.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 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 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.tests.stubs.component;
14
15 import java.util.concurrent.TimeUnit;
16
17 import org.eclipse.linuxtools.internal.tmf.core.component.TmfProviderManager;
18 import org.eclipse.linuxtools.tmf.core.component.ITmfEventProvider;
19 import org.eclipse.linuxtools.tmf.core.component.TmfEventProvider;
20 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
21 import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
22 import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest.ExecutionType;
23 import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
24 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
25 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
26 import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
27 import org.eclipse.linuxtools.tmf.tests.stubs.event.TmfSyntheticEventStub;
28
29 /**
30 * <b><u>TmfSyntheticEventProviderStub</u></b>
31 * <p>
32 * TODO: Implement me. Please.
33 */
34 @SuppressWarnings("javadoc")
35 public class TmfSyntheticEventProviderStub extends TmfEventProvider {
36
37 public static final int NB_EVENTS = 1000;
38
39 public TmfSyntheticEventProviderStub() {
40 super("TmfSyntheticEventProviderStub", TmfSyntheticEventStub.class);
41 }
42
43 @Override
44 public ITmfContext armRequest(final ITmfEventRequest request) {
45
46 // Get the TmfSyntheticEventStub provider
47 final ITmfEventProvider[] eventProviders = TmfProviderManager.getProviders(ITmfEvent.class, TmfEventProviderStub.class);
48 final ITmfEventProvider provider = eventProviders[0];
49
50 final TmfTimeRange range = request.getRange();
51 final TmfEventRequest subRequest =
52 new TmfEventRequest(ITmfEvent.class, range, 0, NB_EVENTS, ExecutionType.FOREGROUND) {
53 @Override
54 public void handleData(final ITmfEvent event) {
55 super.handleData(event);
56 if (event != null) {
57 handleIncomingData(event);
58 } else {
59 request.done();
60 }
61 }
62 };
63 provider.sendRequest(subRequest);
64
65 // Return a dummy context
66 return new TmfContext();
67 }
68
69 // Queue 2 synthetic events per base event
70 private void handleIncomingData(final ITmfEvent e) {
71 queueResult(new TmfSyntheticEventStub(e));
72 queueResult(new TmfSyntheticEventStub(e));
73 }
74
75 private static final int TIMEOUT = 10000;
76
77 @Override
78 public TmfSyntheticEventStub getNext(final ITmfContext context) {
79 TmfSyntheticEventStub data = null;
80 try {
81 data = (TmfSyntheticEventStub) fDataQueue.poll(TIMEOUT, TimeUnit.MILLISECONDS);
82 if (data == null) {
83 throw new InterruptedException();
84 }
85 }
86 catch (final InterruptedException e) {
87 }
88 return data;
89 }
90
91 public void queueResult(final TmfSyntheticEventStub data) {
92 boolean ok = false;
93 try {
94 ok = fDataQueue.offer(data, TIMEOUT, TimeUnit.MILLISECONDS);
95 if (!ok) {
96 throw new InterruptedException();
97 }
98 }
99 catch (final InterruptedException e) {
100 }
101 }
102
103 }
This page took 0.033034 seconds and 5 git commands to generate.