Commit | Line | Data |
---|---|---|
d18dd09b ASL |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009, 2010 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.component; | |
14 | ||
3d62f8b7 FC |
15 | import java.util.concurrent.TimeUnit; |
16 | ||
d18dd09b ASL |
17 | import org.eclipse.linuxtools.tmf.event.TmfEvent; |
18 | import org.eclipse.linuxtools.tmf.event.TmfSyntheticEventStub; | |
19 | import org.eclipse.linuxtools.tmf.event.TmfTimeRange; | |
2fb2eb37 FC |
20 | import org.eclipse.linuxtools.tmf.request.ITmfDataRequest; |
21 | import org.eclipse.linuxtools.tmf.request.ITmfEventRequest; | |
d18dd09b ASL |
22 | import org.eclipse.linuxtools.tmf.request.TmfEventRequest; |
23 | import org.eclipse.linuxtools.tmf.trace.ITmfContext; | |
24 | import org.eclipse.linuxtools.tmf.trace.TmfContext; | |
25 | ||
26 | /** | |
27 | * <b><u>TmfSyntheticEventProviderStub</u></b> | |
28 | * <p> | |
29 | * TODO: Implement me. Please. | |
30 | */ | |
31 | public class TmfSyntheticEventProviderStub extends TmfEventProvider<TmfSyntheticEventStub> { | |
32 | ||
33 | public static final int BLOCK_SIZE = 100; | |
34 | public static final int NB_EVENTS = 1000; | |
35 | ||
36 | public TmfSyntheticEventProviderStub() { | |
ce785d7d | 37 | super("TmfSyntheticEventProviderStub", TmfSyntheticEventStub.class); |
d18dd09b ASL |
38 | } |
39 | ||
40 | @SuppressWarnings("unchecked") | |
41 | @Override | |
2fb2eb37 | 42 | public ITmfContext armRequest(final ITmfDataRequest<TmfSyntheticEventStub> request) { |
d18dd09b ASL |
43 | |
44 | // Get the TmfSyntheticEventStub provider | |
951d134a FC |
45 | ITmfDataProvider<TmfEvent>[] eventProviders = (ITmfDataProvider<TmfEvent>[]) TmfProviderManager.getProviders(TmfEvent.class, TmfEventProviderStub.class); |
46 | ITmfDataProvider<TmfEvent> provider = eventProviders[0]; | |
d18dd09b ASL |
47 | |
48 | // make sure we have the right type of request | |
2fb2eb37 | 49 | if (!(request instanceof ITmfEventRequest<?>)) { |
d18dd09b ASL |
50 | request.cancel(); |
51 | return null; | |
52 | } | |
53 | ||
54 | TmfEventRequest<TmfSyntheticEventStub> eventRequest = (TmfEventRequest<TmfSyntheticEventStub>) request; | |
55 | TmfTimeRange range = eventRequest.getRange(); | |
56 | final TmfEventRequest<TmfEvent> subRequest = | |
57 | new TmfEventRequest<TmfEvent>(TmfEvent.class, range, NB_EVENTS, BLOCK_SIZE) { | |
58 | @Override | |
59 | public void handleData() { | |
60 | TmfEvent[] events = getData(); | |
61 | if (events.length > 0) { | |
62 | for (TmfEvent e : events) { | |
63 | handleIncomingData(e); | |
64 | } | |
65 | } else { | |
66 | request.done(); | |
67 | } | |
68 | } | |
69 | }; | |
550d787e | 70 | provider.sendRequest(subRequest); |
d18dd09b ASL |
71 | |
72 | // Return a dummy context | |
73 | return new TmfContext(); | |
74 | } | |
75 | ||
76 | // Queue 2 synthetic events per base event | |
77 | private void handleIncomingData(TmfEvent e) { | |
3d62f8b7 FC |
78 | queueResult(new TmfSyntheticEventStub(e)); |
79 | queueResult(new TmfSyntheticEventStub(e)); | |
80 | } | |
81 | ||
82 | private static final int TIMEOUT = 10000; | |
83 | ||
84 | public TmfSyntheticEventStub getNext(ITmfContext context) { | |
85 | TmfSyntheticEventStub data = null; | |
550d787e | 86 | try { |
3d62f8b7 FC |
87 | data = fDataQueue.poll(TIMEOUT, TimeUnit.MILLISECONDS); |
88 | if (data == null) { | |
89 | throw new InterruptedException(); | |
90 | } | |
91 | } | |
92 | catch (InterruptedException e) { | |
550d787e | 93 | } |
3d62f8b7 | 94 | return data; |
550d787e FC |
95 | } |
96 | ||
3d62f8b7 FC |
97 | public void queueResult(TmfSyntheticEventStub data) { |
98 | boolean ok = false; | |
99 | try { | |
100 | ok = fDataQueue.offer(data, TIMEOUT, TimeUnit.MILLISECONDS); | |
101 | if (!ok) { | |
102 | throw new InterruptedException(); | |
103 | } | |
104 | } | |
105 | catch (InterruptedException e) { | |
106 | } | |
d18dd09b | 107 | } |
3d62f8b7 | 108 | |
d18dd09b | 109 | } |