示例#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 Test_SendToWrongPort()
        {
            IPEndPoint ep1 = new IPEndPoint (IPAddress.Parse ("10.0.0.1"), 10000);
            IPEndPoint ep2 = new IPEndPoint (IPAddress.Parse ("10.0.0.2"), 10000);

            VirtualNetwork network = new VirtualNetwork (LatencyTypes.Constant (20), 5, PacketLossType.Lossless (), 2);
            byte[] msg = new byte[]{0, 1, 2, 3};
            try {
                using (AutoResetEvent done = new AutoResetEvent (false))
                using (VirtualDatagramEventSocket sock1 = new VirtualDatagramEventSocket (network, ep1.Address))
                using (VirtualDatagramEventSocket sock2 = new VirtualDatagramEventSocket (network, ep2.Address)) {
                    sock1.Bind (new IPEndPoint (IPAddress.Any, ep1.Port));
                    sock2.Bind (new IPEndPoint (IPAddress.Any, ep2.Port));
                    sock1.Received += new DatagramReceiveEventHandler (delegate (object sender, DatagramReceiveEventArgs e) {
                        done.Set ();
                    });
                    sock2.Received += new DatagramReceiveEventHandler (delegate (object sender, DatagramReceiveEventArgs e) {
                        done.Set ();
                    });
                    sock1.SendTo (msg, new IPEndPoint (ep2.Address, ep2.Port + 1));
                    Assert.IsFalse (done.WaitOne (500));
                    sock2.SendTo (msg, ep1);
                    Assert.IsTrue (done.WaitOne ());
                }
            } finally {
                network.Close ();
            }
        }
        public void Test1()
        {
            IPEndPoint ep1 = new IPEndPoint (IPAddress.Parse ("10.0.0.1"), 10000);
            IPEndPoint ep2 = new IPEndPoint (IPAddress.Parse ("10.0.0.2"), 10000);
            IPEndPoint ep3 = new IPEndPoint (IPAddress.Parse ("10.0.0.3"), 10000);

            VirtualNetwork network = new VirtualNetwork (LatencyTypes.Constant (20), 5, PacketLossType.Lossless (), 2);
            try {
                using (VirtualDatagramEventSocket sock1 = new VirtualDatagramEventSocket (network, ep1.Address))
                using (VirtualDatagramEventSocket sock2 = new VirtualDatagramEventSocket (network, ep2.Address))
                using (VirtualDatagramEventSocket sock3 = new VirtualDatagramEventSocket (network, ep3.Address)) {
                    IDatagramEventSocket[] sockets = new IDatagramEventSocket[] { sock1, sock2, sock3 };
                    EndPoint[] endPoints = new EndPoint[] { ep1, ep2, ep3 };
                    base.Test1 (sockets, endPoints);
                }
            } finally {
                network.Close ();
            }
        }
示例#4
0
文件: Program.cs 项目: kazuki/p2pncs
 DebugNode AddNode(int gw_port)
 {
     int nodeIdx = Interlocked.Increment (ref _nodeIdx);
     IPAddress adrs = IPAddress.Loopback;
     VirtualDatagramEventSocket sock = new VirtualDatagramEventSocket (_network, adrs);
     IPEndPoint pubEP = new IPEndPoint (adrs, 1 + nodeIdx);
     IPEndPoint bindTcpEP = new IPEndPoint (IPAddress.Loopback, 30000 + nodeIdx);
     sock.Bind (new IPEndPoint (IPAddress.Any, pubEP.Port));
     DebugNode node = DebugNode.Create (nodeIdx, _ints, sock, bindTcpEP, pubEP, gw_port);
     lock (_list) {
         _list.Add (node);
         _eps.Add (pubEP);
     }
     if (_init_nodes == null) {
         node.PortOpenChecker.Join (_eps.ToArray ());
         _eps.Add (pubEP);
         if (_eps.Count == 4)
             _init_nodes = _eps.ToArray ();
     } else {
         node.PortOpenChecker.Join (_init_nodes);
     }
     return node;
 }
 protected override void CreateMessagingSocket(int idx, SymmetricKey key, out IMessagingSocket socket, out EndPoint endPoint)
 {
     endPoint = new IPEndPoint (_adrsGen.Next (), 10000);
     VirtualDatagramEventSocket sock = new VirtualDatagramEventSocket (_net, ((IPEndPoint)endPoint).Address);
     sock.Bind (endPoint);
     socket = new VirtualMessagingSocket (sock, true, _interrupter, DefaultRTO, DefaultRetryCount, 1024, 1024);
 }
示例#6
0
 public void AddNodes(Key[] keys, ECKeyPair[] keyPairs)
 {
     for (int i = 0; i < keys.Length; i++) {
         IPAddress adrs = _ipGenerator.Next ();
         IPEndPoint ep = new IPEndPoint (adrs, 10000);
         VirtualDatagramEventSocket sock = new VirtualDatagramEventSocket (_network, adrs);
         sock.Bind (new IPEndPoint (IPAddress.Loopback, ep.Port));
         //IMessagingSocket msock = new MessagingSocket (sock, true, SymmetricKey.NoneKey, Serializer.Instance, null, _interrupter, TimeSpan.FromSeconds (1), 2, 1024);
         IMessagingSocket msock = new VirtualMessagingSocket (sock, true, _interrupter, _rtoAlgo, 2, 1024, 1024);
         _sockets.Add (msock);
         IKeyBasedRouter router = new SimpleIterativeRouter2 (keys[i], 0, msock, new SimpleRoutingAlgorithm (), Serializer.Instance, true);
         _routers.Add (router);
         if (_dhts != null) {
             IDistributedHashTable dht = new SimpleDHT (router, msock, new OnMemoryLocalHashTable (router, _dhtInt));
             _dhts.Add (dht);
             if (_anons != null) {
                 IAnonymousRouter anonRouter = new AnonymousRouter (dht, keyPairs[i], _anonInt);
                 _anons.Add (anonRouter);
             }
         }
         if (_endPoints.Count != 0) {
             if (_initEPs == null || _endPoints.Count < 3)
                 _initEPs = _endPoints.ToArray ();
             router.Join (_initEPs);
         }
         _endPoints.Add (ep);
         Thread.Sleep (5);
     }
     Thread.Sleep (500);
 }
示例#7
0
 public VirtualNetworkNode(VirtualDatagramEventSocket sock, EndPoint bindEP)
 {
     _sock = sock;
     _bindEP = bindEP;
 }
示例#8
0
 internal VirtualNetworkNode AddVirtualNode(VirtualDatagramEventSocket sock, EndPoint bindEP)
 {
     VirtualNetworkNode node = new VirtualNetworkNode (sock, bindEP);
     using (_nodeLock.EnterWriteLock ()) {
         _mapping[bindEP] = node;
         _nodes.Add (node);
     }
     return node;
 }
示例#9
0
 internal void AddVirtualMessagingSocketToVirtualNode(VirtualDatagramEventSocket sock, VirtualMessagingSocket msock)
 {
     using (_nodeLock.EnterReadLock ()) {
         VirtualNetworkNode node;
         if (_mapping.TryGetValue (sock.BindedPublicEndPoint, out node)) {
             node.MessagingSocket = msock;
         }
     }
 }
示例#10
0
        public VirtualMessagingSocket(VirtualDatagramEventSocket baseSock, bool ownSocket,
			IntervalInterrupter interrupter, IRTOAlgorithm rtoAlgo, int maxRetry, int retryBufferSize, int inquiryDupCheckSize)
            : base(baseSock, ownSocket, interrupter, rtoAlgo, maxRetry, retryBufferSize, inquiryDupCheckSize)
        {
            baseSock.VirtualNetwork.AddVirtualMessagingSocketToVirtualNode (baseSock, this);
        }