Example #1
0
    public void addItems(List<String> names, List<T> items) {
      if (!locationMap.isEmpty()) {
        throw new IllegalStateException("addItems can only be called once for any given Lattice");
      }
      Queue<String> nameQueue = new LinkedList<String>(names);
      Queue<T> itemQueue = new LinkedList<T>(items);
      for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
          for (int k = 0; k < size; k++) {
            if (nameQueue.isEmpty() || itemQueue.isEmpty()) return;
            String name = nameQueue.remove();
            T item = itemQueue.remove();
            locationMap.put(name, new PVector(i, j, k));
            contentsMap.put(name, item);
          }
        }
      }

      if (!nameQueue.isEmpty()) {
        log.warn(
            "Could not add all items to lattice of size "
                + size
                + ". Items remaining: "
                + nameQueue.size());
      }
    }
Example #2
0
  @Override
  public void run() {

    while (true) {

      switch (playState) {
        case BUFFERING:
          bufferToNextPosition();
          break;

        case PLAYING:
          Date currDate = new Date();
          long elapsed = (int) ((currDate.getTime() - lastSliceRequestDate.getTime()) * playSpeed);
          this.lastSliceRequestDate = currDate;

          // Check if we've been truncated, and move forward if necessary
          if (totalElapsed < timeline.getFirstOffset()) {
            log.info(
                "Elapsed time ({}) occurs before the current timeline ({})",
                totalElapsed,
                timeline.getFirstOffset());

            long position = elapsed;
            boolean noMatch = true;
            while (noMatch) {
              if (position != timeline.getFirstOffset()) {
                position = timeline.getFirstOffset();
                try {
                  Thread.sleep(500);
                } catch (InterruptedException e) {
                  // Ignore
                }
              } else {
                noMatch = false;
              }
            }
            log.info("Will buffer to new position: {}", position);
            bufferAtPosition(position);
            break;
          }

          // Update actors and build a usage map
          Map<String, Integer> slotsUsedByUser = new HashMap<String, Integer>();
          Iterator<? extends Actor> i = getJobActors().values().iterator();
          while (i.hasNext()) {
            Actor actor = i.next();
            if (actor instanceof JobActor) {
              JobActor jobActor = (JobActor) actor;
              if (jobActor.defunct) {
                removeJobActor(jobActor.name, jobActor);
              } else {
                int slots = 1;
                if (jobActor.queued) {
                  // If a job is queued then it is represented by a single sprite, so we need the
                  // actual number of slots
                  GridJob job = state.getJobByFullId(jobActor.name);
                  if (job != null) {
                    slots = job.getSlots();
                  }
                }

                if (jobActor.getName().contains(":")) {
                  // Parse a jobId like this: 1275988.2828-4000:1
                  try {
                    Pattern p = Pattern.compile("(\\d+)\\.(\\d+)-(\\d+):(\\d+)");
                    Matcher m = p.matcher(jobActor.getName());
                    if (m.matches()) {
                      int start = Integer.parseInt(m.group(2));
                      int end = Integer.parseInt(m.group(3));
                      int interval = Integer.parseInt(m.group(4));
                      slots = (end - start) / interval;
                    }
                  } catch (Exception e) {
                    log.error("Error parsing jobId: " + jobActor.getName(), e);
                  }
                } else if (jobActor.getName().contains(",")) {
                  // Parse a jobId like this: 2968157.205,211
                  try {
                    Pattern p = Pattern.compile("(\\d+)\\.(\\d+),(\\d+)");
                    Matcher m = p.matcher(jobActor.getName());
                    if (m.matches()) {
                      int first = Integer.parseInt(m.group(2));
                      int second = Integer.parseInt(m.group(3));
                      // There are two jobs listed here, so we require twice the number of slots
                      slots *= 2;
                    }
                  } catch (Exception e) {
                    log.error("Error parsing jobId: " + jobActor.getName(), e);
                  }
                }

                String user = jobActor.getUsername();
                if (!slotsUsedByUser.containsKey(user)) {
                  slotsUsedByUser.put(user, 0);
                }
                if (!jobActor.queued) {
                  slotsUsedByUser.put(user, slotsUsedByUser.get(user) + slots);
                }
              }
            }
          }

          legend.retain(slotsUsedByUser);

          updateState(elapsed);

          break;

        case READY:
          break;

        case PAUSED:
          break;

        case END:
          return;

        default:
          log.error("Invalid play state: " + playState);
          break;
      }

      try {
        Thread.sleep(50);
      } catch (InterruptedException e) {
        // Ignore
      }
    }
  }
Example #3
0
 public T getContents(String name) {
   return contentsMap.get(name);
 }
Example #4
0
 public PVector getLocation(String name) {
   return locationMap.get(name);
 }