Initial commit for gRPC code.
[stockfish] / src / main.cpp
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4   Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
5   Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
6
7   Stockfish is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   Stockfish 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   You should have received a copy of the GNU General Public License
18   along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <iostream>
22 #include <thread>
23
24 #include "bitboard.h"
25 #include "position.h"
26 #include "search.h"
27 #include "thread.h"
28 #include "tt.h"
29 #include "uci.h"
30 #include "syzygy/tbprobe.h"
31
32 #include <grpc/grpc.h>
33 #include <grpc++/server.h>
34 #include <grpc++/server_builder.h>
35 #include "hashprobe.grpc.pb.h"
36
37 using grpc::Server;
38 using grpc::ServerBuilder;
39 using grpc::ServerContext;
40 using grpc::Status;
41 using grpc::StatusCode;
42
43 class HashProbeImpl final : public HashProbe::Service {
44 public:
45         Status Probe(ServerContext* context,
46                      const HashProbeRequest* request,
47                      HashProbeResponse *response) {
48                 std::cout << "fen=" << request->fen() << std::endl;
49                 Position pos(request->fen(), /*isChess960=*/false, Threads.main());
50                 if (!pos.pos_is_ok()) {
51                         return Status(StatusCode::INVALID_ARGUMENT, "Invalid FEN");
52                 }
53                 std::cout << "parsed=" << pos.fen() << std::endl;
54                 bool found;
55                 TTEntry *entry = TT.probe(pos.key(), found);
56                 response->set_found(found);
57                 if (found) {
58                         response->set_move(entry->move());
59                         response->set_value(entry->value());
60                         response->set_eval(entry->eval());
61                         response->set_depth(entry->depth());
62                         response->set_bound(entry->bound());
63                 }
64                 return Status::OK;
65         }
66 };
67         
68 void rpc_thread()
69 {
70         std::string server_address("0.0.0.0:50051");
71         HashProbeImpl service;
72
73         ServerBuilder builder;
74         builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
75         builder.RegisterService(&service);
76         std::unique_ptr<Server> server(builder.BuildAndStart());
77         std::cout << "Server listening on " << server_address << std::endl;
78         server->Wait();
79 }
80
81 namespace PSQT {
82   void init();
83 }
84
85 int main(int argc, char* argv[]) {
86
87   std::cout << engine_info() << std::endl;
88
89   UCI::init(Options);
90   PSQT::init();
91   Bitboards::init();
92   Position::init();
93   Bitbases::init();
94   Search::init();
95   Pawns::init();
96   Threads.set(Options["Threads"]);
97   Search::clear(); // After threads are up
98
99   std::thread(&rpc_thread).detach();
100
101   UCI::loop(argc, argv);
102
103   Threads.set(0);
104   return 0;
105 }