public void markForDeletion(File file) {
   TempFile tempFile = deleteMap.get(file.getPath());
   if (tempFile != null) {
     deleteQueue.remove(tempFile);
     deleteMap.remove(tempFile);
   } else {
     tempFile = new TempFile(file);
     deleteQueue.add(new TempFile(file));
     deleteMap.put(tempFile.getFile().getPath(), tempFile);
     file.deleteOnExit(); // just in case
   }
 }
Example #2
0
  static {
    threadPools.put(
        Stage.TRANSCODER,
        new ThreadPoolExecutor(
            3,
            3,
            5 * 60,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(),
            new ThreadFactory() {
              public Thread newThread(Runnable r) {
                return ThreadPools.newThread(
                    r, "TranscoderThread-" + Stage.TRANSCODER + "-" + (transSeq++));
              }
            }));
    threadPools.put(
        Stage.RECOGNIZER,
        new ThreadPoolExecutor(
            3,
            3,
            5 * 60,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(),
            new ThreadFactory() {
              public Thread newThread(Runnable r) {
                return ThreadPools.newThread(
                    r, "TranscoderThread-" + Stage.RECOGNIZER + "-" + (transSeq++));
              }
            }));

    // register them too
    ThreadPools.getThreadPools()
        .put(Executors.class.getName() + "." + Stage.TRANSCODER, threadPools.get(Stage.TRANSCODER));
    ThreadPools.getThreadPools()
        .put(Executors.class.getName() + "." + Stage.RECOGNIZER, threadPools.get(Stage.RECOGNIZER));

    // fill the rest of the map too, so we don't have to think about it any more later on.
    for (Stage s : Stage.values()) {
      if (!threadPools.containsKey(s)) {
        threadPools.put(s, ThreadPools.jobsExecutor);
      }
    }
    // default configuration, 5 + 1 executors.
    for (int i = 0; i < 5; i++) {
      executorsMap.put(new CommandExecutor.Method(), Stage.TRANSCODER);
    }
    executorsMap.put(new CommandExecutor.Method(), Stage.RECOGNIZER);
    readConfiguration();
  }
Example #3
0
  public static CommandExecutor.Method getFreeExecutor(Stage st) {
    while (true) {
      synchronized (executorsMap) {
        try {
          for (Map.Entry<CommandExecutor.Method, Stage> entry : executorsMap.entrySet()) {
            if (!entry.getKey().isInUse() && entry.getValue() == st) {
              CommandExecutor.Method m = entry.getKey();
              m.setInUse(true);
              return m;
            }
          }

          LOG.info("All executors for stage " + st + " in use (will wait..)");
          executorsMap.wait();

        } catch (InterruptedException ie) {
          return null;
        }
      }
    }
  }
 public void run() {
   Thread.currentThread().setName("deleteHelper");
   if (deleteQueue.size() > 0) {
     TempFile temp = deleteQueue.peek();
     if (temp != null && temp.getOld()) {
       logger.debug(
           "removing temporary file {tempfile='" + temp.getFile().getAbsolutePath() + "'}");
       deleteQueue.poll();
       deleteMap.remove(temp);
       try {
         temp.getFile().delete();
       } catch (Exception e) {
       }
     }
   }
 }
Example #5
0
  public void handleNotification(INotification notification) {
    //		Logger.debug("@ClientMediator.handleNotification:noti="+notification.getBody());
    String noname = notification.getName();
    if (noname == Const.TICK) {
      if (destroyd) return;
      long t = TimerCmd.timeStamp;

      String[] ary;
      int st, ed;
      if (_lastRotTank != null) {
        ary = _lastRotTank.split(" ");
        st = Integer.parseInt(ary[0]);
        ed = st + Integer.parseInt(ary[6]);
        if (t <= ed && t >= st) {
          int ist = Integer.parseInt(ary[0]);
          if (_move.size() > 0) {
            st = Integer.parseInt(_move.peek().split(" ")[0]);
            while (st < ist && _move.size() > 0) {
              _move.poll();
              if (_move.size() == 0) break;
              st = Integer.parseInt(_move.peek().split(" ")[0]);
            }
          }
        }
      }
      if (_move.size() > 0) {
        ary = _move.peek().split(" ");
        st = Integer.parseInt(ary[0]);
        ed = st + Integer.parseInt(ary[8]);

        while ((t > ed) && _move.size() > 0) {
          //					Logger.debug("@TankMediator.handleNotification: t = " + t + " st = " + st + "
          // ed="+ed);
          String p = (String) _move.poll();
          Logger.debug("ClientMediator.TICK _move.poll=" + p);
          if (_move.size() == 0) break;
          ary = _move.peek().split(" ");
          st = Integer.parseInt(ary[0]);
          ed = st + Integer.parseInt(ary[8]);
        }

        if (_move.size() > 0) {
          String[] li = (String[]) _move.toArray(new String[0]);
          for (int i = li.length - 1; i >= 0; i--) {
            String s = li[i];
            ary = s.split(" ");
            st = Integer.parseInt(ary[0]);
            ed = st + Integer.parseInt(ary[8]);
            if (t <= ed && t >= st) {
              if (t == st) { // ed==st in this situation
                _state.x = (int) Float.parseFloat(ary[4]);
                _state.y = (int) Float.parseFloat(ary[5]);
                _nextX = _state.x;
                _nextY = _state.y;
              } else {
                float sx = Float.parseFloat(ary[4]);
                float ex = Float.parseFloat(ary[6]);
                float sy = Float.parseFloat(ary[5]);
                float ey = Float.parseFloat(ary[7]);
                double eco = 1.0 * (t - st) / (ed - st);
                _state.x = (int) (sx + (ex - sx) * eco);
                _state.y = (int) (sy + (ey - sy) * eco);
                double neco = 1.0 * (t + 1 - st) / (ed - st);
                _nextX = (sx + (ex - sx) * neco);
                _nextY = (sy + (ey - sy) * neco);
              }
              while (_move.size() > 1) _move.poll();
              break;
            }
          }
        }
      }
      if (_state != null) {
        ConcurrentHashMap<Integer, BulletState> bs =
            ((ServerBulletMediator) (appFacade().retrieveMediator(Const.SERVER_BULLET_MEDIATOR)))
                .getBullets();
        for (Enumeration enu = bs.keys(); enu.hasMoreElements(); ) {
          int id = (Integer) enu.nextElement();
          BulletState blt = bs.get(id);
          if (Texter.distance(blt.x, blt.y, _state.x, _state.y) < 16) {
            this.sendNotification(Const.BROAD_CAST, "destroy bullet " + id, null);
            _state.life -= 100;
            if (_state.life <= 0) {
              this.sendNotification(Const.BROAD_CAST, "destroy tank " + _state.tankID, null);
            } else {
              this.sendNotification(
                  Const.BROAD_CAST, "set-life tank " + _state.tankID + " " + _state.life, null);
            }
          }
        }
      }

      // barrier and tank
      if (_state != null) {
        // colision detection
        //				Logger.only("ClientMediator.handleNotification: x="+_state.x+" y="+_state.y+"
        // nx="+_nextX+" ny="+_nextY+" "+Texter.sqrareDis(_nextX, _nextY, 470, 200)+"
        // "+Texter.sqrareDis(_state.x, _state.y, 470, 200));
        if (!Arena.canGo(_state.x, _state.y, _nextX, _nextY)) {
          Arena.addReq(_id, Const.STOP_MOVE, getState());
        }
        for (Enumeration enu = AcceptCmd.clients.elements(); enu.hasMoreElements(); ) {
          ClientMediator cmm = (ClientMediator) enu.nextElement();
          if (cmm.destroyd) return;
          int cmid = cmm.getID();
          if (cmid == _id) continue;
          TankState ts = cmm.getState();
          if (ts == null) continue;
          double curdis = Texter.distance(ts.x, ts.y, _state.x, _state.y);
          double netdis = Texter.distance(ts.x, ts.y, _nextX, _nextY);
          if (curdis < 40 && netdis < curdis) {
            Arena.addReq(_id, Const.STOP_MOVE, getState());
            Arena.addReq(cmid, Const.STOP_MOVE, ts);
          }
        }
      }

      // handle delayed first
      //			ary=_delayOrder.toArray(new String[0]);
      //			_delayOrder.clear();
      //			for(int i=0;i<ary.length;i++){
      //				Logger.only("delayArray: "+ary[i]);
      //				int ttt=Integer.parseInt(ary[i].split(" ")[0]);
      //				if(ttt==t){
      //					tellClientWithTime(ary[i]);
      //				}else if(ttt>t){
      //					_delayOrder.add(ary[i]);
      //				}
      //			}

    } else if (noname == Const.BROAD_CAST) {
      String s = (String) notification.getBody();
      String ary[] = s.split(" ");

      if (notification.getType() == Const.NO_PREFIX) {
        tellClientWithTime(s);
        return;
      } else if (s.indexOf("talk") > -1) {
        tellClient(s, 0);
        return;
      }
      if (_id == Integer.parseInt(ary[2])) {
        if (s.indexOf("rotate-gun") > -1) {
          _lastRotateGun = TimerCmd.timeStamp + " " + s;
          if (_state != null) _state.gunAngle = Float.parseFloat(_lastRotateGun.split(" ")[5]);
        } else if (s.indexOf("move tank") > -1) {
          Logger.debug("ClientMediator: move add s=" + s);
          int offset = Integer.parseInt(notification.getType());
          if (offset == 0) _move.clear();
          _move.add((TimerCmd.timeStamp + offset) + " " + s);
        } else if (s.indexOf("create tank") > -1) {
          _state =
              new TankState(
                  _id,
                  ary[3],
                  Integer.parseInt(ary[6]),
                  (int) Float.parseFloat(ary[8]),
                  (int) Float.parseFloat(ary[9]),
                  Float.parseFloat(ary[10]),
                  Float.parseFloat(ary[11]));
          _state.printState();
        } else if (s.indexOf("rotate-tank") > -1) {
          _lastRotTank = TimerCmd.timeStamp + " " + s;
          if (_state != null) _state.tankAngle = Float.parseFloat(_lastRotTank.split(" ")[5]);
        } else if (s.indexOf("destroy tank") > -1) {
          this.destroyd = true;
        }
      }
      if (notification.getType() != null) tellClient(s, Integer.parseInt(notification.getType()));
      else tellClient(s, 0);
    }
  }
Example #6
0
        @Override
        public void onChange(String resource) {
          try {
            LOG.debug("Reading " + resource);

            HashMap<CommandExecutor.Method, Stage> newexecutorsMap =
                new HashMap<CommandExecutor.Method, Stage>();
            Document document = getResourceLoader().getDocument(resource);
            Map<Stage, Integer> totals = new EnumMap<Stage, Integer>(Stage.class);

            if (document != null) {
              org.w3c.dom.NodeList ellist = document.getDocumentElement().getChildNodes();

              Stage prevStage = Stage.RECOGNIZER;
              for (int i = 0; i <= ellist.getLength(); i++) {
                if (ellist.item(i) instanceof Element) {
                  Element el = (Element) ellist.item(i);
                  if (el.getTagName().equals("localhost")) {
                    int max = Integer.parseInt(el.getAttribute("max_simultaneous_transcoders"));
                    Stage s = Stage.valueOf(el.getAttribute("stage").toUpperCase());
                    Integer t = totals.get(s);
                    if (t == null) t = 0;
                    t += max;
                    totals.put(s, t);
                    for (int j = 1; j <= max; j++) {
                      newexecutorsMap.put(new CommandExecutor.Method(), s);
                    }
                  } else if (el.getTagName().equals("server")) {
                    int max = Integer.parseInt(el.getAttribute("max_simultaneous_transcoders"));
                    Stage s = Stage.valueOf(el.getAttribute("stage").toUpperCase());
                    Integer t = totals.get(s);
                    if (t == null) t = 0;
                    t += max;
                    totals.put(s, t);
                    String host = el.getAttribute("host");
                    int port = Integer.parseInt(el.getAttribute("port"));
                    for (int j = 1; j <= max; j++) {
                      newexecutorsMap.put(new CommandExecutor.Method(host, port), s);
                    }
                  }
                }
              }
              for (Map.Entry<Stage, Integer> e : totals.entrySet()) {
                threadPools.get(e.getKey()).setCorePoolSize(e.getValue());
                threadPools.get(e.getKey()).setMaximumPoolSize(e.getValue());
              }
            } else {
              LOG.warn("No " + resource);
            }
            synchronized (executorsMap) {
              executorsMap.clear();
              executorsMap.putAll(newexecutorsMap);
            }
            LOG.service(
                "Reading of configuration file "
                    + resource
                    + " successfull. Executors "
                    + executorsMap
                    + ". Max simultaneous transcoders: "
                    + totals);
          } catch (Exception e) {
            LOG.error(
                e.getClass()
                    + " "
                    + e.getMessage()
                    + " In "
                    + resource
                    + " Executors now "
                    + executorsMap
                    + " (not changed)",
                e);
          }
        }
Example #7
0
 public static void notifyExecutors() {
   synchronized (executorsMap) {
     executorsMap.notifyAll();
   }
 }