Пример #1
0
        public SystemMapping UnmarshallMappings(string databaseFile, PinballXSystem system)
        {
            if (!_file.Exists(databaseFile))
            {
                return(new SystemMapping());
            }

            _logger.Info("Reading mappings from {0}...", databaseFile);
            try {
                using (var sr = new StreamReader(databaseFile))
                    using (JsonReader reader = new JsonTextReader(sr)) {
                        try {
                            var systemMapping = _serializer.Deserialize <SystemMapping>(reader);
                            reader.Close();
                            return(systemMapping);
                        } catch (Exception e) {
                            _logger.Error(e, "Error parsing vpdb.json, deleting and ignoring.");
                            _crashManager.Report(e, "json");
                            reader.Close();
                            File.Delete(databaseFile);
                            return(new SystemMapping());
                        }
                    }
            } catch (Exception e) {
                _logger.Error(e, "Error reading vpdb.json, ignoring.");
                _crashManager.Report(e, "json");
                return(new SystemMapping());
            }
        }
Пример #2
0
        public string Authorize(string channelName, string socketId)
        {
            Stream       dataStream = null;
            StreamReader reader     = null;
            WebResponse  response   = null;
            string       result     = null;

            try {
                var body     = JsonConvert.SerializeObject(new RegisterRequest(channelName, socketId));
                var bodyData = Encoding.UTF8.GetBytes(body);

                var webRequest = _vpdbClient.GetWebRequest(Endpoint);
                webRequest.Method        = "POST";
                webRequest.ContentType   = "application/json";
                webRequest.ContentLength = bodyData.Length;
                dataStream = webRequest.GetRequestStream();
                dataStream.Write(bodyData, 0, bodyData.Length);
                dataStream.Close();
                response   = webRequest.GetResponse();
                dataStream = response.GetResponseStream();
                reader     = new StreamReader(dataStream);

                result = reader.ReadToEnd();
            } catch (Exception e) {
                _logger.Error(e, "Error retrieving pusher auth token.");
                _crashManager.Report(e, "pusher");
            } finally {
                reader?.Close();
                dataStream?.Close();
                response?.Close();
            }

            return(result);
        }
Пример #3
0
 /// <summary>
 /// Moves a downloaded file to the table folder of the platform.
 /// </summary>
 /// <param name="job">Job of downloaded file</param>
 /// <param name="system">System of the downloaded file</param>
 private void MoveDownloadedFile(Job job, PinballXSystem system)
 {
     // move downloaded file to table folder
     if (job.FilePath != null && File.Exists(job.FilePath))
     {
         try {
             var dest = job.GetFileDestination(system);
             if (dest != null && !File.Exists(dest))
             {
                 _logger.Info("Moving downloaded file from \"{0}\" to \"{1}\"...", job.FilePath, dest);
                 File.Move(job.FilePath, dest);
             }
             else
             {
                 // todo see how to handle, probably name it differently.
                 _logger.Warn("File \"{0}\" already exists at destination!", dest);
             }
         } catch (Exception e) {
             _logger.Error(e, "Error moving downloaded file.");
             _crashManager.Report(e, "fs");
         }
     }
     else
     {
         _logger.Error("Downloaded file \"{0}\" does not exist.", job.FilePath);
     }
 }
Пример #4
0
        /// <summary>
        /// Starts the download.
        /// </summary>
        /// <remarks>
        /// This is called when the queue has a new download slot available.
        /// </remarks>
        /// <param name="job">Job to start</param>
        /// <param name="token">Cancelation token</param>
        /// <returns>Job</returns>
        private async Task <Job> ProcessDownload(Job job, CancellationToken token)
        {
            var dest = Path.Combine(_downloadPath, job.File.Name);

            _logger.Info("Starting download of {0} to \"{1}\"", job.File.Uri, dest);

            // setup cancelation
            token.Register(job.Client.CancelAsync);

            // update statuses
            job.OnStart(token, dest);

            // do the grunt work
            try {
                await job.Client.DownloadFileTaskAsync(job.File.Uri, dest);

                job.OnSuccess();
                _logger.Info("Finished download of {0}", job.File.Uri);
            } catch (WebException e) {
                if (e.Status == WebExceptionStatus.RequestCanceled)
                {
                    job.OnCancelled();
                }
                else
                {
                    job.OnFailure(e);
                    _logger.Error(e, "Error downloading file (server error): {0}", e.Message);
                    _crashManager.Report(e, "network");
                    _messageManager.LogError(e, "Error downloading file");
                }
            } catch (Exception e) {
                job.OnFailure(e);
                _logger.Error(e, "Error downloading file: {0}", e.Message);
                _crashManager.Report(e, "network");
                _messageManager.LogError(e, "Error downloading file");
            }
            return(job);
        }