| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2011 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 | * Polytechnique Montréal - Initial API and implementation |
| 11 | * Bernd Hufmann - Productification, enhancements and fixes |
| 12 | * |
| 13 | *******************************************************************************/ |
| 14 | package org.eclipse.linuxtools.lttng.ui.tracecontrol.utility; |
| 15 | |
| 16 | import java.io.FileNotFoundException; |
| 17 | import java.io.FileOutputStream; |
| 18 | import java.io.IOException; |
| 19 | |
| 20 | import org.eclipse.linuxtools.lttng.core.tracecontrol.model.TraceResource; |
| 21 | import org.eclipse.linuxtools.lttng.core.tracecontrol.model.config.TraceConfig; |
| 22 | import org.eclipse.linuxtools.lttng.ui.tracecontrol.subsystems.TraceSubSystem; |
| 23 | import org.eclipse.rse.ui.SystemBasePlugin; |
| 24 | import org.eclipse.tm.tcf.protocol.JSON; |
| 25 | |
| 26 | /** |
| 27 | * <b><u>DownloadProxy</u></b> |
| 28 | * <p> |
| 29 | * Proxy implementation for storing trace data locally on host where client is running. (writeTraceNetwork) |
| 30 | * </p> |
| 31 | */ |
| 32 | public class DownloadProxy { |
| 33 | |
| 34 | // ------------------------------------------------------------------------ |
| 35 | // Attributes |
| 36 | // ------------------------------------------------------------------------ |
| 37 | |
| 38 | TraceSubSystem fSubSystem; |
| 39 | |
| 40 | // ------------------------------------------------------------------------ |
| 41 | // Constructors |
| 42 | // ------------------------------------------------------------------------ |
| 43 | |
| 44 | /** |
| 45 | * Constructor |
| 46 | * |
| 47 | * @param subSystem The trace SubSystem |
| 48 | */ |
| 49 | public DownloadProxy(TraceSubSystem subSystem) { |
| 50 | fSubSystem = subSystem; |
| 51 | } |
| 52 | |
| 53 | // ------------------------------------------------------------------------ |
| 54 | // Operations |
| 55 | // ------------------------------------------------------------------------ |
| 56 | |
| 57 | /** |
| 58 | * Writes trace data to traces files. |
| 59 | * |
| 60 | * @param data binary data |
| 61 | */ |
| 62 | public void writeDownloadedTrace(byte[] data) { |
| 63 | Object[] args = null; |
| 64 | try { |
| 65 | args = JSON.parseSequence(data); |
| 66 | } catch (IOException e) { |
| 67 | SystemBasePlugin.logError("DownloadProxy", e); //$NON-NLS-1$ |
| 68 | } |
| 69 | if (args != null) { |
| 70 | byte[] traceData = JSON.toByteArray(args[4]); |
| 71 | TraceResource trace = fSubSystem.findTrace(args[0].toString(), args[1].toString(), args[2].toString()); |
| 72 | if (trace != null) { |
| 73 | TraceConfig conf = trace.getTraceConfig(); |
| 74 | FileOutputStream fos = null; |
| 75 | if (!TraceConfig.InvalidTracePath.equals(conf.getTracePath())) { |
| 76 | String fileName = conf.getTracePath() + "/" + args[3].toString(); //$NON-NLS-1$ |
| 77 | try { |
| 78 | fos = new FileOutputStream(fileName, true); |
| 79 | fos.write(traceData); |
| 80 | fos.close(); |
| 81 | // ((TraceResource) args[2]).setSize(sizeFile.length()); |
| 82 | } catch (FileNotFoundException e) { |
| 83 | SystemBasePlugin.logError("DownloadProxy", e); //$NON-NLS-1$ |
| 84 | } catch (IOException e) { |
| 85 | SystemBasePlugin.logError("DownloadProxy", e); //$NON-NLS-1$ |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Method for UST |
| 94 | * |
| 95 | * @param data |
| 96 | */ |
| 97 | public void handleUnwriteTraceDataEvent(byte [] data) { |
| 98 | try { |
| 99 | Object[] args = null; |
| 100 | args = JSON.parseSequence(data); |
| 101 | // Check if it is an ust trace |
| 102 | TraceResource trace = fSubSystem.findTrace(args[0].toString(), args[1].toString(), args[2].toString()); |
| 103 | if ((trace != null) && trace.isUst()) { |
| 104 | // TODO |
| 105 | } |
| 106 | |
| 107 | } catch (IOException e) { |
| 108 | SystemBasePlugin.logError("DownloadProxy", e); //$NON-NLS-1$ |
| 109 | } |
| 110 | |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Method that handles trace done event which is sent at the end of the trace session. |
| 115 | * |
| 116 | * @param data Binary data |
| 117 | */ |
| 118 | public void handleTraceDoneEvent(byte [] data) { |
| 119 | // Nothing to do! |
| 120 | } |
| 121 | } |