Пример #1
0
        public static bool UploadRibceFile(Logger logger, Config config, string BookShortName, string RibceFileName)
        {
            WebClient webClient = new WebClient();
            DataContractJsonSerializer JsonSerializer = new DataContractJsonSerializer(typeof(ServerResponse));
            int attempt = 0;

            while (attempt < config.MaxUploadAttempt)
            {
                try
                {
                    attempt++;
                    logger.WriteToLog(Logger.Level.INFO, "Trying to upload " + BookShortName + ".ribce file. Attempt #" + attempt + " of " + config.MaxUploadAttempt);
                    ServerResponse response = (ServerResponse)JsonSerializer.ReadObject(
                                   new MemoryStream(webClient.UploadFile(config.RibceSiteIfaceURL + "?b=" + BookShortName + "&u=true", "POST", RibceFileName)));
                    if (response.ResponseString == "OK")
                    {
                        logger.WriteToLog(Logger.Level.SUCCESS, BookShortName + ".ribce file successfuly uploaded.");
                        return true;
                    }
                    else
                    {
                        LastServerResponse = response.ResponseString;
                        logger.WriteToLog(Logger.Level.ERROR, "Uploading server side error: " + response.ResponseString);
                        return false;
                    }
                }
                catch (WebException) { }
            }
            return false;
        }
Пример #2
0
        public static bool SendNotification(Logger logger, Config config, string BookShortName, int NotificationCode)
        {
            WebClient webClient = new WebClient();
            DataContractJsonSerializer JsonSerializer = new DataContractJsonSerializer(typeof(ServerResponse));
            int attempt = 0;

            while (attempt < config.MaxNotificationAttempt)
            {
                try
                {
                    attempt++;
                    logger.WriteToLog(Logger.Level.INFO, "Sending notification to RIBCE server...");
                    ServerResponse response = (ServerResponse)JsonSerializer.ReadObject(
                                   new MemoryStream(webClient.DownloadData(config.RibceSiteIfaceURL + "?b=" + BookShortName + "&n=" + NotificationCode)));

                    if (response.ResponseString == "OK")
                    {
                        logger.WriteToLog(Logger.Level.SUCCESS, "Notification successfuly sent.");
                        return true;
                    }
                    else
                    {
                        LastServerResponse = response.ResponseString;
                        logger.WriteToLog(Logger.Level.ERROR, "Sending notification server side error: " + response.ResponseString);
                        return false;
                    }
                }
                catch (WebException) { }
            }
            return false;
        }
Пример #3
0
        public static bool DownloadSourceFile(Logger logger, Config config, string BookShortName)
        {
            WebClient webClient = new WebClient();
            int attempt = 0;

            while (attempt < config.MaxDownloadAttempt)
            {
                try
                {
                    attempt++;
                    logger.WriteToLog(Logger.Level.INFO, "Trying to download source file. Attempt #" + attempt + " of " + config.MaxDownloadAttempt);
                    webClient.DownloadFile(config.RibceSiteBooksURL + "/" + BookShortName + "/src", config.TmpDir + "\\src.doc");
                    logger.WriteToLog(Logger.Level.SUCCESS, "File successfuly downloaded!");
                    return true;
                }
                catch (WebException e) { Console.WriteLine(e.Message); }
            }
            return false;
        }
Пример #4
0
 public static string GetNextBook(Logger logger, Config config)
 {
     WebClient webClient = new WebClient();
     DataContractJsonSerializer JsonSerializer = new DataContractJsonSerializer(typeof(ServerResponse));
     try
     {
         ServerResponse response = (ServerResponse)JsonSerializer.ReadObject(
                new MemoryStream(webClient.DownloadData(
                config.RibceSiteIfaceURL + "?g=" +
                Uri.EscapeDataString(Environment.UserDomainName + "\\" + Environment.UserName) +
                "&i=" + config.SleepTimeMilliseconds +
                "&v=" + Application.ProductVersion)));
         if (response.ResponseString == "OK")
             return response.BookShortName;
         else
         {
             LastServerResponse = response.ResponseString;
             return "";
         }
     }
     catch (Exception)
     {
         logger.WriteToLog(Logger.Level.WARNING, "Can't connect to server!");
         return "";
     }
 }
Пример #5
0
        static void Main(string[] args)
        {
            Config config = null;
            Logger logger = null;
            string ShortBookName;
            bool FirstRun = true;

            if (!mutex.WaitOne(TimeSpan.Zero, true))
            {
                Console.Write("Daemon is already running! Exiting...");
                return;
            }
            Console.Title = Application.ProductName + " v" + Application.ProductVersion;
            Console.WriteLine(Application.ProductName + " v" + Application.ProductVersion);
            Console.WriteLine("(C) 2014 " + Application.CompanyName);
            Console.Write("Loading config...");
            try
            {
                config = new Config();
                logger = new Logger(config.LogFile, config.LogToConsole);
            }
            catch(Exception)
            {
                Console.WriteLine("FAILED");
                Console.Write("Press any key to exit...");
                Console.ReadKey(true);
                Process.GetCurrentProcess().Kill();
            }
            Console.WriteLine("OK");
            Console.WriteLine();

            for (int i = 0; i < config.WaitBeforeStartSeconds; i++)
            {
                Console.Write("Starting after " + (config.WaitBeforeStartSeconds - i) + " seconds... (Press Ctrl-C to cancel launch)\r");
                Thread.Sleep(1000);
                Console.Write("".PadRight(79) + "\r");
            }

            Thread notifyThread = new Thread(delegate()
            {
                trayMenu = new ContextMenu();
                trayMenu.MenuItems.Add("Show log window", OnMenuShowLogClick);
                trayMenu.MenuItems.Add("Hide log window", OnMenuHideLogClick);
                trayMenu.MenuItems.Add("Shutdown Converter", OnMenuExit);
                trayMenu.MenuItems[0].DefaultItem = true;
                trayIcon = new NotifyIcon();
                trayIcon.Text = Application.ProductName;
                trayIcon.Icon = Icon.ExtractAssociatedIcon(Process.GetCurrentProcess().MainModule.FileName);
                trayIcon.ContextMenu = trayMenu;
                trayIcon.Click += OnIconClick;
                trayIcon.DoubleClick += OnIconClick;
                trayIcon.Visible = true;
                Application.Run();
            });

            notifyThread.Start();

            Console.TreatControlCAsInput = true;
            Console.Title = Application.ProductName + " v" + Application.ProductVersion + " | Click min button to hide this window \u2192";
            IntPtr hSystemMenu = GetSystemMenu(Process.GetCurrentProcess().MainWindowHandle, false);
            EnableMenuItem(hSystemMenu, SC_CLOSE, MF_GRAYED);
            RemoveMenu(hSystemMenu, SC_CLOSE, MF_BYCOMMAND);
            HideConsoleWindow();

            logger.WriteToLog(Logger.Level.SUCCESS, "Daemon started successfuly.");
            try
            {
                while (true)
                {
                    if (!FirstRun) Thread.Sleep(config.SleepTimeMilliseconds);
                    FirstRun = false;
                    logger.WriteToLog(Logger.Level.INFO, "Requesting RIBCE server for new book...");
                    ShortBookName = Utils.GetNextBook(logger, config);
                    if (ShortBookName == "")
                    {
                        logger.WriteToLog(Logger.Level.INFO, "No book for conversion. Zzz..");
                        continue;
                    }
                    logger.WriteToLog(Logger.Level.SUCCESS, "New book: \"" + ShortBookName + "\", starting conversion procedure.");
                    logger.WriteToLog(Logger.Level.INFO, "Cleaning temp folder.");
                    Utils.CleanTmpDir(config.TmpDir);
                    logger.WriteToLog(Logger.Level.SUCCESS, "Temp folder cleaned.");
                    logger.WriteToLog(Logger.Level.INFO, "Downloading source file...");
                    if (!Utils.DownloadSourceFile(logger, config, ShortBookName))
                    {
                        logger.WriteToLog(Logger.Level.ERROR, "Error while downloading source file for book \"" + ShortBookName + "\"");
                        logger.WriteToLog(Logger.Level.WARNING, "Check your internet connection!");
                        logger.WriteToLog(Logger.Level.INFO, "Going to the next iteration.");
                        continue;
                    }
                    logger.WriteToLog(Logger.Level.INFO, "Creating Converter instance.");
                    Converter converter = new Converter(config.TmpDir + "\\src.doc", ShortBookName, logger, config);
                    logger.WriteToLog(Logger.Level.INFO, "Starting main sequence.");

                    try
                    {
                        if (!Utils.SendNotification(logger, config, ShortBookName, Utils.STATUS_INPROGRESS))
                            if (Utils.LastServerResponse == "NOTINQUEUE")
                            {
                                logger.WriteToLog(Logger.Level.INFO, "Book \"" + ShortBookName + "\" is not in queue, it goes to another converter.");
                                logger.WriteToLog(Logger.Level.INFO, "Going to the next iteration.");
                                continue;
                            }
                        converter.Run();
                        Utils.SendNotification(logger, config, ShortBookName, Utils.STATUS_CONVERTED);

                        if (Utils.UploadRibceFile(logger, config, ShortBookName, config.TmpDir + "\\" + ShortBookName + ".ribce"))
                            logger.WriteToLog(Logger.Level.SUCCESS, "All done. Waiting for next book...");
                        else
                            logger.WriteToLog(Logger.Level.ERROR, "Unable to uplad!");
                    }
                    catch (Reader.UnsupportedFileFormatException)
                    {
                        logger.WriteToLog(Logger.Level.ERROR, "Unsupported input file format!");
                        Utils.SendNotification(logger, config, ShortBookName, Utils.STATUS_FORMATERR);
                    }
                    catch (Reader.InternalErrorException)
                    {
                        logger.WriteToLog(Logger.Level.ERROR, "Internal converter Error!");
                        Utils.SendNotification(logger, config, ShortBookName, Utils.STATUS_CNVINTERR);
                    }
                    catch (Exception e)
                    {
                        logger.WriteToLog(Logger.Level.ERROR, "Internal Error!\n" + e.Message + "\n" + e.StackTrace);
                        Utils.SendNotification(logger, config, ShortBookName, Utils.STATUS_CNVINTERR);
                    }
                }
            }
            catch(Exception e)
            {
                logger.WriteToLog(Logger.Level.ERROR, "Fatal Error! See StackTrace:\n" + e.Message + "\n" + e.StackTrace);
                Console.ReadKey(true);
            }
            mutex.ReleaseMutex();
        }