/**
  * Notification that a message has been received. If we just connected, we'll do a quick handshake
  * to verify the client, then we just pass the rest on our our output panel.
  *
  * @param message the message that was received.
  */
 public void messageReceived(MessageObject message) {
   lastMessageReceived = message;
   if (waitingOnHandshakeCompletion) // are we still handshaking?
   {
     if (ProtocolConstants.HANDSHAKE_CLIENT.equalsIgnoreCase(message.getMessage())) {
       waitingOnHandshakeCompletion = false; // we've received what we expected
       hasCompletedConnection = true; // and we're now connected
       if (message.getData() != null) {
         killGradleServerPort = (Integer) message.getData();
         killGradleClientProcotol = new KillGradleClientProtocol();
         killGradleClient = new ClientProcess(killGradleClientProcotol);
         killGradleClient.start(killGradleServerPort);
         handShakeCompleted();
       } else {
         addStatus("Invalid handshaking. Missing port number. Stopping connection");
         server.sendMessage("?", "Invalid client handshake protocol!");
         closeConnection();
       }
     } else {
       addStatus("Invalid handshaking. Stopping connection");
       server.sendMessage("?", "Invalid client handshake protocol!");
       closeConnection();
     }
   } else // otherwise, its just a normal message, the protocol should handle it.
   {
     try {
       handleMessageReceived(message);
     } catch (Throwable e) {
       logger.error("Problem while handing message :\n" + message, e);
     }
   }
 }
Exemple #2
0
    /**
     * When this is called, execute the given request.
     *
     * @param request the request to execute.
     */
    public void execute(final Request request) {

      // mark this request as being currently executed
      currentlyExecutingRequests.add(request);

      notifyAboutToExecuteRequest(request);

      // I'm just putting these in temp variables for easier debugging
      File currentDirectory = getCurrentDirectory();
      File gradleHomeDirectory = getGradleHomeDirectory();
      File customGradleExecutor = getCustomGradleExecutor();

      // the protocol handles the command line to launch gradle and messaging between us and said
      // externally launched gradle.
      ProcessLauncherServer.Protocol serverProtocol =
          request.createServerProtocol(
              logLevel,
              stackTraceLevel,
              currentDirectory,
              gradleHomeDirectory,
              customGradleExecutor);

      // the server kicks off gradle as an external process and manages the communication with said
      // process
      ProcessLauncherServer server = new ProcessLauncherServer(serverProtocol);
      request.setProcessLauncherServer(server);

      // we need to know when this command is finished executing so we can mark it as complete and
      // notify any observers.
      server.addServerObserver(
          new ProcessLauncherServer.ServerObserver() {
            public void clientExited(int result, String output) {
              currentlyExecutingRequests.remove(request);
              notifyRequestExecutionComplete(request, result, output);
            }

            public void serverExited() {}
          },
          false);

      server.start();
    }
  /**
   * Notification that the client has shutdown. Note: this can occur before communciations has ever
   * started. You SHOULD get this notification before receiving serverExited, even if the client
   * fails to launch or locks up.
   *
   * @param returnCode the return code of the client application
   * @param output the standard error and standard output of the client application
   */
  public void clientExited(int returnCode, String output) {
    server.requestShutdown();

    boolean wasPremature = false;
    String message;

    if (!hasCompletedConnection) // if we never connected, report it
    {
      message = "Failed to connect to gradle process for command '" + commandLine + "'\n" + output;
      wasPremature = true;
    } else if (!hasReceivedBuildCompleteNotification) // this may happen if the client doesn't
                                                      // execute properly or it was killed/canceled.
                                                      // This is just so we don't lose our output
                                                      // (which may yeild clues to the problem).
    {
      message = output;
      wasPremature = true;
    } else {
      message = output;
    }

    reportClientExit(wasPremature, returnCode, message);
  }
 /** Notification that the connection was accepted by the client. */
 public void connectionAccepted() {
   // let's make sure we're talking to the right client with some tiny handshaking.
   server.sendMessage(ProtocolConstants.HANDSHAKE_TYPE, ProtocolConstants.HANDSHAKE_SERVER);
   continueConnection = true;
   waitingOnHandshakeCompletion = true;
 }