From d8ba79e57506210df56fbb33434f036839d7a661 Mon Sep 17 00:00:00 2001 From: 1evergreen Date: Mon, 1 Apr 2024 13:58:23 +0000 Subject: [PATCH] string implemented buffer in ByteStream --- apps/webget.cc | 15 +++++++++++++-- src/byte_stream.cc | 29 +++++++++++++++++++---------- src/byte_stream.hh | 8 ++++++++ 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/apps/webget.cc b/apps/webget.cc index 87165b3..f5b1054 100644 --- a/apps/webget.cc +++ b/apps/webget.cc @@ -9,8 +9,19 @@ using namespace std; void get_URL( const string& host, const string& path ) { - cerr << "Function called: get_URL(" << host << ", " << path << ")\n"; - cerr << "Warning: get_URL() has not been implemented yet.\n"; + // cerr << "Function called: get_URL(" << host << ", " << path << ")\n"; + // cerr << "Warning: get_URL() has not been implemented yet.\n"; + TCPSocket tcpsock; + tcpsock.connect(Address(host ,"http")); + string request_header = "GET " + path + " HTTP/1.1\r\nHost: " + host + "\r\nConnection: close\r\n\r\n"; + tcpsock.write(request_header); + string buf; + while(!tcpsock.eof()){ + buf.clear(); + tcpsock.read(buf); + cout << buf; + } + tcpsock.shutdown(SHUT_RDWR); } int main( int argc, char* argv[] ) diff --git a/src/byte_stream.cc b/src/byte_stream.cc index 04f3adc..6acc39d 100644 --- a/src/byte_stream.cc +++ b/src/byte_stream.cc @@ -2,64 +2,73 @@ using namespace std; -ByteStream::ByteStream( uint64_t capacity ) : capacity_( capacity ) {} +ByteStream::ByteStream( uint64_t capacity ) : capacity_( capacity ) ,closed_(false), +total_pushed_(0), total_poped_(0), buffer(){} bool Writer::is_closed() const { // Your code here. - return {}; + return closed_; } void Writer::push( string data ) { // Your code here. - (void)data; + if(is_closed() || has_error() || data.empty()){ + return; + } + LEN_T l = min(data.size(), available_capacity()); + buffer.append(data.substr(0, l)); + total_pushed_ += l; return; } void Writer::close() { // Your code here. + closed_ = true; } uint64_t Writer::available_capacity() const { // Your code here. - return {}; + return capacity_ - buffer.size(); } uint64_t Writer::bytes_pushed() const { // Your code here. - return {}; + return total_pushed_; } bool Reader::is_finished() const { // Your code here. - return {}; + return closed_ && bytes_buffered() == 0; } uint64_t Reader::bytes_popped() const { // Your code here. - return {}; + return total_poped_; } string_view Reader::peek() const { // Your code here. - return {}; + return {buffer}; } void Reader::pop( uint64_t len ) { // Your code here. - (void)len; + LEN_T l = min(len, buffer.size()); + buffer.erase(0, l); + total_poped_ += l; } uint64_t Reader::bytes_buffered() const { // Your code here. - return {}; + return buffer.size(); } diff --git a/src/byte_stream.hh b/src/byte_stream.hh index f8df2ad..4bef790 100644 --- a/src/byte_stream.hh +++ b/src/byte_stream.hh @@ -4,12 +4,14 @@ #include #include +using namespace std; class Reader; class Writer; class ByteStream { public: + using LEN_T = uint64_t; explicit ByteStream( uint64_t capacity ); // Helper functions (provided) to access the ByteStream's Reader and Writer interfaces @@ -25,6 +27,12 @@ protected: // Please add any additional state to the ByteStream here, and not to the Writer and Reader interfaces. uint64_t capacity_; bool error_ {}; + + bool closed_; + LEN_T total_pushed_; + LEN_T total_poped_; + string buffer; + }; class Writer : public ByteStream