Package se.sics.cooja

Examples of se.sics.cooja.Simulation


   *
   * @param arguments Command arguments
   * @return Reply to client (XML format)
   */
  private String createSimulation(Collection<Element> arguments) {
    Simulation simulation = new Simulation(myGUI);
    simulation.setTitle("[chakana - no title]");
    simulation.setDelayTime(0);
    simulation.setSimulationTime(0);
    simulation.setTickTime(1);

    try {
      RadioMedium radioMedium = RadioMedium.generateRadioMedium(UDGM.class,
          simulation);
      simulation.setRadioMedium(radioMedium);
    } catch (Exception e) {
      return createErrorMessage("Create simulation: Could not create radio medium: " + e.getMessage());
    }

    // Let simulation parse command arguments
    myGUI.setSimulation(simulation);
    try {
      boolean success = simulation.setConfigXML(arguments, false);
    } catch (Exception e) {
      logger.fatal("Error when configuring simulation: " + e);
      if (DEBUG_OUTPUT) {
        if (e instanceof MoteTypeCreationException) {
          MessageList compilationOutput = ((MoteTypeCreationException) e).getCompilationOutput();
View Full Code Here


   *
   * @param arguments Command arguments
   * @return Reply to client (XML format)
   */
  private String configureSimulation(Collection<Element> arguments) {
    Simulation simulation = myGUI.getSimulation();

    // Let simulation parse command arguments
    try {
      simulation.setConfigXML(arguments, false);
    } catch (Exception e) {
      logger.fatal("Error when configuring simulation: " + e);
      e.printStackTrace();
      return createErrorMessage("Could not configure simulation: " + e.getMessage());
    }
View Full Code Here

   *
   * @param arguments Command arguments
   * @return Reply to client (XML format)
   */
  private String configurePlugins(Collection<Element> arguments) {
    Simulation simulation = myGUI.getSimulation();

    // Let GUI parse command arguments
    try {
      myGUI.setPluginsConfigXML(arguments, simulation, false);
    } catch (Exception e) {
View Full Code Here

   *
   * @param arguments Command arguments
   * @return Reply to client (XML format)
   */
  private String readMemory(Collection<Element> arguments) {
    Simulation simulation = myGUI.getSimulation();

    String type = null;
    String mote = null;
    String variable = null;
    String size = null;
    String address = null;

    for (Element element : arguments) {
      if (element.getName().equals(XML_TYPE_NAME)) {
        type = element.getText();
      } else if (element.getName().equals(XML_MOTE_NAME)) {
        mote = element.getText();
      } else if (element.getName().equals(XML_VARIABLE_NAME)) {
        variable = element.getText();
      } else if (element.getName().equals(XML_SIZE_NAME)) {
        size = element.getText();
      } else if (element.getName().equals(XML_ADDRESS_NAME)) {
        address = element.getText();
      } else {
        return createErrorMessage("Unknown read memory parameter: " + element.getName());
      }
    }

    if (type == null)
      return createErrorMessage("No read memory type specified");
    if (mote == null)
      return createErrorMessage("No mote specified");
    if (mote == null)
      return createErrorMessage("No mote ID specified");
    int moteNr = Integer.parseInt(mote);
    if (moteNr < 0 || simulation.getMotesCount() <= moteNr) {
      return createErrorMessage("Bad mote ID specified: " + moteNr);
    }
    MoteMemory memory = simulation.getMote(moteNr).getMemory();
   
    // Read integer variable
    if (type.equals("int")) {
      if (variable == null)
        return createErrorMessage("No variable name specified");
View Full Code Here

   *
   * @param arguments Command arguments
   * @return Reply to client (XML format)
   */
  private String writeMemory(Collection<Element> arguments) {
    Simulation simulation = myGUI.getSimulation();

    String type = null;
    String mote = null;
    String variable = null;
    String size = null;
    String address = null;
    String value = null;

    for (Element element : arguments) {
      if (element.getName().equals(XML_TYPE_NAME)) {
        type = element.getText();
      } else if (element.getName().equals(XML_MOTE_NAME)) {
        mote = element.getText();
      } else if (element.getName().equals(XML_VARIABLE_NAME)) {
        variable = element.getText();
      } else if (element.getName().equals(XML_SIZE_NAME)) {
        size = element.getText();
      } else if (element.getName().equals(XML_ADDRESS_NAME)) {
        address = element.getText();
      } else if (element.getName().equals(XML_VALUE_NAME)) {
        value = element.getText();
      } else {
        return createErrorMessage("Unknown write memory parameter: " + element.getName());
      }
    }

    if (type == null)
      return createErrorMessage("No write memory type specified");
    if (mote == null)
      return createErrorMessage("No mote specified");
    if (mote == null)
      return createErrorMessage("No mote ID specified");
    int moteNr = Integer.parseInt(mote);
    if (moteNr < 0 || simulation.getMotesCount() <= moteNr) {
      return createErrorMessage("Bad mote ID specified: " + moteNr);
    }
    MoteMemory memory = simulation.getMote(moteNr).getMemory();
   
    // Write integer variable
    if (type.equals("int")) {
      if (variable == null)
        return createErrorMessage("No variable name specified");
View Full Code Here

   *
   * @param arguments Command arguments
   * @return Reply to client (XML format)
   */
  private String addEventpoint(Collection<Element> arguments) {
    Simulation simulation = myGUI.getSimulation();

    String type = null;
    String mote = null;
    String variable = null;
    String time = null;
    String size = null;
    String triggeron = null;
    String address = null;
    String count = null;

    for (Element element : arguments) {
      if (element.getName().equals(XML_TYPE_NAME)) {
        type = element.getText();
      } else if (element.getName().equals(XML_MOTE_NAME)) {
        mote = element.getText();
      } else if (element.getName().equals(XML_VARIABLE_NAME)) {
        variable = element.getText();
      } else if (element.getName().equals(XML_TIME_NAME)) {
        time = element.getText();
      } else if (element.getName().equals(XML_SIZE_NAME)) {
        size = element.getText();
      } else if (element.getName().equals(XML_ADDRESS_NAME)) {
        address = element.getText();
      } else if (element.getName().equals(XML_TRIGGERON_NAME)) {
        triggeron = element.getText();
      } else if (element.getName().equals("count")) {
        count = element.getText();
      } else {
        return createErrorMessage("Unknown eventpoint parameter: " + element.getName());
      }
    }

    logger.debug("Eventpoint type: " + type);
    if (type == null)
      return createErrorMessage("No eventpoint type specified");

    // Integer variable watchpoint
    if (type.equals(XML_WATCHPOINT_INT)) {
      if (variable == null)
        return createErrorMessage("No variable name specified");
      if (variable.contains(" "))
        return createErrorMessage("Variable name must not contain spaces: " + variable);
      if (mote == null)
        return createErrorMessage("No mote ID specified");
      int moteNr = Integer.parseInt(mote);
      if (moteNr < 0 || simulation.getMotesCount() <= moteNr) {
        return createErrorMessage("Bad mote ID specified: " + moteNr);
      }
      Mote moteObject = simulation.getMote(moteNr);

      MoteMemory memory = simulation.getMote(moteNr).getMemory();
     
      if (!(memory instanceof AddressMemory))
        return createErrorMessage("Can't write mote memory variables (not address memory)");
     
      if (!((AddressMemory) memory).variableExists(variable)) {
        return createErrorMessage("Variable does not exist: " + variable);
      }

      Eventpoint newEventpoint = new IntegerWatchpoint(moteObject, variable);
      myEvaluator.addEventpoint(newEventpoint);
      return createOkMessage(newEventpoint, simulation.getSimulationTime());
    }

    // Variable watchpoint
    if (type.equals(XML_WATCHPOINT_VARIABLE)) {
      if (variable == null)
        return createErrorMessage("No variable name specified");
      if (variable.contains(" "))
        return createErrorMessage("Variable name must not contain spaces: " + variable);
      if (mote == null)
        return createErrorMessage("No mote ID specified");
      if (size == null)
        return createErrorMessage("No size specified");
      int sizeParsed = Integer.parseInt(size);
      if (sizeParsed < 0) {
        return createErrorMessage("Bad size specified: " + sizeParsed);
      }
      int moteNr = Integer.parseInt(mote);
      if (moteNr < 0 || simulation.getMotesCount() <= moteNr) {
        return createErrorMessage("Bad mote ID specified: " + moteNr);
      }
      Mote moteObject = simulation.getMote(moteNr);

      MoteMemory memory = simulation.getMote(moteNr).getMemory();
     
      if (!(memory instanceof AddressMemory))
        return createErrorMessage("Can't write mote memory variables (not address memory)");
     
      if (!((AddressMemory) memory).variableExists(variable)) {
        return createErrorMessage("Variable does not exist: " + variable);
      }

      Eventpoint newEventpoint = new VariableWatchpoint(moteObject, variable, sizeParsed);
      myEvaluator.addEventpoint(newEventpoint);
      return createOkMessage(newEventpoint, simulation.getSimulationTime());
    }


    // Memory area watchpoint
    if (type.equals(XML_WATCHPOINT_ADDRESS)) {
      if (mote == null)
        return createErrorMessage("No mote ID specified");
      if (size == null)
        return createErrorMessage("No size specified");
      int sizeParsed = Integer.parseInt(size);
      if (sizeParsed < 0) {
        return createErrorMessage("Bad size specified: " + sizeParsed);
      }
      int addressParsed = Integer.parseInt(address);
      if (addressParsed < 0) {
        return createErrorMessage("Bad start address specified: " + addressParsed);
      }
      int moteNr = Integer.parseInt(mote);
      if (moteNr < 0 || simulation.getMotesCount() <= moteNr) {
        return createErrorMessage("Bad mote ID specified: " + moteNr);
      }
      Mote moteObject = simulation.getMote(moteNr);
      MoteMemory memory = simulation.getMote(moteNr).getMemory();
     
      Eventpoint newEventpoint = new Watchpoint(moteObject, addressParsed, sizeParsed);
      myEvaluator.addEventpoint(newEventpoint);
      return createOkMessage(newEventpoint, simulation.getSimulationTime());
    }

    // Simulation timepoint
    if (type.equals(XML_TIMEPOINT_SIMULATION)) {
      if (time == null)
        return createErrorMessage("No time specified");
      int timeParsed = Integer.parseInt(time);
      if (timeParsed < 0) {
        return createErrorMessage("Bad time specified: " + timeParsed);
      }

      Eventpoint newEventpoint = new SimulationTimepoint(simulation, timeParsed);
      myEvaluator.addEventpoint(newEventpoint);
      return createOkMessage(newEventpoint, simulation.getSimulationTime());
    }

    // Real timepoint
    if (type.equals(XML_TIMEPOINT_REAL)) {
      if (time == null)
        return createErrorMessage("No time specified");
      long timeParsed = Long.parseLong(time);
      if (timeParsed < 0) {
        return createErrorMessage("Bad time specified: " + timeParsed);
      }

      Eventpoint newEventpoint = new RealTimepoint(timeParsed);
      myEvaluator.addEventpoint(newEventpoint);
      return createOkMessage(newEventpoint, simulation.getSimulationTime());
    }

    // Radio medium event point
    if (type.equals(XML_EVENTPOINT_RADIOMEDIUM)) {
      int countInt;
      try {
        countInt = Integer.parseInt(count);
      } catch (NumberFormatException e) {
        return createErrorMessage("Bad count specified: " + e);
      }

      if (triggeron == null || triggeron.equals("all")) {
        Eventpoint newEventpoint = new RadioMediumEventpoint(simulation.getRadioMedium(), countInt);
        myEvaluator.addEventpoint(newEventpoint);
        return createOkMessage(newEventpoint, simulation.getSimulationTime());
      } else if (triggeron.equals("completed")) {
        Eventpoint newEventpoint = new TransmissionRadioMediumEventpoint(simulation.getRadioMedium(), countInt);
        myEvaluator.addEventpoint(newEventpoint);
        return createOkMessage(newEventpoint, simulation.getSimulationTime());
      } else {
        return createErrorMessage("Bad trigger on parameter: " + triggeron);
      }
    }

View Full Code Here

   *
   * @param arguments Command arguments
   * @return Reply to client (XML format)
   */
  private String getInfo(Collection<Element> arguments) {
    Simulation simulation = myGUI.getSimulation();
    String type = null;
    String mote = null;
    String variable = null;

    for (Element element : arguments) {
      if (element.getName().equals(XML_TYPE_NAME)) {
        type = element.getText();
      } else if (element.getName().equals(XML_MOTE_NAME)) {
        mote = element.getText();
      } else if (element.getName().equals(XML_VARIABLE_NAME)) {
        variable = element.getText();
      } else {
        return createErrorMessage("Unknown info parameter: " + element.getName());
      }
    }

    if (type.equals("motestate")) {
      if (mote == null)
        return createErrorMessage("No mote ID specified");
      int moteNr = Integer.parseInt(mote);
      if (moteNr < 0 || simulation.getMotesCount() <= moteNr) {
        return createErrorMessage("Bad mote ID specified: " + moteNr);
      }

      State state = simulation.getMote(moteNr).getState();
      if (state == State.DEAD)
        return XML_OK_START + 0 + XML_OK_END;
      return XML_OK_START + 1 + XML_OK_END;

    } else if (type.equals("time")) {
      return XML_OK_START + myGUI.getSimulation().getSimulationTime() + XML_OK_END;
    } else if (type.equals(XML_MOTECOUNT_NAME)) {
      return XML_OK_START + myGUI.getSimulation().getMotesCount() + XML_OK_END;
    } else if (type.equals("var_address")) {
      if (variable == null)
        return createErrorMessage("No variable name specified");
      if (variable.contains(" "))
        return createErrorMessage("Variable name must not contain spaces: " + variable);
      if (mote == null)
        return createErrorMessage("No mote ID specified");
      int moteNr = Integer.parseInt(mote);
      if (moteNr < 0 || simulation.getMotesCount() <= moteNr) {
        return createErrorMessage("Bad mote ID specified: " + moteNr);
      }
      Mote moteObject = simulation.getMote(moteNr);
      MoteMemory memory = simulation.getMote(moteNr).getMemory();
       
      if (!(memory instanceof AddressMemory))
        return createErrorMessage("Can't read mote memory variable address (not address memory)");

      if (!((AddressMemory) memory).variableExists(variable)) {
View Full Code Here

   *
   * @param arguments Command arguments
   * @return Reply to client (XML format)
   */
  private String handleCustomCommand(Collection<Element> arguments) {
    Simulation simulation = myGUI.getSimulation();
    String action = null;
    int id = -1;
    int visible = -1;
    int hide = -1;

    for (Element element : arguments) {
      if (element.getName().equals("action")) {
        action = element.getText();
      } else if (element.getName().equals("id")) {
        String idString = element.getText();
        id = Integer.parseInt(idString);
      } else if (element.getName().equals("hide")) {
        String hideString = element.getText();
        hide = Integer.parseInt(hideString);
      } else if (element.getName().equals("visible")) {
        String visibleString = element.getText();
        visible = Integer.parseInt(visibleString);
      } else {
        return createErrorMessage("Unknown info parameter: " + element.getName());
      }
    }
   
    if (action.equals("CLICK_SINK")) {
      simulation.getMote(0).getInterfaces().getButton().clickButton();
      return XML_OK;
    }

    if (action.equals("SHOW_FIRE")) {
      if (!simulation.getGUI().isVisualized())
        return XML_OK;

      JInternalFrame[] allPlugins = simulation.getGUI().getDesktopPane().getAllFrames();

      try {
        Class<Visualizer2D> pluginClass = (Class<Visualizer2D>) simulation.getGUI().tryLoadClass(this, Visualizer2D.class, "se.sics.runes.RunesVis");
        Class[] parameterTypes = new Class[1];
        parameterTypes[0] = Boolean.TYPE;
        Method method = pluginClass.getMethod("showFire", parameterTypes);
        Object[] parameterArguments = new Object[1];
        parameterArguments[0] = new Boolean(visible==1?true:false);

        for (JInternalFrame plugin: allPlugins) {
          if (plugin.getClass() == pluginClass) {
            method.invoke(plugin, parameterArguments);
          }
        }

      } catch (Exception e) {
        return createErrorMessage("Exception: " + e.getMessage());
      }
      return XML_OK;
    }

    if (action.equals("RESET_COLORS")) {
      if (!simulation.getGUI().isVisualized())
        return XML_OK;

      JInternalFrame[] allPlugins = simulation.getGUI().getDesktopPane().getAllFrames();

      try {
        Class<Visualizer2D> pluginClass = (Class<Visualizer2D>) simulation.getGUI().tryLoadClass(this, Visualizer2D.class, "se.sics.runes.RunesVis");
        Class[] parameterTypes = new Class[0];
        Method method = pluginClass.getMethod("resetColors", parameterTypes);
        Object[] parameterArguments = new Object[0];

        for (JInternalFrame plugin: allPlugins) {
          if (plugin.getClass() == pluginClass) {
            method.invoke(plugin, parameterArguments);
          }
        }

      } catch (Exception e) {
        return createErrorMessage("Exception: " + e.getMessage());
      }
      return XML_OK;
    }

    if (action.equals("SHOW_TRUCK")) {
      if (!simulation.getGUI().isVisualized())
        return XML_OK;

      JInternalFrame[] allPlugins = simulation.getGUI().getDesktopPane().getAllFrames();

      try {
        Class<Visualizer2D> pluginClass = (Class<Visualizer2D>) simulation.getGUI().tryLoadClass(this, Visualizer2D.class, "se.sics.runes.RunesVis");

        if (hide == 0) {
          Class[] parameterTypes = new Class[0];
          Method method = pluginClass.getMethod("showTruck", parameterTypes);
          Object[] parameterArguments = new Object[0];

          for (JInternalFrame plugin: allPlugins) {
            if (plugin.getClass() == pluginClass) {
              method.invoke(plugin, parameterArguments);
            }
          }
        } else {
          Class[] parameterTypes = new Class[0];
          Method method = pluginClass.getMethod("hideTruck", parameterTypes);
          Object[] parameterArguments = new Object[0];

          for (JInternalFrame plugin: allPlugins) {
            if (plugin.getClass() == pluginClass) {
              method.invoke(plugin, parameterArguments);
            }
          }
        }

      } catch (Exception e) {
        return createErrorMessage("Exception: " + e.getMessage());
      }
      return XML_OK;
    }

    if (action.equals("INDICATE_MOTE")) {
      if (!simulation.getGUI().isVisualized())
        return XML_OK;

      JInternalFrame[] allPlugins = simulation.getGUI().getDesktopPane().getAllFrames();

      try {
        Class<Visualizer2D> pluginClass = (Class<Visualizer2D>) simulation.getGUI().tryLoadClass(this, Visualizer2D.class, "se.sics.runes.RunesVis");
        Class[] parameterTypes = new Class[1];
        parameterTypes[0] = Integer.TYPE;
        Method method = pluginClass.getMethod("indicateMote", parameterTypes);
        Object[] parameterArguments = new Object[1];
        parameterArguments[0] = new Integer(id);
View Full Code Here

   *
   * @param arguments Command arguments
   * @return Reply to client (XML format)
   */
  private String createSimulation(Collection<Element> arguments) {
    Simulation simulation = new Simulation(myGUI);
    simulation.setTitle("[chakana - no title]");
    simulation.setDelayTime(0);
    simulation.setSimulationTime(0);
    simulation.setTickTime(1);

    try {
      RadioMedium radioMedium = RadioMedium.generateRadioMedium(UDGM.class,
          simulation);
      simulation.setRadioMedium(radioMedium);
    } catch (Exception e) {
      return createErrorMessage("Create simulation: Could not create radio medium: " + e.getMessage());
    }

    // Let simulation parse command arguments
    myGUI.setSimulation(simulation);
    try {
      boolean success = simulation.setConfigXML(arguments, false);
    } catch (Exception e) {
      logger.fatal("Error when configuring simulation: " + e);
      if (DEBUG_OUTPUT) {
        if (e instanceof MoteTypeCreationException) {
          MessageList compilationOutput = ((MoteTypeCreationException) e).getCompilationOutput();
View Full Code Here

   *
   * @param arguments Command arguments
   * @return Reply to client (XML format)
   */
  private String configureSimulation(Collection<Element> arguments) {
    Simulation simulation = myGUI.getSimulation();

    // Let simulation parse command arguments
    try {
      simulation.setConfigXML(arguments, false);
    } catch (Exception e) {
      logger.fatal("Error when configuring simulation: " + e);
      e.printStackTrace();
      return createErrorMessage("Could not configure simulation: " + e.getMessage());
    }
View Full Code Here

TOP

Related Classes of se.sics.cooja.Simulation

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.