Пример #1
0
  public void run(int selfId) {
    long initTime, lastRoundTime;
    BufferedWriter out = null;

    if (selfId != 0) // The node is a peer
    {
      try {
        if (OUTPUT) {
          out = new BufferedWriter(new FileWriter(selfId + "_nbDeletedUpdates.txt"));
          out.write(peer.selfId + " " + RTE + " " + UPDATES_PER_ROUND + "\n");
        }

        long currentTimeMillis = System.currentTimeMillis();
        long nextMinuteMillis = (1 + currentTimeMillis / 60000) * (60000);

        try {
          sleepAtLeast(nextMinuteMillis - currentTimeMillis);
        } catch (InterruptedException e) {
        }

        System.out.println("Session starts NOW");

        // Count new rounds until time limit
        initTime = System.currentTimeMillis() / 1000;
        while (System.currentTimeMillis() / 1000 - initTime < SESSION_DURATION) {

          // If the duration of one round passed, treat a new round
          lastRoundTime = System.currentTimeMillis() / 1000;

          // Trigger periodical messages of peer
          peer.newRound();

          // Wait until the end of round
          while (System.currentTimeMillis() / 1000 - lastRoundTime < ROUND_DURATION)
            peer.treatInMessage();

          // End of round: increase the round ID, update the log, and consume received updates
          int[] results = peer.endOfRound();
          if (OUTPUT)
            out.write(
                peer.getRoundId()
                    + " "
                    + results[0]
                    + " "
                    + results[1]
                    + "\n"); // roundId, nodeStatus, nbDeletedUpdates
        }

        if (OUTPUT) out.close();

        peer.sender.mustStop = true;
        peer.receiver.mustStop = true;
        if (OUTPUT) {
          try {
            peer.outLog.close();
          } catch (IOException e1) {
            e1.printStackTrace();
          }
        }

      } catch (IOException e) {
        e.printStackTrace();
      }

    } else // The node is the broadcaster
    {
      long currentTimeMillis = System.currentTimeMillis();
      long nextMinuteMillis = (1 + currentTimeMillis / 60000) * (60000);

      try {
        sleepAtLeast(nextMinuteMillis - currentTimeMillis);
      } catch (InterruptedException e) {
      }

      System.out.println("Session starts NOW");

      // Count new rounds until time limit
      initTime = System.currentTimeMillis() / 1000;

      // If the duration of one round passed, treat a new round
      while (System.currentTimeMillis() / 1000 - initTime < SESSION_DURATION) {
        lastRoundTime = System.currentTimeMillis() / 1000;

        // Trigger periodical messages of peer
        server.newRound();

        // Wait until the end of round
        while (System.currentTimeMillis() / 1000 - lastRoundTime < ROUND_DURATION)
          server.treatInMessage();
      }

      server.stop();

      int nbrServeRounds =
          server.getNbServeRounds()
              - RTE; // The subtraction is related to the final rounds, for which updates cannot
                     // expire
      if (OUTPUT) {
        try {
          out = new BufferedWriter(new FileWriter("0_nbrSentUpdates.txt"));
          out.write(nbrServeRounds + "\n");
          out.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }