Exemple #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);
            }));
        }
Exemple #2
0
        public Task <BroadcastProfileMessage> GetProfile(Signature signature)
        {
            if (signature == null)
            {
                throw new ArgumentNullException(nameof(signature));
            }

            var broadcastMetadata = _coreManager.GetBroadcastMetadata(signature, "Profile");

            if (broadcastMetadata == null)
            {
                return(Task.FromResult <BroadcastProfileMessage>(null));
            }

            // Cache
            {
                if (_cache_Profiles.TryGetValue(broadcastMetadata, out var result))
                {
                    return(Task.FromResult(result));
                }
            }

            return(Task.Run(() =>
            {
                try
                {
                    var stream = _coreManager.VolatileGetStream(broadcastMetadata.Metadata, 1024 * 1024 * 32);
                    if (stream == null)
                    {
                        return null;
                    }

                    var content = ContentConverter.FromStream <ProfileContent>(stream, 0);
                    if (content == null)
                    {
                        return null;
                    }

                    var result = new BroadcastProfileMessage(
                        broadcastMetadata.Certificate.GetSignature(),
                        broadcastMetadata.CreationTime,
                        content);

                    _cache_Profiles[broadcastMetadata] = result;

                    return result;
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }

                return null;
            }));
        }
Exemple #3
0
        public Task <BroadcastProfileMessage> GetProfile(Signature signature, DateTime?creationTimeLowerLimit)
        {
            if (signature == null)
            {
                throw new ArgumentNullException(nameof(signature));
            }

            var broadcastMetadata = _coreManager.GetBroadcastMetadata(signature, "Profile");

            if (broadcastMetadata == null)
            {
                return(Task.FromResult <BroadcastProfileMessage>(null));
            }
            if (creationTimeLowerLimit != null && broadcastMetadata.CreationTime <= creationTimeLowerLimit)
            {
                return(Task.FromResult <BroadcastProfileMessage>(null));
            }

            return(Task.Run(() =>
            {
                try
                {
                    var stream = _coreManager.VolatileGetStream(broadcastMetadata.Metadata, 1024 * 1024 * 32);
                    if (stream == null)
                    {
                        return null;
                    }

                    var content = ContentConverter.FromStream <ProfileContent>(stream, 0);
                    if (content == null)
                    {
                        return null;
                    }

                    var result = new BroadcastProfileMessage(
                        broadcastMetadata.Certificate.GetSignature(),
                        broadcastMetadata.CreationTime,
                        content);

                    return result;
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }

                return null;
            }));
        }
Exemple #4
0
        public Task Upload(Store store, DigitalSignature digitalSignature, CancellationToken token)
        {
            if (store == null)
            {
                throw new ArgumentNullException(nameof(store));
            }
            if (digitalSignature == null)
            {
                throw new ArgumentNullException(nameof(digitalSignature));
            }

            return(_coreManager.VolatileSetStream(ContentConverter.ToStream(store), TimeSpan.FromDays(360), token)
                   .ContinueWith(task =>
            {
                _coreManager.UploadMetadata(new BroadcastMetadata("Store", DateTime.UtcNow, task.Result, digitalSignature));
            }));
        }
Exemple #5
0
        public Task <BroadcastMessage <Store> > GetStore(Signature signature)
        {
            if (signature == null)
            {
                throw new ArgumentNullException(nameof(signature));
            }

            var broadcastMetadata = _coreManager.GetBroadcastMetadata(signature, "Store");

            if (broadcastMetadata == null)
            {
                return(Task.FromResult <BroadcastMessage <Store> >(null));
            }

            // Cache
            {
                if (_cache_Stores.TryGetValue(broadcastMetadata, out var result))
                {
                    return(Task.FromResult(result));
                }
            }

            return(Task.Run(() =>
            {
                var stream = _coreManager.VolatileGetStream(broadcastMetadata.Metadata, 1024 * 1024 * 32);
                if (stream == null)
                {
                    return null;
                }

                var result = new BroadcastMessage <Store>(
                    broadcastMetadata.Certificate.GetSignature(),
                    broadcastMetadata.CreationTime,
                    ContentConverter.FromStream <Store>(stream));

                if (result.Value == null)
                {
                    return null;
                }

                _cache_Stores[broadcastMetadata] = result;

                return result;
            }));
        }
Exemple #6
0
        public Task Upload(Signature targetSignature, MailMessage mailMessage, ExchangePublicKey exchangePublicKey, DigitalSignature digitalSignature, CancellationToken token)
        {
            if (targetSignature == null)
            {
                throw new ArgumentNullException(nameof(targetSignature));
            }
            if (mailMessage == null)
            {
                throw new ArgumentNullException(nameof(mailMessage));
            }
            if (digitalSignature == null)
            {
                throw new ArgumentNullException(nameof(digitalSignature));
            }

            return(_coreManager.VolatileSetStream(ContentConverter.ToCryptoStream(mailMessage, 1024 * 256, exchangePublicKey), TimeSpan.FromDays(360), token)
                   .ContinueWith(task =>
            {
                _coreManager.UploadMetadata(new UnicastMetadata("MailMessage", targetSignature, DateTime.UtcNow, task.Result, digitalSignature));
            }));
        }
Exemple #7
0
        public Task <IEnumerable <MulticastMessage <ChatMessage> > > GetChatMessages(Tag tag)
        {
            if (tag == null)
            {
                throw new ArgumentNullException(nameof(tag));
            }

            var now = DateTime.UtcNow;

            return(Task.Run(() =>
            {
                var results = new List <MulticastMessage <ChatMessage> >();

                var trusts = new List <MulticastMetadata>();
                var untrusts = new List <MulticastMetadata>();

                foreach (var multicastMetadata in _coreManager.GetMulticastMetadatas(tag, "ChatMessage"))
                {
                    if (_searchSignatures.Contains(multicastMetadata.Certificate.GetSignature()))
                    {
                        trusts.Add(multicastMetadata);
                    }
                    else
                    {
                        if ((now - multicastMetadata.CreationTime).TotalDays > 7)
                        {
                            continue;
                        }

                        untrusts.Add(multicastMetadata);
                    }
                }

                trusts.Sort((x, y) => y.CreationTime.CompareTo(x.CreationTime));
                untrusts.Sort((x, y) =>
                {
                    int c;
                    if (0 != (c = y.Cost.CashAlgorithm.CompareTo(x.Cost.CashAlgorithm)))
                    {
                        return c;
                    }
                    if (0 != (c = y.Cost.Value.CompareTo(x.Cost.Value)))
                    {
                        return c;
                    }

                    return y.CreationTime.CompareTo(x.CreationTime);
                });

                foreach (var multicastMetadata in CollectionUtils.Unite(trusts.Take(1024), untrusts.Take(1024)))
                {
                    var dic = _cache_ChatMessages.GetOrAdd(multicastMetadata.Tag, (_) => new LockedHashDictionary <MulticastMetadata, MulticastMessage <ChatMessage> >());

                    // Cache
                    {
                        if (dic.TryGetValue(multicastMetadata, out var result))
                        {
                            results.Add(result);

                            continue;
                        }
                    }

                    {
                        var stream = _coreManager.VolatileGetStream(multicastMetadata.Metadata, 1024 * 1024 * 1);
                        if (stream == null)
                        {
                            continue;
                        }

                        var result = new MulticastMessage <ChatMessage>(
                            multicastMetadata.Tag,
                            multicastMetadata.Certificate.GetSignature(),
                            multicastMetadata.CreationTime,
                            multicastMetadata.Cost,
                            ContentConverter.FromStream <ChatMessage>(stream));

                        if (result.Value == null)
                        {
                            continue;
                        }

                        dic[multicastMetadata] = result;

                        results.Add(result);
                    }
                }

                return (IEnumerable <MulticastMessage <ChatMessage> >)results.ToArray();
            }));
        }
Exemple #8
0
        public Task <IEnumerable <UnicastMessage <MailMessage> > > GetMailMessages(Signature signature, ExchangePrivateKey exchangePrivateKey)
        {
            if (signature == null)
            {
                throw new ArgumentNullException(nameof(signature));
            }
            if (exchangePrivateKey == null)
            {
                throw new ArgumentNullException(nameof(exchangePrivateKey));
            }

            return(Task.Run(() =>
            {
                var results = new List <UnicastMessage <MailMessage> >();

                var trusts = new List <UnicastMetadata>();

                foreach (var unicastMetadata in _coreManager.GetUnicastMetadatas(signature, "MailMessage"))
                {
                    if (_searchSignatures.Contains(unicastMetadata.Certificate.GetSignature()))
                    {
                        trusts.Add(unicastMetadata);
                    }
                }

                trusts.Sort((x, y) => y.CreationTime.CompareTo(x.CreationTime));

                foreach (var unicastMetadata in trusts.Take(1024))
                {
                    var dic = _cache_MailMessages.GetOrAdd(unicastMetadata.Signature, (_) => new LockedHashDictionary <UnicastMetadata, UnicastMessage <MailMessage> >());

                    // Cache
                    {
                        if (dic.TryGetValue(unicastMetadata, out var result))
                        {
                            results.Add(result);

                            continue;
                        }
                    }

                    {
                        var stream = _coreManager.VolatileGetStream(unicastMetadata.Metadata, 1024 * 1024 * 1);
                        if (stream == null)
                        {
                            continue;
                        }

                        var result = new UnicastMessage <MailMessage>(
                            unicastMetadata.Signature,
                            unicastMetadata.Certificate.GetSignature(),
                            unicastMetadata.CreationTime,
                            ContentConverter.FromCryptoStream <MailMessage>(stream, exchangePrivateKey));

                        if (result.Value == null)
                        {
                            continue;
                        }

                        dic[unicastMetadata] = result;

                        results.Add(result);
                    }
                }

                return (IEnumerable <UnicastMessage <MailMessage> >)results.ToArray();
            }));
        }
Exemple #9
0
        public Task <IEnumerable <MulticastCommentMessage> > GetMulticastCommentMessages(Tag tag,
                                                                                         int trustMessageCountUpperLimit, int untrustMessageCountUpperLimit, IEnumerable <MessageCondition> conditions)
        {
            if (tag == null)
            {
                throw new ArgumentNullException(nameof(tag));
            }

            var now = DateTime.UtcNow;

            return(Task.Run(() =>
            {
                try
                {
                    var filter = new Dictionary <Signature, HashSet <DateTime> >();
                    {
                        foreach (var item in conditions)
                        {
                            filter.GetOrAdd(item.AuthorSignature, (_) => new HashSet <DateTime>()).Add(item.CreationTime);
                        }
                    }

                    var trustedMetadatas = new List <MulticastMetadata>();
                    var untrustedMetadatas = new List <MulticastMetadata>();
                    {
                        foreach (var multicastMetadata in _coreManager.GetMulticastMetadatas(tag, "ChatMessage"))
                        {
                            if (_searchSignatures.Contains(multicastMetadata.Certificate.GetSignature()))
                            {
                                trustedMetadatas.Add(multicastMetadata);
                            }
                            else
                            {
                                untrustedMetadatas.Add(multicastMetadata);
                            }
                        }

                        trustedMetadatas.Sort((x, y) => y.CreationTime.CompareTo(x.CreationTime));
                        untrustedMetadatas.Sort((x, y) =>
                        {
                            int c;
                            if (0 != (c = y.Cost.CashAlgorithm.CompareTo(x.Cost.CashAlgorithm)))
                            {
                                return c;
                            }
                            if (0 != (c = y.Cost.Value.CompareTo(x.Cost.Value)))
                            {
                                return c;
                            }

                            return y.CreationTime.CompareTo(x.CreationTime);
                        });
                    }

                    var results = new List <MulticastCommentMessage>();

                    foreach (var multicastMetadata in CollectionUtils.Unite(trustedMetadatas.Take(trustMessageCountUpperLimit), untrustedMetadatas.Take(untrustMessageCountUpperLimit)))
                    {
                        if (filter.TryGetValue(multicastMetadata.Certificate.GetSignature(), out var hashSet) && hashSet.Contains(multicastMetadata.CreationTime))
                        {
                            continue;
                        }

                        var stream = _coreManager.VolatileGetStream(multicastMetadata.Metadata, 1024 * 1024 * 1);
                        if (stream == null)
                        {
                            continue;
                        }

                        var content = ContentConverter.FromStream <CommentContent>(stream, 0);
                        if (content == null)
                        {
                            continue;
                        }

                        var result = new MulticastCommentMessage(
                            multicastMetadata.Tag,
                            multicastMetadata.Certificate.GetSignature(),
                            multicastMetadata.CreationTime,
                            multicastMetadata.Cost,
                            content);

                        results.Add(result);
                    }

                    return (IEnumerable <MulticastCommentMessage>)results.ToArray();
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }

                return Array.Empty <MulticastCommentMessage>();
            }));
        }
Exemple #10
0
        public Task <IEnumerable <UnicastCommentMessage> > GetUnicastCommentMessages(Signature signature, AgreementPrivateKey agreementPrivateKey,
                                                                                     int messageCountUpperLimit, IEnumerable <MessageCondition> conditions)
        {
            if (signature == null)
            {
                throw new ArgumentNullException(nameof(signature));
            }
            if (agreementPrivateKey == null)
            {
                throw new ArgumentNullException(nameof(agreementPrivateKey));
            }

            return(Task.Run(() =>
            {
                try
                {
                    var filter = new Dictionary <Signature, HashSet <DateTime> >();
                    {
                        foreach (var item in conditions)
                        {
                            filter.GetOrAdd(item.AuthorSignature, (_) => new HashSet <DateTime>()).Add(item.CreationTime);
                        }
                    }

                    var trustedMetadatas = new List <UnicastMetadata>();
                    {
                        foreach (var unicastMetadata in _coreManager.GetUnicastMetadatas(signature, "MailMessage"))
                        {
                            if (!_searchSignatures.Contains(unicastMetadata.Certificate.GetSignature()))
                            {
                                continue;
                            }

                            trustedMetadatas.Add(unicastMetadata);
                        }

                        trustedMetadatas.Sort((x, y) => y.CreationTime.CompareTo(x.CreationTime));
                    }

                    var results = new List <UnicastCommentMessage>();

                    foreach (var unicastMetadata in trustedMetadatas.Take(messageCountUpperLimit))
                    {
                        if (filter.TryGetValue(unicastMetadata.Certificate.GetSignature(), out var hashSet) && hashSet.Contains(unicastMetadata.CreationTime))
                        {
                            continue;
                        }

                        var stream = _coreManager.VolatileGetStream(unicastMetadata.Metadata, 1024 * 1024 * 1);
                        if (stream == null)
                        {
                            continue;
                        }

                        var result = new UnicastCommentMessage(
                            unicastMetadata.Signature,
                            unicastMetadata.Certificate.GetSignature(),
                            unicastMetadata.CreationTime,
                            ContentConverter.FromCryptoStream <CommentContent>(stream, agreementPrivateKey, 0));

                        if (result.Value == null)
                        {
                            continue;
                        }

                        results.Add(result);
                    }

                    return (IEnumerable <UnicastCommentMessage>)results.ToArray();
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }

                return Array.Empty <UnicastCommentMessage>();
            }));
        }