| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2009, 2013 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.BlockingQueue; |
| 16 | import java.util.concurrent.LinkedBlockingQueue; |
| 17 | import java.util.concurrent.TimeUnit; |
| 18 | |
| 19 | import org.eclipse.linuxtools.internal.tmf.core.component.TmfProviderManager; |
| 20 | import org.eclipse.linuxtools.tmf.core.component.ITmfEventProvider; |
| 21 | import org.eclipse.linuxtools.tmf.core.component.TmfEventProvider; |
| 22 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
| 23 | import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest; |
| 24 | import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest.ExecutionType; |
| 25 | import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest; |
| 26 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange; |
| 27 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
| 28 | import org.eclipse.linuxtools.tmf.core.trace.TmfContext; |
| 29 | import org.eclipse.linuxtools.tmf.tests.stubs.event.TmfSyntheticEventStub; |
| 30 | |
| 31 | /** |
| 32 | * <b><u>TmfSyntheticEventProviderStub</u></b> |
| 33 | * <p> |
| 34 | * TODO: Implement me. Please. |
| 35 | */ |
| 36 | @SuppressWarnings("javadoc") |
| 37 | public class TmfSyntheticEventProviderStub extends TmfEventProvider { |
| 38 | |
| 39 | public static final int NB_EVENTS = 1000; |
| 40 | |
| 41 | private final BlockingQueue<ITmfEvent> fDataQueue = new LinkedBlockingQueue<>(1000); |
| 42 | |
| 43 | public TmfSyntheticEventProviderStub() { |
| 44 | super("TmfSyntheticEventProviderStub", TmfSyntheticEventStub.class); |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public ITmfContext armRequest(final ITmfEventRequest request) { |
| 49 | |
| 50 | // Get the TmfSyntheticEventStub provider |
| 51 | final ITmfEventProvider[] eventProviders = TmfProviderManager.getProviders(ITmfEvent.class, TmfEventProviderStub.class); |
| 52 | final ITmfEventProvider provider = eventProviders[0]; |
| 53 | |
| 54 | final TmfTimeRange range = request.getRange(); |
| 55 | final TmfEventRequest subRequest = |
| 56 | new TmfEventRequest(ITmfEvent.class, range, 0, NB_EVENTS, ExecutionType.FOREGROUND) { |
| 57 | @Override |
| 58 | public void handleData(final ITmfEvent event) { |
| 59 | super.handleData(event); |
| 60 | if (event != null) { |
| 61 | handleIncomingData(event); |
| 62 | } else { |
| 63 | request.done(); |
| 64 | } |
| 65 | } |
| 66 | }; |
| 67 | provider.sendRequest(subRequest); |
| 68 | |
| 69 | // Return a dummy context |
| 70 | return new TmfContext(); |
| 71 | } |
| 72 | |
| 73 | // Queue 2 synthetic events per base event |
| 74 | private void handleIncomingData(final ITmfEvent e) { |
| 75 | queueResult(new TmfSyntheticEventStub(e)); |
| 76 | queueResult(new TmfSyntheticEventStub(e)); |
| 77 | } |
| 78 | |
| 79 | private static final int TIMEOUT = 10000; |
| 80 | |
| 81 | @Override |
| 82 | public TmfSyntheticEventStub getNext(final ITmfContext context) { |
| 83 | TmfSyntheticEventStub data = null; |
| 84 | try { |
| 85 | data = (TmfSyntheticEventStub) fDataQueue.poll(TIMEOUT, TimeUnit.MILLISECONDS); |
| 86 | if (data == null) { |
| 87 | throw new InterruptedException(); |
| 88 | } |
| 89 | } |
| 90 | catch (final InterruptedException e) { |
| 91 | } |
| 92 | return data; |
| 93 | } |
| 94 | |
| 95 | public void queueResult(final TmfSyntheticEventStub data) { |
| 96 | boolean ok = false; |
| 97 | try { |
| 98 | ok = fDataQueue.offer(data, TIMEOUT, TimeUnit.MILLISECONDS); |
| 99 | if (!ok) { |
| 100 | throw new InterruptedException(); |
| 101 | } |
| 102 | } |
| 103 | catch (final InterruptedException e) { |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | } |