You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by ma...@apache.org on 2018/04/23 07:30:41 UTC

[trafficserver] 02/02: Add "output" option to quic client

This is an automated email from the ASF dual-hosted git repository.

masaori pushed a commit to branch quic-latest
in repository https://gitbox.apache.org/repos/asf/trafficserver.git

commit 98ebf16b32524dea30b042d9b823c3cb98bd5044
Author: Masaori Koshiba <ma...@apache.org>
AuthorDate: Mon Apr 23 16:20:27 2018 +0900

    Add "output" option to quic client
    
    STDOUT is default destination.
---
 cmd/traffic_quic/quic_client.cc  | 50 +++++++++++++++++++++++++++++++++-------
 cmd/traffic_quic/quic_client.h   |  6 ++++-
 cmd/traffic_quic/traffic_quic.cc |  1 +
 3 files changed, 48 insertions(+), 9 deletions(-)

diff --git a/cmd/traffic_quic/quic_client.cc b/cmd/traffic_quic/quic_client.cc
index 7f10fd4..5710b78 100644
--- a/cmd/traffic_quic/quic_client.cc
+++ b/cmd/traffic_quic/quic_client.cc
@@ -23,7 +23,10 @@
 
 #include "quic_client.h"
 
-QUICClient::~QUICClient(const QUICClientConfig *config) : Continuation(new_ProxyMutex()), _config(config)
+#include <iostream>
+#include <fstream>
+
+QUICClient::QUICClient(const QUICClientConfig *config) : Continuation(new_ProxyMutex()), _config(config)
 {
   SET_HANDLER(&QUICClient::start);
 }
@@ -80,7 +83,13 @@ QUICClient::state_http_server_open(int event, void *data)
     Debug("quic_client", "start proxy server ssn/txn");
 
     QUICNetVConnection *conn = static_cast<QUICNetVConnection *>(data);
-    QUICClientApp *app       = new QUICClientApp(conn);
+
+    const char *filename = nullptr;
+    if (this->_config->output[0] != 0x0) {
+      filename = this->_config->output;
+    }
+
+    QUICClientApp *app = new QUICClientApp(conn, filename);
     app->start(this->_config->path);
 
     break;
@@ -106,7 +115,7 @@ QUICClient::state_http_server_open(int event, void *data)
 #define QUICClientAppDebug(fmt, ...) \
   Debug("quic_client_app", "[%" PRIx64 "] " fmt, static_cast<uint64_t>(this->_qc->connection_id()), ##__VA_ARGS__)
 
-QUICClientApp::QUICClientApp(QUICNetVConnection *qvc) : QUICApplication(qvc)
+QUICClientApp::QUICClientApp(QUICNetVConnection *qvc, const char *filename) : QUICApplication(qvc), _filename(filename)
 {
   this->_qc->stream_manager()->set_default_application(this);
 
@@ -116,6 +125,11 @@ QUICClientApp::QUICClientApp(QUICNetVConnection *qvc) : QUICApplication(qvc)
 void
 QUICClientApp::start(const char *path)
 {
+  if (this->_filename) {
+    // Destroy contents if file already exists
+    std::ofstream f_stream(this->_filename, std::ios::binary | std::ios::trunc);
+  }
+
   QUICStreamId stream_id;
   QUICErrorUPtr error = this->_qc->stream_manager()->create_bidi_stream(stream_id);
 
@@ -152,13 +166,33 @@ QUICClientApp::main_event_handler(int event, Event *data)
 
   switch (event) {
   case VC_EVENT_READ_READY:
-  case VC_EVENT_READ_COMPLETE:
-    if (stream_io->is_read_avail_more_than(0)) {
-      uint8_t response[1024] = {0};
-      stream_io->read(response, sizeof(response));
-      QUICClientAppDebug("\n%s", response);
+  case VC_EVENT_READ_COMPLETE: {
+    std::streambuf *default_stream = nullptr;
+    std::ofstream f_stream;
+
+    if (this->_filename) {
+      default_stream = std::cout.rdbuf();
+      f_stream       = std::ofstream(this->_filename, std::ios::binary | std::ios::app);
+      std::cout.rdbuf(f_stream.rdbuf());
     }
+
+    while (stream_io->is_read_avail_more_than(0)) {
+      uint8_t buf[8192] = {0};
+      int64_t len       = stream_io->get_read_buffer_reader()->block_read_avail();
+      len               = std::min(len, (int64_t)sizeof(buf));
+      stream_io->read(buf, len);
+
+      std::cout.write(reinterpret_cast<char *>(buf), len);
+    }
+    std::cout.flush();
+
+    if (this->_filename) {
+      f_stream.close();
+      std::cout.rdbuf(default_stream);
+    }
+
     break;
+  }
   case VC_EVENT_WRITE_READY:
   case VC_EVENT_WRITE_COMPLETE:
     break;
diff --git a/cmd/traffic_quic/quic_client.h b/cmd/traffic_quic/quic_client.h
index 8a13a49..0e3e534 100644
--- a/cmd/traffic_quic/quic_client.h
+++ b/cmd/traffic_quic/quic_client.h
@@ -32,6 +32,7 @@
 
 struct QUICClientConfig {
   char addr[1024]       = "127.0.0.1";
+  char output[1024]     = {0};
   char port[16]         = "4433";
   char path[1018]       = "/";
   char debug_tags[1024] = "quic";
@@ -54,8 +55,11 @@ private:
 class QUICClientApp : public QUICApplication
 {
 public:
-  QUICClientApp(QUICNetVConnection *qvc);
+  QUICClientApp(QUICNetVConnection *qvc, const char *filename);
 
   void start(const char *path);
   int main_event_handler(int event, Event *data);
+
+private:
+  const char *_filename = nullptr;
 };
diff --git a/cmd/traffic_quic/traffic_quic.cc b/cmd/traffic_quic/traffic_quic.cc
index 7e85b38..ec81a45 100644
--- a/cmd/traffic_quic/traffic_quic.cc
+++ b/cmd/traffic_quic/traffic_quic.cc
@@ -53,6 +53,7 @@ main(int argc, const char **argv)
 
   const ArgumentDescription argument_descriptions[] = {
     {"addr", 'a', "Address", "S1023", config.addr, nullptr, nullptr},
+    {"output", 'o', "Write to FILE instead of stdout", "S1023", config.output, nullptr, nullptr},
     {"port", 'p', "Port", "S15", config.port, nullptr, nullptr},
     {"path", 'P', "Path", "S1017", config.path, nullptr, nullptr},
     {"debug", 'T', "Vertical-bar-separated Debug Tags", "S1023", config.debug_tags, nullptr, nullptr},

-- 
To stop receiving notification emails like this one, please contact
masaori@apache.org.