public static void GradeExamsAndGenerateReportFromEncrypted(ref RichTextBox rtb, bool addLogs, string studentPass, string instructorPass, SortedList <string, string> encryptedExamFiles, SortedList <string, ExaminationFilterRule> FirewallRules, bool highSecurity) { rtb.Clear(); bool includeLogs = addLogs; foreach (var item in encryptedExamFiles) { if (highSecurity) { if (FirewallRules.ContainsKey(item.Key)) { var examFile = ExamHelper.GetExamFromByteArray(item.Value, studentPass, FirewallRules[item.Key].SharedKeyIS, FilterationSecurityLevel.High); GradeExamQuestion(ref rtb, addLogs, studentPass, instructorPass, examFile); } else { rtb.AppendText($" \n CANNOT GRADE stdID because no shared key available [{item.Key}] \n"); } } else { var examFile = ExamHelper.GetExamFromByteArray(item.Value, studentPass, "", FilterationSecurityLevel.Moderate); GradeExamQuestion(ref rtb, addLogs, studentPass, instructorPass, examFile); } } // var list = examsStatusUpdateList; //var ExamsEncryptedList = encryptedExamFiles.Select(x => x.Value).ToList(); //for (int i = 0; i < ExamsEncryptedList.Count; i++) //{ //} var tst = rtb.Rtf.Replace("[NEW_PAGE_AUTOMATIC_HERE]", "\\par \\page"); SaveFileDialog sdlg = new SaveFileDialog(); sdlg.FileName = $"StudentExamGradesAnswers"; sdlg.Filter = "RTF File (*.rtf)|*.rtf"; sdlg.DefaultExt = "rtf"; sdlg.ShowDialog(); File.WriteAllText(sdlg.FileName, tst); }
private void UpdateDetails(List <ExamStatusUpdate> list) { if (dgview.InvokeRequired) { //MessageBox.Show("Called Invoke"); dgview.BeginInvoke(new Action <List <ExamStatusUpdate> >(UpdateDetails), list); return; } lock (dgview) { dgview.Rows.Clear(); for (int i = 0; i < list.Count; i++) { var index = dgview.Rows.Add(); dgview.Rows[index].Cells["serialCol"].Value = i + 1; var id = list[i].Details.StudentID;; dgview.Rows[index].Cells["idCol"].Value = id; dgview.Rows[index].Cells["nameCol"].Value = list[i].Details.StudentName; dgview.Rows[index].Cells["rcvdCol"].Value = list[i].sent ? "Yes" : "No"; dgview.Rows[index].Cells["submittedCol"].Value = list[i].submitted ? "Yes" : "No"; if (list[i].submitted) { if (checkBox1.Checked) { if (FirewallRules.ContainsKey(list[i].Details.StudentID)) { dgview.Rows[index].Cells["gradeCol"].Value = ExamHelper.GetExamFromByteArray(aService.submittedFiles[id], studentPassTxtBox.Text, list[i].Details.SharedKeyIS, FilterationSecurityLevel.High).GetExamGrade(instructorPassTxtBox.Text); } else { aLogger.LogMessage($"Wrong Exam Submission for stdID [{list[i].Details.StudentID}]", LogMsgType.Error); } } else { dgview.Rows[index].Cells["gradeCol"].Value = ExamHelper.GetExamFromByteArray(aService.submittedFiles[id], studentPassTxtBox.Text, "", FilterationSecurityLevel.Moderate).GetExamGrade(instructorPassTxtBox.Text); } } } } }
private void PerformTest(TestType type, int numOfQ, int numOfStd) { TestParameters submitParam = new TestParameters(); TestParameters receiveParam = new TestParameters(); submitParam.MethodCallCount = int.Parse(numberOfTimesTxtBox.Text); submitParam.NumberOfQuestions = numOfQ; submitParam.NumberOfStudent = numOfStd; receiveParam.MethodCallCount = int.Parse(numberOfTimesTxtBox.Text); receiveParam.NumberOfQuestions = numOfQ; receiveParam.NumberOfStudent = numOfStd; var secucirytLevel = highSecChkBox.Checked ? FilterationSecurityLevel.High : FilterationSecurityLevel.Moderate; var endpoint = new ScsTcpEndPoint(ipTxtBox.Text, int.Parse(portTxtBox.Text)); SortedList <string, string> ExamsReceived = new SortedList <string, string>(); var stopwatchReceiving = Stopwatch.StartNew(); Parallel.For(0, numOfStd, index => { using (var client = ScsServiceClientBuilder.CreateClient <INetworkExamServiceTesting>(endpoint)) { var val = index % numOfStd; var keyUsed = $"Keyyyyyyy{val}"; //STEP 1 -- Only get exam copy client.Connect(); var requiredDetails = new RequiredDetails($"StdName{val}", $"Std{val}", "123456", keyUsed, 1); try { var copy = client.ServiceProxy.GetExamCopyEncryptedZipped(requiredDetails, numOfQ, numOfStd); lock (ExamsReceived) { if (!ExamsReceived.Keys.Contains(keyUsed)) { ExamsReceived.Add(keyUsed, copy); } } } catch (Exception ex) { lock (receiveParam) { receiveParam.DroppedMessages += 1; } } } }); List <Task> tasksReq = new List <Task>(); Parallel.For(0, receiveParam.MethodCallCount - numOfStd, index => // for (int index = 0; index < param.MethodCallCount; index++) { var t = Task.Run(() => { using (var client = ScsServiceClientBuilder.CreateClient <INetworkExamServiceTesting>(endpoint)) { var val = index % numOfStd; var keyUsed = $"Keyyyyyyy{val}"; //STEP 1 -- Only get exam copy client.Connect(); var requiredDetails = new RequiredDetails($"StdName{val}", $"Std{val}", "123456", keyUsed, 1); try { var copy = client.ServiceProxy.GetExamCopyEncryptedZipped(requiredDetails, numOfQ, numOfStd); } catch (Exception ex) { lock (receiveParam) { receiveParam.DroppedMessages += 1; } } } }); lock (tasksReq) { tasksReq.Add(t); } } ); Task.WaitAll(tasksReq.ToArray()); stopwatchReceiving.Stop(); receiveParam.TimeRequired = stopwatchReceiving; ReceivingResults.Add(receiveParam); SortedList <string, KeyValuePair <RequiredDetails, string> > ExamsToSubmit = new SortedList <string, KeyValuePair <RequiredDetails, string> >(); if (type == TestType.SubmitExam || type == TestType.Both) // we need to answer and submit aswell { // List<KeyValuePair<RequiredDetails, string>> ExamsToSubmit = new List<KeyValuePair<RequiredDetails, string>>(); Parallel.For(0, numOfStd, index => { var val = index % numOfStd; var keyUsed = $"Keyyyyyyy{val}"; //STEP 2 : Answering the EXAM (assuming all students will have the same answers) var examCopyUnencrypted = ExamHelper.GetExamFromByteArray(ExamsReceived[keyUsed], "123456", keyUsed, secucirytLevel); examCopyUnencrypted.ExamLog = new List <string>(); examCopyUnencrypted.RequiredStudentDetails.SequenceNumber = (long.Parse(examCopyUnencrypted.RequiredStudentDetails.SequenceNumber) + 1).ToString(); var rD = examCopyUnencrypted.RequiredStudentDetails; var examEncrypted = ExamHelper.GetExamFileWithoutSave(examCopyUnencrypted, "123456", keyUsed, secucirytLevel); rD.EncryptDetails(); lock (ExamsToSubmit) { if (!ExamsToSubmit.Keys.Contains(keyUsed)) { ExamsToSubmit.Add(keyUsed, new KeyValuePair <RequiredDetails, string>(rD, examEncrypted)); } } }); ExamsReceived = null; var stopwatchSubmitting = Stopwatch.StartNew(); //step 3 Submit (we want to measure only this List <Task> tasks = new List <Task>(); Parallel.For(0, submitParam.MethodCallCount, index => { var t = Task.Run(() => { var val = index % numOfStd; var keyUsed = $"Keyyyyyyy{val}"; try { using (var clientSubmit = ScsServiceClientBuilder.CreateClient <INetworkExamServiceTesting>(endpoint)) { clientSubmit.Connect(); clientSubmit.ServiceProxy.SubmitExamEncryptedZipped(ExamsToSubmit[keyUsed].Value, ExamsToSubmit[keyUsed].Key, numOfQ, numOfStd); } } catch (Exception ex) { lock (submitParam) { submitParam.DroppedMessages += 1; } } }); lock (tasks) { tasks.Add(t); } }); Task.WaitAll(tasks.ToArray()); stopwatchSubmitting.Stop(); submitParam.TimeRequired = stopwatchSubmitting; SubmissionResults.Add(submitParam); } }