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 | */ | |
3b38ea61 | 31 | @SuppressWarnings("nls") |
d18dd09b ASL |
32 | public class TmfSyntheticEventProviderStub extends TmfEventProvider<TmfSyntheticEventStub> { |
33 | ||
34 | public static final int BLOCK_SIZE = 100; | |
35 | public static final int NB_EVENTS = 1000; | |
36 | ||
37 | public TmfSyntheticEventProviderStub() { | |
ce785d7d | 38 | super("TmfSyntheticEventProviderStub", TmfSyntheticEventStub.class); |
d18dd09b ASL |
39 | } |
40 | ||
41 | @SuppressWarnings("unchecked") | |
42 | @Override | |
2fb2eb37 | 43 | public ITmfContext armRequest(final ITmfDataRequest<TmfSyntheticEventStub> request) { |
d18dd09b ASL |
44 | |
45 | // Get the TmfSyntheticEventStub provider | |
951d134a FC |
46 | ITmfDataProvider<TmfEvent>[] eventProviders = (ITmfDataProvider<TmfEvent>[]) TmfProviderManager.getProviders(TmfEvent.class, TmfEventProviderStub.class); |
47 | ITmfDataProvider<TmfEvent> provider = eventProviders[0]; | |
d18dd09b ASL |
48 | |
49 | // make sure we have the right type of request | |
2fb2eb37 | 50 | if (!(request instanceof ITmfEventRequest<?>)) { |
d18dd09b ASL |
51 | request.cancel(); |
52 | return null; | |
53 | } | |
54 | ||
55 | TmfEventRequest<TmfSyntheticEventStub> eventRequest = (TmfEventRequest<TmfSyntheticEventStub>) request; | |
56 | TmfTimeRange range = eventRequest.getRange(); | |
57 | final TmfEventRequest<TmfEvent> subRequest = | |
58 | new TmfEventRequest<TmfEvent>(TmfEvent.class, range, NB_EVENTS, BLOCK_SIZE) { | |
f9673903 FC |
59 | @Override |
60 | public void handleData(TmfEvent event) { | |
61 | super.handleData(event); | |
62 | if (event != null) | |
63 | handleIncomingData(event); | |
64 | else | |
65 | request.done(); | |
66 | } | |
d18dd09b | 67 | }; |
550d787e | 68 | provider.sendRequest(subRequest); |
d18dd09b ASL |
69 | |
70 | // Return a dummy context | |
71 | return new TmfContext(); | |
72 | } | |
73 | ||
74 | // Queue 2 synthetic events per base event | |
75 | private void handleIncomingData(TmfEvent e) { | |
3d62f8b7 FC |
76 | queueResult(new TmfSyntheticEventStub(e)); |
77 | queueResult(new TmfSyntheticEventStub(e)); | |
78 | } | |
79 | ||
80 | private static final int TIMEOUT = 10000; | |
81 | ||
9ccc6d01 | 82 | @Override |
3d62f8b7 FC |
83 | public TmfSyntheticEventStub getNext(ITmfContext context) { |
84 | TmfSyntheticEventStub data = null; | |
550d787e | 85 | try { |
3d62f8b7 FC |
86 | data = fDataQueue.poll(TIMEOUT, TimeUnit.MILLISECONDS); |
87 | if (data == null) { | |
88 | throw new InterruptedException(); | |
89 | } | |
90 | } | |
91 | catch (InterruptedException e) { | |
550d787e | 92 | } |
3d62f8b7 | 93 | return data; |
550d787e FC |
94 | } |
95 | ||
3d62f8b7 FC |
96 | public void queueResult(TmfSyntheticEventStub data) { |
97 | boolean ok = false; | |
98 | try { | |
99 | ok = fDataQueue.offer(data, TIMEOUT, TimeUnit.MILLISECONDS); | |
100 | if (!ok) { | |
101 | throw new InterruptedException(); | |
102 | } | |
103 | } | |
104 | catch (InterruptedException e) { | |
105 | } | |
d18dd09b | 106 | } |
3d62f8b7 | 107 | |
d18dd09b | 108 | } |