Exemple #1
0
        private void LoadCurrentTestAnswer(IAsyncResult ar)
        {
            networkStream.EndRead(ar);

            byte[] dataLoadCurrentTestAnswer = (byte[])ar.AsyncState;

            IFormatter  formatter             = new BinaryFormatter();
            TrueMessage trueMessageFromServer = new TrueMessage();

            using (MemoryStream memoryStream = new MemoryStream(dataLoadCurrentTestAnswer))
            {
                trueMessageFromServer = (TrueMessage)formatter.Deserialize(memoryStream);
            }

            if (trueMessageFromServer.Command == Command.Approve)
            {
                if (trueMessageFromServer.Message == null)
                {
                    MessageBox.Show("Message was null");
                    return;
                }
                KeyValuePair <Test, string> testPair = (KeyValuePair <Test, string>)trueMessageFromServer.Message;
                if (testPair.Key != null)
                {
                    this.Dispatcher.Invoke(() =>
                    {
                        RetryLoadButton.Content    = "Loaded";
                        RetryLoadButton.IsEnabled  = false;
                        RetryLoadButton.Foreground = new SolidColorBrush(Colors.Black);

                        TestNameTextBlock.Text       = testPair.Key.Name;
                        TestNameTextBlock.Visibility = Visibility.Visible;
                    });

                    Test     = testPair.Key;
                    testTime = Convert.ToInt32(testPair.Value);
                }
            }
            else if (trueMessageFromServer.Command == Command.Reject)
            {
                MessageBox.Show("There no test to load");
            }
            else
            {
                MessageBox.Show("Received message from server is incorrect.", "Error");
            }
        }
Exemple #2
0
        private void AddTest(TcpClient tcpClient, TrueMessage trueMessage)
        {
            Console.WriteLine($"AddTest");

            SqlConnection sqlConnection = new SqlConnection(connectionString);

            try
            {
                if (sqlConnection.State == ConnectionState.Closed)
                {
                    sqlConnection.Open();
                }

                TestDLL.Test test = (TestDLL.Test)trueMessage.Message;

                string path    = test.Name;
                string invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());

                foreach (char c in invalid)
                {
                    path = path.Replace(c.ToString(), "");
                }

                path = "..\\..\\..\\Tests\\" + path + ".xml";

                TrueMessage trueMessageToClient = new TrueMessage();

                Random random = new Random();
                while (File.Exists(path))
                {
                    path += random.Next(1, 100);
                }

                TrueXmlSerializer.Save(test, path);

                SqlCommand sqlCommand = new SqlCommand("Proc_AddTest", sqlConnection);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.Parameters.AddWithValue("@Name", test.Name);
                sqlCommand.Parameters.AddWithValue("@Description", test.Description);
                sqlCommand.Parameters.AddWithValue("@Path", path);
                sqlCommand.ExecuteNonQuery();

                trueMessageToClient.Command = Command.Approve;
                trueMessageToClient.Message = test.Name;

                byte[]     dataAddTestAnswer;
                IFormatter formatter = new BinaryFormatter();

                using (MemoryStream stream = new MemoryStream())
                {
                    formatter.Serialize(stream, trueMessageToClient);
                    dataAddTestAnswer = stream.ToArray();
                }

                NetworkStream networkStream = tcpClient.GetStream();

                networkStream.BeginWrite(dataAddTestAnswer, 0, dataAddTestAnswer.Length, new AsyncCallback(AddTestAnswer), tcpClient);

                Console.WriteLine($"AddTest ended");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (sqlConnection.State != ConnectionState.Closed)
                {
                    sqlConnection.Close();
                }
            }
        }
Exemple #3
0
        private void ChooseCurrentTest(TcpClient tcpClient, TrueMessage trueMessage)
        {
            Console.WriteLine($"ChooseCurrentTest");

            SqlConnection sqlConnection = new SqlConnection(connectionString);

            try
            {
                if (sqlConnection.State == ConnectionState.Closed)
                {
                    sqlConnection.Open();
                }

                KeyValuePair <string, string> choosePair = (KeyValuePair <string, string>)trueMessage.Message;

                string testNameToChoose = choosePair.Key;

                SqlCommand sqlCommand = new SqlCommand("Proc_GetTestPathByName", sqlConnection);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.Parameters.AddWithValue("@Name", testNameToChoose);
                string path = Convert.ToString(sqlCommand.ExecuteScalar());

                TrueMessage trueMessageToClient = new TrueMessage();

                if (File.Exists(path))
                {
                    currentTest = TrueXmlSerializer.Load <TestDLL.Test>(path);
                    testTime    = choosePair.Value;

                    trueMessageToClient.Command = Command.Approve;
                }
                else
                {
                    trueMessageToClient.Command = Command.Reject;
                    trueMessageToClient.Message = "Test not found";
                }

                byte[]     dataChooseCurrentTestAnswer;
                IFormatter formatter = new BinaryFormatter();

                using (MemoryStream stream = new MemoryStream())
                {
                    formatter.Serialize(stream, trueMessageToClient);
                    dataChooseCurrentTestAnswer = stream.ToArray();
                }

                NetworkStream networkStream = tcpClient.GetStream();

                networkStream.BeginWrite(dataChooseCurrentTestAnswer, 0, dataChooseCurrentTestAnswer.Length, new AsyncCallback(ChooseCurrentTestAnswer), tcpClient);

                Console.WriteLine($"ChooseCurrentTest ended");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (sqlConnection.State != ConnectionState.Closed)
                {
                    sqlConnection.Close();
                }
            }
        }