Exemple #1
0
        public ChatTopic UploadChatTopic(Chat tag,
                                         HypertextFormatType formatType,
                                         string hypertext,

                                         int miningLimit,
                                         TimeSpan miningTime,
                                         DigitalSignature digitalSignature)
        {
            lock (this.ThisLock)
            {
                var uploadItem = new UploadItem();
                uploadItem.Type             = "ChatTopic";
                uploadItem.ChatTopic        = new ChatTopic(tag, DateTime.UtcNow, formatType, hypertext, digitalSignature);
                uploadItem.MiningLimit      = miningLimit;
                uploadItem.MiningTime       = miningTime;
                uploadItem.DigitalSignature = digitalSignature;

                _settings.UploadItems.RemoveAll((target) =>
                {
                    return(target.Type == uploadItem.Type &&
                           target.ChatTopic.Tag == uploadItem.ChatTopic.Tag &&
                           target.DigitalSignature == digitalSignature);
                });

                _settings.UploadItems.Add(uploadItem);

                return(uploadItem.ChatTopic);
            }
        }
Exemple #2
0
        public WikiDocument UploadWikiDocument(Wiki tag,
                                               IEnumerable <WikiPage> wikiPages,

                                               int miningLimit,
                                               TimeSpan miningTime,
                                               DigitalSignature digitalSignature)
        {
            lock (this.ThisLock)
            {
                var uploadItem = new UploadItem();
                uploadItem.Type             = "WikiDocument";
                uploadItem.WikiDocument     = new WikiDocument(tag, DateTime.UtcNow, wikiPages, digitalSignature);
                uploadItem.MiningLimit      = miningLimit;
                uploadItem.MiningTime       = miningTime;
                uploadItem.DigitalSignature = digitalSignature;

                _settings.UploadItems.RemoveAll((target) =>
                {
                    return(target.Type == uploadItem.Type &&
                           target.WikiDocument.Tag == uploadItem.WikiDocument.Tag &&
                           target.DigitalSignature == digitalSignature);
                });

                _settings.UploadItems.Add(uploadItem);

                return(uploadItem.WikiDocument);
            }
        }
Exemple #3
0
        public Profile UploadProfile(
            int cost,
            ExchangePublicKey exchangePublicKey,
            IEnumerable <string> trustSignatures,
            IEnumerable <string> deleteSignatures,
            IEnumerable <Wiki> wikis,
            IEnumerable <Chat> chats,

            DigitalSignature digitalSignature)
        {
            lock (this.ThisLock)
            {
                var uploadItem = new UploadItem();
                uploadItem.Type             = "Profile";
                uploadItem.Profile          = new Profile(DateTime.UtcNow, cost, exchangePublicKey, trustSignatures, deleteSignatures, wikis, chats, digitalSignature);
                uploadItem.DigitalSignature = digitalSignature;

                _settings.UploadItems.RemoveAll((target) =>
                {
                    return(target.Type == uploadItem.Type &&
                           target.DigitalSignature == digitalSignature);
                });

                _settings.UploadItems.Add(uploadItem);

                return(uploadItem.Profile);
            }
        }
Exemple #4
0
        public ChatMessage UploadChatMessage(Chat tag,
                                             string comment,
                                             IEnumerable <Anchor> anchors,

                                             int miningLimit,
                                             TimeSpan miningTime,
                                             DigitalSignature digitalSignature)
        {
            lock (this.ThisLock)
            {
                var uploadItem = new UploadItem();
                uploadItem.Type             = "ChatMessage";
                uploadItem.ChatMessage      = new ChatMessage(tag, DateTime.UtcNow, comment, anchors, digitalSignature);
                uploadItem.MiningLimit      = miningLimit;
                uploadItem.MiningTime       = miningTime;
                uploadItem.DigitalSignature = digitalSignature;

                _settings.UploadItems.Add(uploadItem);

                return(uploadItem.ChatMessage);
            }
        }
Exemple #5
0
        public SignatureMessage UploadSignatureMessage(string signature,
                                                       string comment,

                                                       ExchangePublicKey exchangePublicKey,
                                                       int miningLimit,
                                                       TimeSpan miningTime,
                                                       DigitalSignature digitalSignature)
        {
            lock (this.ThisLock)
            {
                var uploadItem = new UploadItem();
                uploadItem.Type              = "SignatureMessage";
                uploadItem.SignatureMessage  = new SignatureMessage(signature, DateTime.UtcNow, comment, digitalSignature);
                uploadItem.ExchangePublicKey = exchangePublicKey;
                uploadItem.MiningLimit       = miningLimit;
                uploadItem.MiningTime        = miningTime;
                uploadItem.DigitalSignature  = digitalSignature;

                _settings.UploadItems.Add(uploadItem);

                return(uploadItem.SignatureMessage);
            }
        }
        public WikiDocument UploadWikiDocument(Wiki tag,
            IEnumerable<WikiPage> wikiPages,

            int miningLimit,
            TimeSpan miningTime,
            DigitalSignature digitalSignature)
        {
            lock (this.ThisLock)
            {
                var uploadItem = new UploadItem();
                uploadItem.Type = "WikiDocument";
                uploadItem.WikiDocument = new WikiDocument(tag, DateTime.UtcNow, wikiPages, digitalSignature);
                uploadItem.MiningLimit = miningLimit;
                uploadItem.MiningTime = miningTime;
                uploadItem.DigitalSignature = digitalSignature;

                _settings.UploadItems.RemoveAll((target) =>
                {
                    return target.Type == uploadItem.Type
                        && target.WikiDocument.Tag == uploadItem.WikiDocument.Tag
                        && target.DigitalSignature == digitalSignature;
                });

                _settings.UploadItems.Add(uploadItem);

                return uploadItem.WikiDocument;
            }
        }
        public SignatureMessage UploadSignatureMessage(string signature,
            string comment,

            ExchangePublicKey exchangePublicKey,
            int miningLimit,
            TimeSpan miningTime,
            DigitalSignature digitalSignature)
        {
            lock (this.ThisLock)
            {
                var uploadItem = new UploadItem();
                uploadItem.Type = "SignatureMessage";
                uploadItem.SignatureMessage = new SignatureMessage(signature, DateTime.UtcNow, comment, digitalSignature);
                uploadItem.ExchangePublicKey = exchangePublicKey;
                uploadItem.MiningLimit = miningLimit;
                uploadItem.MiningTime = miningTime;
                uploadItem.DigitalSignature = digitalSignature;

                _settings.UploadItems.Add(uploadItem);

                return uploadItem.SignatureMessage;
            }
        }
        public Profile UploadProfile(
            int cost,
            ExchangePublicKey exchangePublicKey,
            IEnumerable<string> trustSignatures,
            IEnumerable<string> deleteSignatures,
            IEnumerable<Wiki> wikis,
            IEnumerable<Chat> chats,

            DigitalSignature digitalSignature)
        {
            lock (this.ThisLock)
            {
                var uploadItem = new UploadItem();
                uploadItem.Type = "Profile";
                uploadItem.Profile = new Profile(DateTime.UtcNow, cost, exchangePublicKey, trustSignatures, deleteSignatures, wikis, chats, digitalSignature);
                uploadItem.DigitalSignature = digitalSignature;

                _settings.UploadItems.RemoveAll((target) =>
                {
                    return target.Type == uploadItem.Type
                        && target.DigitalSignature == digitalSignature;
                });

                _settings.UploadItems.Add(uploadItem);

                return uploadItem.Profile;
            }
        }
        public ChatMessage UploadChatMessage(Chat tag,
            string comment,

            int miningLimit,
            TimeSpan miningTime,
            DigitalSignature digitalSignature)
        {
            lock (this.ThisLock)
            {
                var uploadItem = new UploadItem();
                uploadItem.Type = "ChatMessage";
                uploadItem.ChatMessage = new ChatMessage(tag, DateTime.UtcNow, comment, digitalSignature);
                uploadItem.MiningLimit = miningLimit;
                uploadItem.MiningTime = miningTime;
                uploadItem.DigitalSignature = digitalSignature;

                _settings.UploadItems.Add(uploadItem);

                return uploadItem.ChatMessage;
            }
        }
Exemple #10
0
        private void UploadThread()
        {
            for (; ;)
            {
                Thread.Sleep(1000 * 1);
                if (this.State == ManagerState.Stop)
                {
                    return;
                }

                {
                    UploadItem item = null;

                    lock (this.ThisLock)
                    {
                        if (_settings.UploadItems.Count > 0)
                        {
                            item = _settings.UploadItems[0];
                        }
                    }

                    try
                    {
                        if (item != null)
                        {
                            ArraySegment <byte> buffer = new ArraySegment <byte>();

                            try
                            {
                                if (item.Type == "Profile")
                                {
                                    buffer = ContentConverter.ToProfileBlock(item.Profile);
                                }
                                else if (item.Type == "SignatureMessage")
                                {
                                    buffer = ContentConverter.ToSignatureMessageBlock(item.SignatureMessage, item.ExchangePublicKey);
                                }
                                else if (item.Type == "WikiDocument")
                                {
                                    buffer = ContentConverter.ToWikiDocumentBlock(item.WikiDocument);
                                }
                                else if (item.Type == "ChatTopic")
                                {
                                    buffer = ContentConverter.ToChatTopicBlock(item.ChatTopic);
                                }
                                else if (item.Type == "ChatMessage")
                                {
                                    buffer = ContentConverter.ToChatMessageBlock(item.ChatMessage);
                                }

                                Key key = null;

                                {
                                    if (_hashAlgorithm == HashAlgorithm.Sha256)
                                    {
                                        key = new Key(Sha256.ComputeHash(buffer), _hashAlgorithm);
                                    }

                                    this.Lock(key);
                                }

                                _cacheManager[key] = buffer;
                                _connectionsManager.Upload(key);

                                var miner = new Miner(CashAlgorithm.Version1, item.MiningLimit, item.MiningTime);

                                var task = Task.Factory.StartNew(() =>
                                {
                                    if (item.Type == "Profile")
                                    {
                                        var metadata = new ProfileMetadata(item.Profile.CreationTime, key, item.DigitalSignature);
                                        _connectionsManager.Upload(metadata);
                                    }
                                    else if (item.Type == "SignatureMessage")
                                    {
                                        var metadata = new SignatureMessageMetadata(item.SignatureMessage.Signature, item.SignatureMessage.CreationTime, key, miner, item.DigitalSignature);
                                        _connectionsManager.Upload(metadata);
                                    }
                                    else if (item.Type == "WikiDocument")
                                    {
                                        var metadata = new WikiDocumentMetadata(item.WikiDocument.Tag, item.WikiDocument.CreationTime, key, miner, item.DigitalSignature);
                                        _connectionsManager.Upload(metadata);
                                    }
                                    else if (item.Type == "ChatTopic")
                                    {
                                        var metadata = new ChatTopicMetadata(item.ChatTopic.Tag, item.ChatTopic.CreationTime, key, miner, item.DigitalSignature);
                                        _connectionsManager.Upload(metadata);
                                    }
                                    else if (item.Type == "ChatMessage")
                                    {
                                        var metadata = new ChatMessageMetadata(item.ChatMessage.Tag, item.ChatMessage.CreationTime, key, miner, item.DigitalSignature);
                                        _connectionsManager.Upload(metadata);
                                    }
                                });

                                while (!task.IsCompleted)
                                {
                                    if (this.State == ManagerState.Stop)
                                    {
                                        miner.Cancel();
                                    }

                                    lock (this.ThisLock)
                                    {
                                        if (!_settings.UploadItems.Contains(item))
                                        {
                                            miner.Cancel();
                                        }
                                    }

                                    Thread.Sleep(1000);
                                }

                                if (task.Exception != null)
                                {
                                    throw task.Exception;
                                }

                                lock (this.ThisLock)
                                {
                                    _settings.UploadItems.Remove(item);
                                }
                            }
                            finally
                            {
                                if (buffer.Array != null)
                                {
                                    _bufferManager.ReturnBuffer(buffer.Array);
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }