Esempio n. 1
0
  /** Start the Client */
  public void start() {
    DEBUG.set(true);
    DEBUG.trace("Pong Client");
    DEBUG.set(false);
    C_PongModel model = new C_PongModel();
    C_PongView view = new C_PongView();
    C_PongController cont = new C_PongController(model, view);

    makeContactWithServer(model, cont);

    model.addObserver(view); // Add observer to the model
    view.setVisible(true); // Display Screen
  }
Esempio n. 2
0
 private void cleanAnteriorFacts() {
   DEBUG.message("clean anterior facts");
   for (FactHandle referencedFact : this.currentReferencedFacts) {
     ksession.retract(referencedFact);
   }
   this.currentReferencedFacts.clear();
 }
  /**
   * Will check if {@link CoreTokenConstant.CTS_STORE_HOSTNAME} property will need to be updated and
   * need the host port appended to the connection string.
   *
   * @param serverName the name of the server Instance.
   * @param serverProperties the configuration of the server Instance.
   */
  protected void checkCTSStoreConnections(String serverName, Properties serverProperties) {
    String ctsHostName = serverProperties.getProperty(CoreTokenConstants.CTS_STORE_HOSTNAME);
    String ctsPort = serverProperties.getProperty(CTS_STORE_PORT);
    String ctsLocation = serverProperties.getProperty(CoreTokenConstants.CTS_STORE_LOCATION);

    DEBUG.message(
        "Checking CTS Store properties for instance: "
            + serverName
            + " \n properties: "
            + "ctsHostName: "
            + ctsHostName
            + " ctsPort: "
            + ctsPort
            + " ctsLocation: "
            + ctsLocation);
    // if no host and port values then return
    if (StringUtils.isBlank(ctsHostName) && StringUtils.isBlank(ctsPort)) {
      return;
    }

    // check current instance values and set to default if needed
    if (StringUtils.isBlank(ctsHostName)) {
      ctsHostName = ctsDefaultHostName;
      DEBUG.message("ctsHostName defaulting to : " + ctsDefaultHostName);
    }
    if (StringUtils.isBlank(ctsPort)) {
      ctsPort = ctsDefaultHostPort;
      DEBUG.message("ctsPort defaulting to : " + ctsDefaultHostPort);
    }
    if (StringUtils.isBlank(ctsLocation)) {
      ctsLocation = ctsDefaultLocation;
      DEBUG.message("ctsLocation defaulting to : " + ctsDefaultLocation);
    }

    // if the configuration is default, the host or port is blank from defaults or if there is
    // already a colon in
    // CTS host name then skip
    if (!"external".equals(ctsLocation)
        || StringUtils.isBlank(ctsHostName)
        || ctsHostName.contains(COLON_CHAR)
        || StringUtils.isBlank(ctsPort)) {
      return;
    }
    // store properties to be updated
    propertiesToModify.put(serverName, ctsHostName + COLON_CHAR + ctsPort);
  }
Esempio n. 4
0
 static {
   mValues.put(VERBOSE.toString().toLowerCase(), VERBOSE);
   mValues.put(DEBUG.toString().toLowerCase(), DEBUG);
   mValues.put(INFO.toString().toLowerCase(), INFO);
   mValues.put(WARNING.toString().toLowerCase(), WARNING);
   mValues.put(ERROR.toString().toLowerCase(), ERROR);
   mValuesList = Arrays.asList(values());
 }
Esempio n. 5
0
  public void run() {
    DEBUG.enableDebugMode(System.getProperty("robot.debug", "true").equals("true"));

    // Creates a knowledge base
    createKnowledgeBase();
    DEBUG.message("KBase created");

    // COLORS
    setBodyColor(Color.blue);
    setGunColor(Color.white);
    setRadarColor(Color.red);
    setScanColor(Color.white);
    setBulletColor(Color.red);

    // Make any movement from tank, radar or gun independent
    setAdjustGunForRobotTurn(true);
    setAdjustRadarForGunTurn(true);
    setAdjustRadarForRobotTurn(true);

    while (true) {
      DEBUG.message("TURN BEGINS");
      loadRobotState();
      loadBattleState();

      // Fire rules
      DEBUG.message("Facts in active memory");
      DEBUG.printFacts(ksession);
      DEBUG.message("firing rules");
      ksession.fireAllRules();
      cleanAnteriorFacts();

      // Get Actions
      Vector<Action> actions = loadActions();
      DEBUG.message("Resulting Actions");
      DEBUG.printActions(actions);

      // Execute Actions
      DEBUG.message("firing actions");
      executeActions(actions);
      DEBUG.message("TURN ENDS\n");
      execute();
    }
  }
Esempio n. 6
0
  private void createKnowledgeBase() {
    String rulesFile = System.getProperty("robot.rules", BaguetteSOLO.RULES_FILE);

    DEBUG.message("Creating knowledge base");
    kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

    DEBUG.message("Loading rules since " + rulesFile);
    kbuilder.add(
        ResourceFactory.newClassPathResource(rulesFile, BaguetteSOLO.class), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
      System.err.println(kbuilder.getErrors().toString());
    }

    kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

    DEBUG.message("Creating session)");
    ksession = kbase.newStatefulKnowledgeSession();
  }
Esempio n. 7
0
 private void loadBattleState() {
   DEBUG.message("load battle state");
   BattleState battleState =
       new BattleState(
           getBattleFieldWidth(),
           getBattleFieldHeight(),
           getNumRounds(),
           getRoundNum(),
           getTime(),
           getOthers());
   currentReferencedFacts.add(ksession.insert(battleState));
 }
 /**
  * @param entry instance of ZipEntry
  * @return
  */
 public InputStream getXMLChunkStream(ZipEntry entry) {
   try {
     if (pptxFilesStreams != null) {
       if (entry != null && entry.getName() != null && entry.getName().trim().length() > 0) {
         return (InputStream) pptxFilesStreams.get(entry.getName());
       }
     }
   } catch (Exception ex) {
     DEBUG.errorPrint(
         "PPTX - Could not get zip entry: " + entry.getName() + " Reason: " + ex.getMessage());
     return null;
   }
   return null;
 }
  /**
   * Set the default values from the server-default values.
   *
   * @param props the properties from the server
   */
  protected void setDefaultValues(Properties props) {
    ctsDefaultLocation = props.getProperty(CoreTokenConstants.CTS_STORE_LOCATION);
    ctsDefaultHostName = props.getProperty(CoreTokenConstants.CTS_STORE_HOSTNAME);
    ctsDefaultHostPort = props.getProperty(CTS_STORE_PORT);

    DEBUG.message(
        "Setting External CTS Store properties defaults: "
            + "\n ctsDefaultHostName: "
            + ctsDefaultHostName
            + " ctsDefaultHostPort: "
            + ctsDefaultHostPort
            + " ctsDefaultLocation:"
            + ctsDefaultLocation);
  }
Esempio n. 10
0
  private Vector<Action> loadActions() {
    DEBUG.message("load actions");
    Action action;
    Vector<Action> actionsList = new Vector<Action>();

    for (QueryResultsRow result : ksession.getQueryResults(BaguetteSOLO.CONSULT_ACTIONS)) {
      action = (Action) result.get("action"); // get the Action object
      action.setRobot(this); // link it to the current robot
      actionsList.add(action);
      ksession.retract(result.getFactHandle("action")); // clears the fact from the active memory
    }

    return actionsList;
  }
 @Override
 public void perform() throws UpgradeException {
   try {
     DEBUG.message("External CTS Configuration upgrading: " + propertiesToModify);
     UpgradeProgress.reportStart("upgrade.cts.property");
     for (Entry<String, String> serverProperty : propertiesToModify.entrySet()) {
       // get existing values
       Map<String, String> existingServerProperties =
           new HashMap(
               ServerConfiguration.getServerInstance(getAdminToken(), serverProperty.getKey()));
       // add new values to existing values
       existingServerProperties.put(
           CoreTokenConstants.CTS_STORE_HOSTNAME, serverProperty.getValue());
       existingServerProperties.keySet().remove(CTS_STORE_PORT);
       ServerConfiguration.upgradeServerInstance(
           getAdminToken(), serverProperty.getKey(), null, existingServerProperties);
     }
     UpgradeProgress.reportEnd("upgrade.success");
   } catch (Exception ex) {
     DEBUG.error("Unable to upgrade External CTS properties", ex);
     throw new UpgradeException(ex);
   }
 }
 @Override
 public void actionPerformed(ActionEvent e) {
   if (e.getSource() == debug) DEBUG.setEnabled(debug.isSelected());
   else if (e.getSource() == draw) DRAW.setEnabled(draw.isSelected());
   else if (e.getSource() == draw_path) DRAW_PATH.setEnabled(draw_path.isSelected());
   else if (e.getSource() == draw_vision) DRAW_VISION.setEnabled(draw_vision.isSelected());
   else if (e.getSource() == draw_border) DRAW_BORDER.setEnabled(draw_border.isSelected());
   else if (e.getSource() == print) PRINT.setEnabled(print.isSelected());
   else if (e.getSource() == print_energy) PRINT_ENERGY.setEnabled(print_energy.isSelected());
   else if (e.getSource() == print_death) PRINT_DEATH.setEnabled(print_death.isSelected());
   else if (e.getSource() == print_ai) PRINT_AI.setEnabled(print_ai.isSelected());
   else if (e.getSource() == print_breed) PRINT_BREED.setEnabled(print_breed.isSelected());
   else if (e.getSource() == print_move) PRINT_MOVE.setEnabled(print_move.isSelected());
   else if (e.getSource() == print_input) PRINT_INPUT.setEnabled(print_input.isSelected());
   else if (e.getSource() == print_other) PRINT_OTHER.setEnabled(print_other.isSelected());
   else if (e.getSource() == cmds) CMD.setEnabled(cmds.isSelected());
 }
Esempio n. 13
0
  /**
   * @param fileStream of pptx file
   * @return true if pptx file is unzipped, otherwise false
   */
  public boolean unzipFileAndStoreStreams(InputStream fileStream) {
    ZipEntry currentEntry = null;
    this.pptxFilesStreams.clear();

    this.pptxZipStreamFile = new ZipInputStream(fileStream);

    try {
      while (true) {
        currentEntry = this.pptxZipStreamFile.getNextEntry();
        if (currentEntry != null) {
          String name = currentEntry.getName();
          this.PPTXEntries.put(name, currentEntry);

          long bufferSize = currentEntry.getSize();
          Long bufferSizeLong = new Long(bufferSize);
          int bufferSizeIntValue = bufferSizeLong.intValue();
          bufferSizeLong = null;

          byte[] buffer = new byte[bufferSizeIntValue];
          byte[] readBuffer = new byte[bufferSizeIntValue];

          int bytesRead = 0;
          while (bytesRead < bufferSizeIntValue) {
            int currBytesRead = this.pptxZipStreamFile.read(readBuffer, 0, bufferSizeIntValue);
            for (int index = 0; index < currBytesRead; index++) {
              buffer[bytesRead] = readBuffer[index];
              bytesRead++;
            }
          }

          ByteArrayInputStream fileBytesStream = new ByteArrayInputStream(buffer);
          this.pptxFilesStreams.put(name, fileBytesStream);
        } else {
          break;
        }
      }

    } catch (Exception ex) {
      DEBUG.errorPrint("PPTX - Could not unzip pptx source file - " + ex.getMessage());
      return false;
    }
    return true;
  }
 @Override
 public void initialize() throws UpgradeException {
   try {
     // get server instances config
     Set<String> serverNames = ServerConfiguration.getServers(getAdminToken());
     // get the default server values and store values
     Properties props =
         ServerConfiguration.getServerInstance(
             getAdminToken(), ServerConfiguration.DEFAULT_SERVER_CONFIG);
     setDefaultValues(props);
     checkCTSStoreConnections(ServerConfiguration.DEFAULT_SERVER_CONFIG, props);
     // check values for each instance
     for (String serverName : serverNames) {
       checkCTSStoreConnections(
           serverName, ServerConfiguration.getServerInstance(getAdminToken(), serverName));
     }
   } catch (Exception ex) {
     DEBUG.error("Unable to upgrade External CTS Configuration", ex);
     throw new UpgradeException(ex);
   }
 }
Esempio n. 15
0
 private void executeActions(Vector<Action> actions) {
   DEBUG.message("execute actions");
   for (Action action : actions) {
     action.initExecution();
   }
 }
Esempio n. 16
0
  /**
   * Called from the model when its state is changed
   *
   * <p>Create two arrays of serializable that contain the updates to be sent to each player. The
   * only difference between the two arrays are the timestamps to calculate the RTT. It is necessary
   * to have two array because the states are sent asynchronously, therefore if we just change the
   * relevant index, both players would get the seconds players timestamp.
   *
   * @param aPongModel Model of game
   * @param arg Arguments - not used
   */
  public synchronized void update(Observable aPongModel, Object arg) {

    DEBUG.trace("Updating clients");

    S_PongModel model = (S_PongModel) aPongModel;

    ball = model.getBall();
    bats = model.getBats();

    // The state the send to the left player
    Serializable[] state =
        new Serializable[] {
          bats[0],
          bats[1],
          ball,
          model.getLastPingTimestamp(Global.LEFT_PLAYER)
              + Global.DELIMITER
              + model.getLastPingTimestamp(Global.RIGHT_PLAYER)
        };

    if (model.isDelayCompensation() && !model.isMulticast()) {

      // Get both players average ping
      // e.g. leftAvgPing = 200
      //      rightAvgPing = 200
      long leftAvgPing = model.getAveragePing(Global.LEFT_PLAYER),
          rightAvgPing = model.getAveragePing(Global.RIGHT_PLAYER);

      // Calculate how much we need to delay each player.
      // One players delay will always be negative
      // so their update will be sent immediately
      // e.g. leftDelay  = 200 - 200 = 0
      //      rightDelay = 200 - 200 = 0
      long leftDelay = (rightAvgPing - leftAvgPing), rightDelay = (leftAvgPing - rightAvgPing);

      // Calculate the actual delay (including the last delay)
      // e.g. Assuming the left player was delayed by 50ms last tick
      //     delay = (0 + 50) = 50 = 50ms delay
      //     delay = (0 + 0)  = 0 = 0ms delay
      //     Anything created than or equal to 0 will treated as 0.
      long leftActualDelay = leftDelay + model.getLastUpdateDelay(Global.LEFT_PLAYER),
          rightActualDelay = rightDelay + model.getLastUpdateDelay(Global.RIGHT_PLAYER);

      // Send the update to both players with their corresponding delay
      left.put(state, leftActualDelay);
      right.put(state, rightActualDelay);

      // Store how much we've delayed each player so we
      // can offset it against their delay on the next tick
      model.setLastUpdateDelay(Global.LEFT_PLAYER, leftDelay);
      model.setLastUpdateDelay(Global.RIGHT_PLAYER, rightDelay);

    } else {

      // Even if this is a multicast game, the broadcast is sent twice,
      // this is because one player may have connected over TCP,
      // This should really be fixed so TCP players are not joined to
      // Multicast games.
      // TODO: Refactor the NetObjectWriter so we can do something like
      // TODO: clients.update(state) and it will determine the rest
      left.put(state);
      right.put(state);
    }
  }
Esempio n. 17
0
 public void onHitRobot(HitRobotEvent event) {
   DEBUG.message("received on hit robot");
   currentReferencedFacts.add(ksession.insert(event));
 }
Esempio n. 18
0
 public void onHitByBullet(HitByBulletEvent event) {
   DEBUG.message("received hit by bullet");
   currentReferencedFacts.add(ksession.insert(event));
 }
Esempio n. 19
0
 public void onHitWall(HitWallEvent event) {
   DEBUG.message("received on hit wall");
   currentReferencedFacts.add(ksession.insert(event));
 }
  public DebugOptionsView() {
    debug = new JCheckBox("Debug");
    debug.setBounds(5, 5, w, h);
    debug.setSelected(DEBUG.isThisEnabled());
    debug.addActionListener(this);

    draw = new JCheckBox("Draw");
    draw.setBounds(10, 25, w, h);
    draw.setSelected(DRAW.isThisEnabled());
    draw.addActionListener(this);
    draw_path = new JCheckBox("Draw path");
    draw_path.setBounds(15, 45, w, h);
    draw_path.addActionListener(this);
    draw_path.setSelected(DRAW_PATH.isThisEnabled());
    draw_vision = new JCheckBox("Draw vision");
    draw_vision.setBounds(15, 65, w, h);
    draw_vision.addActionListener(this);
    draw_vision.setSelected(DRAW_VISION.isThisEnabled());
    draw_border = new JCheckBox("Draw border");
    draw_border.setBounds(15, 85, w, h);
    draw_border.addActionListener(this);
    draw_border.setSelected(DRAW_BORDER.isThisEnabled());

    print = new JCheckBox("Print");
    print.setBounds(10, 105, w, h);
    print.setSelected(PRINT.isThisEnabled());
    print.addActionListener(this);
    print_energy = new JCheckBox("Print energy");
    print_energy.setBounds(15, 125, w, h);
    print_energy.setSelected(PRINT_ENERGY.isThisEnabled());
    print_energy.addActionListener(this);
    print_death = new JCheckBox("Print deaths");
    print_death.setBounds(15, 145, w, h);
    print_death.setSelected(PRINT_DEATH.isThisEnabled());
    print_energy.addActionListener(this);
    print_ai = new JCheckBox("Print ai");
    print_ai.setBounds(15, 165, w, h);
    print_ai.setSelected(PRINT_AI.isThisEnabled());
    print_ai.addActionListener(this);
    print_breed = new JCheckBox("Print breed");
    print_breed.setBounds(15, 185, w, h);
    print_breed.setSelected(PRINT_BREED.isThisEnabled());
    print_breed.addActionListener(this);
    print_move = new JCheckBox("Print move");
    print_move.setBounds(15, 205, w, h);
    print_move.setSelected(PRINT_MOVE.isThisEnabled());
    print_move.addActionListener(this);
    print_input = new JCheckBox("Print input");
    print_input.setBounds(15, 225, w, h);
    print_input.setSelected(PRINT_INPUT.isThisEnabled());
    print_input.addActionListener(this);
    print_other = new JCheckBox("Print other");
    print_other.setBounds(15, 245, w, h);
    print_other.setSelected(PRINT_OTHER.isThisEnabled());
    print_other.addActionListener(this);

    cmds = new JCheckBox("Commands");
    cmds.setBounds(10, 265, w, h);
    cmds.setSelected(CMD.isEnabled());
    cmds.addActionListener(this);
    getContentPane().setLayout(null);

    getContentPane().add(debug);

    getContentPane().add(draw);
    getContentPane().add(draw_path);
    getContentPane().add(draw_vision);
    getContentPane().add(draw_border);

    getContentPane().add(print);
    getContentPane().add(print_energy);
    getContentPane().add(print_death);
    getContentPane().add(print_ai);
    getContentPane().add(print_breed);
    getContentPane().add(print_move);
    getContentPane().add(print_input);
    getContentPane().add(print_other);

    getContentPane().add(cmds);

    setSize(200, 325);
    setTitle("Debug options");
    setLocationRelativeTo(null);
    setVisible(true);
  }
Esempio n. 21
0
 public void onBulletMissed(BulletMissedEvent event) {
   DEBUG.message("received bullet miss");
   currentReferencedFacts.add(ksession.insert(event));
 }
Esempio n. 22
0
 public void onRobotDeath(RobotDeathEvent event) {
   DEBUG.message("received on robot death");
   currentReferencedFacts.add(ksession.insert(event));
 }
Esempio n. 23
0
 public void onScannedRobot(ScannedRobotEvent event) {
   DEBUG.message("received on scanned robot");
   currentReferencedFacts.add(ksession.insert(event));
 }
Esempio n. 24
0
 private void loadRobotState() {
   DEBUG.message("load robot state");
   RobotState robotState = new RobotState(this);
   currentReferencedFacts.add(ksession.insert(robotState));
 }
Esempio n. 25
0
 public void onBulletHitBullet(BulletHitBulletEvent event) {
   DEBUG.message("received on bullet hit another bullet");
   currentReferencedFacts.add(ksession.insert(event));
 }