-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.cpp
61 lines (49 loc) · 1.52 KB
/
client.cpp
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
55
56
57
58
59
60
61
#include <stdio.h>
#include <iostream>
#include <memory>
#include <vector>
#include <gflags/gflags.h>
#include <grpc++/grpc++.h>
#include "service.grpc.pb.h"
using std::cerr;
using std::cout;
using std::endl;
using std::shared_ptr;
using std::string;
using std::unique_ptr;
using std::to_string;
using std::vector;
DEFINE_int32(port, 8080, "Port to connect to");
DEFINE_string(host, "localhost", "Host to connect to");
int main(int argc, char** argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
FILE* file = fopen(argv[1], "r");
fseek(file, 0, SEEK_END);
int64_t length = ftell(file);
rewind(file);
vector<char> data;
data.resize(length);
size_t bytes_read =
fread(data.data(), sizeof(decltype(data)::value_type), length, file);
if (bytes_read != length) {
cout << "Failed to read file" << endl;
return -1;
}
cout << "Read image of: " << bytes_read << " bytes" << endl;
string host = FLAGS_host + ":" + to_string(FLAGS_port);
cout << "Connecting to: " << host << endl;
shared_ptr<grpc::Channel> channel =
grpc::CreateChannel(host, grpc::InsecureChannelCredentials());
unique_ptr<words::Cheater::Stub> stub = words::Cheater::NewStub(channel);
words::Request request;
request.set_image(data.data(), data.size());
words::Response response;
grpc::ClientContext context;
grpc::Status status = stub->FindSolutions(&context, request, &response);
if (!status.ok()) {
cerr << status.error_message() << endl;
return -1;
}
cout << response.DebugString() << endl;
return 0;
}