Add a hack for starting at the _start_ of backlog. Useful for fetching samples of...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 28 Apr 2013 16:48:24 +0000 (18:48 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 28 Apr 2013 16:48:24 +0000 (18:48 +0200)
client.h
server.cpp

index c73810d..cd909e3 100644 (file)
--- a/client.h
+++ b/client.h
@@ -63,7 +63,10 @@ struct Client {
        size_t header_or_error_bytes_sent;
 
        // Number of bytes we are into the stream (ie., the end of last send).
-       // Only relevant for SENDING_DATA.
+       // -1 means we want to send from the end of the backlog (the normal case),
+       // -2 means we want to send from the _beginning_ of the backlog.
+       // Once we go into SENDING_DATA, these negative values will be translated
+       // to real numbers.
        size_t stream_pos;
 
        // Number of bytes we've sent of data. Only relevant for SENDING_DATA.
index 1f5477e..7acf501 100644 (file)
@@ -384,7 +384,15 @@ sending_header_or_error_again:
                // but we'll start sending immediately as we get data.
                // This is postcondition #3.
                client->state = Client::SENDING_DATA;
-               client->stream_pos = client->stream->bytes_received;
+               if (client->stream_pos == size_t(-2)) {
+                       client->stream_pos = std::min<size_t>(
+                           client->stream->bytes_received - client->stream->backlog_size,
+                           0);
+               } else {
+                       // client->stream_pos should be -1, but it might not be,
+                       // if we have clients from an older version.
+                       client->stream_pos = client->stream->bytes_received;
+               }
                client->stream->put_client_to_sleep(client);
                return;
        }
@@ -476,7 +484,15 @@ int Server::parse_request(Client *client)
                return 400;  // Should maybe be 405 instead?
        }
 
-       map<string, int>::const_iterator url_map_it = url_map.find(request_tokens[1]);
+       string url = request_tokens[1];
+       if (url.find("?backlog") == url.size() - 8) {
+               client->stream_pos = -2;
+               url = url.substr(0, url.size() - 8);
+       } else {
+               client->stream_pos = -1;
+       }
+
+       map<string, int>::const_iterator url_map_it = url_map.find(url);
        if (url_map_it == url_map.end()) {
                return 404;  // Not found.
        }