added support for new busy-wait radio interfaces (+fixed null pointer bug)
This commit is contained in:
parent
b25f16936b
commit
d6bbf0cf3d
1 changed files with 46 additions and 35 deletions
|
@ -26,7 +26,7 @@
|
||||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
* SUCH DAMAGE.
|
* SUCH DAMAGE.
|
||||||
*
|
*
|
||||||
* $Id: StandardRadioMedium.java,v 1.2 2006/10/02 15:19:28 fros4943 Exp $
|
* $Id: StandardRadioMedium.java,v 1.3 2006/10/05 07:53:06 fros4943 Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package se.sics.cooja.radiomediums;
|
package se.sics.cooja.radiomediums;
|
||||||
|
@ -69,8 +69,8 @@ public class StandardRadioMedium extends RadioMedium {
|
||||||
private Vector<Position> registeredPositions = new Vector<Position>();
|
private Vector<Position> registeredPositions = new Vector<Position>();
|
||||||
private Vector<Radio> registeredRadios = new Vector<Radio>();
|
private Vector<Radio> registeredRadios = new Vector<Radio>();
|
||||||
|
|
||||||
private Vector<Position> sendingPositions = new Vector<Position>();
|
private Vector<Position> newSendingPositions = new Vector<Position>();
|
||||||
private Vector<Radio> sendingRadios = new Vector<Radio>();
|
private Vector<Radio> newSendingRadios = new Vector<Radio>();
|
||||||
|
|
||||||
private static RadioMedium myRadioMedium;
|
private static RadioMedium myRadioMedium;
|
||||||
|
|
||||||
|
@ -221,6 +221,9 @@ public class StandardRadioMedium extends RadioMedium {
|
||||||
if (moteRadio.isTransmitting())
|
if (moteRadio.isTransmitting())
|
||||||
return new Color[]{Color.BLUE};
|
return new Color[]{Color.BLUE};
|
||||||
|
|
||||||
|
if (moteRadio.isInterfered())
|
||||||
|
return new Color[]{Color.RED};
|
||||||
|
|
||||||
if (moteRadio.isReceiving())
|
if (moteRadio.isReceiving())
|
||||||
return new Color[]{Color.GREEN};
|
return new Color[]{Color.GREEN};
|
||||||
|
|
||||||
|
@ -302,11 +305,12 @@ public class StandardRadioMedium extends RadioMedium {
|
||||||
private static double TRANSMITTING_RANGE = 20; // 20m
|
private static double TRANSMITTING_RANGE = 20; // 20m
|
||||||
private static double INTERFERENCE_RANGE = 40; // 40m
|
private static double INTERFERENCE_RANGE = 40; // 40m
|
||||||
|
|
||||||
private static boolean RECEIVE_MY_OWN_PACKETS = false;
|
|
||||||
private static boolean DETECT_MY_OWN_PACKETS = true;
|
|
||||||
|
|
||||||
private class RadioMediumObservable extends Observable {
|
private class RadioMediumObservable extends Observable {
|
||||||
private void transferredData() {
|
private void transmissionStarted() {
|
||||||
|
setChanged();
|
||||||
|
notifyObservers();
|
||||||
|
}
|
||||||
|
private void radiosChanged() {
|
||||||
setChanged();
|
setChanged();
|
||||||
notifyObservers();
|
notifyObservers();
|
||||||
}
|
}
|
||||||
|
@ -319,46 +323,56 @@ public class StandardRadioMedium extends RadioMedium {
|
||||||
|
|
||||||
private Observer radioDataObserver = new Observer() {
|
private Observer radioDataObserver = new Observer() {
|
||||||
public void update(Observable radio, Object obj) {
|
public void update(Observable radio, Object obj) {
|
||||||
if (((Radio) radio).getLastEvent() == Radio.RadioEvent.TRANSMISSION_FINISHED) {
|
if (((Radio) radio).getLastEvent() == Radio.RadioEvent.TRANSMISSION_STARTED) {
|
||||||
if (!sendingPositions.contains((Position) obj)) {
|
// If obj is the position, use it directly (speeds up things)
|
||||||
sendingPositions.add((Position) obj);
|
// Otherwise we must search for the position ourselves
|
||||||
sendingRadios.add((Radio) radio);
|
Position sendingPosition = (Position) obj;
|
||||||
|
if (sendingPosition == null) {
|
||||||
|
if (!registeredRadios.contains(radio)) {
|
||||||
|
logger.fatal("Sending radio not registered, skipping packet");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sendingPosition = registeredPositions.get(registeredRadios.indexOf(radio));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!newSendingRadios.contains(radio)) {
|
||||||
|
newSendingPositions.add(sendingPosition);
|
||||||
|
newSendingRadios.add((Radio) radio);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
radioMediumObservable.radiosChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private Observer tickObserver = new Observer() {
|
private Observer tickObserver = new Observer() {
|
||||||
public void update(Observable obs, Object obj) {
|
public void update(Observable obs, Object obj) {
|
||||||
|
|
||||||
RadioConnection[] oldTickConnections = lastTickConnections;
|
RadioConnection[] oldTickConnections = lastTickConnections;
|
||||||
lastTickConnections = null;
|
lastTickConnections = null;
|
||||||
if (sendingPositions.size() > 0) {
|
if (newSendingPositions.size() > 0) {
|
||||||
final int numberSending = sendingPositions.size();
|
final int numberSending = newSendingPositions.size();
|
||||||
lastTickConnections = new RadioConnection[numberSending];
|
lastTickConnections = new RadioConnection[numberSending];
|
||||||
|
|
||||||
// Loop through all sending radios
|
// Loop through all new sending radios
|
||||||
for (int sendNr = 0; sendNr < numberSending; sendNr++) {
|
for (int sendNr = 0; sendNr < numberSending; sendNr++) {
|
||||||
Radio sendingRadio = sendingRadios.get(sendNr);
|
Radio sendingRadio = newSendingRadios.get(sendNr);
|
||||||
|
|
||||||
byte[] dataToSend = sendingRadio.getLastPacketTransmitted();
|
byte[] dataToSend = sendingRadio.getLastPacketTransmitted();
|
||||||
|
|
||||||
lastTickConnections[sendNr] = new RadioConnection();
|
lastTickConnections[sendNr] = new RadioConnection();
|
||||||
lastTickConnections[sendNr].setSource(sendingRadios.get(sendNr),
|
lastTickConnections[sendNr].setSource(newSendingRadios.get(sendNr),
|
||||||
sendingPositions.get(sendNr), dataToSend);
|
newSendingPositions.get(sendNr), dataToSend);
|
||||||
|
|
||||||
// Set sending radio unable to receive any more data
|
|
||||||
if (RECEIVE_MY_OWN_PACKETS) {
|
|
||||||
if (sendingRadio.isReceiving())
|
|
||||||
sendingRadio.interferReception();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Loop through all radios that are listening
|
// Loop through all radios that are listening
|
||||||
for (int listenNr = 0; listenNr < registeredPositions.size(); listenNr++) {
|
for (int listenNr = 0; listenNr < registeredPositions.size(); listenNr++) {
|
||||||
|
|
||||||
// If not the sending radio..
|
|
||||||
if (sendingRadios.get(sendNr) != registeredRadios.get(listenNr)) {
|
|
||||||
Radio listeningRadio = registeredRadios.get(listenNr);
|
Radio listeningRadio = registeredRadios.get(listenNr);
|
||||||
|
|
||||||
double distance = sendingPositions.get(sendNr).getDistanceTo(
|
// If not the sending radio..
|
||||||
|
if (sendingRadio != listeningRadio) {
|
||||||
|
|
||||||
|
double distance = newSendingPositions.get(sendNr).getDistanceTo(
|
||||||
registeredPositions.get(listenNr));
|
registeredPositions.get(listenNr));
|
||||||
|
|
||||||
if (distance <= TRANSMITTING_RANGE) {
|
if (distance <= TRANSMITTING_RANGE) {
|
||||||
|
@ -369,24 +383,21 @@ public class StandardRadioMedium extends RadioMedium {
|
||||||
// If close enough to transmit ok..
|
// If close enough to transmit ok..
|
||||||
if (listeningRadio.isReceiving()) {
|
if (listeningRadio.isReceiving()) {
|
||||||
// .. but listening radio already received a packet
|
// .. but listening radio already received a packet
|
||||||
listeningRadio.interferReception();
|
listeningRadio.interferReception(sendingRadio.getTransmissionEndTime());
|
||||||
} else {
|
} else {
|
||||||
// .. send packet
|
// .. send packet
|
||||||
listeningRadio.receivePacket(dataToSend, 0);
|
listeningRadio.receivePacket(dataToSend, sendingRadio.getTransmissionEndTime());
|
||||||
}
|
}
|
||||||
} else if (distance <= INTERFERENCE_RANGE) {
|
} else if (distance <= INTERFERENCE_RANGE) {
|
||||||
// If close enough to sabotage other transmissions..
|
// If close enough to sabotage other transmissions..
|
||||||
if (listeningRadio.isReceiving()) {
|
listeningRadio.interferReception(sendingRadio.getTransmissionEndTime());
|
||||||
// .. and listening radio already received a packet
|
|
||||||
listeningRadio.interferReception();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// else too far away
|
// else too far away
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sendingPositions.clear();
|
newSendingPositions.clear();
|
||||||
sendingRadios.clear();
|
newSendingRadios.clear();
|
||||||
|
|
||||||
if (myLogger != null) {
|
if (myLogger != null) {
|
||||||
for (RadioConnection conn : lastTickConnections)
|
for (RadioConnection conn : lastTickConnections)
|
||||||
|
@ -395,7 +406,7 @@ public class StandardRadioMedium extends RadioMedium {
|
||||||
|
|
||||||
}
|
}
|
||||||
if (lastTickConnections != oldTickConnections)
|
if (lastTickConnections != oldTickConnections)
|
||||||
radioMediumObservable.transferredData();
|
radioMediumObservable.transmissionStarted();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue