public async void TestBootstrapDiscover() { var rnd = new Random(42); Peer master = null; Peer slave = null; try { master = new PeerBuilder(new Number160(rnd)) .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask()) .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(4001, 4001)) .SetPorts(4001) .Start(); slave = new PeerBuilder(new Number160(rnd)) .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask()) .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(4002, 4002)) .SetPorts(4002) .Start(); var tcsDiscover = master.Discover().SetPeerAddress(slave.PeerAddress).Start(); await tcsDiscover.Task; Assert.IsTrue(!tcsDiscover.Task.IsFaulted); } finally { if (master != null) { master.ShutdownAsync().Wait(); } if (slave != null) { slave.ShutdownAsync().Wait(); } } }
/// <summary> /// Creates peers for benchmarking. The first peer will be used as the master. /// This means that shutting down the master will shut down all other peers as well. /// </summary> /// <param name="nrOfPeers">Number of peers to create.</param> /// <param name="rnd">The random object used for peer ID creation.</param> /// <param name="port">The UDP and TCP port.</param> /// <param name="maintenance">Indicates whether maintenance should be enabled.</param> /// <param name="timeout">Indicates whether timeout should be enabled.</param> /// <returns></returns> public static Peer[] CreateNodes(int nrOfPeers, InteropRandom rnd, int port, bool maintenance, bool timeout) { Console.WriteLine("Creating network with {0} peers...", nrOfPeers); var peers = new Peer[nrOfPeers]; var masterId = CreateRandomId(rnd); var masterMap = new PeerMap(new PeerMapConfiguration(masterId)); var pb = new PeerBuilder(masterId) .SetPorts(port) .SetEnableMaintenance(maintenance) .SetExternalBindings(new Bindings()) .SetPeerMap(masterMap); if (!timeout) { pb.SetChannelServerConfiguration(CreateInfiniteTimeoutChannelServerConfiguration(port)); } peers[0] = pb.Start(); //Logger.Info("Created master peer: {0}.", peers[0].PeerId); for (int i = 1; i < nrOfPeers; i++) { peers[i] = CreateSlave(peers[0], rnd, maintenance, timeout); } return peers; }
public async void TestOrder() { Peer sender = null; Peer recv1 = null; ChannelCreator cc = null; try { sender = new PeerBuilder(new Number160("0x50")) .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask()) .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(2525, 2525)) .SetP2PId(55) .SetPorts(2525) .Start(); recv1 = new PeerBuilder(new Number160("0x20")) .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask()) .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(9099, 9099)) .SetP2PId(55) .SetPorts(8088) .Start(); recv1.RawDataReply(new TestOrderRawDataReply()); for (int i = 0; i < 500; i++) { cc = await sender.ConnectionBean.Reservation.CreateAsync(0, 1); var sendDirectBuilder = new SendDirectBuilder(sender, (PeerAddress) null); var buffer = AlternativeCompositeByteBuf.CompBuffer().WriteInt(i); sendDirectBuilder.SetBuffer(new Buffer(buffer)); sendDirectBuilder.SetIsStreaming(); var tr = sender.DirectDataRpc.SendAsync(recv1.PeerAddress, sendDirectBuilder, cc); Core.Utils.Utils.AddReleaseListener(cc, tr); tr.ContinueWith(t => { int j = t.Result.Buffer(0).BackingBuffer.ReadInt(); Console.WriteLine("Received {0}.", j); }); } } finally { if (sender != null) { sender.ShutdownAsync().Wait(); } if (recv1 != null) { recv1.ShutdownAsync().Wait(); } if (cc != null) { cc.ShutdownAsync().Wait(); } } }
public async void TestDirect1() { Peer sender = null; Peer recv1 = null; ChannelCreator cc = null; try { sender = new PeerBuilder(new Number160("0x50")) .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask()) .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(2525, 2525)) .SetP2PId(55) .SetPorts(2525) .Start(); recv1 = new PeerBuilder(new Number160("0x20")) .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask()) .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(9099, 9099)) .SetP2PId(55) .SetPorts(8088) .Start(); recv1.RawDataReply(new TestRawDataReply()); cc = await sender.ConnectionBean.Reservation.CreateAsync(0, 1); var sendDirectBuilder = new SendDirectBuilder(sender, (PeerAddress) null); sendDirectBuilder.SetIsStreaming(); sendDirectBuilder.SetIdleTcpSeconds(Int32.MaxValue); var buffer = CreateTestBuffer(); sendDirectBuilder.SetBuffer(buffer); var tr1 = sender.DirectDataRpc.SendAsync(recv1.PeerAddress, sendDirectBuilder, cc); await tr1; Assert.IsTrue(!tr1.IsFaulted); var ret = tr1.Result.Buffer(0); Assert.AreEqual(buffer, ret); } finally { if (sender != null) { sender.ShutdownAsync().Wait(); } if (recv1 != null) { recv1.ShutdownAsync().Wait(); } if (cc != null) { cc.ShutdownAsync().Wait(); } } }
public async void TestBootstrapFail() { var rnd = new Random(42); Peer master = null; Peer slave = null; try { master = new PeerBuilder(new Number160(rnd)) .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask()) .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(4001, 4001)) .SetPorts(4001) .Start(); slave = new PeerBuilder(new Number160(rnd)) .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask()) .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(4002, 4002)) .SetPorts(4002) .Start(); // bootstrap to another address/port var taskBootstrap = master.Bootstrap() .SetInetAddress(IPAddress.Loopback) .SetPorts(3000) .StartAsync(); try { await taskBootstrap; Assert.Fail("The bootstrapping task should fail."); } catch (Exception) { Assert.IsTrue(taskBootstrap.IsFaulted); } // bootstrap to correct address taskBootstrap = master.Bootstrap() .SetPeerAddress(slave.PeerAddress) .StartAsync(); await taskBootstrap; Assert.IsTrue(!taskBootstrap.IsFaulted); } finally { if (master != null) { master.ShutdownAsync().Wait(); } if (slave != null) { slave.ShutdownAsync().Wait(); } } }
public async void TestReservationUdp() { Peer sender = null; Peer recv1 = null; ChannelCreator cc = null; try { sender = new PeerBuilder(new Number160("0x9876")) .SetP2PId(55) .SetPorts(2424) .Start(); recv1 = new PeerBuilder(new Number160("0x1234")) .SetP2PId(55) .SetPorts(8088) .Start(); cc = await recv1.ConnectionBean.Reservation.CreateAsync(3, 0); for (int i = 0; i < 100; i++) { var tr1 = sender.PingRpc.PingUdpAsync(recv1.PeerAddress, cc, new DefaultConnectionConfiguration()); var tr2 = sender.PingRpc.PingUdpAsync(recv1.PeerAddress, cc, new DefaultConnectionConfiguration()); var tr3 = sender.PingRpc.PingUdpAsync(recv1.PeerAddress, cc, new DefaultConnectionConfiguration()); await tr1; await tr2; await tr3; Assert.IsTrue(!tr1.IsFaulted); Assert.IsTrue(!tr2.IsFaulted); Assert.IsTrue(!tr3.IsFaulted); } } catch (Exception ex) { Console.Error.WriteLine(ex); throw; } finally { if (sender != null) { sender.ShutdownAsync().Wait(); } if (recv1 != null) { recv1.ShutdownAsync().Wait(); } if (cc != null) { cc.ShutdownAsync().Wait(); } } }
private static Peer CreateSlave(Peer master, InteropRandom rnd, bool maintenance, bool timeout) { var slaveId = CreateRandomId(rnd); var slaveMap = new PeerMap(new PeerMapConfiguration(slaveId).SetPeerNoVerification()); var pb = new PeerBuilder(slaveId) .SetMasterPeer(master) .SetEnableMaintenance(maintenance) .SetExternalBindings(new Bindings()) .SetPeerMap(slaveMap); if (!timeout) { pb.SetChannelServerConfiguration(CreateInfiniteTimeoutChannelServerConfiguration(Ports.DefaultPort)); } var slave = pb.Start(); //Logger.Info("Created slave peer {0}.", slave.PeerId); return slave; }
public async void TestPingTcp2() { Peer sender = null; Peer recv1 = null; ChannelCreator cc = null; try { sender = new PeerBuilder(new Number160("0x9876")) .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask()) .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(2424, 2424)) .SetP2PId(55) .SetPorts(2424) .Start(); recv1 = new PeerBuilder(new Number160("0x1234")) .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask()) .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(8088, 8088)) .SetP2PId(55) .SetPorts(8088) .Start(); // TODO check release in cc for first ping cc = await recv1.ConnectionBean.Reservation.CreateAsync(0, 1); var tr = sender.PingRpc.PingTcpAsync(recv1.PeerAddress, cc, new DefaultConnectionConfiguration()); await tr; var tr2 = recv1.PingRpc.PingTcpAsync(sender.PeerAddress, cc, new DefaultConnectionConfiguration()); await tr2; Assert.IsTrue(!tr.IsFaulted); Assert.IsTrue(!tr2.IsFaulted); } finally { if (cc != null) { cc.ShutdownAsync().Wait(); } if (sender != null) { sender.ShutdownAsync().Wait(); } if (recv1 != null) { recv1.ShutdownAsync().Wait(); } } }
public async void TestPingUdpToJava() { // setup Java server and get it's PeerAddress _tcs = new TaskCompletionSource<PeerAddress>(); ThreadPool.QueueUserWorkItem(delegate { JarRunner.Run("JavaPingReceiver-start", DataReceived); }); PeerAddress server = await _tcs.Task; // ping & test Peer sender = null; ChannelCreator cc = null; try { // setup .NET sender sender = new PeerBuilder(new Number160("0x9876")). SetP2PId(55). SetPorts(2424). Start(); cc = await sender.ConnectionBean.Reservation.CreateAsync(1, 0); var handshake = new PingRpc(sender.PeerBean, sender.ConnectionBean); var task = handshake.PingUdpAsync(server, cc, new DefaultConnectionConfiguration()); var responseMessage = await task; Assert.IsTrue(task.IsCompleted && !task.IsFaulted); Assert.AreEqual(responseMessage.Sender, server); Assert.IsTrue(responseMessage.Type == Message.MessageType.Ok); Assert.IsTrue(responseMessage.Command == Core.Rpc.Rpc.Commands.Ping.GetNr()); } finally { if (cc != null) { cc.ShutdownAsync().Wait(); } if (sender != null) { sender.ShutdownAsync().Wait(); } JarRunner.WriteToProcess("JavaPingReceiver-stop"); } }
public async void TestPingReserve() { Peer sender = null; Peer recv1 = null; try { sender = new PeerBuilder(new Number160("0x9876")) .SetP2PId(55) .SetPorts(2424) .Start(); recv1 = new PeerBuilder(new Number160("0x9876")) .SetP2PId(55) .SetPorts(8088) .Start(); var cc = await recv1.ConnectionBean.Reservation.CreateAsync(0, 1); var taskResponse1 = sender.PingRpc.PingTcpAsync(recv1.PeerAddress, cc, new DefaultConnectionConfiguration()); var listenerCompl = Core.Utils.Utils.AddReleaseListener(cc, taskResponse1); await taskResponse1; await listenerCompl; // await release of reservations Assert.IsTrue(!taskResponse1.IsFaulted); var taskResponse2 = sender.PingRpc.PingTcpAsync(recv1.PeerAddress, cc, new DefaultConnectionConfiguration()); try { await taskResponse2; Assert.Fail("The already shut down reservation should have prohibited a new channel creation."); } catch (TaskFailedException) { // reservations have been released already Assert.IsTrue(taskResponse2.IsFaulted); } } finally { if (sender != null) { sender.ShutdownAsync().Wait(); } if (recv1 != null) { recv1.ShutdownAsync().Wait(); } } }
public async void TestPingTimeUdp() { Peer sender = null; Peer recv1 = null; try { sender = new PeerBuilder(new Number160("0x9876")) .SetP2PId(55) .SetPorts(2424) .Start(); recv1 = new PeerBuilder(new Number160("0x9876")) .SetP2PId(55) .SetPorts(8088) .Start(); long start = Convenient.CurrentTimeMillis(); var tasks = new List<Task<Message>>(1000); for (int i = 0; i < 20; i++) { var cc = await recv1.ConnectionBean.Reservation.CreateAsync(50, 0); for (int j = 0; j < 50; j++) { var taskResponse = sender.PingRpc.PingUdpAsync(recv1.PeerAddress, cc, new DefaultConnectionConfiguration()); tasks.Add(taskResponse); } foreach (var task in tasks) { await task; Assert.IsTrue(!task.IsFaulted); } tasks.Clear(); await cc.ShutdownAsync(); } Console.WriteLine("UDP time: {0}ms.", Convenient.CurrentTimeMillis() - start); } finally { if (sender != null) { sender.ShutdownAsync().Wait(); } if (recv1 != null) { recv1.ShutdownAsync().Wait(); } } }
public async void TestPingUdpPool2() { var peers = new Peer[50]; try { for (int i = 0; i < peers.Length; i++) { peers[i] = new PeerBuilder(Number160.CreateHash(i)) .SetP2PId(55) .SetPorts(2424 + i) .Start(); } var tasks = new List<Task<Message>>(50); for (int i = 0; i < peers.Length; i++) { var cc = await peers[0].ConnectionBean.Reservation.CreateAsync(1, 0); var taskResponse = peers[0].PingRpc.PingUdpAsync(peers[i].PeerAddress, cc, new DefaultConnectionConfiguration()); Core.Utils.Utils.AddReleaseListener(cc, taskResponse); tasks.Add(taskResponse); } foreach (var task in tasks) { await task; Assert.IsTrue(!task.IsFaulted); } } finally { for (int i = 0; i < peers.Length; i++) { peers[i].ShutdownAsync().Wait(); } } }
public async void TestPingUdpPool() { Peer sender = null; Peer recv1 = null; ChannelCreator cc = null; try { sender = new PeerBuilder(new Number160("0x9876")) .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask()) .SetP2PId(55) .SetPorts(2424) .Start(); recv1 = new PeerBuilder(new Number160("0x1234")) .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask()) .SetP2PId(55) .SetPorts(8088) .Start(); var tasks = new List<Task<Message>>(50); cc = await recv1.ConnectionBean.Reservation.CreateAsync(tasks.Capacity, 0); for (int i = 0; i < tasks.Capacity; i++) { var taskResponse = sender.PingRpc.PingUdpAsync(recv1.PeerAddress, cc, new DefaultConnectionConfiguration()); tasks.Add(taskResponse); } foreach (var task in tasks) { await task; Assert.IsTrue(!task.IsFaulted); } } finally { if (cc != null) { cc.ShutdownAsync().Wait(); } if (sender != null) { sender.ShutdownAsync().Wait(); } if (recv1 != null) { recv1.ShutdownAsync().Wait(); } } }
public async void TestPingTimeoutUdp() { Peer sender = null; Peer recv1 = null; ChannelCreator cc = null; try { sender = new PeerBuilder(new Number160("0x9876")) .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask()) .SetP2PId(55) .SetPorts(2424) .Start(); recv1 = new PeerBuilder(new Number160("0x1234")) .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask()) .SetP2PId(55) .SetPorts(8088) .Start(); var handshake1 = new PingRpc(sender.PeerBean, sender.ConnectionBean, false, true, true); var handshake2 = new PingRpc(recv1.PeerBean, recv1.ConnectionBean, false, true, true); cc = await recv1.ConnectionBean.Reservation.CreateAsync(1, 0); var tr = handshake1.PingUdpAsync(recv1.PeerAddress, cc, new DefaultConnectionConfiguration()); try { await tr; Assert.Fail("Timeout should have let task fail."); } catch (Exception) { Assert.IsTrue(tr.IsFaulted); } } finally { if (cc != null) { cc.ShutdownAsync().Wait(); } if (sender != null) { sender.ShutdownAsync().Wait(); } if (recv1 != null) { recv1.ShutdownAsync().Wait(); } } }
public async void TestPingHandlerError() { Peer sender = null; Peer recv1 = null; ChannelCreator cc = null; try { sender = new PeerBuilder(new Number160("0x9876")) .SetP2PId(55) .SetPorts(2424) .Start(); recv1 = new PeerBuilder(new Number160("0x1234")) .SetP2PId(55) .SetPorts(8088) .Start(); var handshake1 = new PingRpc(sender.PeerBean, sender.ConnectionBean, false, true, false); var handshake2 = new PingRpc(recv1.PeerBean, recv1.ConnectionBean, false, true, false); cc = await recv1.ConnectionBean.Reservation.CreateAsync(0, 1); var tr = handshake1.PingTcpAsync(recv1.PeerAddress, cc, new DefaultConnectionConfiguration()); await tr; Assert.IsTrue(tr.IsFaulted); } finally { if (cc != null) { cc.ShutdownAsync().Wait(); } if (sender != null) { sender.ShutdownAsync().Wait(); } if (recv1 != null) { recv1.ShutdownAsync().Wait(); } } }
public async void TestPingTcpDeadlock() { Peer sender1 = null; Peer recv1 = null; ChannelCreator cc = null; try { sender1 = new PeerBuilder(new Number160("0x9876")). SetP2PId(55). SetPorts(2424). Start(); recv1 = new PeerBuilder(new Number160("0x1234")). SetP2PId(55). SetPorts(8088). Start(); cc = await recv1.ConnectionBean.Reservation.CreateAsync(0, 1); var tr = sender1.PingRpc.PingTcpAsync(recv1.PeerAddress, cc, new DefaultConnectionConfiguration()); await tr; await tr.ContinueWith(async t => { var tr2 = sender1.PingRpc.PingTcpAsync(recv1.PeerAddress, cc, new DefaultConnectionConfiguration()); try { await tr2; } catch (Exception ex) { Assert.Fail(ex.ToString()); } }); Assert.IsTrue(!tr.IsFaulted); } finally { if (cc != null) { cc.ShutdownAsync().Wait(); } if (sender1 != null) { sender1.ShutdownAsync().Wait(); } if (recv1 != null) { recv1.ShutdownAsync().Wait(); } } }
public async void TestPingUdp() { Peer sender = null; Peer recv1 = null; ChannelCreator cc = null; try { sender = new PeerBuilder(new Number160("0x9876")). SetP2PId(55). SetPorts(2424). Start(); recv1 = new PeerBuilder(new Number160("0x1234")). SetP2PId(55). SetPorts(8088). Start(); var handshake = new PingRpc(sender.PeerBean, sender.ConnectionBean); cc = await recv1.ConnectionBean.Reservation.CreateAsync(1, 0); var t = handshake.PingUdpAsync(recv1.PeerAddress, cc, new DefaultConnectionConfiguration()); await t; Assert.IsTrue(t.IsCompleted && !t.IsFaulted); } catch (Exception ex) { throw ex; } finally { if (cc != null) { cc.ShutdownAsync().Wait(); } if (sender != null) { sender.ShutdownAsync().Wait(); } if (recv1 != null) { recv1.ShutdownAsync().Wait(); } } }
public async void TestDirectReconnect() { // TODO this test must be adapted to newest TomP2P implementation, where ports/configs are treated differently Peer sender = null; Peer recv1 = null; try { var ccohTcp = new CountConnectionOutboundHandler(); var ccohUdp = new CountConnectionOutboundHandler(); var filter = new TestPipelineFilter(ccohTcp, ccohUdp); var csc1 = PeerBuilder.CreateDefaultChannelServerConfiguration().SetPorts(new Ports(2424, 2424)); var csc2 = PeerBuilder.CreateDefaultChannelServerConfiguration().SetPorts(new Ports(8088, 8088)); var ccc = PeerBuilder.CreateDefaultChannelClientConfiguration(); csc1.SetPipelineFilter(filter); csc2.SetPipelineFilter(filter); ccc.SetPipelineFilter(filter); sender = new PeerBuilder(new Number160("0x50")) .SetP2PId(55) .SetEnableMaintenance(false) .SetChannelClientConfiguration(ccc) .SetChannelServerConfiguration(csc1) .Start(); recv1 = new PeerBuilder(new Number160("0x20")) .SetP2PId(55) .SetEnableMaintenance(false) .SetChannelClientConfiguration(ccc) .SetChannelServerConfiguration(csc2) .Start(); recv1.RawDataReply(new TestRawDataReply()); var peerConnection = sender.CreatePeerConnection(recv1.PeerAddress); ccohTcp.Reset(); ccohUdp.Reset(); var taskDirect1 = sender.SendDirect(peerConnection).SetBuffer(CreateTestBuffer()) .SetConnectionTimeoutTcpMillis(2000).SetIdleTcpSeconds(10*1000).Start().Task; await taskDirect1; // TODO await listeners? Assert.IsTrue(!taskDirect1.IsFaulted); Assert.AreEqual(2, ccohTcp.Total); //.NET: there are 2 csc's Assert.AreEqual(0, ccohUdp.Total); // TODO thread sleep? // send second with the same connection var taskDirect2 = sender.SendDirect(peerConnection).SetBuffer(CreateTestBuffer()) .Start().Task; await taskDirect2; Assert.IsTrue(!taskDirect2.IsFaulted); Assert.AreEqual(4, ccohTcp.Total); //.NET: 2 csc's, 2 server sessions Assert.AreEqual(0, ccohUdp.Total); await peerConnection.CloseAsync(); } finally { if (sender != null) { sender.ShutdownAsync().Wait(); } if (recv1 != null) { recv1.ShutdownAsync().Wait(); } } }
public async void TestNeighbor2() { Peer sender = null; Peer recv1 = null; try { sender = new PeerBuilder(new Number160("0x50")) .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask()) .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(2424, 2424)) .SetP2PId(55) .SetPorts(2424) .Start(); recv1 = new PeerBuilder(new Number160("0x20")) .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask()) .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(8088, 8088)) .SetP2PId(55) .SetPorts(2424) .Start(); var neighbors1 = new NeighborRpc(sender.PeerBean, sender.ConnectionBean); var neighbors2 = new NeighborRpc(recv1.PeerBean, recv1.ConnectionBean); var cc = await recv1.ConnectionBean.Reservation.CreateAsync(1, 0); var sv = new SearchValues(new Number160("0x30"), null); var infConfig = Utils2.CreateInfiniteConfiguration(); var tr = neighbors2.CloseNeighborsAsync(sender.PeerAddress, sv, Message.MessageType.Request2, cc, infConfig); await tr; Assert.IsTrue(!tr.IsFaulted); var addresses = tr.Result.NeighborsSet(0); // I see noone, not even myself. My peer was added in the overflow map. Assert.AreEqual(0, addresses.Size); await cc.ShutdownAsync(); } finally { if (sender != null) { sender.ShutdownAsync().Wait(); } if (recv1 != null) { recv1.ShutdownAsync().Wait(); } } }
/// <summary> /// Creates peers for testing. The first peer will be used as the master. /// This means that shutting down the master will shut down all other peers as well. /// </summary> /// <param name="nrOfPeers"></param> /// <param name="rnd"></param> /// <param name="port"></param> /// <param name="automaticTask"></param> /// <param name="maintenance"></param> /// <returns></returns> public static Peer[] CreateNodes(int nrOfPeers, Random rnd, int port, IAutomaticTask automaticTask, bool maintenance) { var bindings = new Bindings(); var peers = new Peer[nrOfPeers]; if (automaticTask != null) { var peerId = new Number160(rnd); var peerMap = new PeerMap(new PeerMapConfiguration(peerId)); peers[0] = new PeerBuilder(peerId) .SetPorts(port) .SetEnableMaintenance(maintenance) .SetExternalBindings(bindings) .SetPeerMap(peerMap) .Start() .AddAutomaticTask(automaticTask); } else { var peerId = new Number160(rnd); var peerMap = new PeerMap(new PeerMapConfiguration(peerId)); peers[0] = new PeerBuilder(peerId) .SetPorts(port) .SetEnableMaintenance(maintenance) .SetExternalBindings(bindings) .SetPeerMap(peerMap) .Start(); } Console.WriteLine("Created master peer: {0}.", peers[0].PeerId); for (int i = 1; i < nrOfPeers; i++) { if (automaticTask != null) { var peerId = new Number160(rnd); var peerMap = new PeerMap(new PeerMapConfiguration(peerId)); peers[i] = new PeerBuilder(peerId) .SetMasterPeer(peers[0]) .SetEnableMaintenance(maintenance) .SetExternalBindings(bindings) .SetPeerMap(peerMap) .Start() .AddAutomaticTask(automaticTask); } else { var peerId = new Number160(rnd); var peerMap = new PeerMap(new PeerMapConfiguration(peerId).SetPeerNoVerification()); peers[i] = new PeerBuilder(peerId) .SetMasterPeer(peers[0]) .SetEnableMaintenance(maintenance) .SetExternalBindings(bindings) .SetPeerMap(peerMap) .Start(); } Console.WriteLine("Created slave peer {0}: {1}.", i, peers[i].PeerId); } return peers; }
public async void TestNeighborFail() { Peer sender = null; Peer recv1 = null; try { sender = new PeerBuilder(new Number160("0x50")) .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask()) .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(2424, 2424)) .SetP2PId(55) .SetPorts(2424) .Start(); recv1 = new PeerBuilder(new Number160("0x20")) .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask()) .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(8088, 8088)) .SetP2PId(55) .SetPorts(2424) .Start(); var neighbors1 = new NeighborRpc(sender.PeerBean, sender.ConnectionBean); var neighbors2 = new NeighborRpc(recv1.PeerBean, recv1.ConnectionBean); var cc = await recv1.ConnectionBean.Reservation.CreateAsync(1, 0); try { var sv = new SearchValues(new Number160("0x30"), null); var infConfig = Utils2.CreateInfiniteConfiguration(); await neighbors2.CloseNeighborsAsync(sender.PeerAddress, sv, Message.MessageType.Exception, cc, infConfig); Assert.Fail("ArgumentException should have been thrown."); } catch (ArgumentException) { cc.ShutdownAsync().Wait(); } } finally { if (sender != null) { sender.ShutdownAsync().Wait(); } if (recv1 != null) { recv1.ShutdownAsync().Wait(); } } }
public void TestPingTcpFromJava() { // setup .NET server and provide it's PeerAddress Peer receiver = null; try { receiver = new PeerBuilder(new Number160("0x1234")). SetP2PId(55). SetPorts(7777). Start(); var bytes = receiver.PeerAddress.ToByteArray().ToByteArray(); var res = JarRunner.WriteBytesAndTestInterop(bytes); Assert.IsTrue(res); // debug server-side and analyse logs for testing } finally { if (receiver != null) { receiver.ShutdownAsync().Wait(); } } }
public async void TestNeighborTcp() { Peer sender = null; Peer recv1 = null; try { sender = new PeerBuilder(new Number160("0x50")) .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask()) .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(2424, 2424)) .SetP2PId(55) .SetPorts(2424) .Start(); // add neighbors to the sender's peer map var addresses = Utils2.CreateDummyAddresses(300, PortTcp, PortUdp); for (int i = 0; i < addresses.Length; i++) { sender.PeerBean.PeerMap.PeerFound(addresses[i], null, null); } // register neighbor RPC handlers var neighbors1 = new NeighborRpc(sender.PeerBean, sender.ConnectionBean); // TODO needed? registering? recv1 = new PeerBuilder(new Number160("0x20")) .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask()) .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(8088, 8088)) .SetP2PId(55) .SetPorts(8088) .Start(); var neighbors2 = new NeighborRpc(recv1.PeerBean, recv1.ConnectionBean); // ask sender for his neighbors var cc = await recv1.ConnectionBean.Reservation.CreateAsync(0, 1); var sv = new SearchValues(new Number160("0x1"), null); var infConfig = Utils2.CreateInfiniteConfiguration() .SetIsForceTcp(); var tr = neighbors2.CloseNeighborsAsync(sender.PeerAddress, sv, Message.MessageType.Request2, cc, infConfig); await tr; Assert.IsTrue(!tr.IsFaulted); // check if receiver got the neighbors var neighborSet = tr.Result.NeighborsSet(0); // 33 peer addresses can be stored in 1000 bytes (NeighborsLimit) Assert.IsTrue(neighborSet.Size == 33); var neighbors = neighborSet.Neighbors.ToList(); Assert.AreEqual(new Number160("0x1"), neighbors[0].PeerId); Assert.AreEqual(PortTcp, neighbors[1].TcpPort); Assert.AreEqual(PortUdp, neighbors[2].UdpPort); await cc.ShutdownAsync(); } finally { if (sender != null) { sender.ShutdownAsync().Wait(); } if (recv1 != null) { recv1.ShutdownAsync().Wait(); } } }