BonnMotionPositionDistribution.java 3.79 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
/*
 * 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.placement;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.zip.GZIPInputStream;

import de.tud.kom.p2psim.impl.simengine.Simulator;
34
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
35
36
37
38
39
40
41
42
43
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

public class BonnMotionPositionDistribution extends PositionDistribution {


	private Vector<String> mobilityTraceFiles;
	
	private Vector<PositionVector> offsets;

	private List<PositionVector> availablePositions;

	private int counter;
	
	private int fileCounter = 1;

	public BonnMotionPositionDistribution() {
		super();
		
		this.mobilityTraceFiles = new Vector<String>();
		this.offsets = new Vector<PositionVector>();
		
		Map<String, String> variables = Simulator.getConfigurator()
				.getVariables();
		System.out.println(variables.toString());
		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) {
			Integer xOffset = Integer.parseInt(xString);
			Integer yOffset = Integer.parseInt(yString);
			this.mobilityTraceFiles.add(traceFile);
			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);
		}
		availablePositions = new LinkedList<PositionVector>();
		extractPositions();
		counter = 0;
	}

	@Override
	public PositionVector getNextPosition() {
		if (counter >= availablePositions.size()) {
			throw new RuntimeException(
					"There are more Hosts configured than available in the corresponding BonnMotionTraceFile."
							+ " Please configure less hosts or run BonnMotion with the correct number of hosts.");
		}
		PositionVector pos = availablePositions.get(counter);
		counter++;
		return pos;
	}

	private void extractPositions() {
		for (int a =0;a<mobilityTraceFiles.size();a++) {
			File mobilityTraceFileObject = new File(mobilityTraceFiles.get(a));
			PositionVector offset = offsets.get(a);
			try {

				BufferedReader buf = new BufferedReader(new InputStreamReader(
						new GZIPInputStream(new FileInputStream(
								mobilityTraceFileObject))));

				String line = null;
				String[] tokens = null;
				while (buf.ready()) {
					line = buf.readLine();
					tokens = line.split(" ");
					availablePositions.add(new PositionVector(Double
							.parseDouble(tokens[1]) + offset.getX(), Double
							.parseDouble(tokens[2]) + offset.getY()));
				}
				buf.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}