Rename xxx.lttng to xxx.lttng.core
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.tests / stubs / org / eclipse / linuxtools / tmf / component / TmfSyntheticEventProviderStub.java
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
15 import java.util.concurrent.TimeUnit;
16
17 import org.eclipse.linuxtools.tmf.event.TmfEvent;
18 import org.eclipse.linuxtools.tmf.event.TmfSyntheticEventStub;
19 import org.eclipse.linuxtools.tmf.event.TmfTimeRange;
20 import org.eclipse.linuxtools.tmf.request.ITmfDataRequest;
21 import org.eclipse.linuxtools.tmf.request.ITmfEventRequest;
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 @SuppressWarnings("nls")
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() {
38 super("TmfSyntheticEventProviderStub", TmfSyntheticEventStub.class);
39 }
40
41 @SuppressWarnings("unchecked")
42 @Override
43 public ITmfContext armRequest(final ITmfDataRequest<TmfSyntheticEventStub> request) {
44
45 // Get the TmfSyntheticEventStub provider
46 ITmfDataProvider<TmfEvent>[] eventProviders = (ITmfDataProvider<TmfEvent>[]) TmfProviderManager.getProviders(TmfEvent.class, TmfEventProviderStub.class);
47 ITmfDataProvider<TmfEvent> provider = eventProviders[0];
48
49 // make sure we have the right type of request
50 if (!(request instanceof ITmfEventRequest<?>)) {
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) {
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 }
67 };
68 provider.sendRequest(subRequest);
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) {
76 queueResult(new TmfSyntheticEventStub(e));
77 queueResult(new TmfSyntheticEventStub(e));
78 }
79
80 private static final int TIMEOUT = 10000;
81
82 @Override
83 public TmfSyntheticEventStub getNext(ITmfContext context) {
84 TmfSyntheticEventStub data = null;
85 try {
86 data = fDataQueue.poll(TIMEOUT, TimeUnit.MILLISECONDS);
87 if (data == null) {
88 throw new InterruptedException();
89 }
90 }
91 catch (InterruptedException e) {
92 }
93 return data;
94 }
95
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 }
106 }
107
108 }
This page took 0.037863 seconds and 5 git commands to generate.