public static void Main()
        {
            try
            {
                TTransport transport = new TSocket("localhost", 9090);
                TProtocol protocol = new TBinaryProtocol(transport);
                Calculator.Client client = new Calculator.Client(protocol);

                transport.Open();
                try
                {
                    client.ping();
                    Console.WriteLine("ping()");

                    int sum = client.add(1, 1);
                    Console.WriteLine("1+1={0}", sum);

                    Work work = new Work();

                    work.Op = Operation.DIVIDE;
                    work.Num1 = 1;
                    work.Num2 = 0;
                    try
                    {
                        int quotient = client.calculate(1, work);
                        Console.WriteLine("Whoa we can divide by 0");
                    }
                    catch (InvalidOperation io)
                    {
                        Console.WriteLine("Invalid operation: " + io.Why);
                    }

                    work.Op = Operation.SUBTRACT;
                    work.Num1 = 15;
                    work.Num2 = 10;
                    try
                    {
                        int diff = client.calculate(1, work);
                        Console.WriteLine("15-10={0}", diff);
                    }
                    catch (InvalidOperation io)
                    {
                        Console.WriteLine("Invalid operation: " + io.Why);
                    }

                    SharedStruct log = client.getStruct(1);
                    Console.WriteLine("Check log: {0}", log.Value);

                }
                finally
                {
                    transport.Close();
                }
            }
            catch (TApplicationException x)
            {
                Console.WriteLine(x.StackTrace);
            }

        }
Пример #2
0
        private static async Task ExecuteCalculatorClientOperations(CancellationToken cancellationToken, Calculator.Client client)
        {
            await client.OpenTransportAsync(cancellationToken);

            // Async version

            Logger.LogInformation($"{client.ClientId} PingAsync()");
            await client.pingAsync(cancellationToken);

            Logger.LogInformation($"{client.ClientId} AddAsync(1,1)");
            var sum = await client.addAsync(1, 1, cancellationToken);

            Logger.LogInformation($"{client.ClientId} AddAsync(1,1)={sum}");

            var work = new Work
            {
                Op   = Operation.DIVIDE,
                Num1 = 1,
                Num2 = 0
            };

            try
            {
                Logger.LogInformation($"{client.ClientId} CalculateAsync(1)");
                await client.calculateAsync(1, work, cancellationToken);

                Logger.LogInformation($"{client.ClientId} Whoa we can divide by 0");
            }
            catch (InvalidOperation io)
            {
                Logger.LogInformation($"{client.ClientId} Invalid operation: " + io);
            }

            work.Op   = Operation.SUBTRACT;
            work.Num1 = 15;
            work.Num2 = 10;

            try
            {
                Logger.LogInformation($"{client.ClientId} CalculateAsync(1)");
                var diff = await client.calculateAsync(1, work, cancellationToken);

                Logger.LogInformation($"{client.ClientId} 15-10={diff}");
            }
            catch (InvalidOperation io)
            {
                Logger.LogInformation($"{client.ClientId} Invalid operation: " + io);
            }

            Logger.LogInformation($"{client.ClientId} GetStructAsync(1)");
            var log = await client.getStructAsync(1, cancellationToken);

            Logger.LogInformation($"{client.ClientId} Check log: {log.Value}");

            Logger.LogInformation($"{client.ClientId} ZipAsync() with delay 100mc on server side");
            await client.zipAsync(cancellationToken);
        }
Пример #3
0
        public static void Main(string[] args)
        {
            /*
               	  +-------------------------------------------+
              | Server                                   |
              | (single-threaded, event-driven etc)      |
              +-------------------------------------------+
              | Processor                                |
              | (compiler generated)                     |
              +-------------------------------------------+
              | Protocol                                 |
              | (JSON, compact etc)                      |
              +-------------------------------------------+
              | Transport                                |
              | (raw TCP, HTTP etc)                      |
              +-------------------------------------------+
             */
            try{

                TTransport transport = new TSocket("localhost", 9090); 	// transport
                TProtocol protocol = new TBinaryProtocol(transport);	// protocol
                Calculator.Client client = new Calculator.Client(protocol);	// processor

                transport.Open();
                try
                {
                    client.ping();
                    Console.WriteLine("ping()");

                    int sum = client.add(1, 1);
                    Console.WriteLine("1+1={0}", sum);

                    Work work = new Work();

                    work.Op = Operation.DIVIDE;
                    work.Num1 = 1;
                    work.Num2 = 0;
                    try
                    {
                        int quotient = client.calculate(1, work);
                        Console.WriteLine("Whoa we can divide by 0 " + quotient);
                    }
                    catch (InvalidOperation io)
                    {
                        Console.WriteLine("Invalid operation: " + io.Why);
                    }

                    work.Op = Operation.SUBTRACT;
                    work.Num1 = 15;
                    work.Num2 = 10;
                    try
                    {
                        int diff = client.calculate(1, work);
                        Console.WriteLine("15-10={0}", diff);
                    }
                    catch (InvalidOperation io)
                    {
                        Console.WriteLine("Invalid operation: " + io.Why);
                    }

                    SharedStruct log = client.getStruct(1);
                    Console.WriteLine("Check log: {0}", log.Value);

                }
                finally
                {
                    transport.Close();
                }
            }
            catch (TApplicationException x)
            {
                Console.WriteLine(x.StackTrace);
            }

            Console.WriteLine ("Hello World!");
        }
Пример #4
0
        static void Main(string[] args)
        {
            if (args.Length < 4)
            {
                Console.WriteLine("server [ns] [hc] [keyname] [key]");
                return;
            }

            var ns      = args[0];
            var hc      = args[1];
            var keyname = args[2];
            var key     = args[3];

            var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(keyname, key);
            var sendAddress   = new Uri($"sb://{ns}/{hc}");
            var relayClient   = new HybridConnectionClient(sendAddress, tokenProvider);

            try
            {
                var relayConnection = relayClient.CreateConnectionAsync().GetAwaiter().GetResult();

                TTransport        transport = new TStreamTransport(relayConnection, relayConnection);
                TProtocol         protocol  = new TBinaryProtocol(transport);
                Calculator.Client client    = new Calculator.Client(protocol);

                transport.Open();
                try
                {
                    client.ping();
                    Console.WriteLine("ping()");

                    int sum = client.add(1, 1);
                    Console.WriteLine("1+1={0}", sum);

                    Work work = new Work();

                    work.Op   = Operation.DIVIDE;
                    work.Num1 = 1;
                    work.Num2 = 0;
                    try
                    {
                        int quotient = client.calculate(1, work);
                        Console.WriteLine("Whoa we can divide by 0");
                    }
                    catch (InvalidOperation io)
                    {
                        Console.WriteLine("Invalid operation: " + io.Why);
                    }

                    work.Op   = Operation.SUBTRACT;
                    work.Num1 = 15;
                    work.Num2 = 10;
                    try
                    {
                        int diff = client.calculate(1, work);
                        Console.WriteLine("15-10={0}", diff);
                    }
                    catch (InvalidOperation io)
                    {
                        Console.WriteLine("Invalid operation: " + io.Why);
                    }

                    SharedStruct log = client.getStruct(1);
                    Console.WriteLine("Check log: {0}", log.Value);
                }
                finally
                {
                    transport.Close();
                }
            }
            catch (TApplicationException x)
            {
                Console.WriteLine(x.StackTrace);
            }
        }
Пример #5
0
        public async Task Run(string sendAddress, string sendToken)
        {
            try
            {
                var relayClient     = new RelayClient(sendAddress, TokenProvider.CreateSharedAccessSignatureTokenProvider(sendToken));
                var relayConnection = relayClient.Connect();

                TTransport        transport = new TStreamTransport(relayConnection, relayConnection);
                TProtocol         protocol  = new TBinaryProtocol(transport);
                Calculator.Client client    = new Calculator.Client(protocol);

                transport.Open();
                try
                {
                    client.ping();
                    Console.WriteLine("ping()");

                    int sum = client.add(1, 1);
                    Console.WriteLine("1+1={0}", sum);

                    Work work = new Work();

                    work.Op   = Operation.DIVIDE;
                    work.Num1 = 1;
                    work.Num2 = 0;
                    try
                    {
                        int quotient = client.calculate(1, work);
                        Console.WriteLine("Whoa we can divide by 0");
                    }
                    catch (InvalidOperation io)
                    {
                        Console.WriteLine("Invalid operation: " + io.Why);
                    }

                    work.Op   = Operation.SUBTRACT;
                    work.Num1 = 15;
                    work.Num2 = 10;
                    try
                    {
                        int diff = client.calculate(1, work);
                        Console.WriteLine("15-10={0}", diff);
                    }
                    catch (InvalidOperation io)
                    {
                        Console.WriteLine("Invalid operation: " + io.Why);
                    }

                    SharedStruct log = client.getStruct(1);
                    Console.WriteLine("Check log: {0}", log.Value);
                }
                finally
                {
                    transport.Close();
                }
            }
            catch (TApplicationException x)
            {
                Console.WriteLine(x.StackTrace);
            }
        }
Пример #6
0
        private static async Task ExecuteCalculatorClientTest(CancellationToken cancellationToken, Calculator.Client client)
        {
            await client.OpenTransportAsync(cancellationToken);

            Stopwatch sw = Stopwatch.StartNew();

            int max = 100000;

            foreach (var i in Enumerable.Range(0, max))
            {
                var sum = await client.addAsync(1, 1, cancellationToken);

                //Logger.LogInformation($"{client.ClientId} AddAsync(1,1)={sum}");
            }

            sw.Stop();
            Logger.LogInformation($"{client.ClientId} AddAsync(1,1) do:{max} ms:{sw.ElapsedMilliseconds}");
        }
Пример #7
0
        private static async Task RunClientAsync(TProtocol protocol,
            CancellationToken cancellationToken)
        {
            try
            {
                var client = new Calculator.Client(protocol);
                await client.OpenTransportAsync(cancellationToken);

                try
                {
                    // Async version

                    Logger.LogInformation("PingAsync()");
                    await client.pingAsync(cancellationToken);

                    Logger.LogInformation("AddAsync(1,1)");
                    var sum = await client.addAsync(1, 1, cancellationToken);
                    Logger.LogInformation($"AddAsync(1,1)={sum}");

                    var work = new Work
                    {
                        Op = Operation.DIVIDE,
                        Num1 = 1,
                        Num2 = 0
                    };

                    try
                    {
                        Logger.LogInformation("CalculateAsync(1)");
                        await client.calculateAsync(1, work, cancellationToken);
                        Logger.LogInformation("Whoa we can divide by 0");
                    }
                    catch (InvalidOperation io)
                    {
                        Logger.LogInformation("Invalid operation: " + io);
                    }

                    work.Op = Operation.SUBTRACT;
                    work.Num1 = 15;
                    work.Num2 = 10;

                    try
                    {
                        Logger.LogInformation("CalculateAsync(1)");
                        var diff = await client.calculateAsync(1, work, cancellationToken);
                        Logger.LogInformation($"15-10={diff}");
                    }
                    catch (InvalidOperation io)
                    {
                        Logger.LogInformation("Invalid operation: " + io);
                    }

                    Logger.LogInformation("GetStructAsync(1)");
                    var log = await client.getStructAsync(1, cancellationToken);
                    Logger.LogInformation($"Check log: {log.Value}");

                    Logger.LogInformation("ZipAsync() with delay 100mc on server side");
                    await client.zipAsync(cancellationToken);
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex.ToString());
                }
                finally
                {
                    protocol.Transport.Close();
                }
            }
            catch (TApplicationException x)
            {
                Logger.LogError(x.ToString());
            }
        }
Пример #8
0
        private static async Task RunClientAsync(TProtocol protocol,
                                                 CancellationToken cancellationToken)
        {
            try
            {
                var client = new Calculator.Client(protocol);
                await client.OpenTransportAsync(cancellationToken);

                try
                {
                    // Async version

                    Logger.LogInformation("PingAsync()");
                    await client.pingAsync(cancellationToken);

                    Logger.LogInformation("AddAsync(1,1)");
                    var sum = await client.addAsync(1, 1, cancellationToken);

                    Logger.LogInformation($"AddAsync(1,1)={sum}");

                    var work = new Work
                    {
                        Op   = Operation.DIVIDE,
                        Num1 = 1,
                        Num2 = 0
                    };

                    try
                    {
                        Logger.LogInformation("CalculateAsync(1)");
                        await client.calculateAsync(1, work, cancellationToken);

                        Logger.LogInformation("Whoa we can divide by 0");
                    }
                    catch (InvalidOperation io)
                    {
                        Logger.LogInformation("Invalid operation: " + io);
                    }

                    work.Op   = Operation.SUBTRACT;
                    work.Num1 = 15;
                    work.Num2 = 10;

                    try
                    {
                        Logger.LogInformation("CalculateAsync(1)");
                        var diff = await client.calculateAsync(1, work, cancellationToken);

                        Logger.LogInformation($"15-10={diff}");
                    }
                    catch (InvalidOperation io)
                    {
                        Logger.LogInformation("Invalid operation: " + io);
                    }

                    Logger.LogInformation("GetStructAsync(1)");
                    var log = await client.getStructAsync(1, cancellationToken);

                    Logger.LogInformation($"Check log: {log.Value}");

                    Logger.LogInformation("ZipAsync() with delay 100mc on server side");
                    await client.zipAsync(cancellationToken);
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex.ToString());
                }
                finally
                {
                    protocol.Transport.Close();
                }
            }
            catch (TApplicationException x)
            {
                Logger.LogError(x.ToString());
            }
        }
        public async Task Run(string sendAddress, string sendToken)
        {
            try
            {
                var relayClient = new RelayClient(sendAddress, TokenProvider.CreateSharedAccessSignatureTokenProvider(sendToken));
                var relayConnection = relayClient.Connect();

                TTransport transport = new TStreamTransport(relayConnection, relayConnection);
                TProtocol protocol = new TBinaryProtocol(transport);
                Calculator.Client client = new Calculator.Client(protocol);

                transport.Open();
                try
                {
                    client.ping();
                    Console.WriteLine("ping()");

                    int sum = client.add(1, 1);
                    Console.WriteLine("1+1={0}", sum);

                    Work work = new Work();

                    work.Op = Operation.DIVIDE;
                    work.Num1 = 1;
                    work.Num2 = 0;
                    try
                    {
                        int quotient = client.calculate(1, work);
                        Console.WriteLine("Whoa we can divide by 0");
                    }
                    catch (InvalidOperation io)
                    {
                        Console.WriteLine("Invalid operation: " + io.Why);
                    }

                    work.Op = Operation.SUBTRACT;
                    work.Num1 = 15;
                    work.Num2 = 10;
                    try
                    {
                        int diff = client.calculate(1, work);
                        Console.WriteLine("15-10={0}", diff);
                    }
                    catch (InvalidOperation io)
                    {
                        Console.WriteLine("Invalid operation: " + io.Why);
                    }

                    SharedStruct log = client.getStruct(1);
                    Console.WriteLine("Check log: {0}", log.Value);

                }
                finally
                {
                    transport.Close();
                }
            }
            catch (TApplicationException x)
            {
                Console.WriteLine(x.StackTrace);
            }

        }