2010-09-17 Francois Chouinard <fchouinard@gmail.com> Contribution for Bug325662
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.tests / stubs / org / eclipse / linuxtools / tmf / component / TmfSyntheticEventProviderStub.java
CommitLineData
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
13package org.eclipse.linuxtools.tmf.component;
14
3d62f8b7
FC
15import java.util.concurrent.TimeUnit;
16
d18dd09b
ASL
17import org.eclipse.linuxtools.tmf.event.TmfEvent;
18import org.eclipse.linuxtools.tmf.event.TmfSyntheticEventStub;
19import org.eclipse.linuxtools.tmf.event.TmfTimeRange;
2fb2eb37
FC
20import org.eclipse.linuxtools.tmf.request.ITmfDataRequest;
21import org.eclipse.linuxtools.tmf.request.ITmfEventRequest;
d18dd09b
ASL
22import org.eclipse.linuxtools.tmf.request.TmfEventRequest;
23import org.eclipse.linuxtools.tmf.trace.ITmfContext;
24import org.eclipse.linuxtools.tmf.trace.TmfContext;
25
26/**
27 * <b><u>TmfSyntheticEventProviderStub</u></b>
28 * <p>
29 * TODO: Implement me. Please.
30 */
31public 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) {
f9673903
FC
58 @Override
59 public void handleData(TmfEvent event) {
60 super.handleData(event);
61 if (event != null)
62 handleIncomingData(event);
63 else
64 request.done();
65 }
d18dd09b 66 };
550d787e 67 provider.sendRequest(subRequest);
d18dd09b
ASL
68
69 // Return a dummy context
70 return new TmfContext();
71 }
72
73 // Queue 2 synthetic events per base event
74 private void handleIncomingData(TmfEvent e) {
3d62f8b7
FC
75 queueResult(new TmfSyntheticEventStub(e));
76 queueResult(new TmfSyntheticEventStub(e));
77 }
78
79 private static final int TIMEOUT = 10000;
80
9ccc6d01 81 @Override
3d62f8b7
FC
82 public TmfSyntheticEventStub getNext(ITmfContext context) {
83 TmfSyntheticEventStub data = null;
550d787e 84 try {
3d62f8b7
FC
85 data = fDataQueue.poll(TIMEOUT, TimeUnit.MILLISECONDS);
86 if (data == null) {
87 throw new InterruptedException();
88 }
89 }
90 catch (InterruptedException e) {
550d787e 91 }
3d62f8b7 92 return data;
550d787e
FC
93 }
94
3d62f8b7
FC
95 public void queueResult(TmfSyntheticEventStub data) {
96 boolean ok = false;
97 try {
98 ok = fDataQueue.offer(data, TIMEOUT, TimeUnit.MILLISECONDS);
99 if (!ok) {
100 throw new InterruptedException();
101 }
102 }
103 catch (InterruptedException e) {
104 }
d18dd09b 105 }
3d62f8b7 106
d18dd09b 107}
This page took 0.033223 seconds and 5 git commands to generate.