Esempio n. 1
0
  public static void main(String[] args) {
    System.out.println("CanModerate Client Tools 1.0");

    try {

      System.out.println("version: " + CanModerateService.getVersion().version);
      System.out.println("server date: " + CanModerateService.getVersion().date);
      System.out.println("--------");
      System.out.println("Open connection with Oauth keys");
      RefreshTokenRequest refreshTokenRequest = new RefreshTokenRequest();
      // fill outh parameters
      refreshTokenRequest.grant_type = "refresh_token";
      refreshTokenRequest.client_id = "BBRALWUMGRRTNZIEQRWOQFET";
      refreshTokenRequest.client_secret = "432526176353";
      refreshTokenRequest.refresh_token = "84d0a86d-2354-416f-a249-e5451848b643";
      RefreshTokenResponse refreshTokenResponse =
          CanModerateService.refreshToken(refreshTokenRequest);

      System.out.println("Access token: " + refreshTokenResponse.access_token);

      MessageRequest message = new MessageRequest();
      message.clientIp = "10.0.0.1";
      message.trackingId = UUID.randomUUID().toString();

      message.message = "test message";

      MessageResponse messageResponse =
          CanModerateService.validateMessage(refreshTokenResponse.access_token, message);
      System.out.println("Response trackingId: " + messageResponse.trackingId);
      for (MessageResult result : messageResponse.results) {
        System.out.println("Result: " + result.result + " type: " + result.vcType);
      }

    } catch (Exception e) {
      System.out.print("Error: ");
      System.out.println(e.getMessage());
    }
  }
  private void send(MessageStartView startView) {
    LogWriter.log(
        replica.getReplicaID(),
        "Sending message STARTVIEW to Replica "
            + receiverID
            + Constants.NEWLINE
            + startView.toString());
    DataOutputStream dataOutput = null;
    try {
      dataOutput = new DataOutputStream(clientSocket.getOutputStream());
      byte[] messageIDBytes = MyByteUtils.toByteArray(startView.getMessageID());
      dataOutput.writeInt(messageIDBytes.length);
      dataOutput.write(messageIDBytes);
      byte[] viewNumberBytes = MyByteUtils.toByteArray(startView.getViewNumber());
      dataOutput.writeInt(viewNumberBytes.length);
      dataOutput.write(viewNumberBytes);
      // log
      ReplicaLog log = startView.getLog();
      if (log != null && log.size() > 0) {
        dataOutput.writeInt(log.size());
        for (int i = 0; i < log.size(); i++) {
          // BEGINING OF REQUEST
          // --------------------------------------------------------------------
          MessageRequest request = log.get(i).getRequest();
          byte[] requestMessageIDBytes = MyByteUtils.toByteArray(request.getMessageID());
          dataOutput.writeInt(requestMessageIDBytes.length);
          dataOutput.write(requestMessageIDBytes);
          byte[] operationIDBytes =
              MyByteUtils.toByteArray(request.getOperation().getOperationID());
          dataOutput.writeInt(operationIDBytes.length);
          dataOutput.write(operationIDBytes);
          byte[] operationPathBytes = MyByteUtils.toByteArray(request.getOperation().getPath());
          dataOutput.writeInt(operationPathBytes.length);
          dataOutput.write(operationPathBytes);
          if (request.getOperation().getOperationID() == 1) {
            byte[] operationFile = request.getOperation().getFile();
            if (operationFile != null) {
              dataOutput.writeInt(operationFile.length);
              dataOutput.write(operationFile);
            } else {
              dataOutput.writeInt(1);
              byte[] nullFile = new byte[1];
              nullFile[0] = 0;
              dataOutput.write(nullFile);
            }
          }
          byte[] clientIDBytes = MyByteUtils.toByteArray(request.getClientID());
          dataOutput.writeInt(clientIDBytes.length);
          dataOutput.write(clientIDBytes);
          byte[] requestNumberBytes = MyByteUtils.toByteArray(request.getRequestNumber());
          dataOutput.writeInt(requestNumberBytes.length);
          dataOutput.write(requestNumberBytes);
          byte[] requestViewNumberBytes = MyByteUtils.toByteArray(request.getViewNumber());
          dataOutput.writeInt(requestViewNumberBytes.length);
          dataOutput.write(requestViewNumberBytes);
          // END OF REQUEST --------------------------------------------------------------------
          // opNumber
          byte[] operationNumberBytes = MyByteUtils.toByteArray(log.get(i).getOperationNumber());
          dataOutput.writeInt(operationNumberBytes.length);
          dataOutput.write(operationNumberBytes);
          // isCommited
          byte[] isCommitedBytes = MyByteUtils.toByteArray(log.get(i).isCommited());
          dataOutput.writeInt(isCommitedBytes.length);
          dataOutput.write(isCommitedBytes);
        }
      } else {
        dataOutput.writeInt(0);
        dataOutput.writeInt(1);
        byte[] nullFile = new byte[1];
        nullFile[0] = 0;
        dataOutput.write(nullFile);
      }

      byte[] lastCommitedBytes = MyByteUtils.toByteArray(startView.getLastCommited());
      dataOutput.writeInt(lastCommitedBytes.length);
      dataOutput.write(lastCommitedBytes);
    } catch (IOException ex) {
      Logger.getLogger(ReplicaClientRunnable.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
      try {
        dataOutput.flush();
        dataOutput.close();
        clientSocket.close();
      } catch (IOException ex) {
        Logger.getLogger(ReplicaClientRunnable.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
  }