Commit | Line | Data |
---|---|---|
e73a4ba5 GB |
1 | /******************************************************************************* |
2 | * Copyright (c) 2013 École Polytechnique de Montréal | |
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 | * Geneviève Bastien - Initial implementation and API | |
11 | *******************************************************************************/ | |
12 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.core.synchronization; |
e73a4ba5 GB |
14 | |
15 | import java.io.File; | |
16 | import java.io.IOException; | |
27213c57 | 17 | import java.util.Collection; |
e73a4ba5 | 18 | |
2bdf0193 AM |
19 | import org.eclipse.tracecompass.internal.tmf.core.Activator; |
20 | import org.eclipse.tracecompass.tmf.core.component.TmfComponent; | |
21 | import org.eclipse.tracecompass.tmf.core.event.matching.ITmfEventMatching; | |
22 | import org.eclipse.tracecompass.tmf.core.event.matching.TmfNetworkEventMatching; | |
23 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; | |
e73a4ba5 GB |
24 | |
25 | /** | |
26 | * This abstract manager class handles loading trace synchronization data or | |
27 | * otherwise their calculation. | |
28 | * | |
29 | * @author Geneviève Bastien | |
30 | * @since 3.0 | |
31 | */ | |
32 | public abstract class SynchronizationManager extends TmfComponent { | |
33 | ||
34 | /** | |
35 | * Function called to synchronize traces using the fully incremental | |
36 | * synchronization algorithm | |
37 | * | |
38 | * @param syncFile | |
39 | * The target name of the synchronization file. If it exists, it | |
40 | * will be opened, otherwise it will be created and data from | |
41 | * this synchro run will be saved there | |
42 | * @param traces | |
43 | * The list of traces to synchronize | |
44 | * @param doSync | |
45 | * Whether to actually synchronize or just try opening a sync | |
46 | * file | |
47 | * @return The synchronization object | |
48 | */ | |
27213c57 | 49 | public static SynchronizationAlgorithm synchronizeTraces(final File syncFile, final Collection<ITmfTrace> traces, boolean doSync) { |
e73a4ba5 GB |
50 | |
51 | SynchronizationAlgorithm syncAlgo; | |
52 | if (doSync) { | |
51c08015 | 53 | syncAlgo = synchronize(syncFile, traces, SynchronizationAlgorithmFactory.getDefaultAlgorithm()); |
e73a4ba5 GB |
54 | } else { |
55 | syncAlgo = openExisting(syncFile); | |
56 | if (syncAlgo == null) { | |
51c08015 | 57 | syncAlgo = SynchronizationAlgorithmFactory.getDefaultAlgorithm(); |
e73a4ba5 GB |
58 | } |
59 | } | |
60 | return syncAlgo; | |
61 | } | |
62 | ||
63 | /** | |
64 | * Function called to synchronize traces with a specific synchronization | |
65 | * algorithm. If a synchronization already exists, but is not the requested | |
66 | * algorithm, the synchronization is done again using the new algorithm | |
67 | * | |
68 | * @param syncFile | |
69 | * The target name of the synchronization file. If it exists, it | |
70 | * will be opened, otherwise it will be created and data from | |
71 | * this synchro run will be saved there | |
72 | * @param traces | |
73 | * The list of traces to synchronize | |
74 | * @param algo | |
75 | * A synchronization algorithm object to determine the algorithm | |
76 | * used to synchronization. | |
77 | * @param doSync | |
78 | * Whether to actually synchronize or just try opening a sync | |
79 | * file | |
80 | * @return The synchronization object | |
81 | */ | |
27213c57 | 82 | public static SynchronizationAlgorithm synchronizeTraces(final File syncFile, final Collection<ITmfTrace> traces, SynchronizationAlgorithm algo, boolean doSync) { |
e73a4ba5 GB |
83 | |
84 | SynchronizationAlgorithm syncAlgo; | |
85 | if (doSync) { | |
86 | syncAlgo = synchronize(syncFile, traces, algo); | |
87 | } else { | |
88 | syncAlgo = openExisting(syncFile); | |
89 | if (syncAlgo == null || (syncAlgo.getClass() != algo.getClass())) { | |
90 | if (algo != null) { | |
91 | syncAlgo = algo; | |
92 | } else { | |
51c08015 | 93 | syncAlgo = SynchronizationAlgorithmFactory.getDefaultAlgorithm(); |
e73a4ba5 GB |
94 | } |
95 | } | |
96 | } | |
97 | ||
98 | return syncAlgo; | |
99 | } | |
100 | ||
101 | private static SynchronizationAlgorithm openExisting(final File syncFile) { | |
102 | if ((syncFile != null) && syncFile.exists()) { | |
103 | /* Load an existing history */ | |
104 | try { | |
105 | SynchronizationBackend syncBackend = new SynchronizationBackend(syncFile); | |
106 | SynchronizationAlgorithm algo = syncBackend.openExistingSync(); | |
107 | return algo; | |
108 | } catch (IOException e) { | |
109 | /* | |
110 | * There was an error opening the existing file. Perhaps it was | |
111 | * corrupted, perhaps it's an old version? We'll just | |
112 | * fall-through and try to build a new one from scratch instead. | |
113 | */ | |
114 | Activator.logInfo("Problem opening existing trace synchronization file", e); //$NON-NLS-1$ | |
115 | } | |
116 | } | |
117 | return null; | |
118 | } | |
119 | ||
27213c57 | 120 | private static SynchronizationAlgorithm synchronize(final File syncFile, final Collection<ITmfTrace> traces, SynchronizationAlgorithm syncAlgo) { |
e73a4ba5 GB |
121 | ITmfEventMatching matching = new TmfNetworkEventMatching(traces, syncAlgo); |
122 | matching.matchEvents(); | |
123 | ||
124 | SynchronizationBackend syncBackend; | |
125 | try { | |
126 | syncBackend = new SynchronizationBackend(syncFile, false); | |
127 | syncBackend.saveSync(syncAlgo); | |
128 | } catch (IOException e) { | |
129 | Activator.logError("Error while saving trace synchronization file", e); //$NON-NLS-1$ | |
130 | } | |
131 | return syncAlgo; | |
132 | } | |
133 | ||
134 | } |