AbstractNetLayer.java 10.8 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-2011 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.network;

import java.util.LinkedList;
import java.util.List;

import de.tud.kom.p2psim.api.analyzer.ConnectivityAnalyzer;
import de.tud.kom.p2psim.api.analyzer.MessageAnalyzer.Reason;
import de.tud.kom.p2psim.api.analyzer.NetlayerAnalyzer;
import de.tud.kom.p2psim.api.common.SimHost;
import de.tud.kom.p2psim.api.linklayer.mac.PhyType;
import de.tud.kom.p2psim.api.network.NetMessage;
import de.tud.kom.p2psim.api.network.NetMessageListener;
import de.tud.kom.p2psim.api.network.NetMsgEvent;
import de.tud.kom.p2psim.api.network.SimNetInterface;
import de.tud.kom.p2psim.api.network.SimNetworkComponent;
import de.tud.kom.p2psim.api.transport.TransProtocol;
import de.tud.kom.p2psim.impl.network.modular.db.NetMeasurementDB;
import de.tudarmstadt.maki.simonstrator.api.Monitor;
39
import de.tudarmstadt.maki.simonstrator.api.Monitor.Level;
40
41
42
43
import de.tudarmstadt.maki.simonstrator.api.component.core.MonitorComponent.AnalyzerNotAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.network.Bandwidth;
import de.tudarmstadt.maki.simonstrator.api.component.network.NetID;
import de.tudarmstadt.maki.simonstrator.api.component.network.NetInterface;
44
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
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
import de.tudarmstadt.maki.simonstrator.api.component.transport.ConnectivityListener;

/**
 * This abstract class provides a skeletal implementation of the
 * <code>StandaloneNetLayer<code> interface to lighten the effort for implementing this interface.
 * 
 * All Netlayers implementing this abstract base class can only provide exactly one {@link NetInterface}, 
 * i.e., they do not have multiple communication interfaces. For simulations with multiple interfaces, 
 * you need to use the routedNetLayer and corresponding Linklayers.
 * 
 * @author Sebastian Kaune
 * @author Konstantin Pussep
 * @version 3.0, 11/29/2007
 * 
 */
public abstract class AbstractNetLayer implements SimNetworkComponent,
		SimNetInterface {

	private final List<NetMessageListener> msgListeners;

	private final List<ConnectivityListener> connListeners;

	private final NetID myID;

	protected NetMeasurementDB.Host hostMeta;

	private boolean online;

73
	private Location position;
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

	Bandwidth currentBandwidth;

	Bandwidth maxBandwidth;

	private final SimHost host;

	protected boolean hasAnalyzer = false;

	protected NetlayerAnalyzer netAnalyzerProxy;

	/**
	 * Abstract constructor called by a subclass of this instance
	 * 
	 * @param maxDownBandwidth
	 *            the maximum physical download bandwidth
	 * @param maxUpBandwidth
	 *            the maximum physical upload bandwidth
	 * @param position
	 *            the NetPosition of the network layer
	 */
	public AbstractNetLayer(SimHost host, NetID netId, Bandwidth maxBandwidth,
96
			Location position, NetMeasurementDB.Host hostMeta) {
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
		this.myID = netId;
		this.msgListeners = new LinkedList<NetMessageListener>();
		this.connListeners = new LinkedList<ConnectivityListener>();
		this.maxBandwidth = maxBandwidth;
		this.currentBandwidth = maxBandwidth.clone();
		this.position = position;
		this.hostMeta = hostMeta;
		this.host = host;
	}

	@Override
	public void initialize() {
		/*
		 * Bind the analyzer-Proxy
		 */
		try {
			netAnalyzerProxy = Monitor.get(NetlayerAnalyzer.class);
			hasAnalyzer = true;
		} catch (AnalyzerNotAvailableException e) {
			// no analyzer, no problem
		}

	}

	@Override
	public void shutdown() {
		throw new AssertionError(
				"You are not supposed to shutdown this component.");
	}

	/**
	 * This message is called by the subnet to deliver a new NetMessage to a
	 * remote NetLayer. (@see de.tud.kom.p2psim.impl.network.AbstractSubnet).
	 * Calling this method informs further all registered NetMsgListeners about
	 * the receipt of this NetMessage using a appropriate NetMsgEvent.
	 * 
	 * @param message
	 *            The NetMessage that was received by the NetLayer.
	 */
	public void receive(NetMessage message) {
		if (this.isOnline()) {

			// log.info(Simulator.getSimulatedRealtime() + " Receiving " +
			// message);

			if (hasAnalyzer) {
				netAnalyzerProxy
						.netMsgEvent(message, getHost(),
					Reason.RECEIVE);
			}
			NetMsgEvent event = new NetMsgEvent(message, this);
			if (msgListeners == null || msgListeners.isEmpty()) {
				if (hasAnalyzer) {
					netAnalyzerProxy.netMsgEvent(message, getHost(),
						Reason.DROP);
				}
153
154
				Monitor.log(AbstractNetLayer.class, Level.WARN,
						"Cannot deliver message "
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
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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
						+ message.getPayload() + " at netID=" + myID
						+ " as no message msgListeners registered");
			} else {
				for (NetMessageListener listener : msgListeners) {
					listener.messageArrived(event);
				}
			}
		} else {
			if (hasAnalyzer) {
				netAnalyzerProxy.netMsgEvent(message, getHost(), Reason.DROP);
			}
		}
	}

	/**
	 * Return whether the required transport protocol is supported by the given
	 * NetLayer instance
	 * 
	 * @param protocol
	 *            the required transport protocol
	 * @return true if supported
	 */
	protected abstract boolean isSupported(TransProtocol protocol);

	/**
	 * As the download bandwidth of a host might be shared between concurrently
	 * established connections, this method will be used by the subnet in order
	 * to adapt the current available download bandwidth.
	 * 
	 * @param currentDownBandwidth
	 *            the new available download bandwidth
	 */
	@Deprecated
	public void setCurrentDownBandwidth(long currentDownBandwidth) {
		this.currentBandwidth.setDownBW(currentDownBandwidth);
	}

	/**
	 * As the upload bandwidth of a host might be shared between concurrently
	 * established connections, this method will be used by the subnet in order
	 * to adapt the current available upload bandwidth.
	 * 
	 * @param currentUpBandwidth
	 *            the new available upload bandwidth
	 */
	@Deprecated
	public void setCurrentUpBandwidth(long currentUpBandwidth) {
		this.currentBandwidth.setUpBW(currentUpBandwidth);
	}

	public void setCurrentBandwidth(Bandwidth currentBandwidth) {
		this.currentBandwidth = currentBandwidth;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seede.tud.kom.p2psim.api.api.network.NetLayer#addNetMsgListener(
	 * NetMessageListener) listener)
	 */
	public void addNetMsgListener(NetMessageListener listener) {
		if (!this.msgListeners.contains(listener)) {
			this.msgListeners.add(listener);
		}
	}

	public List<NetMessageListener> getNetMsgListeners() {
		return this.msgListeners;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seede.tud.kom.p2psim.api.api.network.NetLayer#removeNetMsgListener(
	 * NetMessageListener) listener)
	 */
	public void removeNetMsgListener(NetMessageListener listener) {
		this.msgListeners.remove(listener);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.tud.kom.p2psim.api.api.network.NetLayer#getNetID()
	 */
	public NetID getNetID() {
		return this.myID;
	}

	@Override
	public NetID getLocalInetAddress() {
		return this.myID;
	}

	@Override
	public NetID getBroadcastAddress() {
		return IPv4NetID.LOCAL_BROADCAST;
	}

	@Override
	public SimNetInterface getByName(NetInterfaceName name) {
		if (name == NetInterfaceName.ETHERNET) {
			return this;
		} else {
			throw new AssertionError("This NetLayer supports only ETHERNET!");
		}
	}

	@Override
	public NetID getByName(String name) {
		return new IPv4NetID(name);
	}

	@Override
	public SimNetInterface getByNetId(NetID netID) {
		if (getLocalInetAddress().equals(netID)) {
			return this;
		} else {
			return null;
		}
	}

	@Override
	public Iterable<NetInterface> getNetworkInterfaces() {
		List<NetInterface> list = new LinkedList<NetInterface>();
		list.add(this);
		return list;
	}

	@Override
	public Iterable<SimNetInterface> getSimNetworkInterfaces() {
		List<SimNetInterface> list = new LinkedList<SimNetInterface>();
		list.add(this);
		return list;
	}

	@Override
	public NetInterfaceName getName() {
		return NetInterfaceName.ETHERNET;
	}

	@Override
	public int getMTU() {
		return PhyType.ETHERNET.getDefaultMTU();
	}

	@Override
	public boolean isUp() {
		return isOnline();
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see de.tud.kom.p2psim.api.api.network.NetLayer#goOffline()
	 */
	public void goOffline() {
		if (this.online) {
			this.online = false;
			for (ConnectivityListener connListener : connListeners) {
				connListener.wentOffline(host, this);
			}
			try {
				Monitor.get(ConnectivityAnalyzer.class).wentOffline(host);
			} catch (AnalyzerNotAvailableException e) {
				//
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.tud.kom.p2psim.api.api.network.NetLayer#goOnline()
	 */
	public void goOnline() {
		if (!this.online) {
			this.online = true;
			for (ConnectivityListener connListener : connListeners) {
				connListener.wentOnline(host, this);
			}
			try {
				Monitor.get(ConnectivityAnalyzer.class).wentOnline(host);
			} catch (AnalyzerNotAvailableException e) {
				//
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.tud.kom.p2psim.api.api.network.NetLayer#isOffline()
	 */
	@Override
	public boolean isOffline() {
		return !online;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.tud.kom.p2psim.api.api.network.NetLayer#isOnline()
	 */
	@Override
	public boolean isOnline() {
		return online;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.tud.kom.p2psim.api.common.Component#getHost()
	 */
	@Override
	public SimHost getHost() {
		return this.host;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.tud.kom.p2psim.api.api.network.NetLayer#getNetPosition()
	 */
	@Override
381
	public Location getNetPosition() {
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
		return this.position;
	}

	@Override
	public Bandwidth getCurrentBandwidth() {
		return currentBandwidth;
	}

	@Override
	public Bandwidth getMaxBandwidth() {
		return maxBandwidth;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * de.tud.kom.p2psim.api.network.NetLayer#removeConnectivityListener(de.
	 * tud.kom.p2psim.api.common.ConnectivityListener)
	 */
	@Override
	public void removeConnectivityListener(ConnectivityListener listener) {
		this.connListeners.remove(listener);
	}

	@Override
	public void addConnectivityListener(ConnectivityListener listener) {
		this.connListeners.add(listener);
	}

	/**
	 * Gets the dB host meta from NetMessurementDB
	 * 
	 * @return the dB host meta
	 */
	public NetMeasurementDB.Host getDBHostMeta() {
		return hostMeta;
	}

}