@Override
 public synchronized void unregister(RemoteAPIEventsSender eventsAPI) {
   remoteEventSenders.remove(eventsAPI);
   if (remoteEventSenders.isEmpty()) {
     DownloadController.getInstance().removeListener(this);
     DownloadWatchDog.getInstance().getEventSender().removeListener(this);
   }
 }
 @Override
 public synchronized void register(RemoteAPIEventsSender eventsAPI) {
   boolean wasEmpty = remoteEventSenders.isEmpty();
   remoteEventSenders.add(eventsAPI);
   if (wasEmpty && remoteEventSenders.isEmpty() == false) {
     DownloadController.getInstance().addListener(this, true);
     DownloadWatchDog.getInstance().getEventSender().addListener(this, true);
   }
 }
 @Override
 public void onDownloadControllerStructureRefresh(AbstractNode node, Object param) {
   long newChange = DownloadController.getInstance().getPackageControllerChanges();
   if (backEndChangeID == newChange) {
     // avoid dupe events
     return;
   }
   backEndChangeID = newChange;
   fire(BASIC_EVENT.REFRESH_STRUCTURE.name(), null, BASIC_EVENT.REFRESH_STRUCTURE.name());
   flushBuffer();
 }
Exemplo n.º 4
0
 private PollingResultAPIStorable getAggregatedNumbers() {
   PollingResultAPIStorable prs = new PollingResultAPIStorable();
   prs.setEventName("aggregatedNumbers");
   org.jdownloader.myjdownloader.client.json.JsonMap eventData =
       new org.jdownloader.myjdownloader.client.json.JsonMap();
   eventData.put(
       "data",
       new AggregatedNumbersAPIStorable(
           new AggregatedNumbers(dc.getSelectionInfo()),
           new AggregatedCrawlerNumbers(lc.getSelectionInfo())));
   prs.setEventData(eventData);
   return prs;
 }
Exemplo n.º 5
0
 public boolean matches(CrawledLink link) {
   return link != null && DownloadController.getInstance().hasDownloadLinkByID(link.getLinkID());
 }
  @Override
  public void onDownloadControllerUpdatedData(DownloadLink dl, DownloadLinkProperty property) {

    if (property != null) {
      FilePackage parent = dl.getParentNode();
      HashMap<String, Object> dls = null;
      // [DATA_UPDATE.extractionStatus, DATA_UPDATE.finished, DATA_UPDATE.priority,
      // DATA_UPDATE.speed, DATA_UPDATE.url,
      // DATA_UPDATE.enabled, DATA_UPDATE.skipped, DATA_UPDATE.running, DATA_UPDATE.bytesLoaded,
      // DATA_UPDATE.eta,
      // DATA_UPDATE.maxResults, DATA_UPDATE.packageUUIDs, DATA_UPDATE.host, DATA_UPDATE.comment,
      // DATA_UPDATE.bytesTotal,
      // DATA_UPDATE.startAt, DATA_UPDATE.status]
      System.out.println("Property Change: " + property.getProperty());
      switch (property.getProperty()) {
        case ARCHIVE:
          break;
        case ARCHIVE_ID:
          // //archive properties changed;
          break;

        case AVAILABILITY:
          dls = new HashMap<String, Object>();
          dls.put("uuid", dl.getUniqueID().getID());
          dls.put("availability", property.getValue());
          fire(
              BASIC_EVENT.LINK_UPDATE.name() + ".availability",
              dls,
              BASIC_EVENT.LINK_UPDATE.name() + ".availability." + dl.getUniqueID().getID());
          break;
        case CHUNKS:
          break;
        case COMMENT:
          dls = new HashMap<String, Object>();
          dls.put("uuid", dl.getUniqueID().getID());
          dls.put("comment", property.getValue());
          fire(
              BASIC_EVENT.LINK_UPDATE.name() + ".comment",
              dls,
              BASIC_EVENT.LINK_UPDATE.name() + ".comment." + dl.getUniqueID().getID());
          break;
        case URL_CONTAINER:
        case URL_ORIGIN:
        case URL_REFERRER:
        case URL_CONTENT:
          dls = new HashMap<String, Object>();
          dls.put("uuid", dl.getUniqueID().getID());
          dls.put("url", dl.getView().getDisplayUrl());
          fire(
              BASIC_EVENT.LINK_UPDATE.name() + ".url",
              dls,
              BASIC_EVENT.LINK_UPDATE.name() + ".url." + dl.getUniqueID().getID());

          break;
        case CONDITIONAL_SKIPPED:
          pushStatus(dl);
          break;
        case DOWNLOAD_PASSWORD:
          break;
        case DOWNLOADSIZE:
          dls = new HashMap<String, Object>();
          dls.put("uuid", dl.getUniqueID().getID());
          dls.put("bytesTotal", property.getValue());
          fire(
              BASIC_EVENT.LINK_UPDATE.name() + ".bytesTotal",
              dls,
              BASIC_EVENT.LINK_UPDATE.name() + ".bytesTotal." + dl.getUniqueID().getID());
          break;
        case DOWNLOADSIZE_VERIFIED:
          dls = new HashMap<String, Object>();
          dls.put("uuid", dl.getUniqueID().getID());
          dls.put("bytesTotal", property.getValue());
          fire(
              BASIC_EVENT.LINK_UPDATE.name() + ".bytesTotal",
              dls,
              BASIC_EVENT.LINK_UPDATE.name() + ".bytesTotal." + dl.getUniqueID().getID());
          break;
        case DOWNLOAD_CONTROLLER:
          dls = new HashMap<String, Object>();
          dls.put("uuid", dl.getUniqueID().getID());
          dls.put("running", property.getValue() != null);
          fire(
              BASIC_EVENT.LINK_UPDATE.name() + ".running",
              dls,
              BASIC_EVENT.LINK_UPDATE.name() + ".running." + dl.getUniqueID().getID());
          dls = new HashMap<String, Object>();
          dls.put("uuid", parent.getUniqueID().getID());
          dls.put(
              "running",
              property.getValue() != null
                  || DownloadWatchDog.getInstance().hasRunningDownloads(parent));
          fire(
              BASIC_EVENT.PACKAGE_UPDATE.name() + ".running",
              dls,
              BASIC_EVENT.PACKAGE_UPDATE.name() + ".running." + parent.getUniqueID().getID());
          break;
        case ENABLED:
          dls = new HashMap<String, Object>();
          dls.put("uuid", dl.getUniqueID().getID());
          boolean enabled = dl.isEnabled();
          dls.put("enabled", enabled);
          fire(
              BASIC_EVENT.LINK_UPDATE.name() + ".enabled",
              dls,
              BASIC_EVENT.LINK_UPDATE.name() + ".enabled." + dl.getUniqueID().getID());
          dls = new HashMap<String, Object>();
          dls.put("uuid", parent.getUniqueID().getID());
          if (enabled == false) {
            final boolean readL = parent.getModifyLock().readLock();
            try {
              for (DownloadLink link : parent.getChildren()) {
                if (link.isEnabled()) {
                  enabled = true;
                  break;
                }
              }
            } finally {
              parent.getModifyLock().readUnlock(readL);
            }
          }
          dls.put("enabled", enabled);
          fire(
              BASIC_EVENT.PACKAGE_UPDATE.name() + ".enabled",
              dls,
              BASIC_EVENT.PACKAGE_UPDATE.name() + ".enabled." + parent.getUniqueID().getID());
          break;
        case EXTRACTION_STATUS:
          dls = new HashMap<String, Object>();
          dls.put("uuid", dl.getUniqueID().getID());
          ExtractionStatus es = dl.getExtractionStatus();
          dls.put("extractionStatus", es == null ? null : es.toString());
          fire(
              BASIC_EVENT.LINK_UPDATE.name() + ".extractionStatus",
              dls,
              BASIC_EVENT.LINK_UPDATE.name() + ".extractionStatus." + dl.getUniqueID().getID());

          pushStatus(dl);
          break;
        case FINAL_STATE:
          dls = new HashMap<String, Object>();
          dls.put("uuid", dl.getUniqueID().getID());
          dls.put("finished", (FinalLinkState.CheckFinished(dl.getFinalLinkState())));
          fire(
              BASIC_EVENT.LINK_UPDATE.name() + ".finished",
              dls,
              BASIC_EVENT.LINK_UPDATE.name() + ".finished." + dl.getUniqueID().getID());

          final FinalLinkState finalLinkState = dl.getFinalLinkState();

          pushStatus(dl);

          break;
        case LINKSTATUS:
          dls = new HashMap<String, Object>();
          dls.put("uuid", dl.getUniqueID().getID());
          dls.put("status", property.getValue());
          fire(
              BASIC_EVENT.LINK_UPDATE.name() + ".status",
              dls,
              BASIC_EVENT.LINK_UPDATE.name() + ".status." + dl.getUniqueID().getID());

          break;
        case MD5:
          break;
        case NAME:
          dls = new HashMap<String, Object>();
          dls.put("uuid", dl.getUniqueID().getID());
          dls.put("name", dl.getView().getDisplayName());
          fire(
              BASIC_EVENT.LINK_UPDATE.name() + ".name",
              dls,
              BASIC_EVENT.LINK_UPDATE.name() + ".name." + dl.getUniqueID().getID());

          break;
        case PLUGIN_PROGRESS:
          synchronized (linksWithPluginProgress) {
            if (dl.getPluginProgress() == null) {
              linksWithPluginProgress.remove(dl.getUniqueID().getID());
              pushDiff(dl);
              cleanup(dl);
            } else {
              linksWithPluginProgress.put(dl.getUniqueID().getID(), dl);
              updateExecuter(true);
            }
          }
          break;
        case PRIORITY:
          dls = new HashMap<String, Object>();
          dls.put("uuid", dl.getUniqueID().getID());
          dls.put(
              "priority",
              org.jdownloader.myjdownloader.client.bindings.PriorityStorable.valueOf(
                  dl.getPriorityEnum().name()));
          fire(
              BASIC_EVENT.LINK_UPDATE.name() + ".priority",
              dls,
              BASIC_EVENT.LINK_UPDATE.name() + ".priority." + dl.getUniqueID().getID());

          break;
        case RESET:
          dls = new HashMap<String, Object>();
          dls.put("uuid", dl.getUniqueID().getID());
          dls.put("reset", "true");
          fire(
              BASIC_EVENT.LINK_UPDATE.name() + ".reset",
              dls,
              BASIC_EVENT.LINK_UPDATE.name() + ".reset." + dl.getUniqueID().getID());

          dls = new HashMap<String, Object>();
          dls.put("uuid", dl.getUniqueID().getID());
          dls.put("reset", "true");
          fire(
              BASIC_EVENT.PACKAGE_UPDATE.name() + ".reset",
              dls,
              BASIC_EVENT.PACKAGE_UPDATE.name() + ".reset." + parent.getUniqueID().getID());
          break;
        case RESUMABLE:
          break;
        case SHA1:
          break;
        case SHA256:
          break;
        case SKIPPED:
          pushStatus(dl);
          dls = new HashMap<String, Object>();
          dls.put("uuid", dl.getUniqueID().getID());
          dls.put("skipped", property.getValue() != null);
          if (property.getValue() != null) {
            dls.put("skipreason", property.getValue().toString());
          }
          fire(
              BASIC_EVENT.LINK_UPDATE.name() + ".skipped",
              dls,
              BASIC_EVENT.LINK_UPDATE.name() + ".skipped." + dl.getUniqueID().getID());
          break;
        case SPEED_LIMIT:
          break;
        case URL_PROTECTION:
          break;
        case VARIANT:
          break;
        case VARIANTS:
          break;
        case VARIANTS_ENABLED:
          break;
      }
    }
    long newContentChangesCounter = DownloadController.getInstance().getContentChanges();
    if (newContentChangesCounter != this.contentChangesCounter) {
      // avoid dupes
      this.contentChangesCounter = newContentChangesCounter;
      fire(BASIC_EVENT.REFRESH_CONTENT.name(), null, BASIC_EVENT.REFRESH_CONTENT.name());
    }
    flushBuffer();
  }
Exemplo n.º 7
0
public class PollingAPIImpl implements PollingAPI {

  private DownloadWatchDog dwd = DownloadWatchDog.getInstance();
  private DownloadController dc = DownloadController.getInstance();
  private LinkCollector lc = LinkCollector.getInstance();
  private APIQuery queryParams;

  @Override
  public List<PollingResultAPIStorable> poll(APIQuery queryParams) {
    this.queryParams = queryParams;

    List<PollingResultAPIStorable> result = new ArrayList<PollingResultAPIStorable>();

    if (queryParams.containsKey("downloadProgress")) {
      result.add(getDownloadProgress());
    }
    if (queryParams.containsKey("jdState")) {
      result.add(getJDState());
    }
    if (queryParams.containsKey("linkGrabberState")) {
      result.add(getLinkGrabberState());
    }
    if (queryParams.containsKey("captchasWaiting")) {
      result.add(getCaptchasWaiting());
    }
    if (queryParams.containsKey("aggregatedNumbers")) {
      result.add(getAggregatedNumbers());
    }

    return result;
  }

  private PollingResultAPIStorable getAggregatedNumbers() {
    PollingResultAPIStorable prs = new PollingResultAPIStorable();
    prs.setEventName("aggregatedNumbers");
    org.jdownloader.myjdownloader.client.json.JsonMap eventData =
        new org.jdownloader.myjdownloader.client.json.JsonMap();
    eventData.put(
        "data",
        new AggregatedNumbersAPIStorable(
            new AggregatedNumbers(dc.getSelectionInfo()),
            new AggregatedCrawlerNumbers(lc.getSelectionInfo())));
    prs.setEventData(eventData);
    return prs;
  }

  @SuppressWarnings("rawtypes")
  private PollingResultAPIStorable getDownloadProgress() {

    // get packageUUIDs who should be filled with download progress of the containing links e.g
    // because they are expanded in the
    // view
    List<Long> expandedPackageUUIDs = new ArrayList<Long>();
    if (!queryParams._getQueryParam("downloadProgress", List.class, new ArrayList()).isEmpty()) {
      List uuidsFromQuery =
          queryParams._getQueryParam("downloadProgress", List.class, new ArrayList());
      for (Object o : uuidsFromQuery) {
        try {
          expandedPackageUUIDs.add((Long) o);
        } catch (ClassCastException e) {
          continue;
        }
      }
    }

    PollingResultAPIStorable prs = new PollingResultAPIStorable();
    prs.setEventName("downloadProgress");

    List<PollingAPIFilePackageStorable> fpas = new ArrayList<PollingAPIFilePackageStorable>();

    for (FilePackage fp : dwd.getRunningFilePackages()) {
      PollingAPIFilePackageStorable fps = new PollingAPIFilePackageStorable(fp);
      fps.setSpeed(dwd.getDownloadSpeedbyFilePackage(fp));

      // if packages is expanded in view, current state of all running links inside the package
      if (expandedPackageUUIDs.contains(fp.getUniqueID().getID())) {
        boolean readL = fp.getModifyLock().readLock();
        try {
          for (DownloadLink dl : fp.getChildren()) {
            if (dwd.getRunningDownloadLinks().contains(dl.getDownloadLinkController())) {
              PollingAPIDownloadLinkStorable pdls = new PollingAPIDownloadLinkStorable(dl);
              fps.getLinks().add(pdls);
            }
          }
        } finally {
          fp.getModifyLock().readUnlock(readL);
        }
      }
      fpas.add(fps);
    }

    org.jdownloader.myjdownloader.client.json.JsonMap eventData =
        new org.jdownloader.myjdownloader.client.json.JsonMap();
    eventData.put("data", fpas);
    prs.setEventData(eventData);

    return prs;
  }

  private PollingResultAPIStorable getJDState() {
    PollingResultAPIStorable prs = new PollingResultAPIStorable();
    prs.setEventName("jdState");

    org.jdownloader.myjdownloader.client.json.JsonMap eventData =
        new org.jdownloader.myjdownloader.client.json.JsonMap();
    eventData.put("data", dwd.getStateMachine().getState().getLabel());

    prs.setEventData(eventData);
    return prs;
  }

  private PollingResultAPIStorable getLinkGrabberState() {
    PollingResultAPIStorable prs = new PollingResultAPIStorable();
    prs.setEventName("linkGrabberState");

    LinkCollector lc = LinkCollector.getInstance();

    String status = "UNKNOWN";
    if (lc.getDefaultLinkChecker().isRunning()) {
      status = "RUNNING";
    } else {
      status = "IDLE";
    }

    org.jdownloader.myjdownloader.client.json.JsonMap eventData =
        new org.jdownloader.myjdownloader.client.json.JsonMap();
    eventData.put("data", status);
    prs.setEventData(eventData);

    return prs;
  }

  private CaptchaAPI captchaAPI = CaptchaAPISolver.getInstance();

  private PollingResultAPIStorable getCaptchasWaiting() {
    PollingResultAPIStorable prs = new PollingResultAPIStorable();
    prs.setEventName("captchasWaiting");

    org.jdownloader.myjdownloader.client.json.JsonMap eventData =
        new org.jdownloader.myjdownloader.client.json.JsonMap();
    eventData.put("data", captchaAPI.list());
    prs.setEventData(eventData);

    return prs;
  }
}