📄 udgm.java
字号:
/* * Copyright (c) 2006, Swedish Institute of Computer Science. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the Institute nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Id: UDGM.java,v 1.18 2008/03/18 16:37:35 fros4943 Exp $ */package se.sics.cooja.radiomediums;import java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.*;import javax.swing.event.*;import org.jdom.Element;import org.apache.log4j.Logger;import se.sics.cooja.*;import se.sics.cooja.contikimote.interfaces.ContikiRadio;import se.sics.cooja.interfaces.*;import se.sics.cooja.plugins.Visualizer2D;/** * The Unit Disk Graph medium has two different range parameters; one for * transmitting and one for interfering other transmissions. * * The radio medium supports both byte and packet radios. * * The radio medium registers a visualizer plugin. Via this plugin the current * radio states and range parameters can be viewed and changed. * * The registered radios' signal strengths are updated whenever the radio medium * changes. There are three fixed levels: no surrounding traffic heard, noise * heard and data heard. * * The radio output power indicator (0-100) is used in a very simple way; the * total transmission (and interfering) range is multiplied with [power_ind]%. * * @see #SS_STRONG * @see #SS_WEAK * @see #SS_NOTHING * * @see VisUDGM * @author Fredrik Osterlind */@ClassDescription("Unit Disk Graph Medium (UDGM)")public class UDGM extends AbstractRadioMedium { private static Logger logger = Logger.getLogger(UDGM.class); private static RadioMedium myRadioMedium; /* Signal strengths in dBm. * Approx. values measured on TmoteSky */ public static final double SS_NOTHING = -100; public static final double SS_STRONG = -10; public static final double SS_WEAK = -95; private static double SUCCESS_RATIO_TX = 1.0; private static double SUCCESS_RATIO_RX = 1.0; // Maximum ranges (SS indicator 100) private static double TRANSMITTING_RANGE = 50; private static double INTERFERENCE_RANGE = 100; private Simulation mySimulation; private Random random = new Random(); /** * Visualizes radio traffic in the UDGM. Allows a user to * change transmission ranges. * * Sending motes are blue, receiving motes are green and motes that hear noise * are painted red. Motes without radios are painted gray, and the rest are * white. * * @author Fredrik Osterlind */ @ClassDescription("UDGM Visualizer") @PluginType(PluginType.SIM_PLUGIN) public static class VisUDGM extends Visualizer2D { private Mote selectedMote = null; private JSpinner transmissionSpinner = null; private JSpinner interferenceSpinner = null; private JSpinner successRatioTxSpinner = null; private JSpinner successRatioRxSpinner = null; private Observer radioMediumObserver; private class ChangeRangesMenuAction implements MoteMenuAction { public boolean isEnabled(Mote mote) { return true; } public String getDescription(Mote mote) { return "Change transmission ranges"; } public void doAction(Mote mote) { transmissionSpinner.setVisible(true); interferenceSpinner.setVisible(true); repaint(); } }; private class ChangeSuccessRadioMenuAction implements MoteMenuAction { public boolean isEnabled(Mote mote) { return true; } public String getDescription(Mote mote) { return "Change transmission success ratio"; } public void doAction(Mote mote) { successRatioTxSpinner.setVisible(true); successRatioRxSpinner.setVisible(true); repaint(); } }; public VisUDGM(Simulation sim, GUI gui) { super(sim, gui); setTitle("UDGM Visualizer"); // Create spinners for changing ranges SpinnerNumberModel transmissionModel = new SpinnerNumberModel(); transmissionModel.setValue(new Double(TRANSMITTING_RANGE)); transmissionModel.setStepSize(new Double(1.0)); // 1m transmissionModel.setMinimum(new Double(0.0)); SpinnerNumberModel interferenceModel = new SpinnerNumberModel(); interferenceModel.setValue(new Double(INTERFERENCE_RANGE)); interferenceModel.setStepSize(new Double(1.0)); // 1m interferenceModel.setMinimum(new Double(0.0)); SpinnerNumberModel successRatioTxModel = new SpinnerNumberModel(); successRatioTxModel.setValue(new Double(SUCCESS_RATIO_TX)); successRatioTxModel.setStepSize(new Double(0.001)); // 0.1% successRatioTxModel.setMinimum(new Double(0.0)); successRatioTxModel.setMaximum(new Double(1.0)); SpinnerNumberModel successRatioRxModel = new SpinnerNumberModel(); successRatioRxModel.setValue(new Double(SUCCESS_RATIO_RX)); successRatioRxModel.setStepSize(new Double(0.001)); // 0.1% successRatioRxModel.setMinimum(new Double(0.0)); successRatioRxModel.setMaximum(new Double(1.0)); JSpinner.NumberEditor editor; transmissionSpinner = new JSpinner(transmissionModel); editor = new JSpinner.NumberEditor(transmissionSpinner, "0m"); transmissionSpinner.setEditor(editor); interferenceSpinner = new JSpinner(interferenceModel); editor = new JSpinner.NumberEditor(interferenceSpinner, "0m"); interferenceSpinner.setEditor(editor); successRatioTxSpinner = new JSpinner(successRatioTxModel); editor = new JSpinner.NumberEditor(successRatioTxSpinner, "0.0%"); successRatioTxSpinner.setEditor(editor); successRatioRxSpinner = new JSpinner(successRatioRxModel); editor = new JSpinner.NumberEditor(successRatioRxSpinner, "0.0%"); successRatioRxSpinner.setEditor(editor); ((JSpinner.DefaultEditor) transmissionSpinner.getEditor()).getTextField() .setColumns(5); ((JSpinner.DefaultEditor) interferenceSpinner.getEditor()).getTextField() .setColumns(5); ((JSpinner.DefaultEditor) successRatioTxSpinner.getEditor()).getTextField() .setColumns(5); ((JSpinner.DefaultEditor) successRatioRxSpinner.getEditor()).getTextField() .setColumns(5); transmissionSpinner.setToolTipText("Transmitting range (m)"); interferenceSpinner.setToolTipText("Interference range (m)"); successRatioTxSpinner.setToolTipText("Transmission success ratio (%)"); successRatioRxSpinner.setToolTipText("Reception success ratio (%)"); transmissionSpinner.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { TRANSMITTING_RANGE = ((SpinnerNumberModel) transmissionSpinner .getModel()).getNumber().doubleValue(); repaint(); } }); interferenceSpinner.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { INTERFERENCE_RANGE = ((SpinnerNumberModel) interferenceSpinner .getModel()).getNumber().doubleValue(); repaint(); } }); successRatioTxSpinner.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { SUCCESS_RATIO_TX = ((SpinnerNumberModel) successRatioTxSpinner .getModel()).getNumber().doubleValue(); repaint(); } }); successRatioRxSpinner.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { SUCCESS_RATIO_RX = ((SpinnerNumberModel) successRatioRxSpinner .getModel()).getNumber().doubleValue(); repaint(); } }); getCurrentCanvas().add(transmissionSpinner); getCurrentCanvas().add(interferenceSpinner); getCurrentCanvas().add(successRatioTxSpinner); getCurrentCanvas().add(successRatioRxSpinner); transmissionSpinner.setVisible(false); interferenceSpinner.setVisible(false); successRatioTxSpinner.setVisible(false); successRatioRxSpinner.setVisible(false); // Add mouse listener for selecting motes getCurrentCanvas().addMouseListener(new MouseListener() { public void mouseExited(MouseEvent e) { // Do nothing } public void mouseEntered(MouseEvent e) { // Do nothing } public void mouseReleased(MouseEvent e) { // Do nothing } public void mousePressed(MouseEvent e) { Vector<Mote> clickedMotes = findMotesAtPosition(e.getX(), e.getY()); if (clickedMotes == null || clickedMotes.size() == 0) { selectedMote = null; transmissionSpinner.setVisible(false); interferenceSpinner.setVisible(false); successRatioTxSpinner.setVisible(false); successRatioRxSpinner.setVisible(false); repaint(); return; } // Select one of the clicked motes if (clickedMotes.contains(selectedMote)) { int pos = clickedMotes.indexOf(selectedMote); if (pos < clickedMotes.size() - 1) { selectedMote = clickedMotes.get(pos + 1); } else { selectedMote = clickedMotes.firstElement(); } } else { selectedMote = clickedMotes.firstElement(); } repaint(); } public void mouseClicked(MouseEvent e) { } }); // Register change ranges and change success ratio action addMoteMenuAction(new ChangeRangesMenuAction()); addMoteMenuAction(new ChangeSuccessRadioMenuAction()); // Observe our own radio medium myRadioMedium .addRadioMediumObserver(radioMediumObserver = new Observer() { public void update(Observable obs, Object obj) { getCurrentCanvas().repaint(); } }); } public void closePlugin() { super.closePlugin(); myRadioMedium.deleteRadioMediumObserver(radioMediumObserver); } public Color[] getColorOf(Mote mote) { Radio moteRadio = mote.getInterfaces().getRadio(); if (moteRadio == null) { return new Color[] { Color.BLACK };
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -