Example #1
0
        public Task Upload(Tag tag, ChatMessage chatMessage, DigitalSignature digitalSignature, TimeSpan miningTime, CancellationToken token)
        {
            if (tag == null)
            {
                throw new ArgumentNullException(nameof(tag));
            }
            if (chatMessage == null)
            {
                throw new ArgumentNullException(nameof(chatMessage));
            }
            if (digitalSignature == null)
            {
                throw new ArgumentNullException(nameof(digitalSignature));
            }

            return(_coreManager.VolatileSetStream(ContentConverter.ToStream(chatMessage), TimeSpan.FromDays(360), token)
                   .ContinueWith(task =>
            {
                MulticastMetadata multicastMetadata;

                try
                {
                    var miner = new Miner(CashAlgorithm.Version1, -1, miningTime);
                    multicastMetadata = new MulticastMetadata("ChatMessage", tag, DateTime.UtcNow, task.Result, digitalSignature, miner, token);
                }
                catch (MinerException)
                {
                    return;
                }

                _coreManager.UploadMetadata(multicastMetadata);
            }));
        }
Example #2
0
        public void UploadMetadata(MulticastMetadata metadata)
        {
            this.Check();

            lock (_lockObject)
            {
                _networkManager.Upload(metadata);
            }
        }
Example #3
0
            public bool SetMetadata(MulticastMetadata multicastMetadata)
            {
                using (_lockManager.WriteLock())
                {
                    var now = DateTime.UtcNow;

                    if (multicastMetadata == null ||
                        multicastMetadata.Type == null ||
                        multicastMetadata.Tag == null ||
                        multicastMetadata.Tag.Id == null || multicastMetadata.Tag.Id.Length == 0 ||
                        string.IsNullOrWhiteSpace(multicastMetadata.Tag.Name) ||
                        (multicastMetadata.CreationTime - now).TotalMinutes > 30 ||
                        multicastMetadata.Certificate == null)
                    {
                        return(false);
                    }

                    if (!_multicastMetadatas.TryGetValue(multicastMetadata.Type, out var dic))
                    {
                        dic = new Dictionary <Tag, Dictionary <Signature, HashSet <MulticastMetadata> > >();
                        _multicastMetadatas[multicastMetadata.Type] = dic;
                    }

                    if (!dic.TryGetValue(multicastMetadata.Tag, out var dic2))
                    {
                        dic2 = new Dictionary <Signature, HashSet <MulticastMetadata> >();
                        dic[multicastMetadata.Tag] = dic2;
                    }

                    var signature = multicastMetadata.Certificate.GetSignature();

                    if (!dic2.TryGetValue(signature, out var hashset))
                    {
                        hashset         = new HashSet <MulticastMetadata>();
                        dic2[signature] = hashset;
                    }

                    if (!hashset.Contains(multicastMetadata))
                    {
                        if (!multicastMetadata.VerifyCertificate())
                        {
                            return(false);
                        }

                        hashset.Add(multicastMetadata);
                    }

                    return(true);
                }
            }
        protected override void ProtectedImport(Stream stream, BufferManager bufferManager, int depth)
        {
            using (var reader = new ItemStreamReader(stream, bufferManager))
            {
                while (reader.Available > 0)
                {
                    int id = (int)reader.GetUInt32();

                    if (id == (int)SerializeId.MulticastMetadatas)
                    {
                        for (int i = (int)reader.GetUInt32() - 1; i >= 0; i--)
                        {
                            this.ProtectedMulticastMetadatas.Add(MulticastMetadata.Import(reader.GetStream(), bufferManager));
                        }
                    }
                }
            }
        }