Send gRPC backends from the Perl code to the Node.js.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 2 Sep 2016 16:24:50 +0000 (18:24 +0200)
committerMagne Mæhre <magne@pannekake.samfundet.no>
Sun, 13 Nov 2016 18:12:38 +0000 (19:12 +0100)
Makes it easier to keep all the config for one machine in one place.

config.pm
remoteglot.pl
server/hash-lookup.js
server/serve-analysis.js

index 8050adf..7de4407 100644 (file)
--- a/config.pm
+++ b/config.pm
@@ -45,6 +45,7 @@ our %engine_config = (
        'Hash' => '1024',
 #      'MultiPV' => '2'
 );
+our $engine_grpc_backend = undef;  # Not used by us, but will be communicated to serve-analysis.js.
 
 # Separate engine for multi-PV; can be undef for none.
 our $engine2_cmdline = undef;
@@ -54,6 +55,7 @@ our %engine2_config = (
        'Hash' => '1024',
        'Threads' => '8',
 );
+our $engine2_grpc_backend = undef;  # Not used by us, but will be communicated to serve-analysis.js.
 
 our $uci_assume_full_compliance = 0;                    # dangerous :-)
 our $update_max_interval = 1.0;
index 4b67496..9a1ee4c 100755 (executable)
@@ -67,7 +67,7 @@ select(TBLOG);
 $| = 1;
 
 select(STDOUT);
-umask 0022;
+umask 0027;  # analysis.json should not be served to users.
 
 # open the chess engine
 my $engine = open_engine($remoteglotconf::engine_cmdline, 'E1', sub { handle_uci(@_, 1); });
@@ -847,6 +847,14 @@ sub output_json {
        if (defined($remoteglotconf::engine_details)) {
                $json->{'engine'}{'details'} = $remoteglotconf::engine_details;
        }
+       my @grpc_backends = ();
+       if (defined($remoteglotconf::engine_grpc_backend)) {
+               push @grpc_backends, $remoteglotconf::engine_grpc_backend;
+       }
+       if (defined($remoteglotconf::engine2_grpc_backend)) {
+               push @grpc_backends, $remoteglotconf::engine2_grpc_backend;
+       }
+       $json->{'internal'}{'grpc_backends'} = \@grpc_backends;
        if (defined($remoteglotconf::move_source)) {
                $json->{'move_source'} = $remoteglotconf::move_source;
        }
index b9d5f00..032b87b 100644 (file)
@@ -7,11 +7,27 @@ var hashprobe_proto = grpc.load(PROTO_PATH).hashprobe;
 var board = new Chess();
 
 var clients = [];
+var current_servers = [];
+
+var need_reinit = function(servers) {
+       if (servers.length != current_servers.length) {
+               return true;
+       }
+       for (var i = 0; i < servers.length; ++i) {
+               if (servers[i] != current_servers[i]) {
+                       return true;
+               }
+       }
+       return false;
+}
+exports.need_reinit = need_reinit;
 
 var init = function(servers) {
+       clients = [];
        for (var i = 0; i < servers.length; ++i) {
                clients.push(new hashprobe_proto.HashProbe(servers[i], grpc.credentials.createInsecure()));
        }
+       current_servers = servers;
 }
 exports.init = init;
 
index 22bc20d..ca9047f 100644 (file)
@@ -93,8 +93,18 @@ var replace_json = function(new_json_contents, mtime) {
                }
        }
 
+       var parsed = JSON.parse(new_json_contents);
+
+       if (parsed['internal']) {
+               if (parsed['internal']['grpc_backends'] &&
+                   hash_lookup.need_reinit(parsed['internal']['grpc_backends'])) {
+                       hash_lookup.init(parsed['internal']['grpc_backends']);
+               }
+               delete parsed['internal'];
+       }
+
        var new_json = {
-               parsed: JSON.parse(new_json_contents),
+               parsed: parsed,
                plain: new_json_contents,
                last_modified: mtime
        };