ModularLinkLayer.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
39
40
41
42
43
44
45
46
47
/*
 * 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.linklayer;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import de.tud.kom.p2psim.api.analyzer.LinklayerAnalyzer;
import de.tud.kom.p2psim.api.analyzer.MessageAnalyzer.Reason;
import de.tud.kom.p2psim.api.common.SimHost;
import de.tud.kom.p2psim.api.linklayer.LinkLayer;
import de.tud.kom.p2psim.api.linklayer.LinkLayerMessage;
import de.tud.kom.p2psim.api.linklayer.LinkMessageEvent;
import de.tud.kom.p2psim.api.linklayer.LinkMessageListener;
import de.tud.kom.p2psim.api.linklayer.MacStateListener;
import de.tud.kom.p2psim.api.linklayer.mac.MacAddress;
import de.tud.kom.p2psim.api.linklayer.mac.MacLayer;
import de.tud.kom.p2psim.api.linklayer.mac.PhyType;
import de.tud.kom.p2psim.api.network.BandwidthImpl;
import de.tud.kom.p2psim.api.scenario.ConfigurationException;
import de.tud.kom.p2psim.impl.network.IPv4NetID;
import de.tud.kom.p2psim.impl.util.LiveMonitoring;
import de.tud.kom.p2psim.impl.util.LiveMonitoring.ProgressValue;
import de.tud.kom.p2psim.impl.util.toolkits.NumberFormatToolkit;
import de.tudarmstadt.maki.simonstrator.api.Message;
import de.tudarmstadt.maki.simonstrator.api.Monitor;
48
import de.tudarmstadt.maki.simonstrator.api.Monitor.Level;
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
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
import de.tudarmstadt.maki.simonstrator.api.component.core.MonitorComponent.AnalyzerNotAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.network.NetID;
import de.tudarmstadt.maki.simonstrator.api.component.network.NetInterface;

/**
 * Basic building-block for a LinkLayer. It may contain multiple MAC-Layers as
 * foreseen by 802.xx. There should be no need to specify additional link
 * layers, as all the magic happens inside the MAC. This is just a simple
 * dispatcher to select the right MAC.
 * 
 * LinkLayers are created and configured using the {@link LinkLayerFactory}.
 * This LinkLayer is to be extended for more advanced functionality or in-depth
 * analyzing to keep the main code sleek and fast. Methods declared as final are
 * not to be altered to ensure correct functionality.
 * 
 * @author Bjoern Richerzhagen
 * @version 1.0, 21.02.2012
 */
public class ModularLinkLayer implements LinkLayer, LinkMessageListener {

	/**
	 * All Mac-Layers of this Host
	 */
	private Map<PhyType, MacLayer> macLayers;

	/**
	 * All MessageListeners
	 */
	private List<LinkMessageListener> messageListeners;

	/**
	 * All MacStateLIsteners
	 */
	private List<MacStateListener> macStateListeners;
	
	/**
	 * All ConnectivityListeners
	 */
	// private List<ConnectivityListener> connectivityListeners;

	private static Map<NetID, MacAddress> addressResolution = new HashMap<NetID, MacAddress>();

	/**
	 * Host this LinkLayer belongs to.
	 */
	private SimHost host;

	/*
	 * Analyzing
	 */
	public static long _linkBroadcastSent, _linkUnicastSent, _linkUnicastRcvd,
			_linkBroadcastRcvd, _linkDropped = 0;

	public static boolean _analyzersInitialized = false;


	/**
	 * 
	 */
	public ModularLinkLayer(SimHost host) {
		this.host = host;
		macLayers = new LinkedHashMap<PhyType, MacLayer>();
		messageListeners = new LinkedList<LinkMessageListener>();
		macStateListeners = new LinkedList<MacStateListener>();

		if (!_analyzersInitialized) {
			LiveMonitoring
					.addProgressValueIfNotThere(new LinkMessagesSentProgress());
			LiveMonitoring
					.addProgressValueIfNotThere(new LinkMessagesReceivedProgress());
			LiveMonitoring
					.addProgressValueIfNotThere(new LinkMessagesDroppedProgress());
			_analyzersInitialized = true;
		}
	}

	@Override
	public void initialize() {
		/*
		 * Initialize MACs
		 */
		for (MacLayer mac : macLayers.values()) {
			mac.initialize();
			/*
			 * ARP
			 */
			NetInterface net = host.getNetworkComponent().getByName(
					mac.getPhyType().getNetInterfaceName());
			assert !addressResolution.containsKey(net.getLocalInetAddress());
			addressResolution.put(net.getLocalInetAddress(),
					mac.getMacAddress());
		}
	}

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

	@Override
	public MacAddress addressResolution(NetID netID, PhyType phy) {
		/*
		 * This implementation does not really use ARP, but instead relies on a
		 * global table of all MacAddresses
		 */
		if (netID.equals(IPv4NetID.LOCAL_BROADCAST)) {
			return MacAddress.BROADCAST;
		}

		return addressResolution.get(netID);
	}


	@Override
	public final void addMacLayer(MacLayer macLayer) {
		// prevent duplicate MACs for one PHY
		if (macLayers.containsKey(macLayer.getPhyType())) {
			throw new ConfigurationException(
					"You configured two MACs with the same PhyType!");
		}
		// we register this LinkLayer as a Listener for the MAC
		macLayer.setMessageListener(this);
		macLayers.put(macLayer.getPhyType(), macLayer);
	}

	@Override
	public final SimHost getHost() {
		return host;
	}

	@Override
	public boolean hasPhy(PhyType phyType) {
		return macLayers.containsKey(phyType);
	}

	@Override
	public MacLayer getMac(PhyType phyType) {
		return macLayers.get(phyType);
	}

	@Override
	public void goOffline(PhyType phyType) {
		MacLayer activeMac = macLayers.get(phyType);
		if (activeMac == null) {
			throw new UnsupportedOperationException("There is no MAC for "
					+ phyType.toString()
					+ " on this Host. Your NetLayer messed something up!");
		}

		if (activeMac.isOnline()) {
			activeMac.goOffline();
			for (MacStateListener listener : macStateListeners) {
				listener.goneOffline(phyType);
			}
		} else {
205
206
			Monitor.log(ModularLinkLayer.class, Level.DEBUG,
					"Mac was already offline...");
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
		}
	}

	@Override
	public void goOnline(PhyType phyType) {
		MacLayer activeMac = macLayers.get(phyType);
		if (activeMac == null) {
			throw new UnsupportedOperationException("There is no MAC for "
					+ phyType.toString()
					+ " on this Host. Your NetLayer messed something up!");
		}
		if (!activeMac.isOnline()) {
			activeMac.goOnline();
			for (MacStateListener listener : macStateListeners) {
				listener.goneOnline(phyType);
			}
		} else {
224
225
			Monitor.log(ModularLinkLayer.class, Level.DEBUG,
					"Mac was already online...");
226
227
228
229
230
231
232
		}
	}

	@Override
	public boolean isOnline(PhyType phyType) {
		MacLayer activeMac = macLayers.get(phyType);
		if (activeMac == null) {
233
234
235
			Monitor.log(ModularLinkLayer.class, Level.WARN,
					"A MAC with the type " + phyType.toString()
							+ " is not configured.");
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
381
382
383
384
385
386
387
388
389
390
391
392
393
			return false;
		}
		return activeMac.isOnline();
	}

	@Override
	public void send(PhyType phyType, MacAddress destination, Message data) {
		MacLayer activeMac = macLayers.get(phyType);
		if (activeMac != null) {
			/*
			 * here we might add Analyzers/Inform the Monitor
			 */
			LinkLayerMessage lMsg = new DefaultLinkLayerMessage(data,
					activeMac.getMacAddress(), destination);

			if (destination.isBroadcast()) {
				_linkBroadcastSent++;
			} else {
				_linkUnicastSent++;
			}
			activeMac.send(destination, lMsg);
		} else {
			throw new UnsupportedOperationException("There is no MAC for "
					+ phyType.toString()
							+ " on this Host. Your NetLayer messed something up!");
		}
	}

	@Override
	public void messageArrived(LinkMessageEvent linkMsgEvent) {
		/*
		 * Notify Listeners. Every message that is received at a MAC and is not
		 * a pure MAC-Message is dispatched through this function! Here we might
		 * add Analyzers ;)
		 */
		if (linkMsgEvent.isBroadcast()) {
			_linkBroadcastRcvd++;
		} else {
			_linkUnicastRcvd++;
		}
		_linkMsgEvent(linkMsgEvent.getLinkLayerMessage(), getHost(),
				Reason.RECEIVE);
		for (LinkMessageListener listener : messageListeners) {
			listener.messageArrived(linkMsgEvent);
		}
	}

	@Override
	public final void addLinkMessageListener(LinkMessageListener listener) {
		messageListeners.add(listener);
	}

	@Override
	public final void removeLinkMessageListener(LinkMessageListener listener) {
		messageListeners.remove(listener);
	}

	@Override
	public BandwidthImpl getCurrentBandwidth(PhyType phy) {
		return getMac(phy).getCurrentBandwidth();
	}

	@Override
	public BandwidthImpl getMaxBandwidth(PhyType phy) {
		return getMac(phy).getMaxBandwidth();
	}

	// @Override
	// public final void addConnectivityListener(ConnectivityListener listener)
	// {
	// connectivityListeners.add(listener);
	// }
	//
	// @Override
	// public final void removeConnectivityListener(ConnectivityListener
	// listener) {
	// connectivityListeners.remove(listener);
	// }

	public class LinkMessagesSentProgress implements ProgressValue {

		@Override
		public String getName() {
			return "Link sent (U/B/T)";
		}

		@Override
		public String getValue() {
			return Long.toString(_linkUnicastSent) + " / "
					+ Long.toString(_linkBroadcastSent) + " / "
					+ Long.toString(_linkUnicastSent + _linkBroadcastSent);
		}

	}

	public class LinkMessagesReceivedProgress implements ProgressValue {

		@Override
		public String getName() {
			return "Link rcvd (U/B/T), drop (U)";
		}

		@Override
		public String getValue() {
			return Long.toString(_linkUnicastRcvd)
					+ " / "
					+ Long.toString(_linkBroadcastRcvd)
					+ " / "
					+ Long.toString(_linkUnicastRcvd + _linkBroadcastRcvd)
					+ ", drop: "
					+ NumberFormatToolkit.formatPercentage(_linkDropped
							/ (double) _linkUnicastSent, 3);
		}

	}

	public class LinkMessagesDroppedProgress implements ProgressValue {

		@Override
		public String getName() {
			return "Link dropped";
		}

		@Override
		public String getValue() {
			return Long.toString(_linkDropped);
		}

	}

	@Override
	public void addMacStateListener(MacStateListener listener) {
		macStateListeners.add(listener);
	}

	@Override
	public void removeMacStateListener(MacStateListener listener) {
		macStateListeners.remove(listener);
	}
	
	private static boolean _linkAnalyzerInitialized = false;
	private static LinklayerAnalyzer _linkAnalyzer = null;
	
	protected static void _linkMsgEvent(LinkLayerMessage msg, SimHost host,
			Reason reason) {
		if (!_linkAnalyzerInitialized) {
			try {
				_linkAnalyzer = Monitor.get(LinklayerAnalyzer.class);
			} catch (AnalyzerNotAvailableException e) {
				_linkAnalyzer = null;
			}
			_linkAnalyzerInitialized = true;
		}
		if (_linkAnalyzerInitialized && _linkAnalyzer != null) {
			_linkAnalyzer.linkMsgEvent(msg, host, reason);
		}
	}
}