Commit | Line | Data |
---|---|---|
4c5b8099 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009 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: | |
165c977c | 10 | * Francois Chouinard - Initial API and implementation |
4c5b8099 FC |
11 | *******************************************************************************/ |
12 | ||
165c977c | 13 | package org.eclipse.linuxtools.tmf.request; |
4c5b8099 | 14 | |
165c977c | 15 | import java.io.File; |
4c5b8099 FC |
16 | import java.io.IOException; |
17 | import java.util.Vector; | |
18 | ||
19 | import org.eclipse.linuxtools.tmf.event.TmfEvent; | |
20 | import org.eclipse.linuxtools.tmf.event.TmfTimestamp; | |
8d2e2848 | 21 | import org.eclipse.linuxtools.tmf.trace.TmfTraceContext; |
146a887c | 22 | import org.eclipse.linuxtools.tmf.trace.TmfTraceStub; |
4c5b8099 FC |
23 | |
24 | /** | |
25 | * <b><u>TmfRequestHandlerStub</u></b> | |
26 | * <p> | |
27 | * TODO: Implement me. Please. | |
28 | */ | |
165c977c | 29 | public class TmfRequestHandlerStub implements ITmfRequestHandler<TmfEvent> { |
4c5b8099 FC |
30 | |
31 | // The test file | |
165c977c | 32 | private static final String TEST_STREAM = "M-Test-100K"; |
4c5b8099 FC |
33 | |
34 | // A constant to limit the number of events for the tests | |
35 | public static final int MAX_GENERATED_EVENTS = 1000; | |
36 | ||
146a887c | 37 | private TmfTraceStub fTrace; |
4c5b8099 | 38 | |
165c977c FC |
39 | public TmfRequestHandlerStub() throws IOException { |
40 | String directory = new File(".").getCanonicalPath() + File.separator + "testfiles"; | |
41 | String filename = directory + File.separator + TEST_STREAM; | |
146a887c | 42 | fTrace = new TmfTraceStub(filename); |
4c5b8099 FC |
43 | } |
44 | ||
45 | /* (non-Javadoc) | |
46 | * @see org.eclipse.linuxtools.tmf.eventlog.ITmfRequestHandler#processRequest(org.eclipse.linuxtools.tmf.eventlog.TmfEventRequest, boolean) | |
47 | */ | |
165c977c | 48 | public void processRequest(final TmfDataRequest<TmfEvent> request, boolean waitForCompletion) { |
4c5b8099 FC |
49 | |
50 | Thread thread = new Thread() { | |
51 | @Override | |
52 | public void run() { | |
53 | TmfTimestamp startTime = request.getRange().getStartTime(); | |
54 | TmfTimestamp endTime = request.getRange().getEndTime(); | |
55 | int blockSize = request.getBlockize(); | |
56 | ||
28b94d61 | 57 | int nbRequestedEvents = request.getNbRequestedEvents(); |
4c5b8099 FC |
58 | if (nbRequestedEvents <= 0) { |
59 | nbRequestedEvents = MAX_GENERATED_EVENTS; | |
60 | } | |
61 | ||
62 | Vector<TmfEvent> events = new Vector<TmfEvent>(); | |
63 | int nbEvents = 0; | |
4e3aa37d FC |
64 | TmfTraceContext context = fTrace.seekEvent(startTime); |
65 | TmfEvent event = fTrace.getNextEvent(context); | |
165c977c FC |
66 | while (!request.isCancelled() && nbEvents < nbRequestedEvents && |
67 | event != null && event.getTimestamp().compareTo(endTime, false) <= 0 ) | |
68 | { | |
69 | events.add(event); | |
70 | if (++nbEvents % blockSize == 0) { | |
71 | TmfEvent[] result = new TmfEvent[events.size()]; | |
72 | events.toArray(result); | |
73 | request.setData(result); | |
0ab46cd3 | 74 | request.handleData(); |
165c977c | 75 | events.removeAllElements(); |
4c5b8099 | 76 | } |
146a887c | 77 | event = fTrace.getNextEvent(context); |
4c5b8099 | 78 | } |
165c977c FC |
79 | TmfEvent[] result = new TmfEvent[events.size()]; |
80 | events.toArray(result); | |
81 | ||
82 | request.setData(result); | |
0ab46cd3 | 83 | request.handleData(); |
165c977c | 84 | request.done(); |
4c5b8099 FC |
85 | } |
86 | }; | |
87 | thread.start(); | |
88 | ||
89 | if (waitForCompletion) { | |
90 | request.waitForCompletion(); | |
91 | } | |
92 | } | |
93 | ||
94 | } |