예제 #1
0
        static async Task Main(string[] args)
        {
            // Welcome message
            Log.Write("Welcome to the simple demo client!");

            // RPC initialization, using two server function classes
            var serverDemo    = new DemoServerRpcStub();
            var serverCalc    = new CalcRpcStub();
            var demoRpcConfig = new RpcClientConfig {
                ClientID  = clientID,
                ServerUrl = "http://localhost:5000/rpc"
            };

            RpcMain.InitRpcClient(demoRpcConfig, AuthenticateClient, () => new List <RpcFunctions> {
                new DemoClientRpc(),
                new CalcRpc()
            });

            // Each few seconds, send commands to the server. Log the result.
            var random = new Random();

            while (true)
            {
                try {
                    // Send greeting
                    Log.Write("Sending greeting...");
                    var greeting = new Greeting {
                        Name     = "Andi",
                        MoreData = new SampleData {
                            Text = $"Hi server, now it is {DateTime.Now}"
                        }
                    };
                    await serverDemo.SayHelloToServer(greeting);

                    // Send calculation task. May fail on the remote side, when there is division by zero.
                    Log.Write("Successfully greeted. Now sending a little calculation task:");
                    int  a         = random.Next(1, 100);
                    int  b         = random.Next(0, 10);
                    long startTime = CoreUtils.TimeNow();
                    try {
                        int result = await serverCalc.DivideNumbers(a, b);

                        long runTime = CoreUtils.TimeNow() - startTime;
                        Log.Write($"{a}/{b}={result} (runtime: {runTime}ms)");
                    }
                    catch (RpcException ex) {
                        long runTime = CoreUtils.TimeNow() - startTime;
                        Log.Write($"{a}+{b}=Fail! (runtime: {runTime}ms; {ex.Type}: {ex.Message})");
                    }
                }
                catch (RpcException ex) {
                    Log.Write("Error: " + ex.Failure.Type + ": " + ex.Message);
                }

                // Wait a second before the next round
                await Task.Delay(1000);
            }
        }
예제 #2
0
파일: Program.cs 프로젝트: lulzzz/RpcLib
        static async Task Main(string[] args)
        {
            // First argument: client number
            int clientNumber = 0;

            if (args.Length > 0 && int.TryParse(args[0], out int it))
            {
                clientNumber = it;
            }
            else
            {
                throw new Exception("Must be started with client number as parameter");
            }
            clientID += "-" + clientNumber;

            // Welcome message
            Log.Write("Welcome to the test client: " + clientID);

            // RPC initialization
            var serverCalc    = new CalcRpcStub();
            var demoRpcConfig = new RpcClientConfig {
                ClientID  = clientID,
                ServerUrl = "http://localhost:5000/rpc"
            };

            RpcMain.InitRpcClient(demoRpcConfig, AuthenticateClient, () => new List <RpcFunctions> {
                new CalcRpc()
            });

            // Each 0-100 ms, send a simple calculation task to the server: a + b = ?
            // a is an ascending number, starting from clientNumber * 1000
            // b is a random number between 1 and 100.
            // Write the calculations in the file "{clientID}.calclog" (used in the RpcLibTest project)
            string filename = $"{clientID}.calclog";

            File.Delete(filename);
            int a      = clientNumber * 1000;
            var random = new Random();

            while (true)
            {
                long startTime = CoreUtils.TimeNow();;
                a++;
                int b = random.Next(1, 100);
                try {
                    int result = await serverCalc.AddNumbers(a, b);

                    long runTime = CoreUtils.TimeNow() - startTime;
                    Log.WriteToFile(filename, $"{a}+{b}={result} | {runTime}ms");
                }
                catch (RpcException ex) {
                    long runTime = CoreUtils.TimeNow() - startTime;
                    Log.WriteToFile(filename, $"{a}+{b}=? | {runTime}ms | Fail: {ex.Type}: {ex.Message}");
                }
                await Task.Delay(random.Next(0, 100));
            }
        }