Example #1
0
        private static void SendSolutionChecked(SocketConnection Socket, Int32 RequestCode,
            Int32 SolutionID, Int32 PT, Int32 Result, Int32 Score, List<Tuple<Int64, Int64, TestResults>> TestResults)
        {
            int testsCount = TestResults == null ? 0 : TestResults.Count;

            int n = 4 + 4;
            n += 4;     // solution id
            n += 4;     // problem type
            n += 4;     // result
            n += 4;     // score
            n += 4;     // tests count
            n += (8 + 8 + 4) * testsCount;

            byte[] data = new byte[n];
            BitConverter.GetBytes(n).CopyTo(data, 0);
            BitConverter.GetBytes(RequestCode).CopyTo(data, 4);
            BitConverter.GetBytes(SolutionID).CopyTo(data, 8);
            BitConverter.GetBytes(PT).CopyTo(data, 12);
            BitConverter.GetBytes(Result).CopyTo(data, 16);
            BitConverter.GetBytes(Score).CopyTo(data, 20);
            BitConverter.GetBytes(testsCount).CopyTo(data, 24);

            if (TestResults != null)
            {
                int i = 0;
                foreach (var item in TestResults)
                {
                    BitConverter.GetBytes(item.Item1).CopyTo(data, 28 + i * 20);
                    BitConverter.GetBytes(item.Item2).CopyTo(data, 36 + i * 20);
                    BitConverter.GetBytes((Int32)item.Item3).CopyTo(data, 44 + i * 20);
                    i++;
                }
            }

            Socket.Send(data, data.Length);

            logger.Trace("Send solution checked");
            if (logger.IsTraceEnabled)
                Console.WriteLine(DateTime.Now.ToString(culture) + " - Send solution checked");
        }
Example #2
0
        /// <summary>
        /// Translate received data. Get the key value and the message.
        /// </summary>
        /// <remarks>
        /// First 4 bytes   - length of data.
        /// Second 4 bytes  - key value.
        /// 
        /// Key:
        /// 1   Get cpu usage.
        /// 
        /// 100 Solution file:
        ///     [4b - solution id][4b - file name length][file name][file data]
        /// 
        /// 101 Problem file:
        ///     [4b - file name length][file name][file data]
        /// 
        /// </remarks>
        /// <param name="data"></param>
        static void socket_ReceivedData(object sender, SocketConnection.ReceivedDataEventArgs e)
        {
            Int32 code = BitConverter.ToInt32(e.ReceivedData, 4);
            byte[] data = null;
            int solutionID = -1, problemID = -1;
            string fileName;
            ProgrammingLanguages pl;
            TournamentFormats tf;
            ProblemTypes pt;

            try
            {
                logger.Trace("Received code: {0}", (RequestCodes)code);
                if (logger.IsTraceEnabled)
                    Console.WriteLine(DateTime.Now.ToString(culture) + " - Received code: {0}", (RequestCodes)code);

                switch ((RequestCodes)code)
                {
                    case RequestCodes.MainInfo:
                        SendMainInfo((SocketConnection)sender, (Int32)ResponseCodes.MainInfo);
                        break;
                    case RequestCodes.CPUUsage:
                        SendCPUUsage((SocketConnection)sender, (Int32)ResponseCodes.CPUUsage);
                        break;
                    case RequestCodes.SolutionFile:
                        data = new byte[e.ReceivedData.Length];
                        Array.Copy(e.ReceivedData, data, e.ReceivedData.Length);

                        ThreadPool.QueueUserWorkItem(state =>
                            {
                                TestResults result;
                                int score;

                                SaveSolution(data, out problemID, out solutionID, out fileName, out pl, out tf, out pt);

                                List<Tuple<Int64, Int64, TestResults>> testResults = null;// = new List<Tuple<long, long, TestResult>>();

                                TesterSingleton.Instance.Test(problemID, solutionID, fileName, pl, tf, pt, out result, out score, out testResults);

                                logger.Info("Complete testing solution {0} for problem {1} on PL {2}, result: {3}",
                                    solutionID, problemID, pl, result);
                                Console.WriteLine(DateTime.Now.ToString(culture) + " - Complete testing solution {0} for problem {1} on PL {2}, result: {3}",
                                    solutionID, problemID, pl, result);

                                DeleteSolution(solutionID);

                                SendSolutionChecked((SocketConnection)sender, (Int32)ResponseCodes.SolutionFileChecked,
                                    solutionID, (Int32)pt, (Int32)result, score, testResults);
                            });

                        break;
                    case RequestCodes.ReadyForReceivingProblem:
                        //socket.CheckConnectionTimer.Stop();
                        TesterSingleton.Instance.CanTest.Reset();
                        TesterSingleton.Instance.AllDone.Wait();
                        SendRequestForRecivingProblem((SocketConnection)sender, (Int32)ResponseCodes.ReadyForReceivingProblem);
                        break;
                    case RequestCodes.EndOfReceivingProblem:
                        TesterSingleton.Instance.CanTest.Set();

                        //socket.CheckConnectionTimer.Start();
                        //SendProblemsInfo((SocketConnection)sender, (Int32)ResponseCodes.ProblemsInfo);
                        break;
                    case RequestCodes.ProblemFile:
                        data = new byte[e.ReceivedData.Length];
                        Array.Copy(e.ReceivedData, data, e.ReceivedData.Length);

                        TestResults res;
                        bool isCorrect = false;
                        string info = "";
                        DateTime lastModifiedTime = DateTime.MinValue;
                        try
                        {
                            SaveProblem(data, out problemID, out lastModifiedTime);
                            CompileChecker(problemID, out res);
                            CheckProblem(problemID, out isCorrect, out info);
                        }
                        catch (Exception ex)
                        {
                            logger.Error("Problem receiving/saving failed: {0}", ex.Message);
                            Console.WriteLine(DateTime.Now.ToString(culture) + " - Problem receiving/saving failed: {0}", ex.Message);
                        }
                        finally
                        {
                            ProblemsInfo.Instance.Add(new ProblemInfo()
                                {
                                    ProblemID = problemID,
                                    IsCorrect = isCorrect,
                                    Info = info,
                                    LastModifiedTime = lastModifiedTime
                                }
                            );

                            SendProblemReceived((SocketConnection)sender, (Int32)ResponseCodes.ProblemFileReceived);
                        }
                        break;
                    case RequestCodes.DeleteProblem:
                        TesterSingleton.Instance.CanTest.Reset();
                        TesterSingleton.Instance.AllDone.Wait();

                        problemID = BitConverter.ToInt32(e.ReceivedData, 8);
                        DeleteProblem(problemID);
                        ProblemsInfo.Instance.Delete(problemID);

                        SendProblemDeleted((SocketConnection)sender, (Int32)ResponseCodes.ProblemDeleted);
                        //SendProblemsInfo((SocketConnection)sender, (Int32)ResponseCodes.ProblemsInfo);
                        break;
                    case RequestCodes.ProblemsInfo:
                        SendProblemsInfo((SocketConnection)sender, (Int32)ResponseCodes.ProblemsInfo);
                        break;
                    case RequestCodes.ReadyForReceivingCompilerOptions:
                        //socket.CheckConnectionTimer.Stop();
                        TesterSingleton.Instance.CanTest.Reset();
                        TesterSingleton.Instance.AllDone.Wait();
                        SendRequestForRecivingCompilerOptions((SocketConnection)sender, (Int32)ResponseCodes.ReadyForReceivingCompilerOptions);
                        break;
                    case RequestCodes.CompilerOptionsFile:
                        data = new byte[e.ReceivedData.Length];
                        Array.Copy(e.ReceivedData, data, e.ReceivedData.Length);

                        try
                        {
                            SaveCompilerOptions(data);
                            Compilers.Load(LocalPath.CompilerOptionsDirectory);
                        }
                        catch (Exception ex)
                        {
                            logger.Error("CompilerOptions receiving/saving failed: {0}", ex.Message);
                            Console.WriteLine(DateTime.Now.ToString(culture) + " - CompilerOptions receiving/saving failed: {0}", ex.Message);
                        }
                        finally
                        {
                            SendCompilerOptionsReceived((SocketConnection)sender, (Int32)ResponseCodes.CompilerOptionsFileReceived);
                            SendMainInfo((SocketConnection)sender, (Int32)ResponseCodes.MainInfo);
                            TesterSingleton.Instance.CanTest.Set();
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(DateTime.Now.ToString(culture) + " - Error occurred on request \"{0}\" processing: {1}", (RequestCodes)code, ex.Message);
                logger.Error("Error occurred on request \"{0}\" processing: {1}", (RequestCodes)code, ex.Message);
            }
        }
Example #3
0
        private static void SendRequestForRecivingProblem(SocketConnection Socket, Int32 RequestCode)
        {
            byte[] data = new byte[4 + 4];
            BitConverter.GetBytes(4 + 4).CopyTo(data, 0);
            BitConverter.GetBytes(RequestCode).CopyTo(data, 4);

            Socket.Send(data, data.Length);

            logger.Trace("Send request for receiving problem");
            if (logger.IsTraceEnabled)
                Console.WriteLine(DateTime.Now.ToString(culture) + " - Send request for receiving problem");
        }
Example #4
0
        private static void SendProblemsInfo(SocketConnection Socket, Int32 RequestCode)
        {
            ProblemsInfo.Instance.Serialize();

            if (!File.Exists(LocalPath.ProblemsDirectory + "problems.inf"))
            {
                throw new FileNotFoundException(LocalPath.ProblemsDirectory + "problems.inf");
            }

            byte[] fileName = Encoding.ASCII.GetBytes("problems.inf");
            byte[] fileData = File.ReadAllBytes(LocalPath.ProblemsDirectory + "problems.inf");
            byte[] data = new byte[4 + 4 + 4 + fileName.Length + fileData.Length];
            byte[] fileNameLen = BitConverter.GetBytes(fileName.Length);
            BitConverter.GetBytes(data.Length).CopyTo(data, 0);
            BitConverter.GetBytes(RequestCode).CopyTo(data, 4);
            fileNameLen.CopyTo(data, 8);
            fileName.CopyTo(data, 12);
            fileData.CopyTo(data, 12 + fileName.Length);

            Socket.Send(data, data.Length);

            logger.Trace("Send problems info");
            if (logger.IsTraceEnabled)
                Console.WriteLine(DateTime.Now.ToString(culture) + " - Send problems info");
        }
Example #5
0
        private static void SendMainInfo(SocketConnection Socket, Int32 RequestCode)
        {
            Int32 virtProcessorsCount = Environment.ProcessorCount;

            Int32 compilersCount = CompilerSingleton.Instance.AvailablePL.Count;

            byte[] data = new byte[4 + 4 + 4 + 4 + 4 * compilersCount];
            BitConverter.GetBytes(data.Length).CopyTo(data, 0);
            BitConverter.GetBytes(RequestCode).CopyTo(data, 4);
            BitConverter.GetBytes(virtProcessorsCount).CopyTo(data, 8);
            BitConverter.GetBytes(compilersCount).CopyTo(data, 12);

            int i = 1;
            foreach (ProgrammingLanguages pl in CompilerSingleton.Instance.AvailablePL)
            {
                BitConverter.GetBytes((Int32)pl).CopyTo(data, 12 + 4 * i);
                i++;
            }

            Socket.Send(data, data.Length);

            logger.Trace("Send main info");
            if (logger.IsTraceEnabled)
                Console.WriteLine(DateTime.Now.ToString(culture) + " - Send main info");
        }
Example #6
0
        private static void SendCPUUsage(SocketConnection Socket, Int32 RequestCode)
        {
            Int32 cpuUsage = (Int32)cpuCounter.NextValue();
            byte[] data = new byte[4 + 4 + 4];
            BitConverter.GetBytes(4 + 4 + 4).CopyTo(data, 0);
            BitConverter.GetBytes(RequestCode).CopyTo(data, 4);
            BitConverter.GetBytes(cpuUsage).CopyTo(data, 8);

            Socket.Send(data, data.Length);

            logger.Trace("Send cpu usage");
            if (logger.IsTraceEnabled)
                Console.WriteLine(DateTime.Now.ToString(culture) + " - Send cpu usage");
        }
Example #7
0
        static void Main(string[] args)
        {
            //CultureInfo culture = new CultureInfo(ConfigurationManager.AppSettings["DefaultCulture"]);
            //Thread.CurrentThread.CurrentCulture = culture;
            //Thread.CurrentThread.CurrentUICulture = culture;

            List<Tuple<string, string>> commands = new List<Tuple<string, string>>()
                {
                    new Tuple<string, string>("compilers", "show available compilers"),
                    new Tuple<string, string>("exit", "exit from tester")
                };

            SetupRegistry();

            socket = new SocketConnection();
            socket.ReceivedData += socket_ReceivedData;

            string command = "";
            while (true)
            {
                try
                {
                    command = Console.ReadLine();

                    switch (command)
                    {
                        case "compilers":
                            ShowCompilers();
                            break;
                        case "exit":
                            return;
                        case "help":
                            commands.Each(c => { Console.Write(c.Item1); Console.CursorLeft = 20; Console.WriteLine(" - " + c.Item2); });
                            break;
                        case "":
                            Console.CursorTop--;
                            Console.WriteLine(DateTime.Now);
                            break;
                        default:
                            Console.WriteLine(DateTime.Now.ToString(culture) + " - Unknown command: {0}", command);
                            break;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }