Example #1
0
 public PuppetMasterServerService(ConcurrentDictionary <string, List <string> > serversByPartitions, ConcurrentDictionary <string, string> serverUrls,
                                  List <string> masteredPartitions, ConcurrentBag <string> crashedServers, DelayMessagesInterceptor interceptor)
 {
     ServersByPartition = serversByPartitions;
     ServerUrls         = serverUrls;
     MasteredPartitions = masteredPartitions;
     CrashedServers     = crashedServers;
     Interceptor        = interceptor;
 }
Example #2
0
        static void Main(string[] args)
        {
            if (args.Length != 5)
            {
                Console.WriteLine("Usage: Server.exe server_id host port min_delay max_delay");
                return;
            }


            if (!int.TryParse(args[2], out int Port))
            {
                Console.WriteLine("Invalid port value");
                return;
            }

            if (!int.TryParse(args[3], out int minDelay))
            {
                Console.WriteLine("Invalid min delay");
                return;
            }

            if (!int.TryParse(args[4], out int maxDelay))
            {
                Console.WriteLine("Invalid max delay");
                return;
            }

            if (minDelay > maxDelay)
            {
                Console.WriteLine("Max delay must be greater of equal than min delay");
                return;
            }

            AppContext.SetSwitch(
                "System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

            string id = args[0];

            string host = args[1];

            // Dictionary with values
            ConcurrentDictionary <ObjectKey, ObjectValueManager> keyValuePairs = new ConcurrentDictionary <ObjectKey, ObjectValueManager>(new ObjectKey.ObjectKeyComparer());


            // Dictionary <partition_id, List<URLs>> all servers by partition
            ConcurrentDictionary <string, List <string> > ServersByPartition = new ConcurrentDictionary <string, List <string> >();
            //ServersByPartition.TryAdd("part-1", new List<string> { "s1", "s2" });
            //ServersByPartition.TryAdd("part-2", new List<string> { "s2" });

            ConcurrentDictionary <string, string> serverUrls = new ConcurrentDictionary <string, string>();
            //serverUrls.TryAdd("s1", "http://localhost:10010");
            //serverUrls.TryAdd("s2", "http://localhost:10011");

            // List of crashed servers
            ConcurrentBag <string> CrashedServers = new ConcurrentBag <string>();

            // List partition which im master of
            List <string> MasteredPartitions = new List <string>(); // { Port == 10010 ? "part-1" : "part-2" };

            var interceptor = new DelayMessagesInterceptor(minDelay, maxDelay);

            // ReadWriteLock for listMe functions
            var localReadWriteLock = new ReaderWriterLock();

            var clientServerService = new ClientServerService(keyValuePairs, ServersByPartition, serverUrls, MasteredPartitions, localReadWriteLock, CrashedServers)
            {
                MyId = id
            };

            var serverSyncService = new ServerSyncService(keyValuePairs, ServersByPartition, localReadWriteLock, CrashedServers);

            var puppetMasterService = new PuppetMasterServerService(ServersByPartition, serverUrls, MasteredPartitions, CrashedServers, interceptor);

            Grpc.Core.Server server = new Grpc.Core.Server
            {
                Services =
                {
                    ClientServerGrpcService.BindService(clientServerService).Intercept(interceptor),
                    ServerSyncGrpcService.BindService(serverSyncService).Intercept(interceptor),
                    PuppetMasterServerGrpcService.BindService(puppetMasterService)
                },
                Ports = { new ServerPort(host, Port, ServerCredentials.Insecure) }
            };

            server.Start();

            Console.WriteLine("Press any key to stop the server...");
            Console.ReadKey();

            server.ShutdownAsync().Wait();
        }