Exemple #1
0
        public IDictionaryService GetDictionaryService()
        {
            IDictionaryService dictionaryService = flyWeightPool["DictionaryService"] as IDictionaryService;

            if (dictionaryService == null)
            {
                dictionaryService = new DictionaryServiceImpl();
                flyWeightPool.Add("DictionaryService", dictionaryService);
            }

            return(dictionaryService);
        }
    public static int Main(string[] args)
    {
        Console.WriteLine($"HOST IP: {args[0]}");
        // Validate arguments
        if (args.Length < 1)
        {
            Console.WriteLine("Usage: dotnet server.dll {PORT}");
            return(-1);
        }
        int port;

        if (!int.TryParse(args[1], out port))
        {
            Console.WriteLine("invalid format for port, use a number");
            return(-1);
        }

        // Build client to next server in line
        var myHash     = DictionaryServiceImpl.HashString(port.ToString());
        var otherPorts = args
                         .Skip(2)
                         .Select(p => new { Hash = DictionaryServiceImpl.HashString(p), Port = p })
                         .OrderBy(x => x.Hash);
        var nextPort = otherPorts.First();

        foreach (var otherPort in otherPorts)
        {
            if (otherPort.Hash > myHash)
            {
                nextPort = otherPort;
                break;
            }
        }
        Console.WriteLine($"{port} ({myHash}) => {nextPort.Port} ({nextPort.Hash})");


        var nextClientAddress = $"{args[0]}:{nextPort.Port}";

        //    var callOptions = new CallOptions(deadline: DateTime.UtcNow.Add(TimeSpan.FromSeconds(3)));

        // Set key

        /*  var setRequest = new SetRequest { Key = "foo", Value = "bar" };
         * var setResponse = client.Set(setRequest, callOptions);
         * Console.WriteLine($"setRequest={setRequest}");
         * Console.WriteLine($"setResponse={setResponse}");
         */

        // Build the gRPC server
        Console.WriteLine("Firing off GRPC server");
        var server = new Server {
            Services = { DictionaryService.BindService(new DictionaryServiceImpl(port.ToString(), nextClientAddress)) },
            Ports    = { new ServerPort("*", port, ServerCredentials.Insecure) }
        };

        // Launch the gRPC server
        ThreadPool.QueueUserWorkItem(_ => {
            server.Start();
            Console.WriteLine($"GRPC server listening on port {port}");
        }
                                     );
        autoEvent.WaitOne();
        server.ShutdownAsync().Wait();
        Console.WriteLine("Shutting down ...");
        return(0);
    }