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


22
23
24

package de.tud.kom.p2psim.impl.network.simple;

25
26
27
28
29
30
31
32
33
import de.tud.kom.p2psim.api.analyzer.MessageAnalyzer.Reason;
import de.tud.kom.p2psim.api.common.SimHost;
import de.tud.kom.p2psim.api.network.BandwidthImpl;
import de.tud.kom.p2psim.api.network.NetMessage;
import de.tud.kom.p2psim.api.network.NetProtocol;
import de.tud.kom.p2psim.api.transport.TransProtocol;
import de.tud.kom.p2psim.impl.network.AbstractNetLayer;
import de.tud.kom.p2psim.impl.transport.AbstractTransMessage;
import de.tudarmstadt.maki.simonstrator.api.Message;
34
import de.tudarmstadt.maki.simonstrator.api.Monitor;
35
import de.tudarmstadt.maki.simonstrator.api.component.network.NetID;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;

public class SimpleNetLayer extends AbstractNetLayer {

	private double currentDownBandwidth;

	private double currentUpBandwidth;

	private final SimpleSubnet subNet;

	public SimpleNetLayer(SimHost host, SimpleSubnet subNet, SimpleNetID netID,
			Location netPosition, BandwidthImpl bandwidth) {
		super(host, netID, bandwidth, netPosition, null);
		this.subNet = subNet;
		subNet.registerNetLayer(this);
	}

	public boolean isSupported(TransProtocol transProtocol) {
		if (transProtocol.equals(TransProtocol.UDP))
			return true;
		else
			return false;
	}

	public void send(Message msg, NetID receiver, NetProtocol netProtocol) {
		// outer if-else-block is used to avoid sending although the host is
		// offline
		if (this.isOnline()) {
			TransProtocol usedTransProtocol = ((AbstractTransMessage) msg)
					.getProtocol();
			if (this.isSupported(usedTransProtocol)) {
67
				NetMessage netMsg = new SimpleNetMessage(msg, receiver,
68
						getLocalInetAddress(),
69
70
71
72
						netProtocol);
				if (hasAnalyzer) {
					netAnalyzerProxy
							.netMsgEvent(netMsg, getHost(), Reason.SEND);
73
74
75
76
77
78
79
80
81
				}
				subNet.send(netMsg);
			} else {
				throw new IllegalArgumentException("Transport protocol "
						+ usedTransProtocol
						+ " not supported by this NetLayer implementation.");
			}
		} else {
			int assignedMsgId = subNet.determineTransMsgNumber(msg);
82
			Monitor.log(SimpleNetLayer.class, Monitor.Level.DEBUG,
83
84
85
					"During send: Assigning MsgId " + assignedMsgId
					+ " to dropped message");
			((AbstractTransMessage) msg).setCommId(assignedMsgId);
86
			NetMessage netMsg = new SimpleNetMessage(msg, receiver,
87
					getLocalInetAddress(),
88
89
90
					netProtocol);
			if (hasAnalyzer) {
				netAnalyzerProxy.netMsgEvent(netMsg, getHost(), Reason.DROP);
91
92
93
94
95
96
97
			}
		}

	}

	@Override
	public String toString() {
98
		return "NetLayer(netID=" + getLocalInetAddress() + ", "
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
				+ (isOffline() ? "online" : "offline") + ")";
	}

	@Override
	public int hashCode() {
		final int PRIME = 31;
		int result = 1;
		long temp;
		temp = Double.doubleToLongBits(currentDownBandwidth);
		result = PRIME * result + (int) (temp ^ (temp >>> 32));
		temp = Double.doubleToLongBits(currentUpBandwidth);
		result = PRIME * result + (int) (temp ^ (temp >>> 32));
		result = PRIME * result + ((subNet == null) ? 0 : subNet.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final SimpleNetLayer other = (SimpleNetLayer) obj;
		if (Double.doubleToLongBits(currentDownBandwidth) != Double
				.doubleToLongBits(other.currentDownBandwidth))
			return false;
		if (Double.doubleToLongBits(currentUpBandwidth) != Double
				.doubleToLongBits(other.currentUpBandwidth))
			return false;
		if (subNet == null) {
			if (other.subNet != null)
				return false;
		} else if (!subNet.equals(other.subNet))
			return false;
		return true;
	}

}