2010-07-28 Francois Chouinard <fchouinard@gmail.com> Fix for Bug316349 + a bunch...
[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 public 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() {
37 super("TmfSyntheticEventProviderStub", TmfSyntheticEventStub.class);
38 }
39
40 @SuppressWarnings("unchecked")
41 @Override
42 public ITmfContext armRequest(final ITmfDataRequest<TmfSyntheticEventStub> request) {
43
44 // Get the TmfSyntheticEventStub provider
45 ITmfDataProvider<TmfEvent>[] eventProviders = (ITmfDataProvider<TmfEvent>[]) TmfProviderManager.getProviders(TmfEvent.class, TmfEventProviderStub.class);
46 ITmfDataProvider<TmfEvent> provider = eventProviders[0];
47
48 // make sure we have the right type of request
49 if (!(request instanceof ITmfEventRequest<?>)) {
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) {
58 @Override
59 public void handleData() {
60 TmfEvent[] events = getData();
61 if (events.length > 0) {
62 for (TmfEvent e : events) {
63 handleIncomingData(e);
64 }
65 } else {
66 request.done();
67 }
68 }
69 };
70 provider.sendRequest(subRequest);
71
72 // Return a dummy context
73 return new TmfContext();
74 }
75
76 // Queue 2 synthetic events per base event
77 private void handleIncomingData(TmfEvent e) {
78 queueResult(new TmfSyntheticEventStub(e));
79 queueResult(new TmfSyntheticEventStub(e));
80 }
81
82 private static final int TIMEOUT = 10000;
83
84 @Override
85 public TmfSyntheticEventStub getNext(ITmfContext context) {
86 TmfSyntheticEventStub data = null;
87 try {
88 data = fDataQueue.poll(TIMEOUT, TimeUnit.MILLISECONDS);
89 if (data == null) {
90 throw new InterruptedException();
91 }
92 }
93 catch (InterruptedException e) {
94 }
95 return data;
96 }
97
98 public void queueResult(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 }
106 catch (InterruptedException e) {
107 }
108 }
109
110 }
This page took 0.032078 seconds and 5 git commands to generate.