TracefileMovementModel.java 11.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
 * Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
 *
 * This file is part of PeerfactSim.KOM.
 * 
 * PeerfactSim.KOM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * PeerfactSim.KOM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PeerfactSim.KOM.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package de.tud.kom.p2psim.impl.topology.movement;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedHashMap;
Julian Zobel's avatar
Julian Zobel committed
26
import java.util.LinkedHashSet;
27
import java.util.LinkedList;
28
import java.util.List;
29
30
import java.util.Scanner;

31
32
import de.tud.kom.p2psim.api.network.SimNetInterface;
import de.tud.kom.p2psim.api.network.SimNetworkComponent;
33
34
35
import de.tud.kom.p2psim.api.scenario.ConfigurationException;
import de.tud.kom.p2psim.api.topology.movement.MovementModel;
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
36
import de.tud.kom.p2psim.impl.topology.DefaultTopology;
37
import de.tud.kom.p2psim.impl.topology.movement.modularosm.IAttractionBasedMovementAnalyzer;
38
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.AttractionPointViz;
39
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.IAttractionProvider;
40
41
42
43
44
45
import de.tud.kom.p2psim.impl.topology.movement.modularosm.mapvisualization.IMapVisualization;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
import de.tud.kom.p2psim.impl.topology.views.VisualizationTopologyView.VisualizationInjector;
import de.tudarmstadt.maki.simonstrator.api.Event;
import de.tudarmstadt.maki.simonstrator.api.EventHandler;
import de.tudarmstadt.maki.simonstrator.api.Monitor;
Julian Zobel's avatar
Julian Zobel committed
46
import de.tudarmstadt.maki.simonstrator.api.Monitor.Level;
47
import de.tudarmstadt.maki.simonstrator.api.Time;
48
import de.tudarmstadt.maki.simonstrator.api.component.LifecycleComponent;
49
50
51
52
53

public class TracefileMovementModel implements MovementModel, EventHandler {

	public static String tracefileFolder ="smarter/tracefiles/";
	public static String tracefilePrefix = "smarterTraceFile-";
54
55
56
57
58
59
60
61
62
63
64
	public static long GPS_MISSING_TRACE_THRESHOLD = 60;
	
	public static int ZERO_TO_2 = 0;
	public static int _2_to_5 = 0;
	public static int _5_to_10 = 0;
	public static int TEN_TO_THIRTY = 0;
	public static int THIRTY_TO_SIXTY = 0;
	public static int SIXTY_TO_120 = 0;
	public static int _120_TO_300 = 0;
	public static int _300_TO_600 = 0;
	public static int ABOVE_600 = 0;
65
66
67
	
	protected final int EVENT_MOVE = 1;
	
68
69
	protected final PositionVector DEFAULT_POSITION = new PositionVector(-1000, -1000);
	
70
71
72
	protected long timeBetweenMoveOperations;

	protected IMapVisualization mapVisualization;
73
	protected IAttractionProvider attractionGenerator;
74
	protected AttractionPointViz attractionpointVisualization;
75
76
77
78
	
	protected boolean initialized = false;
	
	private final LinkedHashMap<SimLocationActuator, LinkedList<Step>> components;
79
	private final static LinkedHashMap<SimLocationActuator, Integer> componentToStepSize =  new LinkedHashMap<SimLocationActuator, Integer>();
Julian Zobel's avatar
Julian Zobel committed
80
	private final LinkedHashSet<SimLocationActuator> unusedComponents;
81
82
83
84
85
86
	
	private LinkedList<File> tracefiles;
	private boolean first = true;
	
	public TracefileMovementModel() {
		components = new LinkedHashMap<SimLocationActuator, LinkedList<Step>>();
Julian Zobel's avatar
Julian Zobel committed
87
88
89
90
		
		tracefiles = new LinkedList<File>();	
		
		unusedComponents =  new LinkedHashSet<SimLocationActuator>();
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
	}
	
	public void initialize() {
		if(initialized) {
			return;
		}		
			
		File folder = new File(tracefileFolder);
		if (!folder.exists()) {
			throw new UnsupportedOperationException("Tracefile folder not found");			
		}
				
		for(File tracefile : folder.listFiles()) {
			if(tracefile.getName().contains(tracefilePrefix)) {
				tracefiles.add(tracefile);
			}
		}
		
		if (mapVisualization != null) {
			VisualizationInjector.injectComponent(mapVisualization);
		}
		
113
		if (attractionpointVisualization != null) {
114
115
			
			attractionpointVisualization.setAttractionPoints(new LinkedList<>(attractionGenerator.getAttractionPoints()));
116
117
118
			VisualizationInjector.injectComponent(attractionpointVisualization);
		}
		
119
		initialized = true;
120
						
121
122
123
124
125
126
127
128
129
130
131
132
133
		Event.scheduleWithDelay(timeBetweenMoveOperations, this, null, EVENT_MOVE);
	}
	
	@Override
	public void eventOccurred(Object content, int type) {
		if (type == EVENT_MOVE) {
			move();
		}
	}
	
	private void move() {
		
		if(first) {		
134
			components.forEach((component, steps) -> {					
135
				shutdownComponent(component);	
Julian Zobel's avatar
Julian Zobel committed
136
137
138
139
140
			});	
			
			unusedComponents.forEach((component) -> {					
				shutdownComponent(component);	
			});
141
		}		
142
143
144
145
146
147
148
149
150
151
152
153
		
		// as the files contain the timestamp in seconds, the current time needs to be converted in seconds
		long currentTime = Time.getCurrentTime() / Time.SECOND;

		LinkedList<SimLocationActuator> toRemove = new LinkedList<>();
		
		components.forEach((component, steps) -> {
			
			Step step = steps.peek();			
			if (step != null) {
				
				if(currentTime < step.timestamp) {
154
155
156
157
158
159
					
					if(DefaultTopology.isWithinWorldBoundaries(component.getRealPosition())) {
						if(currentTime + GPS_MISSING_TRACE_THRESHOLD >= step.timestamp) {
							
							if(component.getRealPosition().distanceTo(new PositionVector(step.x, step.y)) > GPS_MISSING_TRACE_THRESHOLD * 2) {								
								shutdownComponent(component);
160
							}							
161
							// do nothing for smoothing?
162
163
164
165
166
167
							// TODO
							else {
								int stepcount = componentToStepSize.get(component);
								stepcount++;
								componentToStepSize.put(component, stepcount);
							}
168
169
						}
						else {
170
							if(DefaultTopology.isWithinWorldBoundaries(component.getRealPosition())) {		
171
										// TODO						
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
								long delta = step.timestamp - currentTime + GPS_MISSING_TRACE_THRESHOLD;
								
								if(delta > 0 && delta <= 2) {
									ZERO_TO_2++;
								}
								else if(delta > 2 && delta <= 5) {
									_2_to_5++;
								}
								else if(delta > 5 && delta <= 10) {
									_5_to_10++;
								}
								else if(delta > 10 && delta <= 30) {
									TEN_TO_THIRTY++;
								}
								else if(delta > 30 && delta <= 60) {
									THIRTY_TO_SIXTY++;
								}
								else if(delta > 60 && delta <= 120) {
									SIXTY_TO_120++;
								}
								else if(delta > 120 && delta <= 300) {
									_120_TO_300++;
								}
								else if(delta > 300 && delta <= 600) {
									_300_TO_600++;
								}
								else if(delta > 600) {
									ABOVE_600++;
								}								
201
202
203
204
								shutdownComponent(component);	
							}								
						}				
					}									
205
				}
206
207
				else {			
					step = steps.pop();
208
209
210
211
212

					// TODO
					int stepcount = componentToStepSize.get(component);
					stepcount++;
					componentToStepSize.put(component, stepcount);
213
					
214
					if(!DefaultTopology.isWithinWorldBoundaries(component.getRealPosition())) {						
215
						component.updateCurrentLocation(new PositionVector(step.x, step.y));	
216
217
						startupComponent(component);
					}
218
219
220
					else {
						component.updateCurrentLocation(new PositionVector(step.x, step.y));	
					}
221
222
				}				
			}
223
			else {							
224
225
226
227
228
229
				toRemove.add(component);
			}
		});
		
		for (SimLocationActuator simLocationActuator : toRemove) {
			components.remove(simLocationActuator);
230
			shutdownComponent(simLocationActuator);			
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
		}
		
		Event.scheduleWithDelay(timeBetweenMoveOperations, this, null, EVENT_MOVE);
		

		if(first) {
			// Inform analyzer of resolved movement
			if(Monitor.hasAnalyzer(IAttractionBasedMovementAnalyzer.class)) {			
				Monitor.getOrNull(IAttractionBasedMovementAnalyzer.class).onAllNodeInitializationCompleted(components.keySet());
			}
			
			first =false;
		}
	}

246
247
	private void shutdownComponent(SimLocationActuator node) {		
		
248
		SimNetworkComponent net = (SimNetworkComponent) node.getHost().getNetworkComponent();		
249
250
251
252
253
		for (SimNetInterface netI : net.getSimNetworkInterfaces()) {
			if (netI.isOnline()) {
				netI.goOffline();
			}
		}
254
255
256
257
258
259
260
261
262
263
264
265
266
267
	
		try {
			List<LifecycleComponent> comps = node.getHost().getComponents(LifecycleComponent.class);
			
			for (LifecycleComponent lifecycleComponent : comps) {
				lifecycleComponent.stopComponent();
			}
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		node.updateCurrentLocation(new PositionVector(DEFAULT_POSITION));				
		
268
269
270
271
272
273
274
275
276
277
278
	}
	
	private void startupComponent(SimLocationActuator node) {
		
		SimNetworkComponent net = (SimNetworkComponent) node.getHost().getNetworkComponent();
		
		for (SimNetInterface netI : net.getSimNetworkInterfaces()) {
			if (netI.isOffline()) {
				netI.goOnline();
			}
		}
279
280
281
282
283
284
285
286
287
288
289
290
291
		
		try {
			List<LifecycleComponent> comps = node.getHost().getComponents(LifecycleComponent.class);
			
			for (LifecycleComponent lifecycleComponent : comps) {
				lifecycleComponent.startComponent();
			}
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		
292
293
	}
		
294
295
296
297
298
299
300
301
302
303
304
	@Override
	public void addComponent(SimLocationActuator actuator) {
		if (!initialized) {
			initialize();
		}
		
		associateTracefile(actuator);
	}

	private void associateTracefile(SimLocationActuator actuator) {
		if(tracefiles.isEmpty()) {
Julian Zobel's avatar
Julian Zobel committed
305
306
307
308
			Monitor.log(this.getClass(), Level.WARN, "No unassociated trace file available. Skip.", actuator);
			//throw new UnsupportedOperationException("List of tracefiles is empty, thus cannot initiate the component!");
			unusedComponents.add(actuator);
			return;
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
		}
		
		if(components.containsKey(actuator)) {
			throw new UnsupportedOperationException("Component was already assigned!");
		}
		
		LinkedList<Step> stepQueue = new LinkedList<Step>(); 
		File tracefile = tracefiles.pop();		
		Scanner filescanner;
		
		try {
			filescanner = new Scanner(tracefile);		
						
			while(filescanner.hasNextLine()) {
				String line = filescanner.nextLine();
				String[] split = line.split(" ");
						
				long timestamp = Long.valueOf(split[0]);
				//double lat = Double.valueOf(split[1]);
				//double lon = Double.valueOf(split[2]);
				double x = Double.valueOf(split[3]);
				double y = Double.valueOf(split[4]);
							
				Step s = new Step(timestamp, x, y);
				stepQueue.add(s);				
			}
			
			filescanner.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		catch (Exception e) {
			System.err.println(tracefile.getName());
			e.printStackTrace();
		}
		
345
		// System.out.println(actuator.getHost().getId() + " <> " + tracefile.getName());
346
347
		
		components.put(actuator, stepQueue);
348
349
350
351
352
353
354
355
356
357
358
		componentToStepSize.put(actuator, 0);
	}
	
	public static int getComponentStepSize(SimLocationActuator component) {
		
		if(!componentToStepSize.containsKey(component)) {
			return -1;
		}
		
		return componentToStepSize.get(component);
		
359
360
361
362
363
	}
	
	@Override
	public void placeComponent(SimLocationActuator actuator) {		
		// Initial placement		
364
		actuator.updateCurrentLocation(new PositionVector(DEFAULT_POSITION));		
365
366
367
368
369
370
371
372
373
374
375
	}

	@Override
	public void setTimeBetweenMoveOperations(long time) {
		this.timeBetweenMoveOperations = time;		
	}

	public void setIMapVisualization(IMapVisualization mapVisualization) {
		this.mapVisualization = mapVisualization;
	}
	
376
377
378
379
	public void setAttractionPointViz(AttractionPointViz viz) {
		this.attractionpointVisualization = viz;
	}
	
Julian Zobel's avatar
Julian Zobel committed
380
	public void setIAttractionProvider(IAttractionProvider attractionGenerator) {
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
		if (attractionGenerator == null) {
			throw new ConfigurationException(
					"AttractionGenerator is missing in ModularMovementModel!");
		}
		this.attractionGenerator = attractionGenerator;
	}
	

	private class Step {

		public final double x;

		public final double y;

		public final long timestamp;

		public Step(long timestamp, double x, double y) {
			this.x = x;
			this.y = y;
			this.timestamp = timestamp;
		}
402
403
404
405
406
		
		@Override
		public String toString() {
			return "Step @ " + timestamp + "s -> ("+x+", "+y+")";
		}
407
408
409
	}
	
}