]> gitweb.hamatoma.de Git - crepublib/commitdiff
TCP commands in dirtool
authorhama <hama@siduction.net>
Tue, 10 Mar 2015 00:10:26 +0000 (01:10 +0100)
committerhama <hama@siduction.net>
Tue, 10 Mar 2015 00:10:26 +0000 (01:10 +0100)
os/ReDirTools.cpp
os/ReDirTools.hpp

index 1f451afa64ab51f77a0259ff93cce9035e8d74b0..51e0cab934a2859d6c29d50de1a4b73d0fa64b48 100644 (file)
@@ -10,6 +10,7 @@
 #include "base/rebase.hpp"\r
 #include "math/remath.hpp"\r
 #include "os/reos.hpp"\r
+#include "net/renet.hpp"\r
 \r
 enum LOCATION_DIRTOOL {\r
        LC_COPY_FILE_1 = LC_DIRTOOLS + 1, // 50101\r
@@ -36,7 +37,7 @@ enum LOCATION_DIRTOOL {
        LC_COMPARE_DIR_1,                       // 50122\r
        LC_DELETE_1,                            // 50123\r
 };\r
-const char* ReDirTools::m_version = "2015.03.04";\r
+const char* ReDirTools::m_version = "2015.03.09";\r
 ReLogger* ReDirTools::m_logger = NULL;\r
 \r
 static const char* s_helpSummary[] = { "dirtool or dt <command> <opts>",\r
@@ -138,6 +139,22 @@ const char* s_syncExamples[] = {
     "dirtool sync --type=r --max-size=1G usr etc /media/backup",\r
     NULL };\r
 \r
+const char* s_tcpUsage[] =\r
+    { "<command>: tcp [<opts>] <subcommand> [<param> ...]",\r
+        "   test tool for network test",\r
+        "<subcommand>:",\r
+        "   server",\r
+        "   client <ip> [<rounds> [<print_interval>]]",\r
+        "   <ip>: URL of the server",\r
+        "   <rounds>: number of messages to send",\r
+        NULL };\r
+const char* s_tcpExamples[] = {\r
+    "dirtool tcp -p 5555 server",\r
+    "dirtool tcp -p 5555 client localhost 10000 10",\r
+    "dirtool tcp -p 5555 --buffer-size=1024 client 192.168.7.3 10 25",\r
+    NULL };\r
+\r
+\r
 const char* s_touchUsage[] =\r
     { "<command>: touch [<opts>] <dir_or_file1> [<dir_or_file2> ...]",\r
         "   sets the filetimes (modification and/or access time) of the selected files",\r
@@ -2339,6 +2356,72 @@ void ReDirSync::doIt() {
        }\r
 }\r
 \r
+/**\r
+ * Constructor.\r
+ *\r
+ * @param logger       logger for error handling\r
+ */\r
+ReDirTCP::ReDirTCP(ReLogger* logger) :\r
+           ReTool(s_tcpUsage, s_tcpExamples, 1, 0, 0, true, logger) {\r
+       m_hasStandardArgs = false;\r
+       m_programArgs.addInt("size", i18n("size of the message to send/receive (in KiByte)"),\r
+           'b', "--buffer-size", 64);\r
+       m_programArgs.addInt("port", i18n("port of the server/client"),\r
+           'p', "--port", 58111);\r
+}\r
+\r
+/**\r
+ * Lists the metadata of the specified files.\r
+ */\r
+void ReDirTCP::doIt() {\r
+       int port = m_programArgs.getInt("port");\r
+       int bufferSize = m_programArgs.getInt("size") * 1024;\r
+\r
+       const char* command = m_programArgs.getArg(0);\r
+       if (_stricmp(command, "server") == 0){\r
+               ReTCPEchoServer server(port, m_logger);\r
+               server.listenForAll();\r
+       } else  if (_stricmp(command, "client") == 0){\r
+               const char* ip = m_programArgs.getArg(1);\r
+               int rounds = 10;\r
+               int interval = 5;\r
+               if (m_programArgs.getArgCount() > 2)\r
+                       rounds = atoi(m_programArgs.getArg(2));\r
+               if (m_programArgs.getArgCount() > 3)\r
+                       interval = atoi(m_programArgs.getArg(3));\r
+               ReTCPClient client(m_logger);\r
+               if (client.connect(ip, port)){\r
+                       time_t start = time(NULL);\r
+                       int64_t millisec;\r
+                       ReByteBuffer message;\r
+                       message.appendChar('x', bufferSize);\r
+                       time_t lastPrint = start;\r
+                       int64_t size = 0;\r
+                       int duration = 0;\r
+                       for (int ii = 0; ii < rounds; ii++){\r
+                               client.send("strlen", message.str(), message.length());\r
+                               size += message.length();\r
+                               time_t now = time(NULL);\r
+                               if (now >= lastPrint + interval){\r
+                                       duration = int(now - start);\r
+                                       printf("%2d: %9.3f MiByte %8.3f kiByte/sec\n", ii,\r
+                                               size / 1024.0 / 1024, (double) size / duration / 1024);\r
+                                       lastPrint = now;\r
+                               }\r
+                       }\r
+                       duration = int(time(NULL) - start);\r
+                       if (duration == 0)\r
+                               duration = 1;\r
+                       printf("%2d: %9.3f MiByte %8.3f kiByte/sec\n", rounds, size / 1024.0 / 1024,\r
+                               (double) size / duration / 1024);\r
+\r
+               }\r
+       } else\r
+               help("unknown subcommand: $1", command);\r
+\r
+}\r
+\r
+\r
 /**\r
  * Constructor.\r
  *\r
@@ -2456,6 +2539,8 @@ void ReDirTools::help(int argc, const char* argv[]) {
                } else if (isArg("test", arg0)) {\r
                        void testAll();\r
                        testAll();\r
+               } else if (isArg("tcp", arg0)) {\r
+                       ReDirTCP(m_logger).help(NULL);\r
                } else if (isArg("touch", arg0)) {\r
                        ReDirTouch(m_logger).help(NULL);\r
                } else if (isArg("which", arg0)) {\r
@@ -2513,6 +2598,8 @@ int ReDirTools::main(int argc, char* orgArgv[]) {
                ReDirStatistic(m_logger).run(argc, argv);\r
        else if (isArg("synchronize", arg0))\r
                ReDirSync(m_logger).run(argc, argv);\r
+       else if (isArg("tcp", arg0))\r
+               ReDirTCP(m_logger).run(argc, argv);\r
        else if (isArg("touch", arg0))\r
                ReDirTouch(m_logger).run(argc, argv);\r
        else if (isArg("which", arg0))\r
index 9e4cde2f225a71e74e8544a1ddfd66ad1929aa00..4b837205c09f9a537a27b86a7313d2a00711939e 100644 (file)
@@ -282,6 +282,17 @@ protected:
        ReByteBuffer m_buffer;
 };
 
+/**
+ * TCP server and client.
+ */
+class ReDirTCP: public ReTool {
+public:
+       ReDirTCP(ReLogger* logger);
+protected:
+       virtual void doIt();
+};
+
+
 /**
  * Changes the filetime of files.
  */