コード例 #1
0
  public Response sendRequest(Request request) throws IOException {
    byte[] requestData = request.getBytes();
    dataOutputStream.writeInt(requestData.length);
    dataOutputStream.write(requestData);
    dataOutputStream.flush();

    byte responseType = dataInputStream.readByte();
    switch (responseType) {
      case Response.TYPE_SENT_OK:
        return new SendMessageResponse(dataInputStream.readDouble());
      case Response.TYPE_SENT_NOT_OK:
        return new SendMessageResponse(dataInputStream.readDouble());
      case Response.TYPE_EMPTY:
        return new EmptyResponse();
      case Response.TYPE_MESSAGE:
        return new MessageResponse(
            dataInputStream.readInt(),
            dataInputStream.readInt(),
            dataInputStream.readUTF(),
            dataInputStream.readDouble());
      case Response.TYPE_QUEUE_CREATED:
        return new CreateQueueResponse(dataInputStream.readInt());
      case Response.TYPE_QUEUE_DELETED:
        return new DeleteQueueResponse(dataInputStream.readBoolean());
      case Response.TYPE_QUEUE_QUERY:
        ArrayList<Integer> queues = new ArrayList<Integer>();
        int size = dataInputStream.readInt();
        for (int i = 0; i < size; ++i) {
          queues.add(dataInputStream.readInt());
        }
        return new QueryQueuesResponse(queues);
      default:
        return new InvalidRequestResponse();
    }
  }
コード例 #2
0
  /**
   * Processamento das mensagens através da execução da operação correspondente. Geração de uma
   * mensagem de resposta.
   *
   * @param request mensagem com o pedido e (eventualmente) os parâmetros necessários para a
   *     realização da operação requerida sobre o monitor
   * @return mensagem de resposta
   * @throws MessageRequestException
   */
  @Override
  public Response processAndReply(Request request) throws MessageRequestException {
    Response response;
    int passageiroId = 0;
    switch (request.getMethodName()) {
      case GO_HOME:
        if (request.getArgs().length != 1)
          throw new MessageRequestException(
              "Formato do request GO_HOME inválido: " + "espera-se 1 parametro!", request);
        passageiroId = (int) request.getArgs()[0];
        if (passageiroId < 0 || passageiroId >= passMax)
          throw new MessageRequestException("Id do passageiro inválido!", request);
        transicao.goHome(passageiroId);
        break;

      case PREPARE_NEXT_LEG:
        if (request.getArgs().length != 1)
          throw new MessageRequestException(
              "Formato do request PREPARE_NEXT_LEG inválido: " + "espera-se 1 parametro!", request);
        passageiroId = (int) request.getArgs()[0];
        if (passageiroId < 0 || passageiroId >= passMax)
          throw new MessageRequestException("Id do passageiro inválido!", request);
        transicao.prepareNextLeg(passageiroId);
        break;
      case BAGAGEIRO_DONE:
        System.out.println("Bagageiro acabou!(ServerInterface)");
        transicao.bagageiroDone();
        break;
      case SHUTDOWN_MONITOR:
        return new Response(OK, request.getSerial(), transicao.shutdownMonitor());
      default:
        throw new MessageRequestException("Tipo de request inválido!", request);
    }
    return new Response(OK, request.getSerial(), null);
  }