[cooja] plugins/sinks/TrafficVisualizerSkin: Synchronized to avoid ConcurrentModificationExcpetions

This commit is contained in:
Enrico Joerns 2014-06-25 17:02:01 +02:00
parent cd9164d5e2
commit d4f7cf6a83

View file

@ -73,18 +73,21 @@ public class TrafficVisualizerSkin implements VisualizerSkin {
private Visualizer visualizer = null; private Visualizer visualizer = null;
private AbstractRadioMedium radioMedium = null; private AbstractRadioMedium radioMedium = null;
private List<RadioConnectionArrow> historyList = new LinkedList<>(); private final List<RadioConnectionArrow> historyList = new LinkedList<>();
private Observer radioMediumObserver = new Observer() { private Observer radioMediumObserver = new Observer() {
@Override @Override
public void update(Observable obs, Object obj) { public void update(Observable obs, Object obj) {
RadioConnection last = radioMedium.getLastConnection(); RadioConnection last = radioMedium.getLastConnection();
if (last != null && historyList.size() < MAX_HISTORY_SIZE) { if (last != null && historyList.size() < MAX_HISTORY_SIZE) {
historyList.add(new RadioConnectionArrow(last)); synchronized(historyList) {
visualizer.repaint(500); historyList.add(new RadioConnectionArrow(last));
visualizer.repaint(500);
}
} }
} }
}; };
private final TimeEvent ageArrowsTimeEvent = new TimeEvent(0) { private final TimeEvent ageArrowsTimeEvent = new TimeEvent(0) {
@Override @Override
public void execute(long t) { public void execute(long t) {
@ -94,13 +97,15 @@ public class TrafficVisualizerSkin implements VisualizerSkin {
if (historyList.size() > 0) { if (historyList.size() > 0) {
/* Increase age and remove too old arrows */ synchronized (historyList) {
Iterator<RadioConnectionArrow> iter = historyList.iterator(); /* Increase age and remove too old arrows */
while (iter.hasNext()) { Iterator<RadioConnectionArrow> iter = historyList.iterator();
RadioConnectionArrow rca = iter.next(); while (iter.hasNext()) {
/* Try to increase age and remove if max age was reached */ RadioConnectionArrow rca = iter.next();
if (!rca.increaseAge()) { /* Try to increase age and remove if max age was reached */
iter.remove(); if (!rca.increaseAge()) {
iter.remove();
}
} }
} }
@ -182,22 +187,24 @@ public class TrafficVisualizerSkin implements VisualizerSkin {
@Override @Override
public void paintBeforeMotes(Graphics g) { public void paintBeforeMotes(Graphics g) {
for (RadioConnectionArrow connArrow : historyList) { synchronized (historyList) {
float colorHistoryIndex = 1.0f - connArrow.getAge(); for (RadioConnectionArrow connArrow : historyList) {
Radio source = connArrow.getConnection().getSource(); float colorHistoryIndex = 1.0f - connArrow.getAge();
Point sourcePoint = visualizer.transformPositionToPixel(source.getPosition()); Radio source = connArrow.getConnection().getSource();
/* If there is no destination, paint red circles to indicate untransmitted message */ Point sourcePoint = visualizer.transformPositionToPixel(source.getPosition());
if (connArrow.getConnection().getDestinations().length == 0) { /* If there is no destination, paint red circles to indicate untransmitted message */
g.setColor(new Color(UNTRANSMITTED_COLOR_RGB[0], UNTRANSMITTED_COLOR_RGB[1], UNTRANSMITTED_COLOR_RGB[2], colorHistoryIndex)); if (connArrow.getConnection().getDestinations().length == 0) {
g.drawOval(sourcePoint.x - 20, sourcePoint.y - 20, 40, 40); g.setColor(new Color(UNTRANSMITTED_COLOR_RGB[0], UNTRANSMITTED_COLOR_RGB[1], UNTRANSMITTED_COLOR_RGB[2], colorHistoryIndex));
g.drawOval(sourcePoint.x - 30, sourcePoint.y - 30, 60, 60); g.drawOval(sourcePoint.x - 20, sourcePoint.y - 20, 40, 40);
continue; g.drawOval(sourcePoint.x - 30, sourcePoint.y - 30, 60, 60);
} continue;
g.setColor(new Color(TRANSMITTED_COLOR_RGB[0], TRANSMITTED_COLOR_RGB[1], TRANSMITTED_COLOR_RGB[2], colorHistoryIndex)); }
for (Radio destRadio : connArrow.getConnection().getDestinations()) { g.setColor(new Color(TRANSMITTED_COLOR_RGB[0], TRANSMITTED_COLOR_RGB[1], TRANSMITTED_COLOR_RGB[2], colorHistoryIndex));
Position destPos = destRadio.getPosition(); for (Radio destRadio : connArrow.getConnection().getDestinations()) {
Point destPoint = visualizer.transformPositionToPixel(destPos); Position destPos = destRadio.getPosition();
drawArrow(g, sourcePoint.x, sourcePoint.y, destPoint.x, destPoint.y, 8); Point destPoint = visualizer.transformPositionToPixel(destPos);
drawArrow(g, sourcePoint.x, sourcePoint.y, destPoint.x, destPoint.y, 8);
}
} }
} }
} }