]> git.sesse.net Git - stockfish/blob - src/client.cpp
9f203575a1c735b941506b9e7924e00a22627b41
[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 using namespace hashprobe;
15
16 std::string FormatMove(const HashProbeMove &move) {
17   if (move.from_sq().empty()) return "MOVE_NONE";
18   return move.from_sq() + move.to_sq() + move.promotion();
19 }
20
21 int main(int argc, char** argv) {
22   std::shared_ptr<Channel> channel(grpc::CreateChannel(
23       "localhost:50051", grpc::InsecureChannelCredentials()));
24   std::unique_ptr<HashProbe::Stub> stub(HashProbe::NewStub(channel));
25
26   for ( ;; ) {
27     char buf[256];
28     if (fgets(buf, sizeof(buf), stdin) == nullptr || buf[0] == '\n') {
29       exit(0);
30     }
31
32     char *ptr = strchr(buf, '\n');
33     if (ptr != nullptr) *ptr = 0;
34
35     HashProbeRequest request;
36     request.set_fen(buf);
37
38     HashProbeResponse response;
39     ClientContext context;
40     Status status = stub->Probe(&context, request, &response);
41
42     if (status.ok()) {
43       for (const HashProbeLine &line : response.line()) {
44         std::cout << FormatMove(line.move()) << " ";
45         std::cout << line.found() << " ";
46         for (const HashProbeMove &move : line.pv()) {
47           std::cout << FormatMove(move) << ",";
48         }
49         std::cout << " ";
50         switch (line.bound()) {
51         case HashProbeLine::BOUND_NONE:
52           std::cout << "?";
53           break;
54         case HashProbeLine::BOUND_EXACT:
55           std::cout << "==";
56           break;
57         case HashProbeLine::BOUND_UPPER:
58           std::cout << "<=";
59           break;
60         case HashProbeLine::BOUND_LOWER:
61           std::cout << ">=";
62           break;
63         } 
64         std::cout << " " << UCI::value(Value(line.value())) << " ";
65         std::cout << line.depth() << std::endl;
66       }
67       std::cout << "END" << std::endl;
68     } else {
69       std::cout << "ERROR" << std::endl;
70     }
71   }
72
73   return 0;
74 }