From: hama Date: Tue, 10 Mar 2015 00:10:26 +0000 (+0100) Subject: TCP commands in dirtool X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=caad5320e299cc75d4c9e1d4f74503e0106a6040;p=crepublib TCP commands in dirtool --- diff --git a/os/ReDirTools.cpp b/os/ReDirTools.cpp index 1f451af..51e0cab 100644 --- a/os/ReDirTools.cpp +++ b/os/ReDirTools.cpp @@ -10,6 +10,7 @@ #include "base/rebase.hpp" #include "math/remath.hpp" #include "os/reos.hpp" +#include "net/renet.hpp" enum LOCATION_DIRTOOL { LC_COPY_FILE_1 = LC_DIRTOOLS + 1, // 50101 @@ -36,7 +37,7 @@ enum LOCATION_DIRTOOL { LC_COMPARE_DIR_1, // 50122 LC_DELETE_1, // 50123 }; -const char* ReDirTools::m_version = "2015.03.04"; +const char* ReDirTools::m_version = "2015.03.09"; ReLogger* ReDirTools::m_logger = NULL; static const char* s_helpSummary[] = { "dirtool or dt ", @@ -138,6 +139,22 @@ const char* s_syncExamples[] = { "dirtool sync --type=r --max-size=1G usr etc /media/backup", NULL }; +const char* s_tcpUsage[] = + { ": tcp [] [ ...]", + " test tool for network test", + ":", + " server", + " client [ []]", + " : URL of the server", + " : number of messages to send", + NULL }; +const char* s_tcpExamples[] = { + "dirtool tcp -p 5555 server", + "dirtool tcp -p 5555 client localhost 10000 10", + "dirtool tcp -p 5555 --buffer-size=1024 client 192.168.7.3 10 25", + NULL }; + + const char* s_touchUsage[] = { ": touch [] [ ...]", " sets the filetimes (modification and/or access time) of the selected files", @@ -2339,6 +2356,72 @@ void ReDirSync::doIt() { } } +/** + * Constructor. + * + * @param logger logger for error handling + */ +ReDirTCP::ReDirTCP(ReLogger* logger) : + ReTool(s_tcpUsage, s_tcpExamples, 1, 0, 0, true, logger) { + m_hasStandardArgs = false; + m_programArgs.addInt("size", i18n("size of the message to send/receive (in KiByte)"), + 'b', "--buffer-size", 64); + m_programArgs.addInt("port", i18n("port of the server/client"), + 'p', "--port", 58111); +} + +/** + * Lists the metadata of the specified files. + */ +void ReDirTCP::doIt() { + int port = m_programArgs.getInt("port"); + int bufferSize = m_programArgs.getInt("size") * 1024; + + const char* command = m_programArgs.getArg(0); + if (_stricmp(command, "server") == 0){ + ReTCPEchoServer server(port, m_logger); + server.listenForAll(); + } else if (_stricmp(command, "client") == 0){ + const char* ip = m_programArgs.getArg(1); + int rounds = 10; + int interval = 5; + if (m_programArgs.getArgCount() > 2) + rounds = atoi(m_programArgs.getArg(2)); + if (m_programArgs.getArgCount() > 3) + interval = atoi(m_programArgs.getArg(3)); + ReTCPClient client(m_logger); + if (client.connect(ip, port)){ + time_t start = time(NULL); + int64_t millisec; + ReByteBuffer message; + message.appendChar('x', bufferSize); + time_t lastPrint = start; + int64_t size = 0; + int duration = 0; + for (int ii = 0; ii < rounds; ii++){ + client.send("strlen", message.str(), message.length()); + size += message.length(); + time_t now = time(NULL); + if (now >= lastPrint + interval){ + duration = int(now - start); + printf("%2d: %9.3f MiByte %8.3f kiByte/sec\n", ii, + size / 1024.0 / 1024, (double) size / duration / 1024); + lastPrint = now; + } + } + duration = int(time(NULL) - start); + if (duration == 0) + duration = 1; + printf("%2d: %9.3f MiByte %8.3f kiByte/sec\n", rounds, size / 1024.0 / 1024, + (double) size / duration / 1024); + + } + } else + help("unknown subcommand: $1", command); + +} + + /** * Constructor. * @@ -2456,6 +2539,8 @@ void ReDirTools::help(int argc, const char* argv[]) { } else if (isArg("test", arg0)) { void testAll(); testAll(); + } else if (isArg("tcp", arg0)) { + ReDirTCP(m_logger).help(NULL); } else if (isArg("touch", arg0)) { ReDirTouch(m_logger).help(NULL); } else if (isArg("which", arg0)) { @@ -2513,6 +2598,8 @@ int ReDirTools::main(int argc, char* orgArgv[]) { ReDirStatistic(m_logger).run(argc, argv); else if (isArg("synchronize", arg0)) ReDirSync(m_logger).run(argc, argv); + else if (isArg("tcp", arg0)) + ReDirTCP(m_logger).run(argc, argv); else if (isArg("touch", arg0)) ReDirTouch(m_logger).run(argc, argv); else if (isArg("which", arg0)) diff --git a/os/ReDirTools.hpp b/os/ReDirTools.hpp index 9e4cde2..4b83720 100644 --- a/os/ReDirTools.hpp +++ b/os/ReDirTools.hpp @@ -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. */