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