예제 #1
0
 /**
  * Returns user feedback.
  *
  * @param info information string
  * @param ok success/error flag
  * @throws IOException I/O exception
  */
 private void info(final String info, final boolean ok) throws IOException {
   // write feedback to log file
   log.write(this, ok ? OK : ERROR_C + info, perf);
   // send {MSG}0 and (0|1) as (success|error) flag
   out.writeString(info);
   send(ok);
 }
예제 #2
0
 /**
  * Executes the specified command.
  *
  * @param cmd command to be executed
  * @throws IOException I/O exception
  */
 private void execute(final Command cmd) throws IOException {
   log.write(this, cmd + " [...]");
   final DecodingInput di = new DecodingInput(in);
   try {
     cmd.setInput(di);
     cmd.execute(context);
     success(cmd.info());
   } catch (final BaseXException ex) {
     di.flush();
     error(ex.getMessage());
   }
 }
예제 #3
0
  /** Exits the session. */
  public synchronized void quit() {
    running = false;
    if (log != null) log.write(this, "LOGOUT " + context.user.name, OK);

    // wait until running command was stopped
    if (command != null) {
      command.stop();
      while (command != null) Performance.sleep(50);
    }
    context.delete(this);

    try {
      new Close().execute(context);
      socket.close();
      if (events) {
        esocket.close();
        // remove this session from all events in pool
        for (final Sessions s : context.events.values()) s.remove(this);
      }
    } catch (final Exception ex) {
      if (log != null) log.write(ex.getMessage());
      Util.stack(ex);
    }
  }
예제 #4
0
  @Override
  public void run() {
    // initialize the session via cram-md5 authentication
    try {
      final String ts = Long.toString(System.nanoTime());
      final byte[] address = socket.getInetAddress().getAddress();

      // send {TIMESTAMP}0
      out = PrintOutput.get(socket.getOutputStream());
      out.print(ts);
      send(true);

      // evaluate login data
      in = new BufferInput(socket.getInputStream());
      // receive {USER}0{PASSWORD}0
      final String us = in.readString();
      final String pw = in.readString();
      context.user = context.users.get(us);
      running = context.user != null && md5(string(context.user.password) + ts).equals(pw);

      // write log information
      if (running) {
        log.write(this, "LOGIN " + context.user.name, OK);
        // send {OK}
        send(true);
        server.unblock(address);
        context.add(this);
      } else {
        if (!us.isEmpty()) log.write(this, ACCESS_DENIED + COLS + us);
        new ClientDelayer(server.block(address), this, server).start();
      }
    } catch (final IOException ex) {
      Util.stack(ex);
      log.write(ex.getMessage());
      return;
    }
    if (!running) return;

    // authentification done, start command loop
    ServerCmd sc = null;
    String cmd = null;

    try {
      while (running) {
        command = null;
        try {
          final int b = in.read();
          if (b == -1) {
            // end of stream: exit session
            quit();
            break;
          }

          last = System.currentTimeMillis();
          perf.time();
          sc = ServerCmd.get(b);
          cmd = null;
          if (sc == ServerCmd.CREATE) {
            create();
          } else if (sc == ServerCmd.ADD) {
            add();
          } else if (sc == ServerCmd.WATCH) {
            watch();
          } else if (sc == ServerCmd.UNWATCH) {
            unwatch();
          } else if (sc == ServerCmd.REPLACE) {
            replace();
          } else if (sc == ServerCmd.STORE) {
            store();
          } else if (sc != ServerCmd.COMMAND) {
            query(sc);
          } else {
            // database command
            cmd = new ByteList().add(b).add(in.readBytes()).toString();
          }
        } catch (final IOException ex) {
          // this exception may be thrown if a session is stopped
          quit();
          break;
        }
        if (sc != ServerCmd.COMMAND) continue;

        // parse input and create command instance
        try {
          command = new CommandParser(cmd, context).parseSingle();
        } catch (final QueryException ex) {
          // log invalid command
          final String msg = ex.getMessage();
          log.write(this, cmd, ERROR_C + msg);
          // send 0 to mark end of potential result
          out.write(0);
          // send {INFO}0
          out.writeString(msg);
          // send 1 to mark error
          send(false);
          continue;
        }

        // start timeout
        command.startTimeout(context.mprop.num(MainProp.TIMEOUT));
        log.write(this, command.toString().replace('\r', ' ').replace('\n', ' '));

        // execute command and send {RESULT}
        boolean ok = true;
        String info;
        try {
          command.execute(context, new EncodingOutput(out));
          info = command.info();
        } catch (final BaseXException ex) {
          ok = false;
          info = ex.getMessage();
          if (info.startsWith(INTERRUPTED)) info = TIMEOUT_EXCEEDED;
        }
        // stop timeout
        command.stopTimeout();

        // send 0 to mark end of result
        out.write(0);
        // send info
        info(info, ok);

        // stop console
        if (command instanceof Exit) {
          command = null;
          quit();
        }
      }
    } catch (final IOException ex) {
      log.write(this, sc == ServerCmd.COMMAND ? cmd : sc, ERROR_C + ex.getMessage());
      Util.debug(ex);
      command = null;
      quit();
    }
    command = null;
  }
예제 #5
0
  /**
   * Processes the query iterator.
   *
   * @param sc server command
   * @throws IOException I/O exception
   */
  private void query(final ServerCmd sc) throws IOException {
    // iterator argument (query or identifier)
    String arg = in.readString();

    String err = null;
    try {
      final QueryListener qp;
      if (sc == ServerCmd.QUERY) {
        final String query = arg;
        qp = new QueryListener(query, context);
        arg = Integer.toString(id++);
        queries.put(arg, qp);
        // send {ID}0
        out.writeString(arg);
        // write log file
        log.write(this, sc + "(" + arg + ')', query, OK, perf);
      } else {
        // find query process
        qp = queries.get(arg);

        // ID has already been removed
        if (qp == null) {
          if (sc != ServerCmd.CLOSE) throw new IOException("Unknown Query ID: " + arg);
        } else if (sc == ServerCmd.BIND) {
          final String key = in.readString();
          final String val = in.readString();
          final String typ = in.readString();
          qp.bind(key, val, typ);
          log.write(this, sc + "(" + arg + ')', key, val, typ, OK, perf);
        } else if (sc == ServerCmd.ITER) {
          qp.execute(true, out, true);
        } else if (sc == ServerCmd.EXEC) {
          qp.execute(false, out, true);
        } else if (sc == ServerCmd.INFO) {
          out.print(qp.info());
        } else if (sc == ServerCmd.OPTIONS) {
          out.print(qp.options());
        } else if (sc == ServerCmd.CLOSE) {
          queries.remove(arg);
        } else if (sc == ServerCmd.NEXT) {
          throw new Exception("Protocol for query iteration is out-of-dated.");
        }
        // send 0 as end marker
        out.write(0);
      }
      // send 0 as success flag
      out.write(0);
      // write log file (bind and execute have been logged before)
      if (sc != ServerCmd.BIND) log.write(this, sc + "(" + arg + ')', OK, perf);
    } catch (final Exception ex) {
      // log exception (static or runtime)
      err = ex.getMessage();
      log.write(this, sc + "(" + arg + ')', ERROR_C + err);
      queries.remove(arg);
    }
    if (err != null) {
      // send 0 as end marker, 1 as error flag, and {MSG}0
      out.write(0);
      out.write(1);
      out.writeString(err);
    }
    out.flush();
  }