public async ValueTask <SignatureCollection <TRollingHashAlgorithm, TStrongHashAlgorithm> > ReadSignatures <TRollingHashAlgorithm, TStrongHashAlgorithm> (SignatureOptions options, CancellationToken ct) where TRollingHashAlgorithm : struct, IRollingHashAlgorithm where TStrongHashAlgorithm : IStrongHashAlgorithm { var signatures = new SignatureCollection <TRollingHashAlgorithm, TStrongHashAlgorithm>(); const int maxSignatures = 1 << 22; for (int i = 0; i < maxSignatures; i++) { var sig = await _reader.Read <BlockSignature <TRollingHashAlgorithm, TStrongHashAlgorithm>, SignatureOptions> (options, ct).ConfigureAwait(false); if (!sig.HasValue) { _reader.Complete(); return(signatures); } long start = options.BlockLength * i; #if !NETSTANDARD2_0 signatures.TryAdd(sig.Value, (ulong)start); #else if (!signatures.ContainsKey(sig.Value)) { signatures.Add(sig.Value, (ulong)start); } #endif } throw new FormatException($"too many signatures"); }
/// <summary> /// Creates a base signature collection for use by the signature generator. /// </summary> /// <returns>Collection of signatures</returns> private SignatureCollection InitializeAndPrepareSignatures(string BaseName) { SignatureCollection signatures = new SignatureCollection(); // Initalize our signature collection. For this // implemenatation we will create a unique file in the // working directory for each recursion level. for (int i = 0; i < recursionDepth; i++) { signatures.Add(new SignatureInfo(BaseName, i + 1, this.workingDir, true)); signatures[i].Index = i; } return(signatures); }
public void Test_ConnectionManager() { for (int i = 0; i < 4; i++) { TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)); listener.Start(); var listenerAcceptSocket = listener.BeginAcceptSocket(null, null); TcpClient client = new TcpClient(); client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)); var server = listener.EndAcceptSocket(listenerAcceptSocket); listener.Stop(); var tcpClient = new BaseConnection(new SocketCap(client.Client), null, Test_Library_Net_Amoeba.MaxReceiveCount, _bufferManager); var tcpServer = new BaseConnection(new SocketCap(server), null, Test_Library_Net_Amoeba.MaxReceiveCount, _bufferManager); List<ConnectionManager> connectionManagers = new List<ConnectionManager>(); { ConnectionManager serverConnectionManager; ConnectionManager clientConnectionManager; Node serverNode = null; Node clientNode = null; byte[] serverSessionId = null; byte[] clientSessionId = null; { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; serverNode = new Node(id, uris); } { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; clientNode = new Node(id, uris); } { serverSessionId = new byte[32]; _random.NextBytes(serverSessionId); } { clientSessionId = new byte[32]; _random.NextBytes(clientSessionId); } serverConnectionManager = new ConnectionManager(tcpServer, serverSessionId, serverNode, ConnectDirection.In, _bufferManager); clientConnectionManager = new ConnectionManager(tcpClient, clientSessionId, clientNode, ConnectDirection.Out, _bufferManager); Thread serverThread = new Thread(new ThreadStart(() => { serverConnectionManager.Connect(); })); Thread clientThread = new Thread(new ThreadStart(() => { clientConnectionManager.Connect(); })); serverThread.Start(); clientThread.Start(); serverThread.Join(); clientThread.Join(); Assert.IsTrue(CollectionUtilities.Equals(serverConnectionManager.SesstionId, clientSessionId), "ConnectionManager SessionId #1"); Assert.IsTrue(CollectionUtilities.Equals(clientConnectionManager.SesstionId, serverSessionId), "ConnectionManager SessionId #2"); Assert.AreEqual(serverConnectionManager.Node, clientNode, "ConnectionManager Node #1"); Assert.AreEqual(clientConnectionManager.Node, serverNode, "ConnectionManager Node #2"); connectionManagers.Add(serverConnectionManager); connectionManagers.Add(clientConnectionManager); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullNodesEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullNodesEvent += (object sender, PullNodesEventArgs e) => { queue.Enqueue(e); }; List<Node> nodes = new List<Node>(); for (int j = 0; j < 32; j++) { Node node = null; { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; node = new Node(id, uris); } nodes.Add(node); } senderConnection.PushNodes(nodes); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(nodes, item.Nodes), "ConnectionManager #1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBlocksLinkEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlocksLinkEvent += (object sender, PullBlocksLinkEventArgs e) => { queue.Enqueue(e); }; var keys = new List<Key>(); for (int j = 0; j < 32; j++) { Key key = null; { var id = new byte[32]; _random.NextBytes(id); key = new Key(id, HashAlgorithm.Sha256); } keys.Add(key); } senderConnection.PushBlocksLink(keys); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(keys, item.Keys), "ConnectionManager #2"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBlocksRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlocksRequestEvent += (object sender, PullBlocksRequestEventArgs e) => { queue.Enqueue(e); }; var keys = new List<Key>(); for (int j = 0; j < 32; j++) { Key key = null; { var id = new byte[32]; _random.NextBytes(id); key = new Key(id, HashAlgorithm.Sha256); } keys.Add(key); } senderConnection.PushBlocksRequest(keys); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(keys, item.Keys), "ConnectionManager #3"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBlockEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlockEvent += (object sender, PullBlockEventArgs e) => { queue.Enqueue(e); }; var buffer = _bufferManager.TakeBuffer(1024 * 1024 * 8); var key = new Key(Sha256.ComputeHash(buffer), HashAlgorithm.Sha256); senderConnection.PushBlock(key, new ArraySegment<byte>(buffer, 0, 1024 * 1024 * 4)); var item = queue.Dequeue(); Assert.AreEqual(key, item.Key, "ConnectionManager #4.1"); Assert.IsTrue(CollectionUtilities.Equals(buffer, 0, item.Value.Array, item.Value.Offset, 1024 * 1024 * 4), "ConnectionManager #4.2"); _bufferManager.ReturnBuffer(buffer); _bufferManager.ReturnBuffer(item.Value.Array); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullSeedsRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullSeedsRequestEvent += (object sender, PullSeedsRequestEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var signatures = new SignatureCollection(); for (int j = 0; j < 32; j++) { signatures.Add(digitalSignature.ToString()); } senderConnection.PushSeedsRequest(signatures); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(signatures, item.Signatures), "ConnectionManager #5"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullSeedsEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullSeedsEvent += (object sender, PullSeedsEventArgs e) => { queue.Enqueue(e); }; List<Seed> seeds = new List<Seed>(); for (int j = 0; j < 32; j++) { var seed = new Seed(); seed.Name = "aaaa.zip"; seed.Keywords.AddRange(new KeywordCollection { "bbbb", "cccc", "dddd", }); seed.CreationTime = DateTime.Now; seed.Length = 10000; seed.Comment = "eeee"; seed.Rank = 1; seed.Key = new Key(new byte[32], HashAlgorithm.Sha256); seed.CompressionAlgorithm = CompressionAlgorithm.Xz; seed.CryptoAlgorithm = CryptoAlgorithm.Aes256; seed.CryptoKey = new byte[32 + 32]; seeds.Add(seed); } senderConnection.PushSeeds(seeds); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(seeds, item.Seeds), "ConnectionManager #6"); } foreach (var connectionManager in connectionManagers) { connectionManager.Dispose(); } client.Close(); server.Close(); } }
public void Test_ConnectionManager() { for (int i = 0; i < 4; i++) { TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)); listener.Start(); var listenerAcceptSocket = listener.BeginAcceptSocket(null, null); TcpClient client = new TcpClient(); client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)); var server = listener.EndAcceptSocket(listenerAcceptSocket); listener.Stop(); var tcpClient = new BaseConnection(new SocketCap(client.Client), null, Test_Library_Net_Amoeba.MaxReceiveCount, _bufferManager); var tcpServer = new BaseConnection(new SocketCap(server), null, Test_Library_Net_Amoeba.MaxReceiveCount, _bufferManager); List<ConnectionManager> connectionManagers = new List<ConnectionManager>(); { ConnectionManager serverConnectionManager; ConnectionManager clientConnectionManager; Node serverNode = null; Node clientNode = null; byte[] serverSessionId = null; byte[] clientSessionId = null; { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "tcp:localhost:9000", "tcp:localhost:9001", "tcp:localhost:9002" }; serverNode = new Node(id, uris); } { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "tcp:localhost:9000", "tcp:localhost:9001", "tcp:localhost:9002" }; clientNode = new Node(id, uris); } { serverSessionId = new byte[32]; _random.NextBytes(serverSessionId); } { clientSessionId = new byte[32]; _random.NextBytes(clientSessionId); } serverConnectionManager = new ConnectionManager(tcpServer, serverSessionId, serverNode, ConnectDirection.In, _bufferManager); clientConnectionManager = new ConnectionManager(tcpClient, clientSessionId, clientNode, ConnectDirection.Out, _bufferManager); var serverTask = Task.Run(() => serverConnectionManager.Connect()); var clientTask = Task.Run(() => clientConnectionManager.Connect()); Task.WaitAll(serverTask, clientTask); Assert.IsTrue(CollectionUtils.Equals(serverConnectionManager.SesstionId, clientSessionId), "ConnectionManager SessionId #1"); Assert.IsTrue(CollectionUtils.Equals(clientConnectionManager.SesstionId, serverSessionId), "ConnectionManager SessionId #2"); Assert.AreEqual(serverConnectionManager.Node, clientNode, "ConnectionManager Node #1"); Assert.AreEqual(clientConnectionManager.Node, serverNode, "ConnectionManager Node #2"); connectionManagers.Add(serverConnectionManager); connectionManagers.Add(clientConnectionManager); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullNodesEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullNodesEvent += (object sender, PullNodesEventArgs e) => { queue.Enqueue(e); }; List<Node> nodes = new List<Node>(); for (int j = 0; j < 32; j++) { Node node = null; { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; node = new Node(id, uris); } nodes.Add(node); } senderConnection.PushNodes(nodes); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtils.Equals(nodes, item.Nodes), "ConnectionManager #1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBlocksLinkEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlocksLinkEvent += (object sender, PullBlocksLinkEventArgs e) => { queue.Enqueue(e); }; var keys = new List<Key>(); for (int j = 0; j < 32; j++) { Key key = null; { var id = new byte[32]; _random.NextBytes(id); key = new Key(HashAlgorithm.Sha256, id); } keys.Add(key); } senderConnection.PushBlocksLink(keys); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtils.Equals(keys, item.Keys), "ConnectionManager #2"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBlocksRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlocksRequestEvent += (object sender, PullBlocksRequestEventArgs e) => { queue.Enqueue(e); }; var keys = new List<Key>(); for (int j = 0; j < 32; j++) { Key key = null; { var id = new byte[32]; _random.NextBytes(id); key = new Key(HashAlgorithm.Sha256, id); } keys.Add(key); } senderConnection.PushBlocksRequest(keys); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtils.Equals(keys, item.Keys), "ConnectionManager #3"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBlockEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlockEvent += (object sender, PullBlockEventArgs e) => { queue.Enqueue(e); }; var buffer = _bufferManager.TakeBuffer(1024 * 1024 * 8); var key = new Key(HashAlgorithm.Sha256, Sha256.ComputeHash(buffer)); senderConnection.PushBlock(key, new ArraySegment<byte>(buffer, 0, 1024 * 1024 * 4)); var item = queue.Dequeue(); Assert.AreEqual(key, item.Key, "ConnectionManager #4.1"); Assert.IsTrue(CollectionUtils.Equals(buffer, 0, item.Value.Array, item.Value.Offset, 1024 * 1024 * 4), "ConnectionManager #4.2"); _bufferManager.ReturnBuffer(buffer); _bufferManager.ReturnBuffer(item.Value.Array); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBroadcastMetadatasRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBroadcastMetadatasRequestEvent += (object sender, PullBroadcastMetadatasRequestEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.Rsa2048_Sha256); var signatures = new SignatureCollection(); for (int j = 0; j < 32; j++) { signatures.Add(digitalSignature.ToString()); } senderConnection.PushBroadcastMetadatasRequest(signatures); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtils.Equals(signatures, item.Signatures), "ConnectionManager #5.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBroadcastMetadatasEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBroadcastMetadatasEvent += (object sender, PullBroadcastMetadatasEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.Rsa2048_Sha256); var metadatas1 = new List<BroadcastMetadata>(); for (int j = 0; j < 4; j++) { var key = new Key(HashAlgorithm.Sha256, new byte[32]); var metadata = new Metadata(1, key, CompressionAlgorithm.Xz, CryptoAlgorithm.Aes256, new byte[32 + 32]); var broadcastMetadata = new BroadcastMetadata("Type", DateTime.UtcNow, metadata, digitalSignature); metadatas1.Add(broadcastMetadata); } senderConnection.PushBroadcastMetadatas(metadatas1); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtils.Equals(metadatas1, item.BroadcastMetadatas), "ConnectionManager #6.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullUnicastMetadatasRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullUnicastMetadatasRequestEvent += (object sender, PullUnicastMetadatasRequestEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.Rsa2048_Sha256); var signatures = new SignatureCollection(); for (int j = 0; j < 32; j++) { signatures.Add(digitalSignature.ToString()); } senderConnection.PushUnicastMetadatasRequest(signatures); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtils.Equals(signatures, item.Signatures), "ConnectionManager #7.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullUnicastMetadatasEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullUnicastMetadatasEvent += (object sender, PullUnicastMetadatasEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.Rsa2048_Sha256); var metadatas1 = new List<UnicastMetadata>(); for (int j = 0; j < 4; j++) { var key = new Key(HashAlgorithm.Sha256, new byte[32]); var metadata = new Metadata(1, key, CompressionAlgorithm.Xz, CryptoAlgorithm.Aes256, new byte[32 + 32]); var unicastMetadata = new UnicastMetadata("Type", digitalSignature.ToString(), DateTime.UtcNow, metadata, digitalSignature); metadatas1.Add(unicastMetadata); } senderConnection.PushUnicastMetadatas(metadatas1); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtils.Equals(metadatas1, item.UnicastMetadatas), "ConnectionManager #8.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullMulticastMetadatasRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullMulticastMetadatasRequestEvent += (object sender, PullMulticastMetadatasRequestEventArgs e) => { queue.Enqueue(e); }; var tags = new TagCollection(); for (int j = 0; j < 32; j++) { var id = new byte[32]; _random.NextBytes(id); tags.Add(new Tag(RandomString.GetValue(256), id)); } senderConnection.PushMulticastMetadatasRequest(tags); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtils.Equals(tags, item.Tags), "ConnectionManager #9.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullMulticastMetadatasEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullMulticastMetadatasEvent += (object sender, PullMulticastMetadatasEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.Rsa2048_Sha256); var metadatas1 = new List<MulticastMetadata>(); for (int j = 0; j < 4; j++) { var key = new Key(HashAlgorithm.Sha256, new byte[32]); var metadata = new Metadata(1, key, CompressionAlgorithm.Xz, CryptoAlgorithm.Aes256, new byte[32 + 32]); var tag = new Tag("oooo", new byte[32]); var miner = new Miner(CashAlgorithm.Version1, -1, TimeSpan.Zero); var multicastMetadata = new MulticastMetadata("Type", tag, DateTime.UtcNow, metadata, miner, digitalSignature); metadatas1.Add(multicastMetadata); } senderConnection.PushMulticastMetadatas(metadatas1); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtils.Equals(metadatas1, item.MulticastMetadatas), "ConnectionManager #10.1"); } foreach (var connectionManager in connectionManagers) { connectionManager.Dispose(); } client.Close(); server.Close(); } }
public void Test_ConnectionManager() { for (int i = 0; i < 4; i++) { TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)); listener.Start(); var listenerAcceptSocket = listener.BeginAcceptSocket(null, null); TcpClient client = new TcpClient(); client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)); var server = listener.EndAcceptSocket(listenerAcceptSocket); listener.Stop(); var tcpClient = new BaseConnection(new SocketCap(client.Client), null, Test_Library_Net_Outopos.MaxReceiveCount, _bufferManager); var tcpServer = new BaseConnection(new SocketCap(server), null, Test_Library_Net_Outopos.MaxReceiveCount, _bufferManager); List<ConnectionManager> connectionManagers = new List<ConnectionManager>(); { ConnectionManager serverConnectionManager; ConnectionManager clientConnectionManager; Node serverNode = null; Node clientNode = null; byte[] serverSessionId = null; byte[] clientSessionId = null; { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; serverNode = new Node(id, uris); } { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; clientNode = new Node(id, uris); } { serverSessionId = new byte[32]; _random.NextBytes(serverSessionId); } { clientSessionId = new byte[32]; _random.NextBytes(clientSessionId); } serverConnectionManager = new ConnectionManager(tcpServer, serverSessionId, serverNode, ConnectDirection.In, _bufferManager); clientConnectionManager = new ConnectionManager(tcpClient, clientSessionId, clientNode, ConnectDirection.Out, _bufferManager); Thread serverThread = new Thread(new ThreadStart(() => { serverConnectionManager.Connect(); })); Thread clientThread = new Thread(new ThreadStart(() => { clientConnectionManager.Connect(); })); serverThread.Start(); clientThread.Start(); serverThread.Join(); clientThread.Join(); Assert.IsTrue(CollectionUtilities.Equals(serverConnectionManager.SesstionId, clientSessionId), "ConnectionManager SessionId #1"); Assert.IsTrue(CollectionUtilities.Equals(clientConnectionManager.SesstionId, serverSessionId), "ConnectionManager SessionId #2"); Assert.AreEqual(serverConnectionManager.Node, clientNode, "ConnectionManager Node #1"); Assert.AreEqual(clientConnectionManager.Node, serverNode, "ConnectionManager Node #2"); connectionManagers.Add(serverConnectionManager); connectionManagers.Add(clientConnectionManager); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullNodesEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullNodesEvent += (object sender, PullNodesEventArgs e) => { queue.Enqueue(e); }; List<Node> nodes = new List<Node>(); for (int j = 0; j < 32; j++) { Node node = null; { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; node = new Node(id, uris); } nodes.Add(node); } senderConnection.PushNodes(nodes); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(nodes, item.Nodes), "ConnectionManager #1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBlocksLinkEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlocksLinkEvent += (object sender, PullBlocksLinkEventArgs e) => { queue.Enqueue(e); }; var keys = new List<Key>(); for (int j = 0; j < 32; j++) { Key key = null; { var id = new byte[32]; _random.NextBytes(id); key = new Key(id, HashAlgorithm.Sha256); } keys.Add(key); } senderConnection.PushBlocksLink(keys); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(keys, item.Keys), "ConnectionManager #2"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBlocksRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlocksRequestEvent += (object sender, PullBlocksRequestEventArgs e) => { queue.Enqueue(e); }; var keys = new List<Key>(); for (int j = 0; j < 32; j++) { Key key = null; { var id = new byte[32]; _random.NextBytes(id); key = new Key(id, HashAlgorithm.Sha256); } keys.Add(key); } senderConnection.PushBlocksRequest(keys); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(keys, item.Keys), "ConnectionManager #3"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBlockEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlockEvent += (object sender, PullBlockEventArgs e) => { queue.Enqueue(e); }; var buffer = _bufferManager.TakeBuffer(1024 * 1024 * 8); var key = new Key(Sha256.ComputeHash(buffer), HashAlgorithm.Sha256); senderConnection.PushBlock(key, new ArraySegment<byte>(buffer, 0, 1024 * 1024 * 4)); var item = queue.Dequeue(); Assert.AreEqual(key, item.Key, "ConnectionManager #4.1"); Assert.IsTrue(CollectionUtilities.Equals(buffer, 0, item.Value.Array, item.Value.Offset, 1024 * 1024 * 4), "ConnectionManager #4.2"); _bufferManager.ReturnBuffer(buffer); _bufferManager.ReturnBuffer(item.Value.Array); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBroadcastMetadatasRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBroadcastMetadatasRequestEvent += (object sender, PullBroadcastMetadatasRequestEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var signatures = new SignatureCollection(); for (int j = 0; j < 32; j++) { signatures.Add(digitalSignature.ToString()); } senderConnection.PushBroadcastMetadatasRequest(signatures); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(signatures, item.Signatures), "ConnectionManager #5.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBroadcastMetadatasEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBroadcastMetadatasEvent += (object sender, PullBroadcastMetadatasEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var metadatas1 = new List<ProfileMetadata>(); for (int j = 0; j < 4; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); var metadata = new ProfileMetadata(DateTime.UtcNow, key, digitalSignature); metadatas1.Add(metadata); } senderConnection.PushBroadcastMetadatas(metadatas1); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(metadatas1, item.ProfileMetadatas), "ConnectionManager #6.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullUnicastMetadatasRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullUnicastMetadatasRequestEvent += (object sender, PullUnicastMetadatasRequestEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var signatures = new SignatureCollection(); for (int j = 0; j < 32; j++) { signatures.Add(digitalSignature.ToString()); } senderConnection.PushUnicastMetadatasRequest(signatures); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(signatures, item.Signatures), "ConnectionManager #7.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullUnicastMetadatasEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullUnicastMetadatasEvent += (object sender, PullUnicastMetadatasEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var metadatas1 = new List<SignatureMessageMetadata>(); for (int j = 0; j < 4; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); var miner = new Miner(CashAlgorithm.Version1, -1, TimeSpan.Zero); var metadata = new SignatureMessageMetadata(digitalSignature.ToString(), DateTime.UtcNow, key, miner, digitalSignature); metadatas1.Add(metadata); } senderConnection.PushUnicastMetadatas(metadatas1); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(metadatas1, item.SignatureMessageMetadatas), "ConnectionManager #8.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullMulticastMetadatasRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullMulticastMetadatasRequestEvent += (object sender, PullMulticastMetadatasRequestEventArgs e) => { queue.Enqueue(e); }; var wikis = new WikiCollection(); var chats = new ChatCollection(); for (int j = 0; j < 32; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); wikis.Add(new Wiki(RandomString.GetValue(256), id)); } for (int j = 0; j < 32; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); chats.Add(new Chat(RandomString.GetValue(256), id)); } senderConnection.PushMulticastMetadatasRequest(wikis, chats); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(wikis, item.Wikis), "ConnectionManager #9.1"); Assert.IsTrue(CollectionUtilities.Equals(chats, item.Chats), "ConnectionManager #9.2"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullMulticastMetadatasEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullMulticastMetadatasEvent += (object sender, PullMulticastMetadatasEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var metadatas1 = new List<WikiDocumentMetadata>(); var metadatas2 = new List<ChatTopicMetadata>(); var metadatas3 = new List<ChatMessageMetadata>(); for (int j = 0; j < 4; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); var tag = new Wiki("oooo", new byte[32]); var miner = new Miner(CashAlgorithm.Version1, -1, TimeSpan.Zero); var metadata = new WikiDocumentMetadata(tag, DateTime.UtcNow, key, miner, digitalSignature); metadatas1.Add(metadata); } for (int j = 0; j < 4; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); var tag = new Chat("oooo", new byte[32]); var miner = new Miner(CashAlgorithm.Version1, -1, TimeSpan.Zero); var metadata = new ChatTopicMetadata(tag, DateTime.UtcNow, key, miner, digitalSignature); metadatas2.Add(metadata); } for (int j = 0; j < 4; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); var tag = new Chat("oooo", new byte[32]); var miner = new Miner(CashAlgorithm.Version1, -1, TimeSpan.Zero); var metadata = new ChatMessageMetadata(tag, DateTime.UtcNow, key, miner, digitalSignature); metadatas3.Add(metadata); } senderConnection.PushMulticastMetadatas(metadatas1, metadatas2, metadatas3); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(metadatas1, item.WikiDocumentMetadatas), "ConnectionManager #10.1"); Assert.IsTrue(CollectionUtilities.Equals(metadatas2, item.ChatTopicMetadatas), "ConnectionManager #10.2"); Assert.IsTrue(CollectionUtilities.Equals(metadatas3, item.ChatMessageMetadatas), "ConnectionManager #10.3"); } foreach (var connectionManager in connectionManagers) { connectionManager.Dispose(); } client.Close(); server.Close(); } }
public void Synchronize(string remoteFile, string seedFile, string outputFile) { this.remoteFile = remoteFile; this.seedFile = seedFile; this.outputFile = outputFile; // Initialize our managed RDC wrapper RdcServices rdcServices = new RdcServices(); SignatureCollection seedSignatures = null; // Get the RDC version of the server so we // can make sure this client is supported. Client.RdcProxy.RdcService rdcWebService = new Client.RdcProxy.RdcService(); Client.RdcProxy.RdcVersion rdcVersion = rdcWebService.GetRdcVersion(); //rdcServices.CheckVersion(rdcVersion); // Open the local seed file stream using (FileStream stream = File.OpenRead(seedFile)) { rdcServices.WorkingDirectory = workingDir; rdcServices.RecursionDepth = recursionDepth; // Generate the signature files seedSignatures = rdcServices.GenerateSignatures(stream, Path.GetFileName(seedFile)); if (seedSignatures.Count < 1) { throw new RdcException("Failed to generate the signatures."); } } // Now make the call the the Rdc web service // and retrieve the copy of the signature // manifest. Client.RdcProxy.SignatureManifest signatureManifest = rdcWebService.GetSignatureManifest(remoteFile, recursionDepth); SignatureCollection sourceSignatures = new SignatureCollection(); /* * Realistically, the soure file length should be checked * against a predetermined minimum limit. So if the source * file length is less than 1MB, just download the source * file instead of generating signaures and needs. */ //if signatureManifest.FileLength < MINIMUM_SIZE) // DownloadSourceFile(...); ulong TargetDataWritten = 0; ulong TotalSourceData = 0; ulong TotalSeedData = 0; ulong TotalSigData = 0; // Now that we have the signature manifiest, let's go ahead // transfer local copies of the signature files to our // working signature directory. int Depth = 0; foreach (Client.RdcProxy.SignatureInfo sig in signatureManifest.Signatures) { Console.WriteLine(string.Format("\n----------\nProcessing: {0}\n", sig.Name + ".sig")); if (sig.Length > 0) { GC.WaitForPendingFinalizers(); // Create the signature stream Microsoft.RDC.SignatureInfo sigInfo = new Microsoft.RDC.SignatureInfo(sig.Name, -1, workingDir, true); sourceSignatures.Add(sigInfo); // hang on to them to keep them alive and for clean up if (Depth == 0) // always transfer the complete first remote signature { Console.WriteLine(string.Format("Transfering: {0}\n", sig.Name + ".sig")); for (int i = 0; i < sig.Length; i += blockSize) { int readBytes = Math.Min((int)(sig.Length - i), blockSize); byte[] data = rdcWebService.TransferDataBlock(Path.Combine(sig.Path, sig.Name) + ".sig", i, readBytes); sigInfo.InnerStream.Write(data, 0, data.Length); TotalSigData += (ulong)data.Length; } } // select source and target stream FileStream SourceStream; FileStream TargetStream; string RemoteSourcePath; // if there are other signatures after this one, they become the source and target if (Depth < seedSignatures.Count - 1) { SourceStream = seedSignatures[Depth + 1].InnerStream; TargetStream = File.Create(Path.Combine(workingDir, signatureManifest.Signatures[Depth + 1].Name) + ".sig", blockSize); RemoteSourcePath = Path.Combine(signatureManifest.Signatures[Depth + 1].Path, signatureManifest.Signatures[Depth + 1].Name) + ".sig"; Console.WriteLine(string.Format("Creating: {0}\n----------\n\n", signatureManifest.Signatures[Depth + 1].Name + ".sig")); } else // create the final target file { SourceStream = File.OpenRead(seedFile); TargetStream = File.Create(outputFile, blockSize); RemoteSourcePath = remoteFile; Console.WriteLine(string.Format("Creating: {0}\n----------\n\n", Path.GetFileName(outputFile))); } // reset signature streams for reading seedSignatures[Depth].InnerStream.Position = 0; sigInfo.InnerStream.Position = 0; // Compare the signatures and get // the needs array that we will use to create the // target output file. ArrayList needsList = rdcServices.CreateNeedsList(seedSignatures[Depth], sigInfo); foreach (RdcNeed need in needsList) { switch (need.blockType) { case RdcNeedType.Source: // Copy this block from the remote server. TotalSourceData += need.blockLength; byte[] data = rdcWebService.TransferDataBlock( RemoteSourcePath, (int)need.fileOffset, (int)need.blockLength); TargetStream.Write(data, 0, (int)need.blockLength); break; case RdcNeedType.Seed: TotalSeedData += need.blockLength; byte[] seedData = new Byte[need.blockLength]; SourceStream.Seek((int)need.fileOffset, SeekOrigin.Begin); SourceStream.Read(seedData, 0, (int)need.blockLength); TargetStream.Write(seedData, 0, (int)need.blockLength); break; default: break; } Console.WriteLine(string.Format("NEED: length:{0,12}\toffset:{1,12}\tsource:{2,12}\tblock type:{3,12}", need.blockLength, need.fileOffset, TargetDataWritten, need.blockType.ToString())); TargetDataWritten += need.blockLength; } // Close our IO file streams. if (Depth == seedSignatures.Count - 1) { SourceStream.Close(); // only non-signature sources } TargetStream.Close(); } Depth++; } Console.WriteLine(string.Format("\nFrom source:{0,12:N0}\tFrom seed:{1,12:N0}\tTotal:{2,12:N0}", TotalSourceData, TotalSeedData, TotalSourceData + TotalSeedData)); Console.WriteLine(string.Format("\nTransfer: {0:N0} bytes from source, file size: {1:N0}, RDC Savings: {2:0.00}%\n", TotalSourceData + TotalSigData, TotalSourceData + TotalSeedData, (1.0 - (double)(TotalSourceData + TotalSigData) / (double)(TotalSourceData + TotalSeedData)) * 100.0)); // release all signature resources rdcServices.PurgeSignatureStore(seedSignatures); rdcServices.PurgeSignatureStore(sourceSignatures); rdcWebService.Finialize(signatureManifest); rdcServices.Dispose(); }
public void Test_ConnectionManager() { for (int i = 0; i < 4; i++) { TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)); listener.Start(); var listenerAcceptSocket = listener.BeginAcceptSocket(null, null); TcpClient client = new TcpClient(); client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)); var server = listener.EndAcceptSocket(listenerAcceptSocket); listener.Stop(); var tcpClient = new BaseConnection(new SocketCap(client.Client), null, Test_Library_Net_Outopos.MaxReceiveCount, _bufferManager); var tcpServer = new BaseConnection(new SocketCap(server), null, Test_Library_Net_Outopos.MaxReceiveCount, _bufferManager); List <ConnectionManager> connectionManagers = new List <ConnectionManager>(); { ConnectionManager serverConnectionManager; ConnectionManager clientConnectionManager; Node serverNode = null; Node clientNode = null; byte[] serverSessionId = null; byte[] clientSessionId = null; { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; serverNode = new Node(id, uris); } { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; clientNode = new Node(id, uris); } { serverSessionId = new byte[32]; _random.NextBytes(serverSessionId); } { clientSessionId = new byte[32]; _random.NextBytes(clientSessionId); } serverConnectionManager = new ConnectionManager(tcpServer, serverSessionId, serverNode, ConnectDirection.In, _bufferManager); clientConnectionManager = new ConnectionManager(tcpClient, clientSessionId, clientNode, ConnectDirection.Out, _bufferManager); Thread serverThread = new Thread(new ThreadStart(() => { serverConnectionManager.Connect(); })); Thread clientThread = new Thread(new ThreadStart(() => { clientConnectionManager.Connect(); })); serverThread.Start(); clientThread.Start(); serverThread.Join(); clientThread.Join(); Assert.IsTrue(CollectionUtilities.Equals(serverConnectionManager.SesstionId, clientSessionId), "ConnectionManager SessionId #1"); Assert.IsTrue(CollectionUtilities.Equals(clientConnectionManager.SesstionId, serverSessionId), "ConnectionManager SessionId #2"); Assert.AreEqual(serverConnectionManager.Node, clientNode, "ConnectionManager Node #1"); Assert.AreEqual(clientConnectionManager.Node, serverNode, "ConnectionManager Node #2"); connectionManagers.Add(serverConnectionManager); connectionManagers.Add(clientConnectionManager); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullNodesEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullNodesEvent += (object sender, PullNodesEventArgs e) => { queue.Enqueue(e); }; List <Node> nodes = new List <Node>(); for (int j = 0; j < 32; j++) { Node node = null; { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; node = new Node(id, uris); } nodes.Add(node); } senderConnection.PushNodes(nodes); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(nodes, item.Nodes), "ConnectionManager #1"); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullBlocksLinkEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlocksLinkEvent += (object sender, PullBlocksLinkEventArgs e) => { queue.Enqueue(e); }; var keys = new List <Key>(); for (int j = 0; j < 32; j++) { Key key = null; { var id = new byte[32]; _random.NextBytes(id); key = new Key(id, HashAlgorithm.Sha256); } keys.Add(key); } senderConnection.PushBlocksLink(keys); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(keys, item.Keys), "ConnectionManager #2"); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullBlocksRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlocksRequestEvent += (object sender, PullBlocksRequestEventArgs e) => { queue.Enqueue(e); }; var keys = new List <Key>(); for (int j = 0; j < 32; j++) { Key key = null; { var id = new byte[32]; _random.NextBytes(id); key = new Key(id, HashAlgorithm.Sha256); } keys.Add(key); } senderConnection.PushBlocksRequest(keys); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(keys, item.Keys), "ConnectionManager #3"); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullBlockEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlockEvent += (object sender, PullBlockEventArgs e) => { queue.Enqueue(e); }; var buffer = _bufferManager.TakeBuffer(1024 * 1024 * 8); var key = new Key(Sha256.ComputeHash(buffer), HashAlgorithm.Sha256); senderConnection.PushBlock(key, new ArraySegment <byte>(buffer, 0, 1024 * 1024 * 4)); var item = queue.Dequeue(); Assert.AreEqual(key, item.Key, "ConnectionManager #4.1"); Assert.IsTrue(CollectionUtilities.Equals(buffer, 0, item.Value.Array, item.Value.Offset, 1024 * 1024 * 4), "ConnectionManager #4.2"); _bufferManager.ReturnBuffer(buffer); _bufferManager.ReturnBuffer(item.Value.Array); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullBroadcastMetadatasRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBroadcastMetadatasRequestEvent += (object sender, PullBroadcastMetadatasRequestEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var signatures = new SignatureCollection(); for (int j = 0; j < 32; j++) { signatures.Add(digitalSignature.ToString()); } senderConnection.PushBroadcastMetadatasRequest(signatures); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(signatures, item.Signatures), "ConnectionManager #5.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullBroadcastMetadatasEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBroadcastMetadatasEvent += (object sender, PullBroadcastMetadatasEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var metadatas1 = new List <ProfileMetadata>(); for (int j = 0; j < 4; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); var metadata = new ProfileMetadata(DateTime.UtcNow, key, digitalSignature); metadatas1.Add(metadata); } senderConnection.PushBroadcastMetadatas(metadatas1); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(metadatas1, item.ProfileMetadatas), "ConnectionManager #6.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullUnicastMetadatasRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullUnicastMetadatasRequestEvent += (object sender, PullUnicastMetadatasRequestEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var signatures = new SignatureCollection(); for (int j = 0; j < 32; j++) { signatures.Add(digitalSignature.ToString()); } senderConnection.PushUnicastMetadatasRequest(signatures); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(signatures, item.Signatures), "ConnectionManager #7.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullUnicastMetadatasEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullUnicastMetadatasEvent += (object sender, PullUnicastMetadatasEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var metadatas1 = new List <SignatureMessageMetadata>(); for (int j = 0; j < 4; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); var miner = new Miner(CashAlgorithm.Version1, -1, TimeSpan.Zero); var metadata = new SignatureMessageMetadata(digitalSignature.ToString(), DateTime.UtcNow, key, miner, digitalSignature); metadatas1.Add(metadata); } senderConnection.PushUnicastMetadatas(metadatas1); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(metadatas1, item.SignatureMessageMetadatas), "ConnectionManager #8.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullMulticastMetadatasRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullMulticastMetadatasRequestEvent += (object sender, PullMulticastMetadatasRequestEventArgs e) => { queue.Enqueue(e); }; var wikis = new WikiCollection(); var chats = new ChatCollection(); for (int j = 0; j < 32; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); wikis.Add(new Wiki(RandomString.GetValue(256), id)); } for (int j = 0; j < 32; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); chats.Add(new Chat(RandomString.GetValue(256), id)); } senderConnection.PushMulticastMetadatasRequest(wikis, chats); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(wikis, item.Wikis), "ConnectionManager #9.1"); Assert.IsTrue(CollectionUtilities.Equals(chats, item.Chats), "ConnectionManager #9.2"); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullMulticastMetadatasEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullMulticastMetadatasEvent += (object sender, PullMulticastMetadatasEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var metadatas1 = new List <WikiDocumentMetadata>(); var metadatas2 = new List <ChatTopicMetadata>(); var metadatas3 = new List <ChatMessageMetadata>(); for (int j = 0; j < 4; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); var tag = new Wiki("oooo", new byte[32]); var miner = new Miner(CashAlgorithm.Version1, -1, TimeSpan.Zero); var metadata = new WikiDocumentMetadata(tag, DateTime.UtcNow, key, miner, digitalSignature); metadatas1.Add(metadata); } for (int j = 0; j < 4; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); var tag = new Chat("oooo", new byte[32]); var miner = new Miner(CashAlgorithm.Version1, -1, TimeSpan.Zero); var metadata = new ChatTopicMetadata(tag, DateTime.UtcNow, key, miner, digitalSignature); metadatas2.Add(metadata); } for (int j = 0; j < 4; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); var tag = new Chat("oooo", new byte[32]); var miner = new Miner(CashAlgorithm.Version1, -1, TimeSpan.Zero); var metadata = new ChatMessageMetadata(tag, DateTime.UtcNow, key, miner, digitalSignature); metadatas3.Add(metadata); } senderConnection.PushMulticastMetadatas(metadatas1, metadatas2, metadatas3); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(metadatas1, item.WikiDocumentMetadatas), "ConnectionManager #10.1"); Assert.IsTrue(CollectionUtilities.Equals(metadatas2, item.ChatTopicMetadatas), "ConnectionManager #10.2"); Assert.IsTrue(CollectionUtilities.Equals(metadatas3, item.ChatMessageMetadatas), "ConnectionManager #10.3"); } foreach (var connectionManager in connectionManagers) { connectionManager.Dispose(); } client.Close(); server.Close(); } }
public void Test_ConnectionManager() { for (int i = 0; i < 4; i++) { TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)); listener.Start(); var listenerAcceptSocket = listener.BeginAcceptSocket(null, null); TcpClient client = new TcpClient(); client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)); var server = listener.EndAcceptSocket(listenerAcceptSocket); listener.Stop(); var tcpClient = new BaseConnection(new SocketCap(client.Client), null, Test_Library_Net_Amoeba.MaxReceiveCount, _bufferManager); var tcpServer = new BaseConnection(new SocketCap(server), null, Test_Library_Net_Amoeba.MaxReceiveCount, _bufferManager); List <ConnectionManager> connectionManagers = new List <ConnectionManager>(); { ConnectionManager serverConnectionManager; ConnectionManager clientConnectionManager; Node serverNode = null; Node clientNode = null; byte[] serverSessionId = null; byte[] clientSessionId = null; { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; serverNode = new Node(id, uris); } { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; clientNode = new Node(id, uris); } { serverSessionId = new byte[32]; _random.NextBytes(serverSessionId); } { clientSessionId = new byte[32]; _random.NextBytes(clientSessionId); } serverConnectionManager = new ConnectionManager(tcpServer, serverSessionId, serverNode, ConnectDirection.In, _bufferManager); clientConnectionManager = new ConnectionManager(tcpClient, clientSessionId, clientNode, ConnectDirection.Out, _bufferManager); Thread serverThread = new Thread(new ThreadStart(() => { serverConnectionManager.Connect(); })); Thread clientThread = new Thread(new ThreadStart(() => { clientConnectionManager.Connect(); })); serverThread.Start(); clientThread.Start(); serverThread.Join(); clientThread.Join(); Assert.IsTrue(CollectionUtilities.Equals(serverConnectionManager.SesstionId, clientSessionId), "ConnectionManager SessionId #1"); Assert.IsTrue(CollectionUtilities.Equals(clientConnectionManager.SesstionId, serverSessionId), "ConnectionManager SessionId #2"); Assert.AreEqual(serverConnectionManager.Node, clientNode, "ConnectionManager Node #1"); Assert.AreEqual(clientConnectionManager.Node, serverNode, "ConnectionManager Node #2"); connectionManagers.Add(serverConnectionManager); connectionManagers.Add(clientConnectionManager); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullNodesEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullNodesEvent += (object sender, PullNodesEventArgs e) => { queue.Enqueue(e); }; List <Node> nodes = new List <Node>(); for (int j = 0; j < 32; j++) { Node node = null; { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; node = new Node(id, uris); } nodes.Add(node); } senderConnection.PushNodes(nodes); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(nodes, item.Nodes), "ConnectionManager #1"); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullBlocksLinkEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlocksLinkEvent += (object sender, PullBlocksLinkEventArgs e) => { queue.Enqueue(e); }; var keys = new List <Key>(); for (int j = 0; j < 32; j++) { Key key = null; { var id = new byte[32]; _random.NextBytes(id); key = new Key(id, HashAlgorithm.Sha256); } keys.Add(key); } senderConnection.PushBlocksLink(keys); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(keys, item.Keys), "ConnectionManager #2"); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullBlocksRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlocksRequestEvent += (object sender, PullBlocksRequestEventArgs e) => { queue.Enqueue(e); }; var keys = new List <Key>(); for (int j = 0; j < 32; j++) { Key key = null; { var id = new byte[32]; _random.NextBytes(id); key = new Key(id, HashAlgorithm.Sha256); } keys.Add(key); } senderConnection.PushBlocksRequest(keys); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(keys, item.Keys), "ConnectionManager #3"); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullBlockEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlockEvent += (object sender, PullBlockEventArgs e) => { queue.Enqueue(e); }; var buffer = _bufferManager.TakeBuffer(1024 * 1024 * 8); var key = new Key(Sha256.ComputeHash(buffer), HashAlgorithm.Sha256); senderConnection.PushBlock(key, new ArraySegment <byte>(buffer, 0, 1024 * 1024 * 4)); var item = queue.Dequeue(); Assert.AreEqual(key, item.Key, "ConnectionManager #4.1"); Assert.IsTrue(CollectionUtilities.Equals(buffer, 0, item.Value.Array, item.Value.Offset, 1024 * 1024 * 4), "ConnectionManager #4.2"); _bufferManager.ReturnBuffer(buffer); _bufferManager.ReturnBuffer(item.Value.Array); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullSeedsRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullSeedsRequestEvent += (object sender, PullSeedsRequestEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var signatures = new SignatureCollection(); for (int j = 0; j < 32; j++) { signatures.Add(digitalSignature.ToString()); } senderConnection.PushSeedsRequest(signatures); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(signatures, item.Signatures), "ConnectionManager #5"); } connectionManagers.Randomize(); { var queue = new WaitQueue <PullSeedsEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullSeedsEvent += (object sender, PullSeedsEventArgs e) => { queue.Enqueue(e); }; List <Seed> seeds = new List <Seed>(); for (int j = 0; j < 32; j++) { var seed = new Seed(); seed.Name = "aaaa.zip"; seed.Keywords.AddRange(new KeywordCollection { "bbbb", "cccc", "dddd", }); seed.CreationTime = DateTime.Now; seed.Length = 10000; seed.Comment = "eeee"; seed.Rank = 1; seed.Key = new Key(new byte[32], HashAlgorithm.Sha256); seed.CompressionAlgorithm = CompressionAlgorithm.Xz; seed.CryptoAlgorithm = CryptoAlgorithm.Aes256; seed.CryptoKey = new byte[32 + 32]; seeds.Add(seed); } senderConnection.PushSeeds(seeds); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(seeds, item.Seeds), "ConnectionManager #6"); } foreach (var connectionManager in connectionManagers) { connectionManager.Dispose(); } client.Close(); server.Close(); } }
/// <summary> /// Метод добавления подписей /// </summary> public bool AddSign() { if (CurrentTechnicalSolution == null) { ShowErrorMsg("Отсутствует тех.решение.", "Отсутствует текущий объект!"); return(false); } else { SignatureCollection signCollection = CurrentTechnicalSolution.Signatures; SignatureType Согл = signCollection.Types.FirstOrDefault(s => s.Name.StartsWith("Согл.")); try { signCollection.Add(Согл, ChiefDesignerDirection as UserReferenceObject); } catch { MessageBox.Show("Произошла ошибка при добавления подписи для главного конструктора по направлению"); return(false); } if (ProgramManager != null) { try { signCollection.Add(Согл, ProgramManager as UserReferenceObject); } catch { MessageBox.Show("Произошла ошибка при добавления подписи для Руководитель программы "); return(false); } } return(true); //SignatureType Утв = signCollection.Types.FirstOrDefault(s => s.Name.StartsWith("Утв.")); //SignatureType Разраб = signCollection.Types.FirstOrDefault(s => s.Name.StartsWith("Разраб.")); //if (Разраб == null) // MessageBox.Show("Тип подписи Разраб не найден"); // else //{ // signCollection.Add(Разраб, CurrentUser as UserReferenceObject); //} //if (Согл == null) // MessageBox.Show("Тип подписи согл не найден"); // else //{ // try // { // signCollection.Add(Согл, GetUserAtPost("Начальник опытно-конструкторского бюро").FirstOrDefault() as UserReferenceObject); // } //catch //{ // MessageBox.Show("Произошла ошибка при добавления подписи для Начальника опытно-конструкторского бюро "); // } //try //{ // signCollection.Add(Согл, GetUserAtPost("Начальник службы качества").FirstOrDefault() as UserReferenceObject); //} //catch // { // MessageBox.Show("Произошла ошибка при добавления подписи для Начальника службы качества "); // } // ReferenceObject LinkedProjectFromBaseProjects = CurrentTechnicalSolution.GetObject(TechnicalSolution_LinkBaseProject_Guid); // if (LinkedProjectFromBaseProjects == null) // System.Windows.Forms.MessageBox.Show("У технического решения не указан номер проекта!", //"Укажите номер проекта.", //System.Windows.Forms.MessageBoxButtons.OK, //System.Windows.Forms.MessageBoxIcon.Exclamation, //System.Windows.Forms.MessageBoxDefaultButton.Button1); // else // { // try // { // signCollection.Add(Согл, ExtensionDinamika.НайтиПользователяПоКодуФизЛица(LinkedProjectFromBaseProjects[Guids.BaseProject.CodUser1C_Guid].GetString()) as UserReferenceObject); // } // catch (Exception) // { // MessageBox.Show("Произошла ошибка при добавления подписи для Руководителя проекта "); // } // } // } // if (Утв == null) // MessageBox.Show("Тип подписи Утв не найден"); // else // { // try // { // signCollection.Add(Утв, GetUserAtPost("Исполнительный директор").FirstOrDefault() as UserReferenceObject); // } // catch // { // MessageBox.Show("Произошла ошибка при добавления подписи для Исполнительного директор "); // } //} } }