private void sessionStart() {
    OverallStatsImpl stats = (OverallStatsImpl) StatsFactory.getStats();

    synchronized (this) {
      if (closing) {

        return;
      }

      boolean enabled = COConfigurationManager.getBooleanParameter("long.term.stats.enable");

      if (active || !enabled) {

        return;
      }

      active = true;

      long[] snap = stats.getLastSnapshot();

      ss_d_received = gm_stats.getTotalDataBytesReceived();
      ss_p_received = gm_stats.getTotalProtocolBytesReceived();

      ss_d_sent = gm_stats.getTotalDataBytesSent();
      ss_p_sent = gm_stats.getTotalProtocolBytesSent();

      ss_dht_sent = 0;
      ss_dht_received = 0;

      if (core.isStarted()) {

        DHT[] dhts = getDHTs();

        if (dhts != null) {

          for (DHT dht : dhts) {

            DHTTransportStats dht_stats = dht.getTransport().getStats();

            ss_dht_sent += dht_stats.getBytesSent();
            ss_dht_received += dht_stats.getBytesReceived();
          }
        }
      }

      st_p_sent = snap[0] + (ss_p_sent - snap[6]);
      st_d_sent = snap[1] + (ss_d_sent - snap[7]);
      st_p_received = snap[2] + (ss_p_received - snap[8]);
      st_d_received = snap[3] + (ss_d_received - snap[9]);
      st_dht_sent = snap[4] + (ss_dht_sent - snap[10]);
      st_dht_received = snap[5] + (ss_dht_received - snap[11]);

      write(
          RT_SESSION_START,
          new long[] {
            st_p_sent, st_d_sent, st_p_received, st_d_received, st_dht_sent, st_dht_received
          });

      if (event == null) { // should always be null but hey ho

        event =
            SimpleTimer.addPeriodicEvent(
                "LongTermStats",
                MIN_IN_MILLIS,
                new TimerEventPerformer() {
                  public void perform(TimerEvent event) {
                    updateStats();
                  }
                });
      }
    }
  }
Esempio n. 2
0
  private static void updateMetaData_handleReply(
      TOTorrent torrent, String hash, String replyType, Map mapHashes) {
    if (hash == null && torrent != null) {
      try {
        hash = torrent.getHashWrapper().toBase32String();
      } catch (Exception e) {
      }
    }

    GlobalManager gm = AzureusCoreFactory.getSingleton().getGlobalManager();
    DownloadManager dm = gm.getDownloadManager(new HashWrapper(Base32.decode(hash)));

    if (torrent == null && dm != null) {
      torrent = dm.getTorrent();
    }
    Map contentMap = PlatformTorrentUtils.getContentMap(torrent);

    final TOTorrent torrentFinal = torrent;

    if (replyType.equals(PlatformMessenger.REPLY_EXCEPTION)) {
      if (torrent != null) {
        // try again in a bit
        log(torrent, "Exception, retrying later");
        SimpleTimer.addEvent(
            "Update MD Retry",
            SystemTime.getCurrentTime() + RETRY_METADATA,
            new TimerEventPerformer() {
              public void perform(TimerEvent event) {
                log(torrentFinal, "retry time");
                PlatformTorrentUtils.updateMetaData(torrentFinal, 15000);
              }
            });
      }
    } else {
      Map jsonMapMetaData = hash == null ? null : (Map) mapHashes.get(hash);
      if (jsonMapMetaData != null) {
        long oldLastUpdated = getContentLastUpdated(torrent);
        long expireyMins = 0;

        for (Iterator iter = jsonMapMetaData.keySet().iterator(); iter.hasNext(); ) {
          String key = (String) iter.next();
          Object value = jsonMapMetaData.get(key);

          if (value == null || value.equals(null)) {
            contentMap.remove(key);
          } else if ((key.equals("Thumbnail") || key.endsWith(".B64")) && value instanceof String) {
            contentMap.put(key, Base64.decode((String) value));
          } else if (key.equals("expires-in-mins") && value instanceof Long) {
            expireyMins = ((Long) value).longValue();
          } else {
            contentMap.put(key, value);
          }
          writeTorrentIfExists(torrent);
        }

        // crappy way of updating the display name
        if (dm != null) {
          String title = PlatformTorrentUtils.getContentTitle(torrent);
          if (title != null
              && title.length() > 0
              && dm.getDownloadState().getDisplayName() == null) {
            dm.getDownloadState().setDisplayName(title);
          }
        }
        triggerMetaDataUpdateListeners(torrent);

        if (torrent != null) {
          // setup next refresh

          long refreshOn;
          if (expireyMins > 0) {
            refreshOn = SystemTime.getCurrentTime() + (expireyMins * 60 * 1000L);
          } else {
            long newLastUpdated = getContentLastUpdated(torrent);

            long diff = newLastUpdated - oldLastUpdated;
            log(
                torrent,
                "Last Updated: new "
                    + new Date(newLastUpdated)
                    + ";old "
                    + new Date(oldLastUpdated)
                    + ";diff="
                    + diff);
            if (diff > 0 && oldLastUpdated != 0) {
              diff *= 2;
              if (diff < MIN_MD_REFRESH_MS) {
                diff = MIN_MD_REFRESH_MS;
              } else if (diff > MAX_MD_REFRESH_MS) {
                diff = MAX_MD_REFRESH_MS;
              }
              refreshOn = SystemTime.getOffsetTime(diff);
            } else {
              refreshOn = SystemTime.getCurrentTime() + (7 * 24 * 60 * 60 * 1000L);
            }
          }

          log(torrent, "got MD. Next refresh in " + (refreshOn - SystemTime.getCurrentTime()));
          setMetaDataRefreshOn(torrent, refreshOn);
          SimpleTimer.addEvent(
              "Update MD",
              refreshOn,
              new TimerEventPerformer() {
                public void perform(TimerEvent event) {
                  PlatformTorrentUtils.updateMetaData(torrentFinal, 15000);
                }
              });
        }
      } else if (torrent != null) {
        long refreshOn = SystemTime.getCurrentTime() + (30 * 24 * 60 * 60 * 1000L);
        setMetaDataRefreshOn(torrent, refreshOn);
        log(torrent, "no hash in reply. Next refresh on " + new Date(refreshOn));
      }
    }
  }