Esempio n. 1
0
        /**
         * Start execution of program
         *
         * TODO: Move a lot of this to functions or another class, way too long at the moment
         */
        public void run()
        {
            // TODO: Include other options besides MySQL
            DatabaseType databaseType = DatabaseType.Invalid;

            while (databaseType == DatabaseType.Invalid)
            {
                databaseType = cmdInterface.GetDatabaseType();
            }

            if (databaseType == DatabaseType.MySql)
            {
                database = new SQLDatabase();
            }
            else
            {
                throw new Exception();
            }

            // Check that can connect to database if not non persistent
            if (databaseType != DatabaseType.NonPersistent)
            {
                String filePath;
                Dictionary <DatabaseParameter, String> databaseParams;
                String tableName;
                do
                {
                    databaseParams = new Dictionary <DatabaseParameter, String>();
                    tableName      = "";
                    filePath       = cmdInterface.GetFilePath();

                    using (StreamReader sr = File.OpenText(filePath))
                    {
                        String s = "";
                        while ((s = sr.ReadLine()) != null)
                        {
                            String[] splitString = s.Split(':');

                            switch (splitString[0])
                            {
                            case "Host":
                                databaseParams.Add(DatabaseParameter.Host, splitString[1]);
                                break;

                            case "Port":
                                databaseParams.Add(DatabaseParameter.Port, splitString[1]);
                                break;

                            case "Database":
                                databaseParams.Add(DatabaseParameter.Database, splitString[1]);
                                break;

                            case "Username":
                                databaseParams.Add(DatabaseParameter.Username, splitString[1]);
                                break;

                            case "Password":
                                databaseParams.Add(DatabaseParameter.Password, splitString[1]);
                                break;

                            case "Table":
                                tableName = splitString[1];
                                break;
                            }
                        }
                    }
                }while (!CanConnectToDatabase(databaseParams, tableName));

                database.SetConnectionString(databaseParams);
                database.TableName = tableName;

                database.ConnectToDatabase();
            }

            // Run operation specified by user
            Operation currOperation;

            while (true)
            {
                // Get action
                currOperation = cmdInterface.GetAction();

                if (currOperation == Operation.GatherData)
                {
                    String requestString = cmdInterface.GetRandomiserUrl();
                    String regexString   = cmdInterface.GetRegexForReturnUrl();

                    if (cmdInterface.ConfirmSelection())
                    {
                        runInfo.RequestString = requestString;
                        runInfo.RegexString   = regexString;

                        runInfo.NumberOfRequests = cmdInterface.GetNumberOfRequests();

                        GetData();
                        Console.WriteLine("All results collected\n");
                    }
                }
                else if (currOperation == Operation.ReportOnDataIntegrity)
                {
                    ReportOnRandomness();
                }
                else if (currOperation == Operation.GenerateReport)
                {
                    AnalyseAllData();
                }
                // If not standard operation, quit program
                else
                {
                    break;
                }
            }
        }