BonnMotionMovementModel.java 10.3 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
26
27
28
29
30
31
32
33
34
35
/*
 * 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.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.Vector;
import java.util.zip.GZIPInputStream;

import de.tud.kom.p2psim.api.topology.movement.MovementSupported;
36
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
37
import de.tud.kom.p2psim.impl.simengine.Simulator;
38
39
import de.tud.kom.p2psim.impl.topology.component.DefaultTopologyComponent;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
40
41
import de.tudarmstadt.maki.simonstrator.api.Event;
import de.tudarmstadt.maki.simonstrator.api.EventHandler;
42
43
import de.tudarmstadt.maki.simonstrator.api.Monitor;
import de.tudarmstadt.maki.simonstrator.api.Monitor.Level;
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
201
202
203
204
205
206
207
208
209
import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;

public class BonnMotionMovementModel extends AbstractMovementModel implements
		EventHandler {

	private final int EVENT_ID = 1;

	private Vector<String> mobilityTraceFiles;

	private Vector<String> mobilityConfigFiles;
	
	private Vector<PositionVector> offsets;
	
	private long hostCounter;

	private long movementInterval;

	private PositionVector previousPosition;

	private long previousTime;

	private long lastNotificationTime;
	
	private int fileCounter = 1;

	@XMLConfigurableConstructor({ "movementInterval" })
	public BonnMotionMovementModel(long movementInterval) {
		super();
		this.movementInterval = movementInterval;
		this.lastNotificationTime = 0;
		this.hostCounter = 1;
		
		this.mobilityTraceFiles = new Vector<String>();
		this.mobilityConfigFiles = new Vector<String>();
		this.offsets = new Vector<PositionVector>();
		
		Map<String, String> variables = Simulator.getConfigurator()
				.getVariables();
		
		String traceFile = variables.get("mobilityTraceFile-File-"+fileCounter);
		String configFile = variables.get("mobilityConfigFile-File-"+fileCounter);
		String xString = variables.get("xOffset-File-" + fileCounter);
		String yString = variables.get("yOffset-File-" + fileCounter);

		while (traceFile != null && configFile != null && xString != null
				&& yString != null) {
			this.mobilityTraceFiles.add(traceFile);
			this.mobilityConfigFiles.add(configFile);
			Integer xOffset = Integer.parseInt(xString);
			Integer yOffset = Integer.parseInt(yString);
			this.offsets.add(new PositionVector(xOffset.intValue(), yOffset
					.intValue()));

			fileCounter++;
			traceFile = variables.get("mobilityTraceFile-File-" + fileCounter);
			configFile = variables
					.get("mobilityConfigFile-File-" + fileCounter);
			xString = variables.get("xOffset-File-" + fileCounter);
			yString = variables.get("yOffset-File-" + fileCounter);
		}
		
		checkConfiguration();
		extractMovement();
	}

	@Override
	public void move() {
		// This method is not required, because the hosts are moved over an
		// trace-file.
	}

	private void checkConfiguration() {
		for (String s : mobilityConfigFiles) {
			StringBuffer sBuf = new StringBuffer();
			File configFile = new File(s);
			Properties props = new Properties();
			try {
				props.load(new FileInputStream(configFile));
				Map<String, String> variables = Simulator.getConfigurator()
						.getVariables();

				// FIXME Find an alternative way to ensure correct number of
				// nodes
				// Check for correct dimensions
				// if (Double.parseDouble(variables.get("WORLD-X")) != Double
				// .parseDouble((String) props.get("x"))
				// || Double.parseDouble(variables.get("WORLD-Y")) != Double
				// .parseDouble((String) props.get("y"))) {
				// sBuf.append("- The dimensions in the PFS-Config-File do not match the dimensions in the BonnMotion-Config-File!\n");
				// }

				// FIXME Find an alternative way to ensure correct number of
				// nodes
				// Check for correct number of nodes
				// if (Integer.parseInt(variables.get("movingPeersSize")) >
				// Integer
				// .parseInt((String) props.get("nn"))) {
				// sBuf.append("- The PFS-Config-File specifies more nodes than the BonnMotion-Config-File!\n");
				// }

				// Check for correct length of the simulation
				if ((Simulator.getEndTime() / (double) Simulator.SECOND_UNIT) > Double
						.parseDouble((String) props.get("duration"))) {
					sBuf.append("- The PFS-Config-File specifies a longer simulation than the BonnMotion-Config-File!\n");
				}
				if (sBuf.length() > 0) {
					throw new RuntimeException(sBuf.toString());
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void extractMovement() {
		for (int a=0;a<mobilityTraceFiles.size();a++) {
			PositionVector offset = offsets.get(a);
			File mobilityTraceFileObject = new File(mobilityTraceFiles.get(a));
			BufferedReader buf;
			try {
				buf = new BufferedReader(new InputStreamReader(
						new GZIPInputStream(new FileInputStream(
								mobilityTraceFileObject))));
				String line = null;
				String[] tokens = null;
				PositionVector nextPosition = null;
				long nextTime = -1;

				PositionVector direction = null;
				long timeSpan = -1;

				while (buf.ready()) {
					line = buf.readLine();
					tokens = line.split(" ");
					assert tokens.length % 3 == 0;
					previousPosition = new PositionVector(
							Double.valueOf(tokens[1]) + offset.getX(),
							Double.valueOf(tokens[2]) + offset.getY());
					previousTime = (long) (Double.parseDouble(tokens[0]) * Simulator.SECOND_UNIT);
					for (int i = 3; i < tokens.length; i += 3) {
						nextPosition = new PositionVector(
								Double.parseDouble(tokens[i + 1])+ offset.getX(),
								Double.parseDouble(tokens[i + 2])+ offset.getY());
						nextTime = (long) Double.parseDouble(tokens[i])
								* Simulator.SECOND_UNIT;

						// Check if a fragmentation should be done
						if (movementInterval != -1
								&& (nextPosition.getX() != previousPosition
										.getX())
								&& (nextPosition.getY() != previousPosition
										.getY())
								&& ((nextTime - previousTime) > movementInterval)) {

							// calculate the direction and the time interval
							// between
							// the previous and the next point
							direction = new PositionVector(nextPosition.getX()
									- previousPosition.getX(),
									nextPosition.getY()
											- previousPosition.getY());
							timeSpan = nextTime - previousTime;
							long deltaTime = movementInterval;

210
211
212
213
214
215
							Monitor.log(
									BonnMotionMovementModel.class,
									Level.DEBUG,
									"Creating "
											+ timeSpan
											/ movementInterval
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
									+ " intermediate points between "
									+ previousPosition + " and " + nextPosition
									+ " at time "
									+ Simulator.getFormattedTime(previousTime)
									+ " and "
									+ Simulator.getFormattedTime(nextTime));

							// add the intermediate steps
							while (deltaTime < timeSpan) {
								double x = previousPosition.getX() + deltaTime
										/ (double) timeSpan * direction.getX();
								double y = previousPosition.getY() + deltaTime
										/ (double) timeSpan * direction.getY();
								PositionVector interStep = new PositionVector(
										x, y);
231
232
233
234
								Monitor.log(
										BonnMotionMovementModel.class,
										Level.DEBUG,
										"Creating "
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
										+ interStep
										+ " for time "
										+ Simulator.getFormattedTime(deltaTime
												+ previousTime));
								Event.scheduleWithDelay(deltaTime
										+ previousTime, this,
										new BonnMotionEvent(interStep,
												hostCounter), EVENT_ID);
								deltaTime = deltaTime + movementInterval;
							}

							// Schedule the final position and set this position
							// and
							// time as the previous ones
							Event.scheduleWithDelay(nextTime, this,
									new BonnMotionEvent(nextPosition,
											hostCounter), EVENT_ID);

							previousPosition = nextPosition;
							previousTime = nextTime;

						} else {
							// Schedule the final position and set this position
							// and
							// time as the previous ones
							Event.scheduleWithDelay(nextTime, this,
									new BonnMotionEvent(nextPosition,
											hostCounter), EVENT_ID);

							previousPosition = nextPosition;
							previousTime = nextTime;
						}

					}
					hostCounter++;
				}
271
272
				Monitor.log(BonnMotionMovementModel.class, Level.WARN,
						"Successfully parsed " + mobilityTraceFiles.get(a)
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
						+ " and created movement for " + (hostCounter - 1)
						+ " hosts");
				buf.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void eventOccurred(Object content, int type) {
		if (type == EVENT_ID) {
			BonnMotionEvent bme = (BonnMotionEvent) content;
288
289
			Set<SimLocationActuator> components = getComponents();
			for (SimLocationActuator comp : components) {
290
291
292
				if (((DefaultTopologyComponent) comp).getHost().getHostId() == bme
						.getHostID()) {
					PositionVector newPos = bme.getNextPosition();
293
					updatePosition(comp, newPos);
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
					
					// FIXME Delete!! Only test-logger!
//					log.warn(Simulator.getFormattedTime(Simulator.getCurrentTime())+" ID = " + bme.getHostID() +" pos = " + pos);
				}
			}
		}
	}

	private class BonnMotionEvent {
		private PositionVector nextPosition;

		private long hostID;

		public BonnMotionEvent(PositionVector nextPosition, long hostID) {
			super();
			this.nextPosition = nextPosition;
			this.hostID = hostID;
		}

		public PositionVector getNextPosition() {
			return nextPosition;
		}

		public long getHostID() {
			return hostID;
		}

	}

}