private static void Sleep(double seconds) { int millis = (int)(1000 * seconds); while (millis > 0) { ThreadSafeConsoleWriter.Flush(); if (millis > 50) { millis -= 50; System.Threading.Thread.Sleep(50); } else { System.Threading.Thread.Sleep(millis); millis = 0; } } }
public void Run() { while (true) { string token = this.queue.PopWorkItem(); if (token == null) { System.Threading.Thread.Sleep(100); } else { HttpRequest request = new HttpRequest("POST", "http://np10.nfshost.com/autograder/graderpoll/claim", new Dictionary <string, string>() { { "key", Util.HashWithSecret(token) }, { "token", token }, }); ThreadSafeConsoleWriter.Print("[" + token + "] Claiming..."); request.Send(); if (request.ResponseCode == 200) { string[] data = request.ResponseBody.Trim().Split(','); if (data[0] == "OK" && data.Length == 10) { string language = Util.HexToString(data[1]); string code = Util.HexToString(data[2]); string callback = Util.HexToString(data[3]); string testFunctionName = Util.HexToString(data[4]); string expectedArgCount = Util.HexToString(data[5]); string[] argTypes = Util.HexToString(data[6]).Split('|'); string returnType = Util.HexToString(data[7]); string testJson = Util.HexToString(data[8]); string feature = Util.HexToString(data[9]); List <object> testInput = null; List <object> expectedOutput = null; Dictionary <string, object> tests = JsonParser.ParseJsonIntoValue(testJson) as Dictionary <string, object>; if (tests != null && tests.ContainsKey("input") && tests.ContainsKey("output")) { testInput = tests["input"] as List <object>; expectedOutput = tests["output"] as List <object>; } ThreadSafeConsoleWriter.Print("[" + token + "] running " + language + " entry for " + feature + "."); switch (language) { case "crayon": this.RunCrayonCode(token, code, callback, testFunctionName, expectedArgCount, testInput, expectedOutput, returnType, argTypes, feature); break; case "python": this.RunPythonCode(token, code, callback, testFunctionName, expectedArgCount, testInput, expectedOutput, returnType, argTypes, feature); break; default: break; } ThreadSafeConsoleWriter.Print("[" + token + "] finished."); } else if (data[0] == "ERR" && data.Length > 1) { if (data[1] == "CONFLICT") { ThreadSafeConsoleWriter.Print("[" + token + "] already claimed!"); } else { ThreadSafeConsoleWriter.Print("[" + token + "] unknown server response."); } } else { ThreadSafeConsoleWriter.Print("[" + token + "] unknown server response."); } } } } }