- Minor modification of the FW API (better trace/parser integration)
[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.TmfTraceStub;
22 import org.eclipse.linuxtools.tmf.trace.ITmfTrace.TmfTraceContext;
23
24 /**
25 * <b><u>TmfRequestHandlerStub</u></b>
26 * <p>
27 * TODO: Implement me. Please.
28 */
29 public class TmfRequestHandlerStub implements ITmfRequestHandler<TmfEvent> {
30
31 // The test file
32 private static final String TEST_STREAM = "M-Test-100K";
33
34 // A constant to limit the number of events for the tests
35 public static final int MAX_GENERATED_EVENTS = 1000;
36
37 private TmfTraceStub fTrace;
38
39 public TmfRequestHandlerStub() throws IOException {
40 String directory = new File(".").getCanonicalPath() + File.separator + "testfiles";
41 String filename = directory + File.separator + TEST_STREAM;
42 fTrace = new TmfTraceStub(filename);
43 }
44
45 /* (non-Javadoc)
46 * @see org.eclipse.linuxtools.tmf.eventlog.ITmfRequestHandler#processRequest(org.eclipse.linuxtools.tmf.eventlog.TmfEventRequest, boolean)
47 */
48 public void processRequest(final TmfDataRequest<TmfEvent> request, boolean waitForCompletion) {
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
57 int nbRequestedEvents = request.getNbRequestedItems();
58 if (nbRequestedEvents <= 0) {
59 nbRequestedEvents = MAX_GENERATED_EVENTS;
60 }
61
62 Vector<TmfEvent> events = new Vector<TmfEvent>();
63 int nbEvents = 0;
64 TmfTraceContext context = new TmfTraceContext(null);
65 TmfEvent event = fTrace.getEvent(context, startTime);
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);
74 request.handleData();
75 events.removeAllElements();
76 }
77 event = fTrace.getNextEvent(context);
78 }
79 TmfEvent[] result = new TmfEvent[events.size()];
80 events.toArray(result);
81
82 request.setData(result);
83 request.handleData();
84 request.done();
85 }
86 };
87 thread.start();
88
89 if (waitForCompletion) {
90 request.waitForCompletion();
91 }
92 }
93
94 }
This page took 0.031898 seconds and 5 git commands to generate.