| 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.tests.stubs.component; |
| 14 | |
| 15 | import java.util.concurrent.TimeUnit; |
| 16 | |
| 17 | import org.eclipse.linuxtools.tmf.core.component.ITmfDataProvider; |
| 18 | import org.eclipse.linuxtools.tmf.core.component.TmfEventProvider; |
| 19 | import org.eclipse.linuxtools.tmf.core.component.TmfProviderManager; |
| 20 | import org.eclipse.linuxtools.tmf.core.event.TmfEvent; |
| 21 | import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange; |
| 22 | import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest; |
| 23 | import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest; |
| 24 | import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest; |
| 25 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
| 26 | import org.eclipse.linuxtools.tmf.core.trace.TmfContext; |
| 27 | import org.eclipse.linuxtools.tmf.tests.stubs.event.TmfSyntheticEventStub; |
| 28 | |
| 29 | /** |
| 30 | * <b><u>TmfSyntheticEventProviderStub</u></b> |
| 31 | * <p> |
| 32 | * TODO: Implement me. Please. |
| 33 | */ |
| 34 | @SuppressWarnings("nls") |
| 35 | public 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() { |
| 41 | super("TmfSyntheticEventProviderStub", TmfSyntheticEventStub.class); |
| 42 | } |
| 43 | |
| 44 | @SuppressWarnings("unchecked") |
| 45 | @Override |
| 46 | public ITmfContext armRequest(final ITmfDataRequest<TmfSyntheticEventStub> request) { |
| 47 | |
| 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]; |
| 51 | |
| 52 | // make sure we have the right type of request |
| 53 | if (!(request instanceof ITmfEventRequest<?>)) { |
| 54 | request.cancel(); |
| 55 | return null; |
| 56 | } |
| 57 | |
| 58 | final TmfEventRequest<TmfSyntheticEventStub> eventRequest = (TmfEventRequest<TmfSyntheticEventStub>) request; |
| 59 | final TmfTimeRange range = eventRequest.getRange(); |
| 60 | final TmfEventRequest<TmfEvent> subRequest = |
| 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 | }; |
| 71 | provider.sendRequest(subRequest); |
| 72 | |
| 73 | // Return a dummy context |
| 74 | return new TmfContext(); |
| 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 | } |
| 108 | |
| 109 | } |