Example #1
0
        private void Remove(BackgroundUploadItem item)
        {
            lock (this.ThisLock)
            {
                foreach (var key in item.LockedKeys)
                {
                    _cacheManager.Unlock(key);
                }

                _settings.BackgroundUploadItems.Remove(item);
            }
        }
Example #2
0
        public void Upload(Store store, DigitalSignature digitalSignature)
        {
            if (store == null)
            {
                throw new ArgumentNullException("store");
            }
            if (digitalSignature == null)
            {
                throw new ArgumentNullException("digitalSignature");
            }

            lock (this.ThisLock)
            {
                {
                    foreach (var item in _settings.BackgroundUploadItems.ToArray())
                    {
                        if (item.DigitalSignature.ToString() != digitalSignature.ToString())
                        {
                            continue;
                        }

                        this.Remove(item);
                    }
                }

                {
                    BackgroundUploadItem item = new BackgroundUploadItem();

                    item.Value = store;
                    item.Type  = BackgroundItemType.Store;
                    item.State = BackgroundUploadState.Encoding;
                    item.Rank  = 1;
                    item.CompressionAlgorithm = CompressionAlgorithm.Xz;
                    item.CryptoAlgorithm      = CryptoAlgorithm.Aes256;
                    item.CorrectionAlgorithm  = CorrectionAlgorithm.ReedSolomon8;
                    item.HashAlgorithm        = HashAlgorithm.Sha256;
                    item.DigitalSignature     = digitalSignature;
                    item.Seed = new Seed();
                    item.Seed.Keywords.Add(ConnectionsManager.Keyword_Store);
                    item.Seed.CreationTime = DateTime.UtcNow;
                    item.BlockLength       = 1024 * 1024 * 1;

                    _settings.BackgroundUploadItems.Add(item);
                }
            }
        }
Example #3
0
        private void SetKeyCount(BackgroundUploadItem item)
        {
            lock (this.ThisLock)
            {
                foreach (var key in item.UploadKeys.ToArray())
                {
                    if (!_connectionsManager.IsUploadWaiting(key))
                    {
                        item.UploadedKeys.Add(key);
                        item.UploadKeys.Remove(key);
                    }
                }

                if (item.State == BackgroundUploadState.Uploading)
                {
                    if (item.UploadKeys.Count == 0)
                    {
                        item.State = BackgroundUploadState.Completed;
                    }
                }
            }
        }
        public void MulticastUpload(Tag tag,
            Message message,

            int miningLimit,
            TimeSpan miningTime,
            DigitalSignature digitalSignature)
        {
            if (tag == null) throw new ArgumentNullException(nameof(tag));
            if (message == null) throw new ArgumentNullException(nameof(message));
            if (digitalSignature == null) throw new ArgumentNullException(nameof(digitalSignature));

            lock (_thisLock)
            {
                var item = new BackgroundUploadItem();

                item.State = BackgroundUploadState.Encoding;
                item.Tag = tag;
                item.Message = message;
                item.Scheme = "Multicast";
                item.Type = "Message";
                item.CreationTime = DateTime.UtcNow;
                item.Depth = 1;
                item.BlockLength = 1024 * 1024 * 1;
                item.CorrectionAlgorithm = CorrectionAlgorithm.ReedSolomon8;
                item.HashAlgorithm = HashAlgorithm.Sha256;
                item.MiningLimit = miningLimit;
                item.MiningTime = miningTime;
                item.DigitalSignature = digitalSignature;

                _settings.UploadItems.Add(item);
            }
        }
        private void Remove(BackgroundUploadItem item)
        {
            lock (_thisLock)
            {
                foreach (var key in item.LockedKeys)
                {
                    _cacheManager.Unlock(key);
                }

                _settings.UploadItems.Remove(item);
            }
        }
        private void CheckState(BackgroundUploadItem item)
        {
            lock (_thisLock)
            {
                if (item.State == BackgroundUploadState.Uploading)
                {
                    foreach (var key in item.UploadKeys.ToArray())
                    {
                        if (!_connectionsManager.IsUploadWaiting(key))
                        {
                            item.UploadKeys.Remove(key);
                        }
                    }

                    if (item.UploadKeys.Count == 0)
                    {
                        item.State = BackgroundUploadState.Completed;
                    }
                }
            }
        }
        public void Upload(Store store, DigitalSignature digitalSignature)
        {
            if (store == null) throw new ArgumentNullException(nameof(store));
            if (digitalSignature == null) throw new ArgumentNullException(nameof(digitalSignature));

            lock (_thisLock)
            {
                var item = new BackgroundUploadItem();

                item.State = BackgroundUploadState.Encoding;
                item.Store = store;
                item.Scheme = "Broadcast";
                item.Type = "Store";
                item.CreationTime = DateTime.UtcNow;
                item.Depth = 1;
                item.BlockLength = 1024 * 1024 * 1;
                item.CorrectionAlgorithm = CorrectionAlgorithm.ReedSolomon8;
                item.HashAlgorithm = HashAlgorithm.Sha256;
                item.DigitalSignature = digitalSignature;

                foreach (var target in _settings.UploadItems.ToArray())
                {
                    if (target.Scheme == item.Scheme && target.Type == item.Type
                        && target.DigitalSignature == digitalSignature)
                    {
                        this.Remove(target);
                    }
                }

                _settings.UploadItems.Add(item);
            }
        }
        public void UnicastUpload(string signature,
            Message message,

            ExchangePublicKey exchangePublicKey,
            DigitalSignature digitalSignature)
        {
            if (signature == null) throw new ArgumentNullException(nameof(signature));
            if (message == null) throw new ArgumentNullException(nameof(message));
            if (exchangePublicKey == null) throw new ArgumentNullException(nameof(exchangePublicKey));
            if (digitalSignature == null) throw new ArgumentNullException(nameof(digitalSignature));

            lock (_thisLock)
            {
                var item = new BackgroundUploadItem();

                item.State = BackgroundUploadState.Encoding;
                item.Signature = signature;
                item.Message = message;
                item.Scheme = "Unicast";
                item.Type = "Message";
                item.CreationTime = DateTime.UtcNow;
                item.Depth = 1;
                item.BlockLength = 1024 * 1024 * 1;
                item.CorrectionAlgorithm = CorrectionAlgorithm.ReedSolomon8;
                item.HashAlgorithm = HashAlgorithm.Sha256;
                item.ExchangePublicKey = exchangePublicKey;
                item.DigitalSignature = digitalSignature;

                _settings.UploadItems.Add(item);
            }
        }
        public void Upload(Store store, DigitalSignature digitalSignature)
        {
            if (store == null) throw new ArgumentNullException("store");
            if (digitalSignature == null) throw new ArgumentNullException("digitalSignature");

            lock (this.ThisLock)
            {
                {
                    foreach (var item in _settings.BackgroundUploadItems.ToArray())
                    {
                        if (item.DigitalSignature.ToString() != digitalSignature.ToString()) continue;

                        this.Remove(item);
                    }
                }

                {
                    BackgroundUploadItem item = new BackgroundUploadItem();

                    item.Value = store;
                    item.Type = BackgroundItemType.Store;
                    item.State = BackgroundUploadState.Encoding;
                    item.Rank = 1;
                    item.CompressionAlgorithm = CompressionAlgorithm.Xz;
                    item.CryptoAlgorithm = CryptoAlgorithm.Aes256;
                    item.CorrectionAlgorithm = CorrectionAlgorithm.ReedSolomon8;
                    item.HashAlgorithm = HashAlgorithm.Sha256;
                    item.DigitalSignature = digitalSignature;
                    item.Seed = new Seed();
                    item.Seed.Keywords.Add(ConnectionsManager.Keyword_Store);
                    item.Seed.CreationTime = DateTime.UtcNow;
                    item.BlockLength = 1024 * 1024 * 1;

                    _settings.BackgroundUploadItems.Add(item);
                }
            }
        }
Example #10
0
        private void UploadManagerThread()
        {
            for (; ;)
            {
                Thread.Sleep(1000 * 1);
                if (this.State == ManagerState.Stop)
                {
                    return;
                }

                BackgroundUploadItem item = null;

                try
                {
                    lock (this.ThisLock)
                    {
                        if (_settings.BackgroundUploadItems.Count > 0)
                        {
                            item = _settings.BackgroundUploadItems
                                   .Where(n => n.State == BackgroundUploadState.Encoding)
                                   .FirstOrDefault();
                        }
                    }
                }
                catch (Exception)
                {
                    return;
                }

                if (item == null)
                {
                    continue;
                }

                try
                {
                    if (item.Groups.Count == 0 && item.Keys.Count == 0)
                    {
                        Stream stream = null;

                        try
                        {
                            if (item.Type == BackgroundItemType.Link)
                            {
                                var link = item.Value as Link;
                                if (link == null)
                                {
                                    throw new FormatException();
                                }

                                stream = link.Export(_bufferManager);
                            }
                            else if (item.Type == BackgroundItemType.Store)
                            {
                                var store = item.Value as Store;
                                if (store == null)
                                {
                                    throw new FormatException();
                                }

                                stream = store.Export(_bufferManager);
                            }
                            else
                            {
                                throw new FormatException();
                            }

                            if (stream.Length == 0)
                            {
                                lock (this.ThisLock)
                                {
                                    item.Seed.Rank = 0;

                                    if (item.DigitalSignature != null)
                                    {
                                        item.Seed.CreateCertificate(item.DigitalSignature);
                                    }

                                    _connectionsManager.Upload(item.Seed);

                                    item.State = BackgroundUploadState.Completed;
                                }
                            }
                            else
                            {
                                KeyCollection keys      = null;
                                byte[]        cryptoKey = null;

                                try
                                {
                                    using (ProgressStream encodingProgressStream = new ProgressStream(stream, (object sender, long readSize, long writeSize, out bool isStop) =>
                                    {
                                        isStop = (this.State == ManagerState.Stop || !_settings.BackgroundUploadItems.Contains(item));
                                    }, 1024 * 1024, true))
                                    {
                                        item.Seed.Length = stream.Length;

                                        if (item.Seed.Length == 0)
                                        {
                                            throw new InvalidOperationException("Stream Length");
                                        }

                                        if (item.HashAlgorithm == HashAlgorithm.Sha256)
                                        {
                                            cryptoKey = Sha256.ComputeHash(encodingProgressStream);
                                        }

                                        encodingProgressStream.Seek(0, SeekOrigin.Begin);

                                        item.State = BackgroundUploadState.Encoding;
                                        keys       = _cacheManager.Encoding(encodingProgressStream, item.CompressionAlgorithm, item.CryptoAlgorithm, cryptoKey, item.BlockLength, item.HashAlgorithm);
                                    }
                                }
                                catch (StopIoException)
                                {
                                    continue;
                                }

                                lock (this.ThisLock)
                                {
                                    foreach (var key in keys)
                                    {
                                        item.UploadKeys.Add(key);
                                        item.LockedKeys.Add(key);
                                    }

                                    item.CryptoKey = cryptoKey;
                                    item.Keys.AddRange(keys);
                                }
                            }
                        }
                        finally
                        {
                            if (stream != null)
                            {
                                stream.Dispose();
                            }
                        }
                    }
                    else if (item.Groups.Count == 0 && item.Keys.Count == 1)
                    {
                        lock (this.ThisLock)
                        {
                            item.Seed.Rank = item.Rank;
                            item.Seed.Key  = item.Keys[0];
                            item.Keys.Clear();

                            item.Seed.CompressionAlgorithm = item.CompressionAlgorithm;

                            item.Seed.CryptoAlgorithm = item.CryptoAlgorithm;
                            item.Seed.CryptoKey       = item.CryptoKey;

                            if (item.DigitalSignature != null)
                            {
                                item.Seed.CreateCertificate(item.DigitalSignature);
                            }

                            item.UploadKeys.Add(item.Seed.Key);

                            foreach (var key in item.UploadKeys)
                            {
                                _connectionsManager.Upload(key);
                            }

                            this.SetKeyCount(item);

                            foreach (var key in item.LockedKeys)
                            {
                                _cacheManager.Unlock(key);
                            }

                            item.LockedKeys.Clear();

                            item.State = BackgroundUploadState.Uploading;

                            _connectionsManager.Upload(item.Seed);
                        }
                    }
                    else if (item.Keys.Count > 0)
                    {
                        var   length = Math.Min(item.Keys.Count, 128);
                        var   keys   = new KeyCollection(item.Keys.Take(length));
                        Group group  = null;

                        try
                        {
                            group = _cacheManager.ParityEncoding(keys, item.HashAlgorithm, item.BlockLength, item.CorrectionAlgorithm, (object state2) =>
                            {
                                return(this.State == ManagerState.Stop || !_settings.BackgroundUploadItems.Contains(item));
                            });
                        }
                        catch (StopException)
                        {
                            continue;
                        }

                        lock (this.ThisLock)
                        {
                            foreach (var key in group.Keys.Skip(group.InformationLength))
                            {
                                item.UploadKeys.Add(key);
                                item.LockedKeys.Add(key);
                            }

                            item.Groups.Add(group);

                            item.Keys.RemoveRange(0, length);
                        }
                    }
                    else if (item.Groups.Count > 0 && item.Keys.Count == 0)
                    {
                        var index = new Index();
                        index.Groups.AddRange(item.Groups);
                        index.CompressionAlgorithm = item.CompressionAlgorithm;
                        index.CryptoAlgorithm      = item.CryptoAlgorithm;
                        index.CryptoKey            = item.CryptoKey;

                        byte[]        cryptoKey = null;
                        KeyCollection keys      = null;

                        try
                        {
                            using (var stream = index.Export(_bufferManager))
                                using (ProgressStream encodingProgressStream = new ProgressStream(stream, (object sender, long readSize, long writeSize, out bool isStop) =>
                                {
                                    isStop = (this.State == ManagerState.Stop || !_settings.BackgroundUploadItems.Contains(item));
                                }, 1024 * 1024, true))
                                {
                                    if (item.HashAlgorithm == HashAlgorithm.Sha256)
                                    {
                                        cryptoKey = Sha256.ComputeHash(encodingProgressStream);
                                    }

                                    encodingProgressStream.Seek(0, SeekOrigin.Begin);

                                    item.State = BackgroundUploadState.Encoding;
                                    keys       = _cacheManager.Encoding(encodingProgressStream, item.CompressionAlgorithm, item.CryptoAlgorithm, cryptoKey, item.BlockLength, item.HashAlgorithm);
                                }
                        }
                        catch (StopIoException)
                        {
                            continue;
                        }

                        lock (this.ThisLock)
                        {
                            foreach (var key in keys)
                            {
                                item.UploadKeys.Add(key);
                                item.LockedKeys.Add(key);
                            }

                            item.CryptoKey = cryptoKey;
                            item.Keys.AddRange(keys);
                            item.Rank++;
                            item.Groups.Clear();
                        }
                    }
                }
                catch (Exception e)
                {
                    item.State = BackgroundUploadState.Error;

                    Log.Error(e);

                    this.Remove(item);
                }
            }
        }