⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 udgm.java

📁 Contiki是一个开源
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      }      if (mote.getState() == Mote.State.DEAD) {        return new Color[] { Color.BLACK };      }      if (selectedMote != null && mote == selectedMote) {        return new Color[] { Color.CYAN };      }      if (moteRadio instanceof ContikiRadio && !((ContikiRadio) moteRadio).isOn()) {        return new Color[] { Color.GRAY };      }      if (moteRadio.isTransmitting()) {        return new Color[] { Color.BLUE };      }      if (moteRadio.isInterfered()) {        return new Color[] { Color.RED };      }      if (moteRadio.isReceiving()) {        return new Color[] { Color.GREEN };      }      return new Color[] { Color.WHITE };    }    public void visualizeSimulation(Graphics g) {      // Paint transmission+interference areas for selected mote (if any)      if (selectedMote != null) {        Position motePos = selectedMote.getInterfaces().getPosition();        Point pixelCoord = transformPositionToPixel(motePos);        int x = pixelCoord.x;        int y = pixelCoord.y;        // Fetch current output power indicator (scale with as percent)        if (selectedMote.getInterfaces().getRadio() != null) {          Radio selectedRadio = selectedMote.getInterfaces().getRadio();          double moteInterferenceRange = INTERFERENCE_RANGE              * ((double) selectedRadio.getCurrentOutputPowerIndicator() / (double) selectedRadio.getOutputPowerIndicatorMax());          double moteTransmissionRange = TRANSMITTING_RANGE              * ((double) selectedRadio.getCurrentOutputPowerIndicator() / (double) selectedRadio.getOutputPowerIndicatorMax());          Point translatedZero = transformPositionToPixel(0.0, 0.0, 0.0);          Point translatedInterference = transformPositionToPixel(              moteInterferenceRange, moteInterferenceRange, 0.0);          Point translatedTransmission = transformPositionToPixel(              moteTransmissionRange, moteTransmissionRange, 0.0);          translatedInterference.x = Math.abs(translatedInterference.x              - translatedZero.x);          translatedInterference.y = Math.abs(translatedInterference.y              - translatedZero.y);          translatedTransmission.x = Math.abs(translatedTransmission.x              - translatedZero.x);          translatedTransmission.y = Math.abs(translatedTransmission.y              - translatedZero.y);          // Interference          g.setColor(Color.DARK_GRAY);          g.fillOval(x - translatedInterference.x,              y - translatedInterference.y, 2 * translatedInterference.x,              2 * translatedInterference.y);          // Transmission          g.setColor(Color.GREEN);          g.fillOval(x - translatedTransmission.x,              y - translatedTransmission.y, 2 * translatedTransmission.x,              2 * translatedTransmission.y);        }      }      // Let parent paint motes      super.visualizeSimulation(g);      // Paint just finished connections      RadioConnection[] conns;      if (myRadioMedium != null          && (conns = myRadioMedium.getLastTickConnections()) != null) {        for (RadioConnection conn : conns) {          if (conn != null) {            Point sourcePoint = transformPositionToPixel(conn.getSource()                .getPosition());            // Paint destinations            for (Radio destRadio : conn.getDestinations()) {              Position destPos = destRadio.getPosition();              Point destPoint = transformPositionToPixel(destPos);              g.setColor(Color.BLACK);              g                  .drawLine(sourcePoint.x, sourcePoint.y, destPoint.x,                      destPoint.y);            }          }        }      }    }  }  public UDGM(Simulation simulation) {    super(simulation);    // Register this radio medium's plugins    simulation.getGUI().registerTemporaryPlugin(VisUDGM.class);    myRadioMedium = this;    mySimulation = simulation;    random.setSeed(simulation.getRandomSeed());  }  public RadioConnection createConnections(Radio sendingRadio) {    Position sendingPosition = sendingRadio.getPosition();    RadioConnection newConnection = new RadioConnection(sendingRadio);    // Fetch current output power indicator (scale with as percent)    double moteTransmissionRange = TRANSMITTING_RANGE        * ((double) sendingRadio.getCurrentOutputPowerIndicator() / (double) sendingRadio.getOutputPowerIndicatorMax());    double moteInterferenceRange = INTERFERENCE_RANGE        * ((double) sendingRadio.getCurrentOutputPowerIndicator() / (double) sendingRadio.getOutputPowerIndicatorMax());    /* Fail transmission randomly (affects all receiving nodes) */    if (SUCCESS_RATIO_TX < 1.0 && random.nextDouble() > SUCCESS_RATIO_TX) {      return newConnection;    }    // Loop through all radios    for (int listenNr = 0; listenNr < getRegisteredRadios().size(); listenNr++) {      Radio listeningRadio = getRegisteredRadios().get(listenNr);      Position listeningRadioPosition = listeningRadio.getPosition();      // Ignore sending radio and radios on different channels      if (sendingRadio == listeningRadio) {        continue;      }      if (sendingRadio.getChannel() >= 0 &&          listeningRadio.getChannel() >= 0 &&          sendingRadio.getChannel() != listeningRadio.getChannel()) {        continue;      }      double distance = sendingPosition.getDistanceTo(listeningRadioPosition);      if (distance <= moteTransmissionRange) {        // Check if this radio is able to receive transmission        if (listeningRadio.isInterfered()) {          // Keep interfering radio          newConnection.addInterfered(listeningRadio);        } else if (listeningRadio.isReceiving() ||            (SUCCESS_RATIO_RX < 1.0 && random.nextDouble() > SUCCESS_RATIO_RX)) {          newConnection.addInterfered(listeningRadio);          // Start interfering radio          listeningRadio.interfereAnyReception();          // Update connection that is transmitting to this radio          RadioConnection existingConn = null;          for (RadioConnection conn : getActiveConnections()) {            for (Radio dstRadio : conn.getDestinations()) {              if (dstRadio == listeningRadio) {                existingConn = conn;                break;              }            }          }          if (existingConn != null) {            // Change radio from receiving to interfered            existingConn.removeDestination(listeningRadio);            existingConn.addInterfered(listeningRadio);          }        } else {          // Radio OK to receive          newConnection.addDestination(listeningRadio);          listeningRadio.signalReceptionStart();        }      } else if (distance <= moteInterferenceRange) {        // Interfere radio        newConnection.addInterfered(listeningRadio);        listeningRadio.interfereAnyReception();      }    }    return newConnection;  }  public void updateSignalStrengths() {    // // Save old signal strengths    // double[] oldSignalStrengths = new double[registeredRadios.size()];    // for (int i = 0; i < registeredRadios.size(); i++) {    // oldSignalStrengths[i] = registeredRadios.get(i)    // .getCurrentSignalStrength();    // }    // Reset signal strength on all radios    for (Radio radio : getRegisteredRadios()) {      radio.setCurrentSignalStrength(SS_NOTHING);    }    // Set signal strength on all OK transmissions    for (RadioConnection conn : getActiveConnections()) {      conn.getSource().setCurrentSignalStrength(SS_STRONG);      for (Radio dstRadio : conn.getDestinations()) {        double dist = conn.getSource().getPosition().getDistanceTo(dstRadio.getPosition());        double maxTxDist = TRANSMITTING_RANGE        * ((double) conn.getSource().getCurrentOutputPowerIndicator() / (double) conn.getSource().getOutputPowerIndicatorMax());        double distFactor = dist/maxTxDist;        double signalStrength = SS_STRONG + distFactor*(SS_WEAK - SS_STRONG);        dstRadio.setCurrentSignalStrength(signalStrength);      }    }    // Set signal strength on all interferences    for (RadioConnection conn : getActiveConnections()) {      for (Radio intfRadio : conn.getInterfered()) {        double dist = conn.getSource().getPosition().getDistanceTo(intfRadio.getPosition());        double maxTxDist = TRANSMITTING_RANGE        * ((double) conn.getSource().getCurrentOutputPowerIndicator() / (double) conn.getSource().getOutputPowerIndicatorMax());        double distFactor = dist/maxTxDist;        if (distFactor < 1) {          double signalStrength = SS_STRONG + distFactor*(SS_WEAK - SS_STRONG);          intfRadio.setCurrentSignalStrength(signalStrength);        } else {          intfRadio.setCurrentSignalStrength(SS_WEAK);        }        if (!intfRadio.isInterfered()) {          // Set to interfered again          intfRadio.interfereAnyReception();        }      }    }    // // Fetch new signal strengths    // double[] newSignalStrengths = new double[registeredRadios.size()];    // for (int i = 0; i < registeredRadios.size(); i++) {    // newSignalStrengths[i] = registeredRadios.get(i)    // .getCurrentSignalStrength();    // }    //    // // Compare new and old signal strengths    // for (int i = 0; i < registeredRadios.size(); i++) {    // if (oldSignalStrengths[i] != newSignalStrengths[i])    // logger.warn("Signal strengths changed on radio[" + i + "]: "    // + oldSignalStrengths[i] + " -> " + newSignalStrengths[i]);    // }  }  public Collection<Element> getConfigXML() {    Vector<Element> config = new Vector<Element>();    Element element;    // Transmitting range    element = new Element("transmitting_range");    element.setText(Double.toString(TRANSMITTING_RANGE));    config.add(element);    // Interference range    element = new Element("interference_range");    element.setText(Double.toString(INTERFERENCE_RANGE));    config.add(element);    /* Transmission success probability */    element = new Element("success_ratio_tx");    element.setText("" + SUCCESS_RATIO_TX);    config.add(element);    /* Reception success probability */    element = new Element("success_ratio_rx");    element.setText("" + SUCCESS_RATIO_RX);    config.add(element);    return config;  }  public boolean setConfigXML(Collection<Element> configXML,      boolean visAvailable) {    for (Element element : configXML) {      if (element.getName().equals("transmitting_range")) {        TRANSMITTING_RANGE = Double.parseDouble(element.getText());      }      if (element.getName().equals("interference_range")) {        INTERFERENCE_RANGE = Double.parseDouble(element.getText());      }      /* Backwards compatibility */      if (element.getName().equals("success_ratio")) {        SUCCESS_RATIO_TX = Double.parseDouble(element.getText());        logger.warn("Loading old COOJA Config, XML element \"sucess_ratio\" parsed at \"sucess_ratio_tx\"");      }      if (element.getName().equals("success_ratio_tx")) {        SUCCESS_RATIO_TX = Double.parseDouble(element.getText());      }      if (element.getName().equals("success_ratio_rx")) {        SUCCESS_RATIO_RX = Double.parseDouble(element.getText());      }    }    random.setSeed(mySimulation.getRandomSeed());    return true;  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -