]> git.sesse.net Git - stockfish/blob - src/client.cpp
Make the client prober output something a bit more easily parseable, and run in a...
[stockfish] / src / client.cpp
1 #include <iostream>
2 #include <memory>
3 #include <string>
4
5 #include <grpc++/grpc++.h>
6
7 #include "hashprobe.grpc.pb.h"
8 #include "types.h"
9 #include "uci.h"
10
11 using grpc::Channel;
12 using grpc::ClientContext;
13 using grpc::Status;
14
15 int main(int argc, char** argv) {
16   std::shared_ptr<Channel> channel(grpc::CreateChannel(
17       "localhost:50051", grpc::InsecureChannelCredentials()));
18   std::unique_ptr<HashProbe::Stub> stub(HashProbe::NewStub(channel));
19
20   for ( ;; ) {
21     char buf[256];
22     if (fgets(buf, sizeof(buf), stdin) == nullptr || buf[0] == '\n') {
23       exit(0);
24     }
25
26     char *ptr = strchr(buf, '\n');
27     if (ptr != nullptr) *ptr = 0;
28
29     HashProbeRequest request;
30     request.set_fen(buf);
31
32     HashProbeResponse response;
33     ClientContext context;
34     Status status = stub->Probe(&context, request, &response);
35
36     if (status.ok()) {
37       std::cout << response.found() << " "
38                 << UCI::square(from_sq(Move(response.move())))
39                 << UCI::square(to_sq(Move(response.move()))) << " ";
40       switch (response.bound()) {
41       case HashProbeResponse::BOUND_NONE:
42       case HashProbeResponse::BOUND_EXACT:
43         break;
44       case HashProbeResponse::BOUND_UPPER:
45         std::cout << "<=";
46         break;
47       case HashProbeResponse::BOUND_LOWER:
48         std::cout << ">=";
49         break;
50       } 
51       std::cout << UCI::value(Value(response.value())) << " ";
52       std::cout << response.depth() << std::endl;
53     } else {
54       std::cout << "ERROR" << std::endl;
55     }
56   }
57
58   return 0;
59 }