Exemple #1
0
        public VirtualNode(EvalEnvironment env, VirtualNetwork network, EvalOptionSet opt,
			IntervalInterrupter messagingInt, IntervalInterrupter kbrStabilizeInt, IntervalInterrupter anonInt, IntervalInterrupter dhtInt)
        {
            IPAddress pubAdrs = _ipGenerator.Next ();
            int bindPort;
            lock (_rnd) {
                bindPort = _rnd.Next (1024, ushort.MaxValue);
            }
            _pubEP = new IPEndPoint (pubAdrs, bindPort);
            _nodePrivateKey = ECKeyPair.Create (DefaultECDomain);
            _nodeId = Key.Create (_nodePrivateKey);
            VirtualDatagramEventSocket sock = new VirtualDatagramEventSocket (network, pubAdrs);
            sock.Bind (new IPEndPoint (IPAddress.Any, bindPort));
            _msock = opt.BypassMessagingSerializer
                ? (IMessagingSocket)new VirtualMessagingSocket (sock, true, messagingInt, DefaultMessagingRTO, DefaultMessagingRetries, DefaultMessagingRetryBufferSize, DefaultMessagingDupCheckSize)
                : (IMessagingSocket)new MessagingSocket (sock, true, SymmetricKey.NoneKey, Serializer.Instance, null, messagingInt, DefaultMessagingRTO, DefaultMessagingRetries, DefaultMessagingRetryBufferSize, DefaultMessagingDupCheckSize);
            _kbr = new SimpleIterativeRouter2 (_nodeId, 0, _msock, new SimpleRoutingAlgorithm (), Serializer.Instance, opt.NewKBRStrictMode);
            _localDHT = new OnMemoryLocalHashTable (_kbr, dhtInt);
            _dht = new SimpleDHT (_kbr, _msock, _localDHT);
            _dht.RegisterTypeID (typeof (string), 0, new LocalHashTableValueMerger<string> ());
            p2pncs.Net.Overlay.Anonymous.AnonymousRouter.DefaultRelayNodes = opt.AnonymousRouteRelays;
            p2pncs.Net.Overlay.Anonymous.AnonymousRouter.DefaultSubscribeRoutes = opt.AnonymousRouteRoutes + opt.AnonymousRouteBackupRoutes;
            p2pncs.Net.Overlay.Anonymous.AnonymousRouter.AC_DefaultUseSubscribeRoutes = opt.AnonymousRouteRoutes;
            _anonRouter = new AnonymousRouter (_dht, _nodePrivateKey, anonInt);
            _kbrStabilizeInt = kbrStabilizeInt;
            _kbrStabilizeInt.AddInterruption (_kbr.RoutingAlgorithm.Stabilize);
            _env = env;
        }
        public void Evaluate(EvalOptionSet opt)
        {
            using (EvalEnvironment env = new EvalEnvironment (opt)) {
                env.AddNodes (opt.NumberOfNodes, true);
                env.StartChurn ();

                ISubscribeInfo subscribeInfo1 = env.Nodes[0].Subscribe ();
                ISubscribeInfo subscribeInfo2 = env.Nodes[1].Subscribe ();

                while (true) {
                    if (subscribeInfo1.Status == SubscribeRouteStatus.Stable && subscribeInfo2.Status == SubscribeRouteStatus.Stable)
                        break;
                    Thread.Sleep (10);
                }

                bool routeEstablished = false;
                IAnonymousSocket sock1 = null, sock2 = null;
                StreamSocket strm1 = null, strm2 = null;
                do {
                    int datasize = 1000 * 1000;
                    IntervalInterrupter timeoutChecker = new IntervalInterrupter (TimeSpan.FromMilliseconds (100), "StreamSocket TimeoutChecker");
                    timeoutChecker.Start ();
                    try {
                        IAsyncResult ar = env.Nodes[0].AnonymousRouter.BeginConnect (subscribeInfo1.Key, subscribeInfo2.Key, AnonymousConnectionType.HighThroughput, null, null, null);
                        sock1 = env.Nodes[0].AnonymousRouter.EndConnect (ar);
                        if (env.Nodes[1].AnonymousSocketInfoList.Count == 0)
                            throw new System.Net.Sockets.SocketException ();
                        routeEstablished = true;
                        sock2 = env.Nodes[1].AnonymousSocketInfoList[0].BaseSocket;
                        strm1 = new StreamSocket (sock1, AnonymousRouter.DummyEndPoint, 500, timeoutChecker);
                        strm2 = new StreamSocket (sock2, AnonymousRouter.DummyEndPoint, 500, timeoutChecker);
                        sock1.InitializedEventHandlers ();
                        sock2.InitializedEventHandlers ();
                        Stopwatch sw = Stopwatch.StartNew ();
                        byte[] data = new byte[datasize];
                        strm1.Send (data, 0, data.Length);
                        strm1.Shutdown ();
                        strm2.Shutdown ();
                        Logger.Log (LogLevel.Info, this, "{0:f1}sec, {1:f2}Mbps", sw.Elapsed.TotalSeconds, datasize * 8 / sw.Elapsed.TotalSeconds / 1000.0 / 1000.0);
                    } catch {
                    } finally {
                        timeoutChecker.Dispose ();
                        if (sock1 != null) sock1.Dispose ();
                        if (sock2 != null) sock2.Dispose ();
                        if (strm1 != null) strm1.Dispose ();
                        if (strm2 != null) strm2.Dispose ();
                    }
                } while (!routeEstablished);
            }
        }
Exemple #3
0
        public void Evaluate(EvalOptionSet opt)
        {
            using (EvalEnvironment env = new EvalEnvironment (opt)) {
                env.AddNodes (opt.NumberOfNodes, true);
                env.StartChurn ();

                VirtualNode testNode = env.Nodes[0];

                int returned = 0;
                int[] candidates = new int[3];
                ManualResetEvent reqDone = new ManualResetEvent (false);
                for (int i = 0; i < opt.Tests; i++) {
                    Key target = Key.CreateRandom (testNode.NodeID.KeyBytes);
                    testNode.KeyBasedRouter.BeginRoute (target, null, candidates.Length, 4, delegate (IAsyncResult ar) {
                        Key target2 = ar.AsyncState as Key;
                        RoutingResult result = testNode.KeyBasedRouter.EndRoute (ar);
                        if (result != null && result.RootCandidates != null && result.RootCandidates.Length > 0) {
                            VirtualNode[] nodes;
                            lock (env.Nodes) {
                                nodes = env.Nodes.ToArray ();
                            }
                            Array.Sort<VirtualNode> (nodes, delegate (VirtualNode x, VirtualNode y) {
                                Key diff1 = testNode.KeyBasedRouter.RoutingAlgorithm.ComputeDistance (target2, x.NodeID);
                                Key diff2 = testNode.KeyBasedRouter.RoutingAlgorithm.ComputeDistance (target2, y.NodeID);
                                return diff1.CompareTo (diff2);
                            });
                            for (int k = 0; k < Math.Min (candidates.Length, result.RootCandidates.Length); k ++) {
                                if (nodes[k].NodeID.Equals (result.RootCandidates[k].NodeID))
                                    candidates[k] ++;
                            }
                        }
                        int ret = Interlocked.Increment (ref returned);
                        if (ret == opt.Tests)
                            reqDone.Set ();
                        Console.Write ("*");
                    }, target);
                    Thread.Sleep (TimeSpan.FromSeconds (0.2));
                }
                reqDone.WaitOne ();
                for (int i = 0; i < candidates.Length; i ++)
                    Console.Write ("{0}/", candidates[i]);
                Console.WriteLine ("{0}", returned);
            }
        }
Exemple #4
0
        public void Evaluate(EvalOptionSet opt)
        {
            using (EvalEnvironment env = new EvalEnvironment (opt)) {
                env.AddNodes (opt.NumberOfNodes, true);
                env.StartChurn ();

                VirtualNode testNode = env.Nodes[0];
                List<Key> list = new List<Key> ();
                for (int i = 0; i < opt.Tests; i++) {
                    Key key = Key.CreateRandom (testNode.NodeID.KeyBytes);
                    list.Add (key);
                    testNode.DistributedHashTable.BeginPut (key, TimeSpan.FromHours (1), Convert.ToBase64String (key.GetByteArray ()), null, null);
                    Thread.Sleep (TimeSpan.FromSeconds (0.2));
                }

                int returned = 0, successed = 0;
                ManualResetEvent getDone = new ManualResetEvent (false);
                for (int i = 0; i < opt.Tests; i++) {
                    testNode.DistributedHashTable.BeginGet (list[i], typeof (string), delegate (IAsyncResult ar) {
                        GetResult result = testNode.DistributedHashTable.EndGet (ar);
                        string expected = ar.AsyncState as string;
                        if (result != null && result.Values != null && result.Values.Length > 0) {
                            if (expected.Equals (result.Values[0] as string)) {
                                Interlocked.Increment (ref successed);
                                Console.Write ("*");
                            } else {
                                Console.Write ("=");
                            }
                        } else {
                            Console.Write ("?");
                        }
                        if (Interlocked.Increment (ref returned) == opt.Tests)
                            getDone.Set ();
                    }, Convert.ToBase64String (list[i].GetByteArray ()));
                    Thread.Sleep (TimeSpan.FromSeconds (0.2));
                }
                getDone.WaitOne ();
                Console.WriteLine ("{0}/{1}", successed, returned);
            }
        }
        public void Evaluate(EvalOptionSet opt)
        {
            using (EvalEnvironment env = new EvalEnvironment (opt)) {
                env.AddNodes (opt.NumberOfNodes, true);

                ISubscribeInfo subscribeInfo1 = env.Nodes[0].Subscribe ();
                ISubscribeInfo subscribeInfo2 = env.Nodes[1].Subscribe ();
                string strKey1 = subscribeInfo1.Key.ToString ();
                string strKey2 = subscribeInfo2.Key.ToString ();

                IMessagingSocket msock1 = null, msock2 = null;

                while (true) {
                    if (subscribeInfo1.Status == SubscribeRouteStatus.Stable && subscribeInfo2.Status == SubscribeRouteStatus.Stable)
                        break;
                    Thread.Sleep (10);
                }

                env.StartChurn ();
                bool routeEstablished = false;
                do {
                    try {
                        IAsyncResult ar = env.Nodes[0].AnonymousRouter.BeginConnect (subscribeInfo1.Key, subscribeInfo2.Key, AnonymousConnectionType.LowLatency, null, null, null);
                        Stopwatch sw = Stopwatch.StartNew ();
                        IAnonymousSocket sock1 = env.Nodes[0].AnonymousRouter.EndConnect (ar);
                        sw.Stop ();
                        if (env.Nodes[1].AnonymousSocketInfoList.Count == 0) {
                            throw new System.Net.Sockets.SocketException ();
                        }
                        Logger.Log (LogLevel.Info, this, "Connected: {0}ms", sw.ElapsedMilliseconds);

                        msock1 = env.Nodes[0].CreateAnonymousSocket (sock1);
                        msock2 = env.Nodes[1].AnonymousSocketInfoList[env.Nodes[1].AnonymousSocketInfoList.Count - 1].MessagingSocket;
                        routeEstablished = true;

                        DateTime dt = DateTime.Now;
                        StandardDeviation rtt_sd = new StandardDeviation (false);
                        int tests = 0, success = 0;
                        for (int i = 0; i < opt.Tests; i ++) {
                            string ret;

                            tests ++;
                            string msg = "Hello-" + strKey1 + "-" + i.ToString ();
                            sw.Reset (); sw.Start ();
                            ar = msock1.BeginInquire (msg, AnonymousRouter.DummyEndPoint, null, null);
                            ret = msock1.EndInquire (ar) as string;
                            sw.Stop ();
                            if (ret == null) {
                                Console.Write ("?");
                            } else if (ret != msg + "-" + strKey2 + "-ok") {
                                Console.Write ("@");
                            } else {
                                rtt_sd.AddSample ((float)sw.Elapsed.TotalMilliseconds);
                                Console.Write ("*");
                                success ++;
                            }

                            tests++;
                            msg = "Hello-" + strKey1 + "-" + i.ToString ();
                            sw.Reset (); sw.Start ();
                            ar = msock2.BeginInquire (msg, AnonymousRouter.DummyEndPoint, null, null);
                            ret = msock2.EndInquire (ar) as string;
                            sw.Stop ();
                            if (ret == null) {
                                Console.Write ("?");
                            } else if (ret != msg + "-" + strKey1 + "-ok") {
                                Console.Write ("@");
                            } else {
                                rtt_sd.AddSample ((float)sw.Elapsed.TotalMilliseconds);
                                Console.Write ("=");
                                success++;
                            }
                        }
                        Console.WriteLine ();
                        int minJitter, maxJitter;
                        double avgJitter, sdJitter;
                        env.Network.GetAndResetJitterHistory (out minJitter, out avgJitter, out sdJitter, out maxJitter);
                        Logger.Log (LogLevel.Info, this, "Time: {0:f2}sec, Jitter: {1}/{2:f1}({3:f1})/{4}, DeliverSuccess={5:p}, RTT: Avg={6:f1}({7:f1})",
                            DateTime.Now.Subtract (dt).TotalSeconds, minJitter, avgJitter, sdJitter, maxJitter, (double)success / (double)tests, rtt_sd.Average, rtt_sd.ComputeStandardDeviation ());
                    } catch {
                        Logger.Log (LogLevel.Info, this, "Establish Failed. Retry...");
                        return;
                    } finally {
                        if (msock1 != null) msock1.Dispose ();
                        if (msock2 != null) msock2.Dispose ();
                    }
                } while (!routeEstablished);
            }
        }
        public void Evaluate(EvalOptionSet opt)
        {
            // 強制的にchurn=0, packet-loss=0を設定
            opt.ChurnInterval = 0;
            opt.PacketLossRate = 0.0;

            // 総コネクション数 = EvalOptionSet.Tests
            int connections = opt.Tests;

            Random rnd = new Random ();
            using (EvalEnvironment env = new EvalEnvironment (opt)) {
                env.AddNodes (opt.NumberOfNodes, true);
                List<Info> subscribedList = new List<Info> ();
                HashSet<Key> aliveRecipients = new HashSet<Key> ();
                Dictionary<VirtualNode, Info> mapping = new Dictionary<VirtualNode,Info> ();
                int simultaneouslyProcess = 2;
                int px, py;

                Console.Write ("Subscribe ALL Nodes...");
                px = Console.CursorLeft;
                py = Console.CursorTop;
                for (int i = 0; i < opt.NumberOfNodes; i++) {
                    _connectingDone.WaitOne ();
                    if (Interlocked.Increment (ref _connecting) < simultaneouslyProcess)
                        _connectingDone.Set ();

                    Info info = new Info (env.Nodes[i]);
                    aliveRecipients.Add (info.PublicKey);
                    mapping.Add (env.Nodes[i], info);
                    subscribedList.Add (info);
                    ThreadPool.QueueUserWorkItem (SubscribeWait_Thread, info);
                    Console.CursorLeft = px;
                    Console.CursorTop = py;
                    Console.Write ("{0}/{1}", i, opt.NumberOfNodes);
                }

                Console.CursorLeft = px;
                Console.CursorTop = py;
                Console.Write ("waiting...{0}", new string (' ', Console.WindowWidth - px - 11));
                while (true) {
                    _connectingDone.WaitOne ();
                    if (Interlocked.Add (ref _connecting, 0) == 0)
                        break;
                }
                Console.CursorLeft = px;
                Console.CursorTop = py;
                Console.WriteLine ("ok{0}", new string (' ', Console.WindowWidth - px - 3));

                Console.Write ("Establishing ALL Connection...");
                _connectingDone.Set ();
                px = Console.CursorLeft;
                py = Console.CursorTop;
                for (int i = 0; i < connections; i++) {
                    _connectingDone.WaitOne ();
                    if (Interlocked.Increment (ref _connecting) < simultaneouslyProcess)
                        _connectingDone.Set ();

                    Info info = subscribedList[rnd.Next (subscribedList.Count)];
                    Info destInfo;
                    while (true) {
                        int idx = rnd.Next (subscribedList.Count);
                        destInfo = subscribedList[idx];
                        info.TempDest = destInfo.PublicKey;
                        if (destInfo != info)
                            break;
                    }
                    ThreadPool.QueueUserWorkItem (EstablishConnect_Thread, new object[] {info, destInfo});
                    Console.CursorLeft = px;
                    Console.CursorTop = py;
                    Console.Write ("{0}/{1}", i, connections);
                }
                Console.CursorLeft = px;
                Console.CursorTop = py;
                Console.Write ("waiting...{0}", new string (' ', Console.WindowWidth - px - 11));
                while (true) {
                    _connectingDone.WaitOne ();
                    if (Interlocked.Add (ref _connecting, 0) == 0)
                        break;
                }
                Console.CursorLeft = px;
                Console.CursorTop = py;
                Console.WriteLine ("ok{0}", new string (' ', Console.WindowWidth - px - 3));

                Console.WriteLine ("Start");
                long lastPackets = env.Network.Packets;
                do {
                    for (int i = 0; i < subscribedList.Count; i ++) {
                        IList<AnonymousSocketInfo> list;
                        lock (subscribedList[i].Node.AnonymousSocketInfoList) {
                            list = new List<AnonymousSocketInfo> (subscribedList[i].Node.AnonymousSocketInfoList);
                        }
                        if (list.Count == 0) continue;
                        string msg = "Hello-" + DateTime.Now.Ticks.ToString () + "-" + subscribedList[i].PublicKey.ToString ();
                        for (int k = 0; k < list.Count; k ++) {
                            list[k].MessagingSocket.BeginInquire (msg, AnonymousRouter.DummyEndPoint, Messaging_Callback,
                                new object[] {subscribedList[i], list[k], msg, Stopwatch.StartNew ()});
                        }
                        Thread.Sleep (50);
                    }

                    Thread.Sleep (1000);
                    long packets = env.Network.Packets;
                    lock (Console.Out) {
                        Console.WriteLine ();
                        int minJitter, maxJitter;
                        double avgJitter, sdJitter, avgRtt, sdRtt;
                        env.Network.GetAndResetJitterHistory (out minJitter, out avgJitter, out sdJitter, out maxJitter);
                        lock (_rtt_sd) {
                            avgRtt = _rtt_sd.Average;
                            sdRtt = _rtt_sd.ComputeStandardDeviation ();
                        }
                        Logger.Log (LogLevel.Info, this, "Jitter={0}/{1:f1}({2:f1})/{3}, DeliverSuccess={4:p}, RTT={5:f1}({6:f1}), Packets={7}",
                            minJitter, avgJitter, sdJitter, maxJitter, (double)_success_count / (double)_tests, avgRtt, sdRtt, packets - lastPackets);
                    }
                    lastPackets = packets;
                } while (false);
            }
        }