Esempio n. 1
0
  public static void playback(int idCompetition) {
    // Get distances from start

    // Get the distances
    final List<DistanceFromStart> distances = HsqldbInterface.getDistancesFromStart(idCompetition);
    // Remove the distances for not to duplicate them
    HsqldbInterface.removeDistancesFromStart(idCompetition);

    // Set has started
    HsqldbInterface.setCompetitionHasStarted(idCompetition);

    // DISTANCES
    new Thread() {

      public void run() {
        long startTime = new Date().getTime();
        for (DistanceFromStart d : distances) {
          // Time from start of the race
          long timeFromStart = new Date().getTime() - startTime;

          // If it's not its time, sleeps
          long sleepTime = d.getTime() - timeFromStart;
          if (sleepTime > 0) {
            try {
              Thread.sleep(sleepTime);
            } catch (InterruptedException ex) {
              Logger.getLogger(RacePlayback.class.getName()).log(Level.SEVERE, null, ex);
            }
          }
          // Save distance from start
          HsqldbInterface.saveDistanceFromStart(
              d.getIdAthlete(), d.getIdCompetition(), d.getDistance());
        }
      }
    }.start();

    // COMMENTS
    Competition comp = HsqldbInterface.getCompetition(idCompetition);
    final Comment c1 =
        new Comment(
            -1,
            "@luis, @victor y @jaime han salido un poco despacio... "
                + "Espero que no les ganen los robots!",
            "dani",
            new ArrayList<String>(),
            comp.getId(),
            40452722,
            -3727811,
            comp.getScheduledDate());
    final Comment c2 =
        new Comment(
            -1,
            "#1 en cabeza! Y #5 y #2 persiguiendo desde cerca! Vamos!!!",
            "sara",
            new ArrayList<String>(),
            comp.getId(),
            40452231,
            -3729784,
            comp.getScheduledDate());
    final Comment c3 =
        new Comment(
            -1,
            "Como van?? ya no alcanzo a verlos!!",
            "sara",
            new ArrayList<String>(),
            comp.getId(),
            40452112,
            -3729768,
            comp.getScheduledDate());
    final Comment c4 =
        new Comment(
            -1,
            "Por aqui pasa @luis con algo de ventaja... aunque #5 y #2 le tienen a tiro! Carreron!",
            "pablo",
            new ArrayList<String>(),
            comp.getId(),
            40450965,
            -3728459,
            comp.getScheduledDate());
    final Comment c5 =
        new Comment(
            -1,
            "vamos @luis!! La victoria es tuya!!!",
            "dani",
            new ArrayList<String>(),
            comp.getId(),
            40452081,
            -3727756,
            comp.getScheduledDate());
    final Comment c6 =
        new Comment(
            -1,
            "Venga @jaime!! Arriba ese podium!!",
            "dani",
            new ArrayList<String>(),
            comp.getId(),
            40452044,
            -3727745,
            comp.getScheduledDate());
    final Comment c7 =
        new Comment(
            -1,
            "Genial @victor!! Vaya carreron!",
            "dani",
            new ArrayList<String>(),
            comp.getId(),
            40451991,
            -3727743,
            comp.getScheduledDate());

    new Thread() {

      public void run() {
        try {
          Thread.sleep(8000);
        } catch (InterruptedException ex) {
          Logger.getLogger(RacePlayback.class.getName()).log(Level.SEVERE, null, ex);
        }
        HsqldbInterface.saveComment(c1);
        try {
          Thread.sleep(44000);
        } catch (InterruptedException ex) {
          Logger.getLogger(RacePlayback.class.getName()).log(Level.SEVERE, null, ex);
        }
        HsqldbInterface.saveComment(c2);
        try {
          Thread.sleep(38000);
        } catch (InterruptedException ex) {
          Logger.getLogger(RacePlayback.class.getName()).log(Level.SEVERE, null, ex);
        }
        HsqldbInterface.saveComment(c3);
        try {
          Thread.sleep(10000);
        } catch (InterruptedException ex) {
          Logger.getLogger(RacePlayback.class.getName()).log(Level.SEVERE, null, ex);
        }
        HsqldbInterface.saveComment(c4);
        try {
          Thread.sleep(40000);
        } catch (InterruptedException ex) {
          Logger.getLogger(RacePlayback.class.getName()).log(Level.SEVERE, null, ex);
        }
        HsqldbInterface.saveComment(c5);
        try {
          Thread.sleep(7000);
        } catch (InterruptedException ex) {
          Logger.getLogger(RacePlayback.class.getName()).log(Level.SEVERE, null, ex);
        }
        HsqldbInterface.saveComment(c6);
        try {
          Thread.sleep(6000);
        } catch (InterruptedException ex) {
          Logger.getLogger(RacePlayback.class.getName()).log(Level.SEVERE, null, ex);
        }
        HsqldbInterface.saveComment(c7);
      }
    }.start();
  }
Esempio n. 2
0
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    // Look for the request action
    String action = request.getServletPath();

    // Writer for the response
    PrintWriter out = response.getWriter();

    // Parsing of the athlete
    action = action.substring(action.lastIndexOf('/') + 1, action.indexOf('.'));
    response.setCharacterEncoding("UTF8");
    response.setContentType("application/json; charset=UTF-8");

    // Gets the competitions
    if (action.equals(COMPETITIONS)) {
      List<Competition> competitions = HsqldbInterface.getCompetitions();
      JSONArray jsonArray;
      try {
        jsonArray = JSONAdapter.competitionListToJSON(competitions);
        out.println(jsonArray);
      } catch (JSONException ex) {
        Logger.getLogger(Servlet.class.getName()).log(Level.SEVERE, null, ex);
      }

      // Gets the athletes
    } else if (action.equals(ATHLETES)) {
      int idCompetition = 0;
      try {
        idCompetition = Integer.parseInt(request.getParameter(IDCOMPETITION));
      } catch (IllegalArgumentException e) {
        out.println(0);
        e.printStackTrace();
        return;
      }
      List<Athlete> athletes = HsqldbInterface.getCompetitionAthletes(idCompetition, false);
      JSONArray jsonArray;
      try {
        jsonArray = JSONAdapter.athleteListToJSON(athletes);
        out.println(jsonArray);
      } catch (JSONException ex) {
        Logger.getLogger(Servlet.class.getName()).log(Level.SEVERE, null, ex);
      }

      // Gets the circuit
    } else if (action.equals(CIRCUIT)) {
      int idCompetition = 0;
      try {
        idCompetition = Integer.parseInt(request.getParameter(IDCOMPETITION));
      } catch (IllegalArgumentException e) {
        out.println(0);
        e.printStackTrace();
        return;
      }
      List<CircuitPoint> circuitPoints = HsqldbInterface.getCircuitPoints(idCompetition);
      JSONArray jsonArr;
      try {
        jsonArr = JSONAdapterServer.circuitPointListToJSON(circuitPoints);
        out.println(jsonArr);
      } catch (JSONException ex) {
        Logger.getLogger(Servlet.class.getName()).log(Level.SEVERE, null, ex);
      }

      // Saves an athlete distance from start
    } else if (action.equals(SEND_DISTANCE)) {
      int idAthlete = 0;
      int idCompetition = 0;
      int distance = 0;
      try {
        idAthlete = Integer.parseInt(request.getParameter(IDATHLETE));
        idCompetition = Integer.parseInt(request.getParameter(IDCOMPETITION));
        distance = Integer.parseInt(request.getParameter(DISTANCE));
      } catch (IllegalArgumentException e) {
        out.println(0);
        e.printStackTrace();
        return;
      }
      HsqldbInterface.saveDistanceFromStart(idAthlete, idCompetition, distance);

      // Gets the last athlete distances and positions
    } else if (action.equals(GET_DISTANCES)) {
      int idCompetition = 0;
      try {
        idCompetition = Integer.parseInt(request.getParameter(IDCOMPETITION));
      } catch (IllegalArgumentException e) {
        out.println(0);
        e.printStackTrace();
        return;
      }

      try {
        JSONObject jsonObj =
            JSONAdapter.participationsToJSON(
                HsqldbInterface.getCompetitionAthletes(idCompetition, true),
                HsqldbInterface.getCompetition(idCompetition));
        out.println(jsonObj);
      } catch (JSONException ex) {
        out.println(0);
        ex.printStackTrace();
        return;
      }

      // Receives an attempt of login
    } else if (action.equals(LOGIN)) {
      String username = request.getParameter(USERNAME);
      String password = request.getParameter(PASSWORD);
      Athlete ath = HsqldbInterface.logIn(username, password);
      if (ath == null) {
        out.println(0);
      } else {
        try {
          out.println(JSONAdapter.athleteToJSON(ath));
        } catch (JSONException ex) {
          Logger.getLogger(Servlet.class.getName()).log(Level.SEVERE, null, ex);
        }
      }

      // Asking if the competition has started
    } else if (action.equals(CHECK_HAS_STARTED)) {
      int idCompetition = 0;
      try {
        idCompetition = Integer.parseInt(request.getParameter(IDCOMPETITION));
      } catch (IllegalArgumentException e) {
        out.println(-1);
        e.printStackTrace();
        return;
      }
      Competition competition = HsqldbInterface.getCompetition(idCompetition);
      if (competition == null) {
        out.println(0);
        return;
      }
      if (competition.getState() != Competition.NOT_STARTED) {
        out.println(competition.gtRealDateAndTime());
      } else {
        out.println(0);
      }

      // Sets the competition has started and starts the simulation
    } else if (action.equals(SET_HAS_STARTED)) {
      int idCompetition = 0;
      try {
        idCompetition = Integer.parseInt(request.getParameter(IDCOMPETITION));
      } catch (IllegalArgumentException e) {
        out.println(-1);
        e.printStackTrace();
        return;
      }
      HsqldbInterface.setCompetitionHasStarted(idCompetition);
      HsqldbInterface.setIsSimulating(idCompetition, true);
      // Starts the simulator (internally in other thread
      Simulator.simulate(idCompetition);
      out.println(0);

      // Gets the circuit test names
    } else if (action.equals(TEST_CIRCUITS)) {
      String[] circuitNames = TestCompetitionCreator.getCircuitNames();
      JSONArray jsonArr;
      try {
        jsonArr = JSONAdapter.circuitNamesListToJSON(circuitNames);
        out.println(jsonArr.toString());
      } catch (JSONException ex) {
        Logger.getLogger(Servlet.class.getName()).log(Level.SEVERE, null, ex);
        out.println(0);
      }

      // Creates a test competition
    } else if (action.equals(TEST_COMPETITION)) {
      int idCircuit = 0;
      try {
        idCircuit = Integer.parseInt(request.getParameter(IDCIRCUIT));
      } catch (IllegalArgumentException e) {
        out.println(0);
        e.printStackTrace();
        return;
      }
      TestCompetitionCreator.deletePreviousAndCreateNewCompetition(idCircuit);
      out.println(0);

      // Stops the simulation
    } else if (action.equals(STOP_SIMULATING)) {
      int idCompetition = 0;
      try {
        idCompetition = Integer.parseInt(request.getParameter(IDCOMPETITION));
      } catch (IllegalArgumentException e) {
        out.println(-1);
        e.printStackTrace();
        return;
      }
      HsqldbInterface.setIsSimulating(idCompetition, false);
      out.println(0);

      // Saves a comment posted
    } else if (action.equals(POST_COMMENT)) {
      String comment = request.getParameter(COMMENT);
      String writer = request.getParameter(USERNAME);
      int latE6 = 0;
      int lonE6 = 0;
      int idCompetition = 0;
      try {
        latE6 = Integer.parseInt(request.getParameter(LATE6));
        lonE6 = Integer.parseInt(request.getParameter(LONE6));
        idCompetition = Integer.parseInt(request.getParameter(IDCOMPETITION));
      } catch (IllegalArgumentException e) {
        out.println(-1);
        e.printStackTrace();
        return;
      }
      Comment c = new Comment(-1, comment, writer, null, idCompetition, latE6, lonE6, new Date());
      CommentProcessor.addReferences(c);
      HsqldbInterface.saveComment(c);
    } // Gets the athletes
    else if (action.equals(COMMENTS)) {
      int idCompetition = 0;
      int lastIdComment = 0;
      try {
        idCompetition = Integer.parseInt(request.getParameter(IDCOMPETITION));
        lastIdComment = Integer.parseInt(request.getParameter(LAST_IDCOMMENT));
      } catch (IllegalArgumentException e) {
        out.println(0);
        e.printStackTrace();
        return;
      }
      List<Comment> comments = HsqldbInterface.getComments(idCompetition, lastIdComment);
      JSONArray jsonArray;
      try {
        jsonArray = JSONAdapter.commentListToJSON(comments);
        out.println(jsonArray.toString());
      } catch (JSONException ex) {
        Logger.getLogger(Servlet.class.getName()).log(Level.SEVERE, null, ex);
      }
    } // Send image
    else if (action.equals(SEND_IMAGE)) {
      System.out.println("SEND IMAGE");
      InputStream is = request.getInputStream();

      byte[] photo = new byte[request.getContentLength()];
      System.out.println("bya: " + photo[0] + photo[1]);
      is.read(photo);
      System.out.println("byd: " + photo[0] + photo[1]);
      File file = new File("/home/luis", "img.jpg");
      OutputStream os = new FileOutputStream(file);
      os.write(photo);
      os.close();
      // Reset a competition
    } else if (action.equals(RESET)) {
      int idCompetition = 0;
      try {
        idCompetition = Integer.parseInt(request.getParameter(IDCOMPETITION));
      } catch (IllegalArgumentException e) {
        out.println(-1);
        e.printStackTrace();
        return;
      }
      RacePlayback.reset(idCompetition);
      out.println(0);
      // Reset a competition
    } else if (action.equals(PLAYBACK)) {
      int idCompetition = 0;
      try {
        idCompetition = Integer.parseInt(request.getParameter(IDCOMPETITION));
      } catch (IllegalArgumentException e) {
        out.println(-1);
        e.printStackTrace();
        return;
      }
      RacePlayback.playback(idCompetition);
      out.println(0);
    } else if (action.equals(SHUTDOWN)) {
      HsqldbInterface.shutdown();
    } else {
      out.println("Not still implemented or wrong address!");
    }
  }