- Introduced TmfExperiment (single trace for now)
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.tests / stubs / org / eclipse / linuxtools / tmf / request / TmfRequestHandlerStub.java
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:
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.request;
14
15 import java.io.File;
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;
21 import org.eclipse.linuxtools.tmf.trace.ITmfEventParser;
22 import org.eclipse.linuxtools.tmf.trace.TmfEventParserStub;
23 import org.eclipse.linuxtools.tmf.trace.TmfEventStreamStub;
24 import org.eclipse.linuxtools.tmf.trace.ITmfTrace.StreamContext;
25
26 /**
27 * <b><u>TmfRequestHandlerStub</u></b>
28 * <p>
29 * TODO: Implement me. Please.
30 */
31 public class TmfRequestHandlerStub implements ITmfRequestHandler<TmfEvent> {
32
33 // The test file
34 private static final String TEST_STREAM = "M-Test-100K";
35
36 // A constant to limit the number of events for the tests
37 public static final int MAX_GENERATED_EVENTS = 1000;
38
39 private ITmfEventParser fParser;
40 private TmfEventStreamStub fStream;
41
42 public TmfRequestHandlerStub() throws IOException {
43 String directory = new File(".").getCanonicalPath() + File.separator + "testfiles";
44 String filename = directory + File.separator + TEST_STREAM;
45
46 fParser = new TmfEventParserStub();
47 fStream = new TmfEventStreamStub(filename, fParser);
48 }
49
50 /* (non-Javadoc)
51 * @see org.eclipse.linuxtools.tmf.eventlog.ITmfRequestHandler#processRequest(org.eclipse.linuxtools.tmf.eventlog.TmfEventRequest, boolean)
52 */
53 public void processRequest(final TmfDataRequest<TmfEvent> request, boolean waitForCompletion) {
54
55 Thread thread = new Thread() {
56 @Override
57 public void run() {
58 TmfTimestamp startTime = request.getRange().getStartTime();
59 TmfTimestamp endTime = request.getRange().getEndTime();
60 int blockSize = request.getBlockize();
61
62 int nbRequestedEvents = request.getNbRequestedItems();
63 if (nbRequestedEvents <= 0) {
64 nbRequestedEvents = MAX_GENERATED_EVENTS;
65 }
66
67 Vector<TmfEvent> events = new Vector<TmfEvent>();
68 int nbEvents = 0;
69 StreamContext context = new StreamContext(null);
70 TmfEvent event = fStream.getEvent(context, startTime);
71 while (!request.isCancelled() && nbEvents < nbRequestedEvents &&
72 event != null && event.getTimestamp().compareTo(endTime, false) <= 0 )
73 {
74 events.add(event);
75 if (++nbEvents % blockSize == 0) {
76 TmfEvent[] result = new TmfEvent[events.size()];
77 events.toArray(result);
78 request.setData(result);
79 request.handleData();
80 events.removeAllElements();
81 }
82 event = fStream.getNextEvent(context);
83 }
84 TmfEvent[] result = new TmfEvent[events.size()];
85 events.toArray(result);
86
87 request.setData(result);
88 request.handleData();
89 request.done();
90 }
91 };
92 thread.start();
93
94 if (waitForCompletion) {
95 request.waitForCompletion();
96 }
97 }
98
99 }
This page took 0.032856 seconds and 5 git commands to generate.