📄 radiomodelplugin.java
字号:
// $Id: RadioModelPlugin.java,v 1.7.4.4 2003/09/10 03:41:52 mdwelsh Exp $/* tab:2 * * * "Copyright (c) 2000 and The Regents of the University * of California. All rights reserved. * * Permission to use, copy, modify, and distribute this software and * its documentation for any purpose, without fee, and without written * agreement is hereby granted, provided that the above copyright * notice and the following two paragraphs appear in all copies of * this software. * * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS * DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, * UPDATES, ENHANCEMENTS, OR MODIFICATIONS." * * Authors: Nelson Lee * Date: December 11 2002 * Desc: Default Radio Plugin * Implements features for manipulating a lossy model * *//** * @author Nelson Lee */package net.tinyos.sim.plugins;import java.lang.*;import java.util.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.text.DecimalFormat;import net.tinyos.message.*;import net.tinyos.sim.*;import net.tinyos.sim.event.*;public class RadioModelPlugin extends Plugin implements SimConst { private static final boolean DEBUG = false; private Hashtable models = new Hashtable(); private PropagationModel curModel; private JTextField scalingFactorTextField; private JCheckBox cbOutEdges; private boolean outEdges = true; private Hashtable connectivityGraph; private DecimalFormat df = new DecimalFormat(); public void handleEvent(SimEvent event) { if (event instanceof SimObjectEvent) { SimObjectEvent simObjectEvent = (SimObjectEvent)event; switch (simObjectEvent.getType()) { case SimObjectEvent.OBJECT_ADDED: updateModel(false); break; case SimObjectEvent.OBJECT_REMOVED: updateModel(false); break; } motePanel.refresh(); } else if (event instanceof AttributeEvent) { AttributeEvent attributeEvent = (AttributeEvent)event; switch (attributeEvent.getType()) { case ATTRIBUTE_CHANGED: if (attributeEvent.getAttribute() instanceof MoteCoordinateAttribute) updateModel(false); motePanel.refresh(); break; } } else if (event instanceof SimObjectDraggedEvent) { updateModel(true); motePanel.refresh(); } else if (event instanceof OptionSetEvent) { OptionSetEvent ose = (OptionSetEvent)event; if (ose.name.equals("radiomodel")) { PropagationModel pm = (PropagationModel)models.get(ose.value); if (pm != null) { if (DEBUG) System.err.println("RADIOMODEL: Setting model to "+pm); curModel = pm; updateModel(true); motePanel.refresh(); } } else if (ose.name.equals("radioscaling")) { scalingFactorTextField.setText(ose.value); } } else if (event instanceof TossimInitEvent) { tv.pause(); updateModel(true); publishModel(); tv.resume(); } } public void updateModel(boolean force) { // Don't update if AutoRun is active if (!force && tv.getAutoRun() != null) return; Iterator it1 = state.getMoteSimObjects().iterator(); while (it1.hasNext()) { MoteSimObject moteSender = (MoteSimObject)it1.next(); MoteCoordinateAttribute moteSenderCoord = moteSender.getCoordinate(); Iterator it2 = state.getMoteSimObjects().iterator(); while (it2.hasNext()) { MoteSimObject moteReceiver = (MoteSimObject)it2.next(); if (moteReceiver.getID() == moteSender.getID()) continue; MoteCoordinateAttribute moteReceiverCoord = moteReceiver.getCoordinate(); double dx = (double)(moteSenderCoord.getX() - moteReceiverCoord.getX()); double dy = (double)(moteSenderCoord.getY() - moteReceiverCoord.getY()); double distance = Math.sqrt((dx * dx) + (dy * dy)); double prob = curModel.getPacketLossRate(distance, Double.parseDouble(scalingFactorTextField.getText())); if (DEBUG) System.err.println("RADIOMODEL: "+moteSender+"->"+moteReceiver+" dist "+distance+" scale "+scalingFactorTextField.getText()+" prob "+prob); long scaledBitLossRate = (long)(curModel.getBitLossRate(prob)*10000); if (DEBUG) System.out.println("RadioModelPlugin: sampleLossRate [moteSender "+moteSender+"] [moteReceiver "+moteReceiver+"] [packetLossRate "+prob+"] [scaledBitLossRate "+scaledBitLossRate+"]"); connectivityGraph.put(""+moteSender.getID()+", "+moteReceiver.getID(), new Double(prob)); } } } public void publishModel() { if (DEBUG) System.err.println("RADIOMODEL: Publishing model, current is "+curModel); Iterator it1 = state.getMoteSimObjects().iterator(); try { while (it1.hasNext()) { MoteSimObject moteSender = (MoteSimObject)it1.next(); MoteCoordinateAttribute moteSenderCoord = moteSender.getCoordinate(); Iterator it2 = state.getMoteSimObjects().iterator(); while (it2.hasNext()) { MoteSimObject moteReceiver = (MoteSimObject)it2.next(); if (moteReceiver.getID() == moteSender.getID()) continue; // Sender -> Receiver String s = ""+moteSender.getID()+", "+moteReceiver.getID(); double prob = ((Double)connectivityGraph.get(s)).doubleValue(); long scaledBitLossRate = (long)(curModel.getBitLossRate(prob)*10000); SetLinkProbCommand cmd = new SetLinkProbCommand((short)moteSender.getID(), 0L, (short)moteReceiver.getID(), scaledBitLossRate); simComm.sendCommand(cmd); // Receiver -> Sender s = ""+moteReceiver.getID()+", "+moteSender.getID(); prob = ((Double)connectivityGraph.get(s)).doubleValue(); if (DEBUG) System.err.println("RADIOMODEL: "+s+" "+prob); scaledBitLossRate = (long)(curModel.getBitLossRate(prob)*10000); cmd = new SetLinkProbCommand((short)moteReceiver.getID(), 0L, (short)moteSender.getID(), scaledBitLossRate); simComm.sendCommand(cmd); } } } catch (java.io.IOException ioe) { System.err.println("Cannot send command: "+ioe); } } public void register() { df.applyPattern("#.###"); connectivityGraph = new Hashtable(); JPanel parameterPane = new JPanel(); parameterPane.setLayout(new GridLayout(2,2)); // Create the out edge checkbox cbOutEdges = new JCheckBox("Out Edges", outEdges); cbOutEdges.addItemListener(new cbListener()); cbOutEdges.setFont(tv.labelFont); // Create radius constant text field and label JLabel scalingFactorLabel = new JLabel("Distance scaling factor"); scalingFactorLabel.setFont(tv.defaultFont); scalingFactorTextField = new JTextField("1", 5); scalingFactorTextField.setFont(tv.smallFont); scalingFactorTextField.setEditable(true); parameterPane.add(scalingFactorLabel); parameterPane.add(scalingFactorTextField); // Create button to update radio model JButton updateButton = new JButton("Update"); updateButton.addActionListener(new UpdateListener()); updateButton.setFont(tv.defaultFont); // Create combo box for different Propagation models JComboBox cb = new JComboBox(); cb.addActionListener(new ComboBoxListener()); EmpiricalModel empiricalModel = new EmpiricalModel(); models.put("empirical", empiricalModel); cb.addItem(empiricalModel); cb.setSelectedItem(empiricalModel); curModel = empiricalModel; // User can use scaling factor to adjust DiscModel dm; dm = new DiscModel(10.0); models.put("disc10", dm); cb.addItem(dm); dm = new DiscModel(100.0); models.put("disc100", dm); cb.addItem(dm); dm = new DiscModel(1000.0); models.put("disc1000", dm); cb.addItem(dm); //pluginPanel.setLayout(new BorderLayout()); pluginPanel.add(parameterPane); pluginPanel.add(updateButton); pluginPanel.add(cbOutEdges); pluginPanel.add(cb); pluginPanel.revalidate(); updateModel(false); } public void draw(Graphics graphics) { Iterator selectedMotes = state.getSelectedMoteSimObjects().iterator(); while (selectedMotes.hasNext()) { MoteSimObject selMote = (MoteSimObject)selectedMotes.next(); Iterator motes = state.getMoteSimObjects().iterator(); while (motes.hasNext()) { MoteSimObject mote = (MoteSimObject)motes.next(); if (selMote != mote) { MoteCoordinateAttribute selMoteCoord = selMote.getCoordinate(); MoteCoordinateAttribute moteCoord = mote.getCoordinate(); int x1 = (int)cT.simXToGUIX(selMoteCoord.getX()); int y1 = (int)cT.simYToGUIY(selMoteCoord.getY()); int x2 = (int)cT.simXToGUIX(moteCoord.getX()); int y2 = (int)cT.simYToGUIY(moteCoord.getY()); double prob; if (outEdges) { String s = ""+selMote.getID()+", "+mote.getID(); prob = ((Double)connectivityGraph.get((Object)s)).doubleValue(); if (prob < 1.0) { graphics.setColor(getColor(1-prob)); Arrow.drawArrow(graphics, x2, y2, x1, y1, Arrow.SIDE_TRAIL); } } else { String s = ""+mote.getID()+", "+selMote.getID(); prob = ((Double)connectivityGraph.get((Object)s)).doubleValue(); if (prob < 1.0) { graphics.setColor(getColor(1-prob)); Arrow.drawArrow(graphics, x1, y1, x2, y2, Arrow.SIDE_TRAIL); } } if (prob < 1.0) { int xMidPoint = x1 + (x2-x1)/2; int yMidPoint = y1 + (y2-y1)/2; graphics.drawString(new String(df.format((1-prob)*100)), xMidPoint, yMidPoint); } } } } } public static Color getColor(double value) { if (value < 0.0) return Color.gray; if (value > 1.0) value = 1.0; int red = Math.min(255,(int)(512.0 - (value * 512.0))); int green = Math.min(255,(int)(value * 512.0)); int blue = 0; return new Color(red, green, blue); } public void deregister() {} public String toString() { return "Radio model"; } class UpdateListener implements ActionListener { public void actionPerformed(ActionEvent e) { updateModel(true); publishModel(); motePanel.refresh(); } } class cbListener implements ItemListener { public void itemStateChanged(ItemEvent e) { outEdges = (e.getStateChange() == e.SELECTED); motePanel.refresh(); } } class ComboBoxListener implements ActionListener { public void actionPerformed(ActionEvent e) { JComboBox cb = (JComboBox)e.getSource(); PropagationModel pm = (PropagationModel)cb.getSelectedItem(); curModel = pm; updateModel(true); motePanel.refresh(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -