Esempio n. 1
0
        ///// <summary>
        ///// De-compress the file.
        ///// </summary>
        ///// <param name="fileInfo">The file information.</param>
        ///// <param name="destinationPath">The destination path.</param>
        ///// <param name="deCompressed">The de compressed.</param>
        ///// <returns>True if successful</returns>
        public static bool DeCompressFile(this FileInfo fileInfo, string destinationPath, out DeCompressedElement deCompressed)
        {
            bool success;
            ICompression compression;
            switch (fileInfo.Extension.ToLower())
            {
                case "gz":
                    compression = new GZipCompression();
                    success = compression.DeCompressFile(fileInfo.FullName, destinationPath, out deCompressed);
                    break;
                case "zip":
                    compression = new ZipCompression();
                    success = compression.DeCompressFile(fileInfo.FullName, destinationPath, out deCompressed);
                    break;
                case "ziparc":
                    compression = new ZipArchiveCompression();
                    success = compression.DeCompressFile(fileInfo.FullName, destinationPath, out deCompressed);
                    break;
                default:
                    success = false;
                    deCompressed = null;
                    break;

            }
            return success;
        }
        public byte[] EncodeMessage(object message)
        {
            if (message == null)
            {
                throw new CannotSerializeMessageException("The message which is to be serialized cannot be null.");
            }

            var json = JsonConvert.SerializeObject(message, this.Formatting);

            if (CompressionEnabled)
            {
                return(GZipCompression.Compress(Encoding.UTF8.GetBytes(json)));
            }
            else
            {
                return(Encoding.UTF8.GetBytes(json));
            }
        }
        public T DecodeMessage <T>(byte[] data, int dataOffset, int dataLength)
        {
            if (data == null)
            {
                throw new CannotDeserializeMessageException("The data which is to be deserialized cannot be null.");
            }

            if (CompressionEnabled)
            {
                string json = Encoding.UTF8.GetString(GZipCompression.Decompress(data, dataOffset, dataLength));
                return(JsonConvert.DeserializeObject <T>(json));
            }
            else
            {
                string json = Encoding.UTF8.GetString(data, dataOffset, dataLength);
                return(JsonConvert.DeserializeObject <T>(json));
            }
        }
        /// <summary>
        /// Base constructor
        /// </summary>
        protected ServerListener()
        {
            //Set timer that checks all clients every 5 minutes
            _keepAliveTimer           = new System.Timers.Timer(300000);
            _keepAliveTimer.Elapsed  += KeepAlive;
            _keepAliveTimer.AutoReset = true;
            _keepAliveTimer.Enabled   = true;
            WhiteList = new List <IPAddress>();
            BlackList = new List <IPAddress>();

            IsRunning           = false;
            AllowReceivingFiles = false;

            ByteCompressor    = new DeflateByteCompression();
            MessageEncryption = new Aes256();
            FileCompressor    = new GZipCompression();
            FolderCompressor  = new ZipCompression();
        }
        public byte[] EncodeMessage <T>(T message)
        {
            if (message == null)
            {
                throw new CannotSerializeMessageException("The message which is to be serialized cannot be null.");
            }

            if (CompressionEnabled)
            {
                var xml = XmlConvert.SerializeObject(message);
                return(GZipCompression.Compress(Encoding.UTF8.GetBytes(xml)));
            }
            else
            {
                var xml = XmlConvert.SerializeObject(message);
                return(Encoding.UTF8.GetBytes(xml));
            }
        }
Esempio n. 6
0
        public byte[] EncodeMessage <T>(T message)
        {
            if (message == null)
            {
                throw new CannotSerializeMessageException("The message which is to be serialized cannot be null.");
            }

            var raw = ProtocolBuffersConvert.SerializeObject <T>(message);

            if (CompressionEnabled)
            {
                return(GZipCompression.Compress(raw));
            }
            else
            {
                return(raw);
            }
        }
Esempio n. 7
0
        //const string fileName = "whazzup.txt";

        public static void Main(string[] args)
        {
            string           path        = GetPath();
            IGZipCompression compression = new GZipCompression();

            IIVAOWhazzupDataSource nonCachedWebDataSource        = new WebIVAOWhazzupDataSource("http://api.ivao.aero/getdata/whazzup/whazzup.txt");
            IIVAOWhazzupDataSource nonCachedWebGZippedDataSource = new WebGZippedIVAOWhazzupDataSource("http://api.ivao.aero/getdata/whazzup/whazzup.txt.gz", compression);

            IIVAOWhazzupDataSource nonCachedLocalDataSource        = new LocalIVAOWhazzupDataSource(path);
            IIVAOWhazzupDataSource nonCachedLocalGZippedDataSource = new LocalGZippedIVAOWhazzupDataSource(path, compression);

            ICachedIVAOWhazzupDataSource dataSource = new CachedIVAOWhazzupDataSource(nonCachedLocalGZippedDataSource);

            IParserFactory    parserFactory    = new ParserFactory();
            IGeneralSelector  generalSelector  = new GeneralSelector();
            IClientsSelector  clientsSelector  = new ClientsSelector();
            IServersSelector  serverSelector   = new ServersSelector();
            IAirportsSelector airportsSelector = new AirportsSelector();

            IGeneralDataProvider generalDataProvider  = new GeneralDataProvider(dataSource, parserFactory, generalSelector);
            IClientsProvider     clientsDataProvider  = new ClientsDataProvider(dataSource, parserFactory, clientsSelector);
            IServersProvider     serversDataProvider  = new ServersDataProvider(dataSource, parserFactory, serverSelector);
            IAirportsProvider    airportsDataProvider = new AirportsDataProvider(dataSource, parserFactory, airportsSelector);

            IClientsProvider atcClientsDataProvider      = new AirTrafficControllersDataProvider(dataSource, parserFactory, clientsSelector);
            IClientsProvider pilotClientsDataProvider    = new PilotsDataProvider(dataSource, parserFactory, clientsSelector);
            IClientsProvider followMeClientsDataProvider = new FollowMesDataProvider(dataSource, parserFactory, clientsSelector);

            List <GeneralData> generalDataModels  = generalDataProvider.GetData().ToList();
            List <Client>      clientDataModels   = clientsDataProvider.GetData().ToList();
            List <Server>      serversDataModels  = serversDataProvider.GetData().ToList();
            List <Airport>     airportsDataModels = airportsDataProvider.GetData().ToList();

            List <Client> atcDataModels      = atcClientsDataProvider.GetData().ToList();
            List <Client> pilotDataModels    = pilotClientsDataProvider.GetData().ToList();
            List <Client> followMeDataModels = followMeClientsDataProvider.GetData().ToList();

            Client item = pilotDataModels.First();

            TrySerialize(item);
        }
Esempio n. 8
0
        /// <summary>
        /// Compresses the specified type.
        /// </summary>
        /// <param name="directory">The directory.</param>
        /// <param name="type">The type.</param>
        /// <param name="destinationPath">The destination path.</param>
        /// <param name="compressedFileInfo">The compressed file information.</param>
        /// <param name="name">The name.</param>
        /// <returns>True if succeeded</returns>
        public static bool Compress(this DirectoryInfo directory, CompressionType type, string destinationPath, out CompressedElement compressedFileInfo, string name = "")
        {
            bool success;
            ICompression compression;
            switch (type)
            {
                case CompressionType.GZip:
                    compression = new GZipCompression();
                    success = compression.CompressFolder(directory.FullName, destinationPath, out compressedFileInfo);
                    break;
                case CompressionType.ZipArchive:
                    compression = new ZipArchiveCompression();
                    success = compression.CompressFolder(directory.FullName, destinationPath, out compressedFileInfo);
                    break;
                default:
                    compression = new ZipCompression();
                    success = compression.CompressFolder(directory.FullName, destinationPath, out compressedFileInfo);
                    break;

            }
            return success;
        }
Esempio n. 9
0
        public void TestCompressDecompress()
        {
            Console.WriteLine("Testing Compression, Decompression:\n" + new string('_', 100));

            for (int i = 0; i < 100; i++)
            {
                string message = Hash.GetComplexHash() + Hash.GetComplexHash()
                                 + Hash.GetComplexHash() + Hash.GetComplexHash();

                while (message.Length > 0)
                {
                    Assert.AreEqual(message, GZipCompression.DecompressString(GZipCompression.CompressString(message, CompressionLevel.NoCompression)));
                    Assert.AreEqual(message, GZipCompression.DecompressString(GZipCompression.CompressString(message, CompressionLevel.Fastest)));
                    Assert.AreEqual(message, GZipCompression.DecompressString(GZipCompression.CompressString(message, CompressionLevel.Optimal)));

                    message = message.Remove(0, 1);
                }

                Console.Write(".");
            }

            Console.WriteLine();
        }
Esempio n. 10
0
        /// <summary>
        /// Compresses the specified type.
        /// </summary>
        /// <param name="directory">The directory.</param>
        /// <param name="type">The type.</param>
        /// <param name="destinationPath">The destination path.</param>
        /// <param name="compressedFileInfo">The compressed file information.</param>
        /// <param name="name">The name.</param>
        /// <returns>True if succeeded</returns>
        public static bool Compress(this DirectoryInfo directory, CompressionType type, string destinationPath, out CompressedElement compressedFileInfo, string name = "")
        {
            bool         success;
            ICompression compression;

            switch (type)
            {
            case CompressionType.GZip:
                compression = new GZipCompression();
                success     = compression.CompressFolder(directory.FullName, destinationPath, out compressedFileInfo);
                break;

            case CompressionType.ZipArchive:
                compression = new ZipArchiveCompression();
                success     = compression.CompressFolder(directory.FullName, destinationPath, out compressedFileInfo);
                break;

            default:
                compression = new ZipCompression();
                success     = compression.CompressFolder(directory.FullName, destinationPath, out compressedFileInfo);
                break;
            }
            return(success);
        }
Esempio n. 11
0
        public void DeCompressStringThrowsArgumentNullExceptionIfInputIsNull()
        {
            IGZipCompression compress = new GZipCompression();

            compress.Decompress("");
        }
Esempio n. 12
0
        public Map LoadMap(string name)
        {
            var filepath1  = Path.Combine(BaseDir, $"{name}.{FileFormatCompressed}");
            var filepath2  = Path.Combine(BaseDir, $"{name}.{FileFormatNonCompressed}");
            var file1exist = File.Exists(filepath1);
            var file2exist = File.Exists(filepath2);

            object data = null;

            if (!file1exist && !file2exist)
            {
                Logger($"Content data is empty and being ignored.");

                return((Map)data);
            }

            try
            {
                data = file1exist ? JsonConvert.DeserializeObject <Map>(Encoding.UTF8.GetString(GZipCompression.UnZip(File.ReadAllBytes(filepath1))))
                    : JsonConvert.DeserializeObject <Map>(File.ReadAllText(filepath2));

                Logger($"Loaded from path: '{(file1exist ? filepath1 : filepath2)}'");
            }
            catch (Exception e)
            {
                Logger($"(LoadException) An error occurred along load method:" +
                       $"\n- File: '{(file1exist ? filepath1 : filepath2)}'" +
                       $"\n- Error: {e}");
            }

            return((Map)data);
        }
Esempio n. 13
0
        public T Load <T>(string subfolder, string filename)
        {
            var filepath = Path.Combine(GetSubDirectory(subfolder), $"{filename}.{Format}");

            object data = null;

            try
            {
                data = EnableCompression ? JsonConvert.DeserializeObject <T>(Encoding.UTF8.GetString(GZipCompression.UnZip(File.ReadAllBytes(filepath))))
                    : JsonConvert.DeserializeObject <T>(File.ReadAllText(filepath));

                Logger($"[SubDir: '{subfolder}'] Loaded from path: '{filepath}'");
            }
            catch (Exception e)
            {
                Logger($"(LoadException) An error occurred along load method:" +
                       $"\n- File: '{filepath}'" +
                       $"\n- Error: {e}");
            }

            return((T)data);
        }
Esempio n. 14
0
        protected void VersionDownloadCompleted(
            string displayAppName,
            VersionManifest downloadManifest,
            VersionManifest latestManifest,
            VersionData latestVersionInfo,
            InstallInfo installInfo)
        {
            if (!AskUserForInstall(displayAppName, latestVersionInfo))
            {
                return;
            }

            //Unzip downloaded version
            foreach (var item in downloadManifest.VersionItems)
            {
                var tempFile      = Path.Combine(installInfo.TempPath, item.GetItemFullPath());
                var tempFileUnzip = Path.Combine(installInfo.TempPath, item.GetUnzipItemFullPath());

                GZipCompression.DecompressFile(tempFile, tempFileUnzip);
            }

            LocationHash badLocation;

            if (!CheckHash(installInfo.TempPath, downloadManifest.VersionItems.ConvertAll(vi => vi.GetLocationHash()), out badLocation))
            {
                throw new UpdateException(UpdStr.BAD_VERSION);
            }

            //Unzip bootstrapper
            var tempInstallerPath = GetInstallerDir(installInfo.TempPath, installInfo.AppName, latestManifest.VersionNumber);

            foreach (var item in downloadManifest.BootStrapper)
            {
                var tempFile      = Path.Combine(tempInstallerPath, item.GetItemFullPath());
                var tempFileUnzip = Path.Combine(tempInstallerPath, item.GetUnzipItemFullPath());

                GZipCompression.DecompressFile(tempFile, tempFileUnzip);
            }

            if (!CheckHash(tempInstallerPath, downloadManifest.BootStrapper, out badLocation))
            {
                throw new UpdateException(UpdStr.BAD_VERSION);
            }

            //Saving latest manifest
            XmlSerializeHelper.SerializeItem(
                latestManifest,
                Path.Combine(installInfo.TempPath, VersionManifest.VersionManifestFileName));

            //Saving downloaded manifest
            XmlSerializeHelper.SerializeItem(
                downloadManifest,
                Path.Combine(installInfo.TempPath, VersionManifest.DownloadedVersionManifestFileName));

            //var installerPath = CopyInstaller(installInfo.TempPath, installInfo.AppName, latestManifest.VersionNumber, installInfo);
            var installerDir  = GetInstallerDir(installInfo.TempPath, installInfo.AppName, latestManifest.VersionNumber);
            var installerPath = Path.Combine(installerDir, "Updater.exe");

            //Saving install info
            XmlSerializeHelper.SerializeItem(
                installInfo,
                Path.Combine(installerDir, InstallInfo.InstallInfoFileName));

            //Start installing
            Process.Start(installerPath);
            //_UpdatingFlag.Close();

            InvokeUpdateCompleted(true, true, null);
            DispatcherHelper.Invoke(new SimpleMethod(OnNeedCloseApp));
        }
Esempio n. 15
0
        public void CompressByteArrayThrowsArgumentNullExceptionIfInputIsNull()
        {
            IGZipCompression compress = new GZipCompression();

            compress.Compress(null);
        }
Esempio n. 16
0
 private static Map GetMapFromBytes(bool compressed, byte[] data)
 => compressed?JsonConvert.DeserializeObject <Map>(Encoding.UTF8.GetString(GZipCompression.UnZip(data))) :
     JsonConvert.DeserializeObject <Map>(Encoding.UTF8.GetString(data));
Esempio n. 17
0
        public void DecompressNullThrow()
        {
            var gz = new GZipCompression();

            gz.Decompress(null);
        }