Percy++
A C++ implementation of Private Information Retrieval (PIR) protocols
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
percyclient.h
1 // Percy++ Copyright 2007,2012,2013,2014
2 // Ian Goldberg <iang@cs.uwaterloo.ca>,
3 // Casey Devet <cjdevet@uwaterloo.ca>,
4 // Paul Hendry <pshendry@uwaterloo.ca>,
5 // Ann Yang <y242yang@uwaterloo.ca>,
6 // Ryan Henry <rhenry@cs.uwaterloo.ca>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of version 2 of the GNU General Public License as
10 // published by the Free Software Foundation.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // There is a copy of the GNU General Public License in the COPYING file
18 // packaged with this plugin; if you cannot find it, write to the Free
19 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 // 02110-1301 USA
21 
22 #ifndef __PERCYCLIENT_H__
23 #define __PERCYCLIENT_H__
24 
25 #include <vector>
26 #include <set>
27 #include <iostream>
28 #include <string.h>
29 #include "percyresult.h"
30 #include "percyparams.h"
31 #include "percystats.h"
32 #include "percyio.h"
33 
35 class PercyClient {
36 public:
38  virtual ~PercyClient ();
39 
51  nservers_t num_servers, nservers_t t, sid_t * sids = NULL,
52  PercyStats * stats = NULL);
53 
59  nqueries_t encode_request (vector<dbsize_t> block_numbers,
60  nqueries_t querybsize = 1);
61 
69  dbsize_t send_request(nqueries_t request_identifier,
70  std::vector<ostream*> &osvec, bool send_num_queries = true);
71 
77  dbsize_t receive_replies(nqueries_t request_identifier,
78  std::vector<istream*> &isvec);
79 
85  nqueries_t process_replies(nservers_t h);
86 
87  // If the request is finished (fully and successfully processed by
88  // process_replies), return true and put results in the results vector.
89  // The results are then removed from the server (no longer stored in this
90  // class). Otherwise, return false and nothing is done to results.
97  bool get_result (nqueries_t request_identifier,
98  vector<PercyBlockResults> & results);
99 
114  bool fetch_blocks(nqueries_t& request_identifier,
115  vector<dbsize_t> block_numbers, vector<ostream*> &osvec,
116  vector<istream*> &isvec, vector<PercyBlockResults> &results,
117  nqueries_t querybsize = 1);
118 
119 protected:
120  friend class RecursiveClient;
121 
125  const vector<dbsize_t>& get_block_numbers (nqueries_t request_identifier) {
126  return requested_blocks[request_identifier];
127  }
128 
134  nqueries_t get_qbs (nqueries_t request_identifier) {
135  return blocks_per_query[request_identifier];
136  }
137 
140  virtual void encode_request_impl (nqueries_t request_identifier) = 0;
141 
149  virtual dbsize_t send_request_impl (nqueries_t request_identifier,
150  vector<ostream*> &osvec, bool send_num_queries = true) = 0;
151 
157  virtual dbsize_t receive_replies_impl (nqueries_t request_identifier,
158  vector<istream*> &isvec) = 0;
159 
165  virtual nqueries_t process_replies_impl (nservers_t h,
166  vector<vector<PercyResult> >& results) = 0;
167 
177  PercyClient (const PercyClientParams * clientparams, nservers_t num_servers,
178  nservers_t t, PercyStats * stats = NULL);
179 
183  nservers_t num_servers;
186  nservers_t t;
188  vector<nservers_t> goodservers;
190  static const bool randomize = true;
191 
192 private:
193  typedef map<nqueries_t, vector<dbsize_t> > BlockNumbers;
194  typedef map<nqueries_t, vector<PercyBlockResults> > BlockResults;
195  enum RequestStatus { ENCODE, SEND, RECEIVE, DECODE, INCOMPLETE, DONE };
196  typedef map<nqueries_t, RequestStatus> RequestStatuses;
197 
198  nqueries_t next_request_identifier;
199  BlockNumbers requested_blocks;
200  std::map<nqueries_t, nqueries_t> blocks_per_query;
201  RequestStatuses statuses;
202  BlockNumbers undecoded_blocks;
203  BlockResults decoded_blocks;
204 
205  PercyStats * stats;
206  map<nqueries_t, nqueries_t> request_id_to_stats_batch_number;
207 };
208 
209 inline ostream& operator<<(ostream &out, vector<dbsize_t> v) {
210  out << "<";
211  for (vector<dbsize_t>::const_iterator i=v.begin();
212  i != v.end(); ++i) {
213  out << " " << *i;
214  }
215  out << " >";
216  return out;
217 }
218 
219 #endif
Client parameters.
Definition: percyparams.h:189
vector< nservers_t > goodservers
The server indices of servers that have not acted up.
Definition: percyclient.h:188
Definition: percystats.h:66
const PercyClientParams * clientparams
Parameters for the client.
Definition: percyclient.h:181
bool fetch_blocks(nqueries_t &request_identifier, vector< dbsize_t > block_numbers, vector< ostream * > &osvec, vector< istream * > &isvec, vector< PercyBlockResults > &results, nqueries_t querybsize=1)
Do encode_request(), send_request(), receive_replies(), process_replies(), and get_result() all in on...
virtual dbsize_t receive_replies_impl(nqueries_t request_identifier, vector< istream * > &isvec)=0
Implementation for encode_request().
PercyClient(const PercyClientParams *clientparams, nservers_t num_servers, nservers_t t, PercyStats *stats=NULL)
Constructor.
dbsize_t send_request(nqueries_t request_identifier, std::vector< ostream * > &osvec, bool send_num_queries=true)
Send the request for the given request ID.
bool get_result(nqueries_t request_identifier, vector< PercyBlockResults > &results)
Get the result for a given request ID.
static const bool randomize
Randomize the requests for IT-PIR clients.
Definition: percyclient.h:190
nservers_t t
The privacy level.
Definition: percyclient.h:186
A PIR client for recursive PIR protocols.
Definition: recursiveclient.h:30
virtual nqueries_t process_replies_impl(nservers_t h, vector< vector< PercyResult > > &results)=0
Implementation for encode_request().
nqueries_t process_replies(nservers_t h)
Process the servers' replies for all undecoded replies.
static PercyClient * make_client(const PercyClientParams *clientparams, nservers_t num_servers, nservers_t t, sid_t *sids=NULL, PercyStats *stats=NULL)
Factory method called to get a client object for the given parameters.
const vector< dbsize_t > & get_block_numbers(nqueries_t request_identifier)
Get the block numbers for a given request ID.
Definition: percyclient.h:125
nservers_t num_servers
The number of servers.
Definition: percyclient.h:183
dbsize_t receive_replies(nqueries_t request_identifier, std::vector< istream * > &isvec)
Receive the servers' replies for a given request ID.
nqueries_t encode_request(vector< dbsize_t > block_numbers, nqueries_t querybsize=1)
Encode a request for the given block numbers.
nqueries_t get_qbs(nqueries_t request_identifier)
Get the number of blocks to request in a single query for a given request ID.
Definition: percyclient.h:134
virtual ~PercyClient()
Destructor.
virtual void encode_request_impl(nqueries_t request_identifier)=0
Implementation for encode_request().
An abstract base class for a PIR client.
Definition: percyclient.h:35
virtual dbsize_t send_request_impl(nqueries_t request_identifier, vector< ostream * > &osvec, bool send_num_queries=true)=0
Implementation for send_request().
Defines the basic structure of protocol parameters (PercyParams), client parameters (PercyClientParam...