public BandwidthResult Test(int byteCount = 256 * 1024)
        {
            var r = new BandwidthResult();
            var pinger = new PingClient(ping);
            var pingTime = pinger.Ping();
            r.RountTripTimeInMilliseconds = pinger.Ping();

            var data = BandwidthActor.RandomData(byteCount);
            var sw = Stopwatch.StartNew();
            proxy.Upload(data);
            sw.Stop();

            r.UploadTimeInSeconds = (sw.Elapsed.TotalSeconds - pingTime / 1000.0);
            r.UploadBytesPerSecond = data.Length / r.UploadTimeInSeconds;

            sw = Stopwatch.StartNew();
            proxy.Download((int)data.Length);
            sw.Stop();

            r.DownloadTimeInSeconds = (sw.Elapsed.TotalSeconds - pingTime / 1000.0);
            r.DownloadBytesPerSecond = data.Length / r.DownloadTimeInSeconds;

            return r;
        }
Exemple #2
0
        private void RunAsClient()
        {
            string machine = "localhost";
            // create a node
            using(var node = new TcpNode(18222, "client"))
            // connect to an endpoint which is in this case a separate node.
            // so now we can route messages from actors in this node to the
            // other node
            /* optional */ //           using (var conn = node.AddConnection("127.0.0.1", 18222, serializer))
            using (var conn = node.Connect(machine, 18222, serializer))
            {
                // send async but receive sync. send to localhost/echo which happens to
                // be in the other node
                var result = node.Default.SendReceive<string>("server.{0}/System.Echo".FormatWith(machine), "Echo", "hi");
                // print response
                Console.WriteLine(result);

                // DynamicProxy wraps a remote actor.
                var echo = node.New<IEcho>("server.{0}/System.Echo".FormatWith(machine));
                // this is the same as SendReceive<string>("localhost/System.Echo", "echo", "hey dude");
                var r2 = echo.Echo("hey dude");
                // print response
                Console.WriteLine(r2);
                r2 = echo.Echo("hey dude");
                // print response
                Console.WriteLine(r2);

                var ping = node.New<IPing>("server.{0}/System.Ping".FormatWith(machine));
                var pong = ping.Ping(new byte[1024]);
                Console.WriteLine("ping-pong " + pong.Length);

                var client = new PingClient(ping);
                Console.WriteLine("delay = " + client.Ping(10));

                var bandwidth = node.New<IBandwidth>("server.{0}/System.Bandwidth".FormatWith(machine));
                var bw = new BandwidthClient(bandwidth, ping);
                Console.WriteLine("bandwidth = " + bw.Test());

            //                using (var dht = new DhtClient(node.Proxy.New<IByteDht>(new ActorId("localhost", "System.Dht")), new JsonSerializer()))
            //                {
            //                    dht.Add("abc", "123");
            //                    dht.Subscribe(DhtOperation.All, ".*");
            //                    dht.KeyMatch += (operation, key) =>
            //                    {
            //                        Console.WriteLine("DHT callback " + operation + " key=" + key);
            //                    };
            //                    dht.Add("def", "456");
            //                    var x = dht.Get<string>("abc");
            //					Console.WriteLine("DHT result " + x);
            //                }
                //Thread.Sleep(100000);
                node.AddBuiltins();
                var dhtLocal = node.New<IDht>("System.Dht");
                dhtLocal.Join(ActorId.FromUrl("server.{0}/System.Dht".FormatWith(machine)) );
                var dht = node.New<IDht>("server.{0}/System.Dht".FormatWith(machine));

                dhtLocal.Put("abc", "def");

                Console.WriteLine("press a key to test dht get");
                Console.ReadKey();
                Console.WriteLine("found " + dht.Get("abc"));
                Console.WriteLine("found " + dhtLocal.Get("abc"));

                Console.ReadKey();
                using (var shell = new ConsoleClientActor("cmd.exe-client"))
                {
                    node.Add(shell);
                    var proxy = node.New<IShell>("server.{0}/System.Shell".FormatWith(machine));
                    var remoteId = proxy.RunConsole("cmd.exe", new string[0], shell.Id);
                    node.Link(shell.Id, remoteId);
                    while (shell.IsAlive)
                        Thread.Sleep(10);
                }

                //var console = n2.GetProxy("localhost/cmd.exe");
                //using (var realConsole = new ConsoleClientActor("cmd2.exe"))
                //{
                //    n2.Add(realConsole);
                //    n2.Link(realConsole.Box.Id, ((IDynamicProxy)console).Proxy.Remote);
                //    console.SendAttach(realConsole.Box.Id);
                //    while (realConsole.IsAlive)
                //        Thread.Sleep(10);
                //}

            } // close the connection and remove from n2. Now there is no route to the first node and the echo actor
            // TODO: set default ports so given a computer name we can try to connect to a few ports if someone
            // tries to send a message to one of those actors then we could avoid this explicit connection and
            // let the node/VM handle it.

            //TODO: change router to directly put messages in mailbox using function without going through tcp sockets
            // if the other actor is in the same node. This would make it feasible to use actors for some things inside a single
            // process/node/program
        }