コード例 #1
0
        private async Task RunServer(IActorRuntime runtime, ActorId clusterManager, string subscriptionName)
        {
            if (this.Debug)
            {
                Console.WriteLine("Attach debugger");
                await Task.Delay(60000);
            }

            CancellationTokenSource cancelSource = new CancellationTokenSource();

            if (this.ClientProcessId == 0)
            {
                throw new Exception("Server should have a client process id");
            }

            MonitorClientProcess(this.ClientProcessId);

            // We create a server host that will create and wrap a Raft server instance (implemented
            // as a Coyote state machine), and execute it using the Coyote runtime.
            var host = new AzureServer(runtime, this.ConnectionString, this.TopicName, this.ServerId, this.ClusterSize, clusterManager);

            this.LocalId = host.HostedServer;
            host.Initialize();
            host.Start();

            var receiver = new AzureMessageReceiver(runtime, this.ConnectionString, this.TopicName, this.LocalId, subscriptionName);
            await receiver.RunAsync(cancelSource.Token);
        }
コード例 #2
0
        private async Task RunClient(IActorRuntime runtime, ActorId clusterManager, string subscriptionName)
        {
            CancellationTokenSource cancelSource = new CancellationTokenSource();

            StartRaftServers(this.ConnectionString, this.TopicName, this.ClusterSize);

            var receiver = new AzureMessageReceiver(runtime, this.ConnectionString, this.TopicName, this.LocalId, subscriptionName);
            var nowait   = receiver.RunAsync(cancelSource.Token);

            receiver.ResponseReceived += (s, e) =>
            {
                this.completed.SetResult(e);
            };

            // Now send the requested number of ClientRequestEvents to the cluster, and wait for each response.
            for (int i = 0; i < this.NumRequests; i++)
            {
                string command = $"request-{i}";
                Console.WriteLine($"<Client> sending {command}.");
                this.completed = new TaskCompletionSource <ClientResponseEvent>();
                runtime.SendEvent(clusterManager, new ClientRequestEvent(command));
                var response = await this.completed.Task;
                Console.WriteLine($"<Client> received response for {response.Command} from  {response.Server}.");
            }
        }