@Override
  public void onDownloadControllerAddedPackage(FilePackage pkg) {
    HashMap<String, Object> dls = new HashMap<String, Object>();
    long afterUuid = -1l;
    PackageController<FilePackage, DownloadLink> controller = pkg.getControlledBy();
    if (controller != null) {
      boolean locked = controller.readLock();
      try {
        int index = controller.indexOf(pkg);
        if (index > 0) {
          FilePackage fp = controller.getPackages().get(index - 1);
          if (fp != null) {
            afterUuid = fp.getUniqueID().getID();
          }
        }
      } finally {
        controller.readUnlock(locked);
      }
    }
    dls.put("uuid", pkg.getUniqueID().getID());
    dls.put("afterUuid", afterUuid);

    fire(BASIC_EVENT.ADD_CONTENT.name(), null, BASIC_EVENT.ADD_CONTENT.name());
    fire(
        BASIC_EVENT.ADD_PACKAGE.name(),
        dls,
        BASIC_EVENT.ADD_PACKAGE.name() + "." + pkg.getUniqueID().getID());
    flushBuffer();
  }
 /** save the current FilePackages/DownloadLinks controlled by this DownloadController */
 public void saveDownloadLinks() {
   if (isSaveAllowed() == false) return;
   java.util.List<FilePackage> packages = null;
   final boolean readL = this.readLock();
   try {
     packages = new ArrayList<FilePackage>();
     for (Iterator iterator = this.packages.iterator(); iterator.hasNext(); ) {
       FilePackage p1 = (FilePackage) iterator.next();
       boolean exists = false;
       for (Iterator iterator2 = packages.iterator(); iterator2.hasNext(); ) {
         FilePackage p2 = (FilePackage) iterator2.next();
         if (p1.getUniqueID().equals(p2.getUniqueID())) exists = true;
       }
       if (!exists) packages.add(p1);
     }
   } finally {
     readUnlock(readL);
   }
   /* save as new Json ZipFile */
   try {
     save(packages, null);
   } catch (IOException e) {
     logger.log(e);
   }
 }
  private void pushDiff(DownloadLink dl) {

    for (Entry<Long, ChannelCollector> es : collectors.entrySet()) {
      if (es.getValue().hasIntervalSubscriptions()) {

        es.getValue().setLastPush(System.currentTimeMillis());

        HashMap<String, Object> diff = es.getValue().getDiff(dl);

        for (Entry<String, Object> entry : diff.entrySet()) {
          HashMap<String, Object> dls = new HashMap<String, Object>();
          dls.put("uuid", dl.getUniqueID().getID());
          dls.put(entry.getKey(), entry.getValue());
          EventObject eventObject =
              new SimpleEventObject(
                  DownloadControllerEventPublisher.this,
                  BASIC_EVENT.LINK_UPDATE.name() + "." + entry.getKey(),
                  dls,
                  BASIC_EVENT.LINK_UPDATE.name()
                      + "."
                      + entry.getKey()
                      + "."
                      + dl.getUniqueID().getID());
          // List<Long> publishTo = new ArrayList<Long>();
          // publishTo.add(es.getValue().getSubscriber().getSubscriptionID());
          pushToBuffer(es.getValue().getSubscriber(), eventObject);
        }

        FilePackage p = dl.getParentNode();
        if (p != null) {
          diff = es.getValue().getDiff(p);

          for (Entry<String, Object> entry : diff.entrySet()) {
            HashMap<String, Object> dls = new HashMap<String, Object>();
            dls.put("uuid", p.getUniqueID().getID());
            dls.put(entry.getKey(), entry.getValue());
            SimpleEventObject eventObject =
                new SimpleEventObject(
                    DownloadControllerEventPublisher.this,
                    BASIC_EVENT.PACKAGE_UPDATE.name() + "." + entry.getKey(),
                    dls,
                    BASIC_EVENT.LINK_UPDATE.name()
                        + "."
                        + entry.getKey()
                        + "."
                        + p.getUniqueID().getID());
            // List<Long> publishTo = new ArrayList<Long>();
            // publishTo.add(es.getValue().getSubscriber().getSubscriptionID());
            pushToBuffer(es.getValue().getSubscriber(), eventObject);
            // for (RemoteAPIEventsSender eventSender : remoteEventSenders) {
            // eventSender.publishEvent(eventObject, publishTo);
            // }
          }
        }
      }
    }
  }
 @Override
 public void onDownloadControllerRemovedPackage(FilePackage pkg) {
   HashMap<String, Object> dls = new HashMap<String, Object>();
   dls.put("uuid", pkg.getUniqueID().getID());
   fire(BASIC_EVENT.REMOVE_CONTENT.name(), null, null);
   fire(
       BASIC_EVENT.REMOVE_PACKAGE.name(),
       dls,
       BASIC_EVENT.REMOVE_PACKAGE.name() + "." + pkg.getUniqueID().getID());
   flushBuffer();
 }
Esempio n. 5
0
  @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;
  }
  @Override
  public void onDownloadControllerUpdatedData(FilePackage pkg, FilePackageProperty property) {

    if (property != null) {
      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]

      switch (property.getProperty()) {
        case COMMENT:
          dls = new HashMap<String, Object>();
          dls.put("uuid", pkg.getUniqueID().getID());
          dls.put("comment", pkg.getComment());
          fire(
              BASIC_EVENT.PACKAGE_UPDATE,
              FilePackageProperty.Property.COMMENT.toString(),
              dls,
              pkg.getUniqueID());
          break;
        case FOLDER:
          break;
        case NAME:
          dls = new HashMap<String, Object>();
          dls.put("uuid", pkg.getUniqueID().getID());
          dls.put("name", pkg.getName());
          fire(
              BASIC_EVENT.PACKAGE_UPDATE.name() + ".name",
              dls,
              BASIC_EVENT.PACKAGE_UPDATE.name() + ".name." + pkg.getUniqueID().getID());
          break;
        case PRIORITY:
          dls = new HashMap<String, Object>();
          dls.put("uuid", pkg.getUniqueID().getID());
          dls.put(
              "priority",
              org.jdownloader.myjdownloader.client.bindings.PriorityStorable.valueOf(
                  pkg.getPriorityEnum().name()));
          fire(
              BASIC_EVENT.PACKAGE_UPDATE.name() + ".priority",
              dls,
              BASIC_EVENT.PACKAGE_UPDATE.name() + ".priority." + pkg.getUniqueID().getID());
      }
    }

    fire(BASIC_EVENT.REFRESH_CONTENT.name(), null, BASIC_EVENT.REFRESH_CONTENT.name());
    flushBuffer();
  }
 public void setUID(long id) {
   filePackage.getUniqueID().setID(id);
 }
 public long getUID() {
   return filePackage.getUniqueID().getID();
 }
  @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();
  }