/*
 * 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.views;

import de.tud.kom.p2psim.api.linklayer.mac.MacAddress;
import de.tud.kom.p2psim.impl.topology.views.wifi.WifiTopologyView;
import de.tud.kom.p2psim.impl.topology.views.wifi.phy.InterferenceHelper;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;

/**
 * A Link that sets isConnected() and some other parameters based on the current
 * positions of source and destination. It supports obstacles in that a link is
 * not connected as soon as it intersects an obstacle
 * 
 * @author Bjoern Richerzhagen
 * @version 1.0, 07.03.2012
 */
public class RangedLink extends DefaultLink {

	private double currentDistance = 0;

	private Location sourceLocation;

	private Location destLocation;

	WifiTopologyView wifiTopologyView;

	/**
	 * 
	 * @param source
	 * @param destination
	 * @param isConnected
	 * @param dropProbability
	 * @param bandwidth
	 * @param latency
	 * @param mtu
	 * @param maxDistance
	 */
	public RangedLink(MacAddress source, MacAddress destination,
			Location sourceLocation, Location destLocation, boolean isConnected,
			double dropProbability, long bandwidth, long latency, int mtu,
			WifiTopologyView wifiTopologyView) {
		super(source, destination, isConnected, dropProbability, bandwidth,
				latency, mtu);
		this.setSourceLocation(sourceLocation);
		this.setDestLocation(destLocation);
		this.wifiTopologyView = wifiTopologyView;
	}

	/**
	 * Update the distance between source and destination after movement
	 * occurred.
	 * 
	 * @param distance
	 */
	public void updateNodeDistance(double distance, boolean connected) {
		this.currentDistance = distance;
		setConnected(connected);
	}

	@Override
	public boolean isConnected() {
		return wifiTopologyView.inSendRange(sourceLocation, destLocation);
	}

	@Override
	public String toString() {
		return super.toString() + " distance: " + currentDistance;
	}

	public Location getSourceLocation() {
		return sourceLocation;
	}

	public void setSourceLocation(Location sourceLocation) {
		this.sourceLocation = sourceLocation;
	}

	public Location getDestLocation() {
		return destLocation;
	}

	public void setDestLocation(Location destLocation) {
		this.destLocation = destLocation;
	}

	public double getNodeDistance() {
		return currentDistance;
	}

}
