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