forked from ravinet/mahimahi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_memory_store.cc
54 lines (42 loc) · 1.79 KB
/
http_memory_store.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* -*-mode:c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "http_memory_store.hh"
#include "http_record.pb.h"
#include "file_descriptor.hh"
#include "http_header.hh"
#include "int32.hh"
#include "file_descriptor.hh"
using namespace std;
HTTPMemoryStore::HTTPMemoryStore()
: mutex_(),
requests(),
responses()
{}
void HTTPMemoryStore::save( const HTTPResponse & response, const Address & server_address )
{
unique_lock<mutex> ul( mutex_ );
cout << "Request to: " << server_address.ip() << endl;
/* Add the current request to requests BulkMessage and current response to responses BulkMessage */
requests.add_msg()->CopyFrom( response.request().toprotobuf() );
responses.add_msg()->CopyFrom( response.toprotobuf() );
}
void HTTPMemoryStore::serialize_to_socket( Socket && client )
{
/* bulk response format: # of pairs, request protobuf size, request protobuf, # of pairs, response protobuf size, response protobuf */
unique_lock<mutex> ul( mutex_ );
/* make sure there is a 1 to 1 matching of requests and responses */
assert( requests.msg_size() == responses.msg_size() );
/* Serialize both request and response to String for transmission */
string all_requests;
string all_responses;
requests.SerializeToString( &all_requests );
responses.SerializeToString( &all_responses );
/* Put sizes and messages into bulk response formats (one for request, one for response) */
string requests_ret = static_cast<string>( Integer32( all_requests.size() ) ) + all_requests;
string responses_ret = static_cast<string>( Integer32( all_responses.size() ) ) + all_responses;
client.write( requests_ret );
client.write( responses_ret );
}