AbstractTopologyComponent.java 10.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * 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/>.
 *
 */

21
package de.tud.kom.p2psim.impl.topology.component;
22
23
24
25
26
27
28
29
30
31
32
33

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import de.tud.kom.p2psim.api.common.SimHost;
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.placement.PlacementModel;
34
35
36
import de.tud.kom.p2psim.impl.topology.util.LocalGraphView;
import de.tud.kom.p2psim.impl.topology.util.LocationRequestImpl;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import de.tudarmstadt.maki.simonstrator.api.common.graph.Graph;
import de.tudarmstadt.maki.simonstrator.api.common.graph.IEdge;
import de.tudarmstadt.maki.simonstrator.api.common.graph.INode;
import de.tudarmstadt.maki.simonstrator.api.common.graph.INodeID;
import de.tudarmstadt.maki.simonstrator.api.component.ComponentNotAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.LocationListener;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.LocationRequest;
import de.tudarmstadt.maki.simonstrator.api.component.sis.SiSComponent;
import de.tudarmstadt.maki.simonstrator.api.component.sis.SiSDataCallback;
import de.tudarmstadt.maki.simonstrator.api.component.sis.exception.InformationNotAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.sis.type.SiSTypes;
import de.tudarmstadt.maki.simonstrator.api.component.sis.SiSInformationProvider.SiSProviderHandle;
import de.tudarmstadt.maki.simonstrator.api.component.sis.SiSInfoProperties;
import de.tudarmstadt.maki.simonstrator.api.Event;
import de.tudarmstadt.maki.simonstrator.api.EventHandler;
import de.tudarmstadt.maki.simonstrator.api.component.sis.util.SiSTopologyProvider;
import de.tudarmstadt.maki.simonstrator.api.component.topology.TopologyID;
import de.tudarmstadt.maki.simonstrator.api.component.network.NetworkComponent.NetInterfaceName;

Julian Zobel's avatar
Julian Zobel committed
57
58
59
60
/**
 * Abstract implementation of the topology component interface, to support basic 
 * functionalities to all topology components.
 * 
Julian Zobel's avatar
Julian Zobel committed
61
 * @author Julian Zobel, others before
Julian Zobel's avatar
Julian Zobel committed
62
63
 * @version 1.0, 6 Sep 2018
 */
64
65
66
67
public abstract class AbstractTopologyComponent implements TopologyComponent {
	
	protected SimHost host;	
	protected final PositionVector position;
68
	protected final Double POSITION_REQUEST_UPDATE_ACCURACY = 0.1; // accuracy for location request updates
69
70
71
72
73
74
75
76
77
78
	protected Topology topology;
	
	protected MovementModel movementModel;
	protected PlacementModel placementModel;
	
	private final boolean registerAsInformationProviderInSiS;
	
	private Map<LocationListener, LocationRequestImpl> openRequests = new LinkedHashMap<LocationListener, LocationRequestImpl>();
	private List<LocationListener> listeners = new LinkedList<>();
	
Julian Zobel's avatar
Julian Zobel committed
79
	//
80
81
82
83
84
85
86
87
88
	public AbstractTopologyComponent(SimHost host, Topology topology, MovementModel movementModel, PlacementModel placementModel, boolean registerAsInformationProviderInSiS) {
		this.topology = topology;
		this.host = host;
		this.position = new PositionVector(0, 0);

		this.movementModel = movementModel;
		if (this.movementModel != null) {
			this.movementModel.addComponent(this);
		}
89
		
90
91
92
93
94
95
96
97
98
99
100
101
102
		this.placementModel = placementModel;
		if (this.placementModel != null) {
			this.placementModel.addComponent(this);
		}
		this.registerAsInformationProviderInSiS = registerAsInformationProviderInSiS;
	}
	
	@Override
	public void initialize()  {		
				
		topology.addComponent(this);
		movementModel.placeComponent(this);

103
104
		if (placementModel != null) {			
			// retrieve the initial position of this topology component 
105
			position.set(placementModel.place(this));				
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
210
211
212
213
		}

		if (registerAsInformationProviderInSiS) {
			try {
				final SiSComponent sis = host.getComponent(SiSComponent.class);
				sis.provide().nodeState(SiSTypes.PHY_LOCATION,
						new SiSDataCallback<Location>() {

							Set<INodeID> localID = INodeID
									.getSingleIDSet(getHost().getId());

							@Override
							public Location getValue(INodeID nodeID,
									SiSProviderHandle providerHandle)
									throws InformationNotAvailableException {
								if (nodeID.equals(getHost().getId())) {
									return getLastLocation();
								} else {
									throw new InformationNotAvailableException();
								}
							}

							@Override
							public Set<INodeID> getObservedNodes() {
								return localID;
							}

							@Override
							public SiSInfoProperties getInfoProperties() {
								return new SiSInfoProperties();
							}
						});

				sis.provide().nodeState(SiSTypes.SPEED,
						new SiSDataCallback<Double>() {

							Set<INodeID> localID = INodeID
									.getSingleIDSet(getHost().getId());

							@Override
							public Double getValue(INodeID nodeID,
									SiSProviderHandle providerHandle)
									throws InformationNotAvailableException {
								if (nodeID.equals(getHost().getId())) {
									return getMovementSpeed();
								} else {
									throw new InformationNotAvailableException();
								}
							}

							@Override
							public Set<INodeID> getObservedNodes() {
								return localID;
							}

							@Override
							public SiSInfoProperties getInfoProperties() {
								return new SiSInfoProperties();
							}
						});

				// Provide Underlay topology
				Event.scheduleImmediately(new EventHandler() {

					@Override
					public void eventOccurred(Object content, int type) {
						if (getHost().getLinkLayer().hasPhy(PhyType.WIFI)) {
							new SiSTopologyProvider(sis,
									SiSTypes.NEIGHBORS_WIFI,
									AbstractTopologyComponent.this,
									getTopologyID(NetInterfaceName.WIFI, true),
									AbstractTopologyComponent.class);
						}
					}
				}, null, 0);

			} catch (ComponentNotAvailableException e) {
				// OK
			}
		}
	}
	
	@Override
	public void shutdown() {
		topology = null;
		host = null;
		movementModel = null;
	}
	
	@Override
	public SimHost getHost() {
		return host;
	}

	@Override
	public Topology getTopology() {
		return topology;
	}
	
	/**
	 * Access to the movement model
	 * @return
	 */
	public MovementModel getMovementModel() {
		return movementModel;
	}
	
	@Override
Julian Zobel's avatar
Julian Zobel committed
214
	public Location getLastLocation() { 
215
216
217
218
219
220
221
222
223
		/*
		 * As we want to mimic real world behavior, the current position
		 * snapshot is cloned to prevent information propagation due to Java.
		 */
		return position.clone();
	}
	
	@Override
	public PositionVector getRealPosition() {
224
		return position; // no copy! See SimLocationActuator Interface Description!
225
226
227
	}	
	
	@Override
228
	public void updateCurrentLocation(Location location) {	
229
		
230
231
232
233
		boolean informListeners = false;
		// only inform listeners if the location has changed
		if(location.distanceTo(position) >= POSITION_REQUEST_UPDATE_ACCURACY) {
			informListeners = true;
234
		}
235
236
237
238
239
240
241
242
				
		position.set(location);
		
		if(informListeners) {
			for (LocationListener locationListener : listeners) {				
				locationListener.onLocationChanged(getHost(), getLastLocation());
			}
		}		
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
	}
	
	@Override
	public void requestLocationUpdates(LocationRequest request,
			LocationListener listener) {
		if (openRequests.containsKey(listener)) {
			throw new AssertionError(
					"This LocationListener is already in use.");
		}
		if (request == null) {
			/*
			 * This listener wants to be triggered on EVERY position update, but
			 * it does not want to request position updates.
			 */
			if (!listeners.contains(listener)) {
				listeners.add(listener);
			}
		} else {
			/*
			 * Listener has its own request timing.
			 */
			LocationRequestImpl req = (LocationRequestImpl) request;
			openRequests.put(listener, req);
			req.immunizeAndStart(listener);
		}
	}

	@Override
	public void removeLocationUpdates(LocationListener listener) {
		listeners.remove(listener);
		LocationRequestImpl impl = openRequests.remove(listener);
		if (impl != null) {
			impl.cancel(listener);
		}
	}

	@Override
	public LocationRequest getLocationRequest() {
		return new LocationRequestImpl(getHost(), this);
	}
	
	/*
	 * Methods for the Graph Interface
	 */

	/**
	 * Graph views: static, as we use global knowledge and maintain one shared
	 * graph (potentially with partitions!)
	 */
	private final static LinkedHashMap<TopologyID, LocalGraphView> graphViews = new LinkedHashMap<>();

	@Override
	public TopologyID getTopologyID(NetInterfaceName netName,
			boolean onlyOnline) {
		TopologyID id = TopologyID.getIdentifier(
				netName.toString() + (onlyOnline ? "-online" : "-all"),
				DefaultTopologyComponent.class);
		if (!this.graphViews.containsKey(id)) {
			this.graphViews.put(id, new LocalGraphView(netName, onlyOnline, topology));
		}
		return id;
	}

	@Override
	public TopologyID getTopologyID(NetInterfaceName netName,
			boolean onlyOnline, double range) {
		TopologyID id = TopologyID.getIdentifier(
				netName.toString() + (onlyOnline ? "-online" : "-all")
				+ String.valueOf(range),
				DefaultTopologyComponent.class);
		if (!this.graphViews.containsKey(id)) {
			this.graphViews.put(id,
					new LocalGraphView(netName, onlyOnline, range, topology));
		}
		return id;
	}

	@Override
	public INode getNode(TopologyID identifier) {
		assert graphViews.containsKey(identifier);
		return graphViews.get(identifier).getOwnNode(host);
	}

	@Override
	public Set<IEdge> getNeighbors(TopologyID topologyIdentifier) {
		assert graphViews.containsKey(topologyIdentifier);
		return graphViews.get(topologyIdentifier).getNeighbors(host);
	}

	@Override
	public Graph getLocalView(TopologyID topologyIdentifier) {
		assert graphViews.containsKey(topologyIdentifier);
		return graphViews.get(topologyIdentifier).getLocalView();
	}

	@Override
	public Iterable<TopologyID> getTopologyIdentifiers() {
		return graphViews.keySet();
	}


	@Override
	public String toString() {
346
		return "Topology Component #" + getHost().getId() + " at pos " + position.toString();
347
348
	}	
}