Small refactor of the Java agent's TCP client
[lttng-ust.git] / liblttng-ust-java-agent / java / lttng-ust-agent-common / org / lttng / ust / agent / client / LttngTcpSessiondClient.java
index d7ed6da4af582f29c4d63ac3669c25b2cfa903b7..3e9e24dd65463d2100d7d8095595919a24cddbcb 100644 (file)
@@ -48,7 +48,6 @@ public class LttngTcpSessiondClient implements Runnable {
        private static int protocolMinorVersion = 0;
 
        /** Command header from the session deamon. */
-       private final SessiondHeaderCommand headerCmd = new SessiondHeaderCommand();
        private final CountDownLatch registrationLatch = new CountDownLatch(1);
 
        private Socket sessiondSock;
@@ -147,54 +146,92 @@ public class LttngTcpSessiondClient implements Runnable {
                }
        }
 
-       /**
-        * Receive header data from the session daemon using the LTTng command
-        * static buffer of the right size.
-        */
-       private void recvHeader() throws IOException {
-               byte data[] = new byte[SessiondHeaderCommand.HEADER_SIZE];
+       private void connectToSessiond() throws IOException {
+               int port;
 
-               int readLen = this.inFromSessiond.read(data, 0, data.length);
-               if (readLen != data.length) {
-                       throw new IOException();
+               if (this.isRoot) {
+                       port = getPortFromFile(ROOT_PORT_FILE);
+                       if (port == 0) {
+                               /* No session daemon available. Stop and retry later. */
+                               throw new IOException();
+                       }
+               } else {
+                       port = getPortFromFile(getHomePath() + USER_PORT_FILE);
+                       if (port == 0) {
+                               /* No session daemon available. Stop and retry later. */
+                               throw new IOException();
+                       }
                }
-               this.headerCmd.populate(data);
+
+               this.sessiondSock = new Socket(SESSION_HOST, port);
+               this.inFromSessiond = new DataInputStream(sessiondSock.getInputStream());
+               this.outToSessiond = new DataOutputStream(sessiondSock.getOutputStream());
+       }
+
+       private static String getHomePath() {
+               return System.getProperty("user.home");
        }
 
        /**
-        * Receive payload from the session daemon. This MUST be done after a
-        * recvHeader() so the header value of a command are known.
+        * Read port number from file created by the session daemon.
         *
-        * The caller SHOULD use isPayload() before which returns true if a payload
-        * is expected after the header.
+        * @return port value if found else 0.
         */
-       private byte[] recvPayload() throws IOException {
-               byte payload[] = new byte[(int) this.headerCmd.getDataSize()];
+       private static int getPortFromFile(String path) throws IOException {
+               int port;
+               BufferedReader br = null;
 
-               /* Failsafe check so we don't waste our time reading 0 bytes. */
-               if (payload.length == 0) {
-                       return null;
+               try {
+                       br = new BufferedReader(new FileReader(path));
+                       String line = br.readLine();
+                       port = Integer.parseInt(line, 10);
+                       if (port < 0 || port > 65535) {
+                               /* Invalid value. Ignore. */
+                               port = 0;
+                       }
+               } catch (FileNotFoundException e) {
+                       /* No port available. */
+                       port = 0;
+               } finally {
+                       if (br != null) {
+                               br.close();
+                       }
                }
 
-               this.inFromSessiond.read(payload, 0, payload.length);
-               return payload;
+               return port;
+       }
+
+       private void registerToSessiond() throws IOException {
+               byte data[] = new byte[16];
+               ByteBuffer buf = ByteBuffer.wrap(data);
+               String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
+
+               buf.putInt(logAgent.getDomain().value());
+               buf.putInt(Integer.parseInt(pid));
+               buf.putInt(protocolMajorVersion);
+               buf.putInt(protocolMinorVersion);
+               this.outToSessiond.write(data, 0, data.length);
+               this.outToSessiond.flush();
        }
 
        /**
         * Handle session command from the session daemon.
         */
        private void handleSessiondCmd() throws IOException {
-               byte data[] = null;
+               /* Data read from the socket */
+               byte inputData[] = null;
+               /* Reply data written to the socket, sent to the sessiond */
+               byte responseData[] = null;
 
                while (true) {
                        /* Get header from session daemon. */
-                       recvHeader();
+                       SessiondCommandHeader cmdHeader = recvHeader();
 
-                       if (headerCmd.getDataSize() > 0) {
-                               data = recvPayload();
+                       if (cmdHeader.getDataSize() > 0) {
+                               inputData = recvPayload(cmdHeader);
                        }
 
-                       switch (headerCmd.getCommandType()) {
+                       switch (cmdHeader.getCommandType()) {
                        case CMD_REG_DONE:
                        {
                                /*
@@ -210,125 +247,82 @@ public class LttngTcpSessiondClient implements Runnable {
                        }
                        case CMD_LIST:
                        {
-                               SessiondListLoggersResponse listLoggerCmd = new SessiondListLoggersResponse();
-                               listLoggerCmd.execute(logAgent);
-                               data = listLoggerCmd.getBytes();
+                               ISessiondCommand listLoggerCmd = new SessiondListLoggersCommand();
+                               ILttngAgentResponse response = listLoggerCmd.execute(logAgent);
+                               responseData = response.getBytes();
                                break;
                        }
                        case CMD_ENABLE:
                        {
-                               SessiondEnableHandler enableCmd = new SessiondEnableHandler();
-                               if (data == null) {
-                                       enableCmd.code = ISessiondResponse.LttngAgentRetCode.CODE_INVALID_CMD;
+                               if (inputData == null) {
+                                       /* Invalid command */
+                                       responseData = ILttngAgentResponse.FAILURE_RESPONSE.getBytes();
                                        break;
                                }
-                               enableCmd.populate(data);
-                               enableCmd.execute(logAgent);
-                               data = enableCmd.getBytes();
+                               ISessiondCommand enableCmd = new SessiondEnableEventCommand(inputData);
+                               ILttngAgentResponse response = enableCmd.execute(logAgent);
+                               responseData = response.getBytes();
                                break;
                        }
                        case CMD_DISABLE:
                        {
-                               SessiondDisableHandler disableCmd = new SessiondDisableHandler();
-                               if (data == null) {
-                                       disableCmd.setRetCode(ISessiondResponse.LttngAgentRetCode.CODE_INVALID_CMD);
+                               if (inputData == null) {
+                                       /* Invalid command */
+                                       responseData = ILttngAgentResponse.FAILURE_RESPONSE.getBytes();
                                        break;
                                }
-                               disableCmd.populate(data);
-                               disableCmd.execute(logAgent);
-                               data = disableCmd.getBytes();
+                               ISessiondCommand disableCmd = new SessiondDisableEventCommand(inputData);
+                               ILttngAgentResponse response = disableCmd.execute(logAgent);
+                               responseData = response.getBytes();
                                break;
                        }
                        default:
                        {
-                               data = new byte[4];
-                               ByteBuffer buf = ByteBuffer.wrap(data);
+                               /* Unknown command, send empty reply */
+                               responseData = new byte[4];
+                               ByteBuffer buf = ByteBuffer.wrap(responseData);
                                buf.order(ByteOrder.BIG_ENDIAN);
                                break;
                        }
                        }
 
-                       if (data == null) {
-                               /*
-                                * Simply used to silence a potential null access warning below.
-                                *
-                                * The flow analysis gets confused here and thinks "data" may be
-                                * null at this point. It should not happen according to program
-                                * logic, if it does we've done something wrong.
-                                */
-                               throw new IllegalStateException();
-                       }
-                       /* Send payload to session daemon. */
-                       this.outToSessiond.write(data, 0, data.length);
+                       /* Send response to the session daemon. */
+                       this.outToSessiond.write(responseData, 0, responseData.length);
                        this.outToSessiond.flush();
                }
        }
 
-       private static String getHomePath() {
-               return System.getProperty("user.home");
-       }
-
        /**
-        * Read port number from file created by the session daemon.
-        *
-        * @return port value if found else 0.
+        * Receive header data from the session daemon using the LTTng command
+        * static buffer of the right size.
         */
-       private static int getPortFromFile(String path) throws IOException {
-               int port;
-               BufferedReader br = null;
+       private SessiondCommandHeader recvHeader() throws IOException {
+               byte data[] = new byte[SessiondCommandHeader.HEADER_SIZE];
 
-               try {
-                       br = new BufferedReader(new FileReader(path));
-                       String line = br.readLine();
-                       port = Integer.parseInt(line, 10);
-                       if (port < 0 || port > 65535) {
-                               /* Invalid value. Ignore. */
-                               port = 0;
-                       }
-               } catch (FileNotFoundException e) {
-                       /* No port available. */
-                       port = 0;
-               } finally {
-                       if (br != null) {
-                               br.close();
-                       }
+               int readLen = this.inFromSessiond.read(data, 0, data.length);
+               if (readLen != data.length) {
+                       throw new IOException();
                }
-
-               return port;
+               return new SessiondCommandHeader(data);
        }
 
-       private void connectToSessiond() throws IOException {
-               int port;
+       /**
+        * Receive payload from the session daemon. This MUST be done after a
+        * recvHeader() so the header value of a command are known.
+        *
+        * The caller SHOULD use isPayload() before which returns true if a payload
+        * is expected after the header.
+        */
+       private byte[] recvPayload(SessiondCommandHeader headerCmd) throws IOException {
+               byte payload[] = new byte[(int) headerCmd.getDataSize()];
 
-               if (this.isRoot) {
-                       port = getPortFromFile(ROOT_PORT_FILE);
-                       if (port == 0) {
-                               /* No session daemon available. Stop and retry later. */
-                               throw new IOException();
-                       }
-               } else {
-                       port = getPortFromFile(getHomePath() + USER_PORT_FILE);
-                       if (port == 0) {
-                               /* No session daemon available. Stop and retry later. */
-                               throw new IOException();
-                       }
+               /* Failsafe check so we don't waste our time reading 0 bytes. */
+               if (payload.length == 0) {
+                       return null;
                }
 
-               this.sessiondSock = new Socket(SESSION_HOST, port);
-               this.inFromSessiond = new DataInputStream(sessiondSock.getInputStream());
-               this.outToSessiond = new DataOutputStream(sessiondSock.getOutputStream());
+               this.inFromSessiond.read(payload, 0, payload.length);
+               return payload;
        }
 
-       private void registerToSessiond() throws IOException {
-               byte data[] = new byte[16];
-               ByteBuffer buf = ByteBuffer.wrap(data);
-               String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
-
-               buf.putInt(logAgent.getDomain().value());
-               buf.putInt(Integer.parseInt(pid));
-               buf.putInt(protocolMajorVersion);
-               buf.putInt(protocolMinorVersion);
-               this.outToSessiond.write(data, 0, data.length);
-               this.outToSessiond.flush();
-       }
 }
This page took 0.027313 seconds and 5 git commands to generate.