TopologyFactory.java 8.56 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
36
37
38
39
/*
 * 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;

import java.util.LinkedHashMap;
import java.util.Map;

import de.tud.kom.p2psim.api.common.SimHost;
import de.tud.kom.p2psim.api.linklayer.LinkLayer;
import de.tud.kom.p2psim.api.linklayer.mac.PhyType;
import de.tud.kom.p2psim.api.topology.Topology;
import de.tud.kom.p2psim.api.topology.TopologyComponent;
import de.tud.kom.p2psim.api.topology.movement.MovementModel;
import de.tud.kom.p2psim.api.topology.obstacles.ObstacleModel;
import de.tud.kom.p2psim.api.topology.placement.PlacementModel;
import de.tud.kom.p2psim.api.topology.social.SocialView;
import de.tud.kom.p2psim.api.topology.views.TopologyView;
import de.tud.kom.p2psim.api.topology.waypoints.WaypointModel;
import de.tud.kom.p2psim.impl.network.modular.DBHostListManager;
import de.tud.kom.p2psim.impl.network.modular.db.NetMeasurementDB;
import de.tud.kom.p2psim.impl.topology.movement.AbstractWaypointMovementModel;
40
import de.tud.kom.p2psim.impl.topology.movement.NoMovement;
41
42
import de.tud.kom.p2psim.impl.topology.placement.GNPPlacement;
import de.tud.kom.p2psim.impl.topology.views.latency.GNPLatency;
43
import de.tudarmstadt.maki.simonstrator.api.Binder;
44
45
46
47
48
49
50
51
52
53
import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.Monitor;
import de.tudarmstadt.maki.simonstrator.api.Monitor.Level;
import de.tudarmstadt.maki.simonstrator.api.component.HostComponentFactory;
import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;

/**
 * This factory is configured with one or more {@link TopologyView}s if the
 * {@link LinkLayer} is used.
 * 
Julian Zobel's avatar
Julian Zobel committed
54
55
 * @author Bjoern Richerzhagen, Julian Zobel
 * @version 1.1, 06.09.2018
56
57
58
59
60
61
62
63
64
65
66
67
68
 */
public class TopologyFactory implements HostComponentFactory {

	private Topology topo;

	/**
	 * Placement model for the current group of hosts
	 */
	private PlacementModel placement;

	/**
	 * Movement model for the current group of hosts
	 */
69
	private MovementModel movement = new NoMovement();
70
71
72
73

	private WaypointModel waypointModel;

	private ObstacleModel obstacleModel;
74
75
	
	private boolean registerAsInformationProviderInSiS = false;
76
77
78
79
80
81
82
83

	private static NetMeasurementDB measurementDB = null;

	private static Map<SimHost, NetMeasurementDB.Host> measurementDBHosts = new LinkedHashMap<SimHost, NetMeasurementDB.Host>();

	private static DBHostListManager dbHostList = null;

	private static boolean useRegionGroups = false;
84

85
86
87
	private boolean alreadyCreatedInstances = false;

	private boolean alreadyAddedMovement = false;
Julian Zobel's avatar
Julian Zobel committed
88
89
	
	private boolean isUAVComponent = false;
90
91
92
93
94
95

	/**
	 * 
	 */
	@XMLConfigurableConstructor({ "worldX", "worldY" })
	public TopologyFactory(double worldX, double worldY) {
Julian Zobel's avatar
Julian Zobel committed
96
		topo = new DefaultTopology(new PositionVector(worldX, worldY));		
97
98
		// Make the topology component available globally
		Binder.registerComponent(topo);
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
	}

	@Override
	public TopologyComponent createComponent(Host pHost) {
		alreadyCreatedInstances = true;
		SimHost host = (SimHost) pHost;
		if (measurementDB != null) {
			String groupStr = host.getProperties().getGroupID();
			NetMeasurementDB.Host hostMeta;
			if (useRegionGroups) {
				// In case of a DB presence, look up the host's specific
				// metadata there
				NetMeasurementDB.Group g = measurementDB
						.getStringAddrObjFromStr(NetMeasurementDB.Group.class,
								groupStr);
				if (g == null) {
					throw new IllegalArgumentException(
							"There is no group named '" + groupStr + "'");
				}
				hostMeta = g.tGetNextMember();
			} else {
				// The hosts are not grouped by their region name, we will
				// return random hosts in the world for each group.
				hostMeta = dbHostList.getNextHost();
			}
			measurementDBHosts.put(host, hostMeta);
		}
126

127
128
129
130
		/*
		 * Create a TopologyComponent and register it with the Topology and its
		 * movement model.
		 */
Julian Zobel's avatar
Julian Zobel committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
		TopologyComponent toCo;
		
		/*
		 * Choose the UAV topology component, if this host is part of the UAV group!
		 * 
		 */
		if(isUAVComponent) {			
			toCo = new UAVTopologyComponent(host, topo, movement, placement, registerAsInformationProviderInSiS);
		}
		else {
			toCo = new DefaultTopologyComponent(host, topo, movement, placement, registerAsInformationProviderInSiS);
		}
	
		
145
146
147
148
149
150
151
152
		/*
		 * Need to register TopoViews as movement listeners, as they might need
		 * to update topologies after each movement.
		 */
		for (PhyType phy : PhyType.values()) {
			TopologyView view = topo.getTopologyView(phy);
			if (view != null) {
				toCo.requestLocationUpdates(null, view);
153
			}
154
		}
155

156
157
		Monitor.log(TopologyFactory.class, Level.INFO,
				"Topology Component for Host %s created. Placement: %s, Movement: %s",
Björn Richerzhagen's avatar
Björn Richerzhagen committed
158
				host.getHostId(), placement, movement);
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
		return toCo;
	}

	/**
	 * Set the {@link PlacementModel} for this group of hosts
	 * 
	 * @param placementModel
	 */
	public void setPlacement(PlacementModel placementModel) {
		// Here, we loose the previous model. Intended!
		placement = placementModel;
	}

	public void setWaypoints(WaypointModel waypointModel) {
		waypointModel.setWorldDimensions(topo.getWorldDimensions());
		this.waypointModel = waypointModel;

		this.waypointModel.setObstacleModel(this.obstacleModel);

		this.waypointModel.generateWaypoints();

		topo.setWaypointModel(waypointModel);
	}

	public void setObstacles(ObstacleModel obstacleModel) {
		obstacleModel.setWorldDimensions(topo.getWorldDimensions());
		this.obstacleModel = obstacleModel;

		this.obstacleModel.setWaypointModel(this.waypointModel);

		this.obstacleModel.generateObstacles();

		topo.setObstacleModel(obstacleModel);
	}

	/**
	 * Add a {@link TopologyView}
	 * 
	 * @param topologyView
	 */
	public void setView(TopologyView topologyView) {
		if (alreadyCreatedInstances || alreadyAddedMovement) {
			throw new AssertionError(
					"Topology Views have to be specified globally, not in a host-section! "
							+ "Furthermore, they have to be specified BEFORE the movement models!");
		}
		topo.addTopologyView(topologyView);
	}

	public void setSocialView(SocialView sView) {
		topo.addSocialView(sView);
	}

	/**
	 * Set the {@link MovementModel} for this group of hosts
	 * 
	 * @param movement
	 */
	public void setMovement(MovementModel movement) {
		alreadyAddedMovement = true;
		if (movement instanceof AbstractWaypointMovementModel) {
			((AbstractWaypointMovementModel) movement)
					.setWaypointModel(waypointModel);
		}
		this.movement = movement;
	}

	/**
	 * For the {@link GNPLatency} and the {@link GNPPlacement}, a
	 * {@link NetMeasurementDB} is needed.
	 * 
	 * @param db
	 */
	public void setMeasurementDB(NetMeasurementDB db) {
		if (this.measurementDB == null) {
			this.measurementDB = db;
			this.dbHostList = new DBHostListManager(measurementDB);
		} else {
			throw new AssertionError(
					"You are only allowed to set the MeasurementDB once!");
		}
	}

	public void setUseRegionGroups(boolean useRegionGroups) {
		TopologyFactory.useRegionGroups = useRegionGroups;
	}
245
246
	
	/**
247
	 * Option to enable the behavior of nodes registering as 
248
249
250
251
252
253
254
255
	 * topology providers.
	 * 
	 * @param registerAsLocationProviderInSiS
	 */
	public void setRegisterAsInformationProviderInSiS(
			boolean registerAsInformationProviderInSiS) {
		this.registerAsInformationProviderInSiS = registerAsInformationProviderInSiS;
	}
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283

	/**
	 * Allows GNP-based strategies to retrieve the unique
	 * {@link NetMeasurementDB.Host} - as this object should only be created
	 * once per host, it is maintained in this static manner.
	 * 
	 * @param host
	 * @return
	 */
	public static NetMeasurementDB.Host getMeasurementDBHost(SimHost host) {
		if (measurementDB == null) {
			throw new AssertionError();
		}
		return measurementDBHosts.get(host);
	}

	/**
	 * The Measurement-DB
	 * 
	 * @return
	 */
	public static NetMeasurementDB getMeasurementDB() {
		if (measurementDB == null) {
			throw new AssertionError();
		}
		return measurementDB;
	}

284
285
286
	public ObstacleModel getObstacleModel() {
		return obstacleModel;
	}
Julian Zobel's avatar
Julian Zobel committed
287
288
289
290
	
	public void setIsUAVComponent(boolean uav) {
		this.isUAVComponent = uav;		
	}
291
}