Exemplo n.º 1
0
  public boolean execute(ShellContext<MessageCounter> context) {
    ShellServer<MessageCounter> shellServer = context.getShellServer();
    MessageCounter msgCounter = context.getOpaqueData();
    PrintStream out = context.getOutputStream();

    msgCounter.stop();

    if (shellServer != null) {
      // interrupt all shell threads
      shellServer.interruptAllInterruptible();

      // interrupt shell server thread
      shellServer.stopServSockThread();
    }

    out.print("halted." + Shell.CRLF);
    out.flush();

    return true;
  }
  public boolean execute(ShellContext<EmulatorContext> context) {
    EmulatorContext cxt = context.getOpaqueData();
    PrintStream out = context.getOutputStream();
    String[] args = context.getArguments();

    if (args.length < 2) {
      out.println("Usage: " + this.getHelp());

      if (context.isInteractive()) return false;
      else return true;
    }

    // parse arguments
    long startTime = 0L, interval = 0L;
    int times = 1;
    boolean timeIsDifferential = false;

    StringTokenizer st = new StringTokenizer(args[0], ",");

    String timeStr = st.nextToken();
    if (timeStr.startsWith("+")) { // +<start time>
      timeIsDifferential = true;
      timeStr = timeStr.substring(1);
    }
    startTime = parseLong(timeStr);

    try {
      interval = parseLong(st.nextToken());
      times = 0; // indicates endless repeat

      times = Integer.parseInt(st.nextToken());
    } catch (NoSuchElementException e) {
    }

    // parse the another command
    Map<String, Command<EmulatorContext>> commandTable = context.getShellServer().getCommandTable();

    Command<EmulatorContext> command = commandTable.get(args[1]);
    if (command == null) {
      out.println("No such command: " + args[1]);
      if (context.isInteractive()) return false;
      else return true;
    }

    SchedulableCommand<EmulatorContext> schedulableCommand = null;
    try {
      schedulableCommand = (SchedulableCommand<EmulatorContext>) command;
    } catch (ClassCastException e) {
      out.println("Command is not schedulable: " + args[1]);
      if (context.isInteractive()) return false;
      else return true;
    }

    // schedule
    String[] taskArgs = new String[args.length - 2];
    System.arraycopy(args, 2, taskArgs, 0, taskArgs.length);
    context.setArguments(taskArgs);

    EmulatorTask task = null;
    try {
      task = schedulableCommand.getEmulatorTask(context);
    } catch (Exception e) {
      out.println("An exception thrown during parsing command: " + args[1]);
      if (context.isInteractive()) return false;
      else return true;
    }

    this.schedule(cxt, startTime, interval, times, task, timeIsDifferential);

    if (cxt.getEmulatorMode() != EmulatorMode.WORKER && cxt.getVerboseInParsing()) {
      out.print("schedule: " + schedulableCommand.getNames()[0]);
      for (int i = 0; i < taskArgs.length; i++) {
        out.print(" " + taskArgs[i]);
      }
      out.println();
    }

    return false;
  }