TopologyFactory.java 9.2 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
/*
 * 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;
39
import de.tud.kom.p2psim.impl.topology.component.BaseTopologyComponent;
40
41
import de.tud.kom.p2psim.impl.topology.component.DefaultTopologyComponent;
import de.tud.kom.p2psim.impl.topology.component.UAVTopologyComponent;
42
import de.tud.kom.p2psim.impl.topology.movement.AbstractWaypointMovementModel;
43
import de.tud.kom.p2psim.impl.topology.movement.NoMovement;
44
import de.tud.kom.p2psim.impl.topology.placement.GNPPlacement;
45
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
46
import de.tud.kom.p2psim.impl.topology.views.latency.GNPLatency;
47
import de.tudarmstadt.maki.simonstrator.api.Binder;
48
49
50
51
52
53
54
55
56
57
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
58
59
 * @author Bjoern Richerzhagen, Julian Zobel
 * @version 1.1, 06.09.2018
60
61
62
63
64
65
66
67
68
69
70
71
72
 */
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
	 */
73
	private MovementModel movement = new NoMovement();
74
75
76
77

	private WaypointModel waypointModel;

	private ObstacleModel obstacleModel;
78
79
	
	private boolean registerAsInformationProviderInSiS = false;
80
81
82
83
84
85
86
87

	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;
88

89
90
91
	private boolean alreadyCreatedInstances = false;

	private boolean alreadyAddedMovement = false;
Julian Zobel's avatar
Julian Zobel committed
92
	
93
	// FIXME this can be inconsistent (both true e.g) and should be refactored
Julian Zobel's avatar
Julian Zobel committed
94
	private boolean isUAVComponent = false;
95
96
	
	private boolean isBaseStationComponent = false;
97
98
99
100
101
102

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

	@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);
		}
133

134
135
136
137
		/*
		 * Create a TopologyComponent and register it with the Topology and its
		 * movement model.
		 */
Julian Zobel's avatar
Julian Zobel committed
138
139
140
141
142
143
144
145
146
		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);
		}
147
148
149
		else if(isBaseStationComponent) {			
			toCo = new BaseTopologyComponent(host, topo, movement, placement, registerAsInformationProviderInSiS);
		}
Julian Zobel's avatar
Julian Zobel committed
150
151
152
153
154
		else {
			toCo = new DefaultTopologyComponent(host, topo, movement, placement, registerAsInformationProviderInSiS);
		}
	
		
155
156
157
158
159
160
161
162
		/*
		 * 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);
163
			}
164
		}
165

166
167
		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
168
				host.getHostId(), placement, movement);
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
245
246
247
248
249
250
251
252
253
254
		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;
	}
255
256
	
	/**
257
	 * Option to enable the behavior of nodes registering as 
258
259
260
261
262
263
264
265
	 * topology providers.
	 * 
	 * @param registerAsLocationProviderInSiS
	 */
	public void setRegisterAsInformationProviderInSiS(
			boolean registerAsInformationProviderInSiS) {
		this.registerAsInformationProviderInSiS = registerAsInformationProviderInSiS;
	}
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293

	/**
	 * 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;
	}

294
295
296
	public ObstacleModel getObstacleModel() {
		return obstacleModel;
	}
Julian Zobel's avatar
Julian Zobel committed
297
298
299
300
	
	public void setIsUAVComponent(boolean uav) {
		this.isUAVComponent = uav;		
	}
301
302
303
304
	
	public void setIsBaseStationComponent(boolean uav) {
		this.isBaseStationComponent = uav;		
	}
305
}