-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.h
121 lines (93 loc) · 3.2 KB
/
client.h
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#pragma once
#ifndef THISGRPCLIENT
#define THISGRPCCLIENT
#include <iostream>
#include <memory>
#include <string>
#include <random>
#include <Windows.h>
#include <windows.h>
#include <stdlib.h>
#include <grpcpp/grpcpp.h>
#include "equations.h"
using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using validator::Validator;
using validator::IP;
using validator::Answer;
using validator::Variables;
class ValidatorClient {
private:
double answer;
public:
ValidatorClient(std::shared_ptr<Channel> channel) : stub_(Validator::NewStub(channel)) {
answer = 0;
}
void EstablishConnection(const std::string& ip) {
IP request;
request.set_ip(ip);
Variables reply;
ClientContext context;
Status status = stub_->EstablishCon(&context, request, &reply);
if (status.ok()) {
std::cout << "Received: a=" << reply.a()
<< ", b=" << reply.b()
<< ", c=" << reply.c()
<< ", x=" << reply.x()
<< " Equation context=" << reply.equtioncontext() << std::endl;
Equations calc = Equations();
answer = calc.CalcAnswer(reply.a(), reply.b(), reply.c(), reply.x(), reply.equtioncontext());
}
else {
std::cerr << "RPC failed: " << status.error_code()
<< ": " << status.error_message() << std::endl;
}
}
void SendAnswer() {
Answer request;
request.set_answer(answer);
Variables reply;
ClientContext context;
Status status = stub_->GiveVariables(&context, request, &reply);
if (status.ok()) {
std::cout << "Echoed: a=" << reply.a()
<< ", b=" << reply.b()
<< ", c=" << reply.c()
<< ", x=" << reply.x() << std::endl;
Equations calc = Equations();
answer = calc.CalcAnswer(reply.a(), reply.b(), reply.c(), reply.x(), reply.equtioncontext());
}
else {
std::cerr << "RPC failed: " << status.error_code()
<< ": " << status.error_message() << std::endl;
}
}
private:
std::unique_ptr<Validator::Stub> stub_;
};
void WebHookclient(const std::string url) {
// Open the URL in the default web browser
std::system("start http://127.0.0.1:8080/adbfinejwnileubfjksn");
}
int client(std::string port) {
std::string target_str = "127.0.0.1:" + port;
std::string input;
bool isFInished = false;
ValidatorClient client(grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials()));
client.EstablishConnection(target_str);
std::cout << std::endl << "client started" << std::endl << std::endl;
while (isFInished == false) {
client.SendAnswer();
std::cout << std::endl << "again(y/n):";
std::getline(std::cin, input);
if (input == "y") {
continue;
}
else {
isFInished = true;
}
}
return 0;
}
#endif // !"THISGRPCLIENT"