//public static long result_value; public static void Calculation(int[] val_1, int[] val_2, BitArray bitArray /* odebrany BIArray*/) { if (bitArray[0] == false && bitArray[1] == true && bitArray[2] == false && bitArray[3] == false && bitArray[4] == false && val_2[0] == 0) { sendingErrorAboutDivisionByZero(bitArray); } else { long result_value = 0; SessionClass.session(bitArray); //Nadanie sesji if (bitArray[2] == false && bitArray[3] == false && bitArray[4] == false) //Operacje + - * / { if (bitArray[0] == false && bitArray[1] == false) { result_value = multiOperation(val_1[0], val_2[0]); } else if (bitArray[0] == false && bitArray[1] == true) { result_value = diviOperation(val_1[0], val_2[0]); } else if (bitArray[0] == true && bitArray[1] == false) { result_value = addOperation(val_1[0], val_2[0]); } else if (bitArray[0] == true && bitArray[1] == true) { result_value = subOperation(val_1[0], val_2[0]); if (val_1[0] < val_2[0]) { bitArray[3] = true; } } } else if (bitArray[2] == false && bitArray[3] == false && bitArray[4] == true) //Silnia { result_value = factorialOperation(val_1[0]); } if (result_value < 0) { result_value *= -1; bitArray[4] = true; } else if (result_value > int.MaxValue && (bitArray[0] == false && bitArray[1] == false || bitArray[0] == true && bitArray[1] == false)) { bitArray[2] = true; bitArray[3] = true; bitArray[4] = true; result_value = 0; } FeedbackMessage.feedback((int)result_value, bitArray); } }
static void sendingErrorAboutDivisionByZero(BitArray recvBitArray) { BitArray divisionByZero = new BitArray(80, false); divisionByZero[1] = divisionByZero[2] = divisionByZero[3] = divisionByZero[4] = true; //kodowanie błedu 01 111 divisionByZero[65] = divisionByZero[67] = divisionByZero[68] = true; //kodowanie długości bitów (11) SessionClass.session(recvBitArray); //Nadanie sesji for (int i = recvBitArray.Count - 11, j = divisionByZero.Count - 11; i < recvBitArray.Count && j < divisionByZero.Count; i++, j++) { divisionByZero[j] = recvBitArray[i]; } byte[] sendByteToTheClient = new byte[divisionByZero.Count / 8]; sendByteToTheClient = CoddingClass.CoddingBitToByteArray(divisionByZero); SendClass.SendMessageToTheClient(sendByteToTheClient); //Wysłanie wiadomośći do klineta Console.WriteLine("Wiadomosć została wysłana"); }
public static void SendHistory(BitArray recvBit /*MA WARTOŚĆ 80*/) { int check = 0; SessionClass.session(recvBit); for (int i = 0, j = recvBit.Count - 11; i < SessionClass.SessionCheck.Count && j < recvBit.Count; i++, j++) { if (SessionClass.SessionCheck[i] == recvBit[j]) { check++; } } if (history_count == 0 /* || check>0*/) { BitArray sendHistoryToTheClient = new BitArray(80, false); sendHistoryToTheClient[2] = sendHistoryToTheClient[3] = true; //Bład historii - historia pusta sendHistoryToTheClient[65] = sendHistoryToTheClient[67] = sendHistoryToTheClient[68] = true; for (int i = 69; i < sendHistoryToTheClient.Count; i++) { sendHistoryToTheClient[i] = recvBit[i]; } //WriteClass.WriteBitTab(sendHistoryToTheClient); byte[] sendByteToTheClient = new byte[sendHistoryToTheClient.Count / 8]; sendByteToTheClient = CoddingClass.CoddingBitToByteArray(sendHistoryToTheClient); SendClass.SendMessageToTheClient(sendByteToTheClient); //Wysyła tablice 80 bitów } else if (history_count > 0) //historia z użyciem id sesji { int sizeOfData = 0; int howMuch = 0; int test = 0; if (recvBit[0] == false && recvBit[1] == false && recvBit[2] == false && recvBit[3] == true && recvBit[4] == true) //Historia z użyciem id Sesji { for (int i = 0; i < history_count; i++) //Sprawzamy ile elementów odpowiada warunkowi { for (int j = 0, k = 69; j < history[i].session.Count && k < recvBit.Count; j++, k++) { if (history[i].session[j] == recvBit[k]) { test++; } } } howMuch = test / 11; HistoryClass[] historyToSend = new HistoryClass[howMuch]; for (int i = 0, l = 0; i < history_count && l < howMuch; i++) { test = 0; for (int j = 0, k = 69; j < history[i].session.Count && k < recvBit.Count; j++, k++) { if (history[i].session[j] == recvBit[k]) { test++; } } if (test == 11) { historyToSend[l] = new HistoryClass(); for (int a = 0; a < historyToSend[l].operation_ID.Count; a++) //Kopia ID operacji { historyToSend[l].operation_ID[a] = history[i].operation_ID[a]; } for (int a = 0; a < historyToSend[l].variable_1.Count; a++) //Kopia zmiennej 1 { historyToSend[l].variable_1[a] = history[i].variable_1[a]; } for (int a = 0; a < historyToSend[l].variable_2.Count; a++) //Kopia zmiennej 2 { historyToSend[l].variable_2[a] = history[i].variable_2[a]; } for (int a = 0; a < historyToSend[l].result.Count; a++) //Kopia wyniku { historyToSend[l].result[a] = history[i].result[a]; } for (int a = 0; a < historyToSend[l].session.Count; a++) //Kopia sesji { historyToSend[l].session[a] = history[i].session[a]; } l++; //inkrementacja pozycji w tablicy do wysłania } } //Zakodowanie wiadomości do klienta BitArray sendSizeHistoryToTheClient = new BitArray(112, false); //Tablica posiadająca rozmiar danych temp[0] = howMuch; BitArray numbersOfStories = new BitArray(temp); //Tworzenie BItArray z wynikiem działania numbersOfStories = ReverseClass.reverseOrder(numbersOfStories); sendSizeHistoryToTheClient[2] = sendSizeHistoryToTheClient[4] = true; //Zakodowanie ilości plików w historii //Kodowanie długości bitów sendSizeHistoryToTheClient[63] = sendSizeHistoryToTheClient[65] = sendSizeHistoryToTheClient[67] = sendSizeHistoryToTheClient[68] = true; for (int i = 69, j = 0; i < sendSizeHistoryToTheClient.Count - 11 && j < numbersOfStories.Count; i++, j++) { sendSizeHistoryToTheClient[i] = numbersOfStories[j]; } //Kodowanie sesji for (int i = 101, j = 69; i < sendSizeHistoryToTheClient.Count && j < recvBit.Count; i++, j++) { sendSizeHistoryToTheClient[i] = recvBit[j]; } byte[] sendByteToTheClient = new byte[sendSizeHistoryToTheClient.Count / 8]; //Stworzenie tablicy byte to wysylki sendByteToTheClient = CoddingClass.CoddingBitToByteArray(sendSizeHistoryToTheClient); SendClass.SendMessageToTheClient(sendByteToTheClient); BitArray sendHistoryFileToTheClient = new BitArray(176, false); //Wysyłane dane //Kodowanie długości bitów sendHistoryFileToTheClient[62] = sendHistoryFileToTheClient[63] = sendHistoryFileToTheClient[65] = sendHistoryFileToTheClient[67] = sendHistoryFileToTheClient[68] = true; //Wysyłanie wiadomości do klineta byte[] FinishHistryToSend = new byte[176 / 8]; for (int a = 0; a < howMuch; a++) { for (int i = 0, j = 0; i < 2 && j < historyToSend[a].operation_ID.Count; i++, j++) //ID OPERACJI { sendHistoryFileToTheClient[i] = historyToSend[a].operation_ID[j]; } for (int i = 69, j = 0; i < 101 && j < historyToSend[a].variable_1.Count; i++, j++) //VAL 1 { sendHistoryFileToTheClient[i] = historyToSend[a].variable_1[j]; } for (int i = 101, j = 0; i < 133 && j < historyToSend[a].variable_2.Count; i++, j++) //VAL 2 { sendHistoryFileToTheClient[i] = historyToSend[a].variable_2[j]; } for (int i = 133, j = 0; i < 165 && j < historyToSend[a].result.Count; i++, j++) //result { sendHistoryFileToTheClient[i] = historyToSend[a].result[j]; } for (int i = 165, j = 0; i < 176 && j < historyToSend[a].session.Count; i++, j++) //sesja { sendHistoryFileToTheClient[i] = historyToSend[a].session[j]; } sendHistoryFileToTheClient[3] = sendHistoryFileToTheClient[4] = true; FinishHistryToSend = CoddingClass.CoddingBitToByteArray(sendHistoryFileToTheClient); SendClass.SendMessageToTheClient(FinishHistryToSend); } } else if (recvBit[2] == true && recvBit[3] == false && recvBit[4] == false) //historia z użyciem id obliczen { for (int i = 0; i < history_count; i++) //Sprawzamy ile elementów odpowiada warunkowi { for (int j = 0, k = 69; j < history[i].session.Count && k < recvBit.Count; j++, k++) { if (history[i].session[j] == recvBit[k]) { if (history[i].operation_ID[0] == recvBit[0] && history[i].operation_ID[1] == recvBit[1]) { if (recvBit[0] == false && recvBit[1] == false) { if (checkMultiply(history[i]) == true) { test++; } } else { test++; } } } //if (recvBit[0] == false && recvBit[1] == false) // if (checkMultiply(history[i]) == false) // continue; } } howMuch = test / 11; HistoryClass[] historyToSend = new HistoryClass[howMuch]; for (int i = 0, l = 0; i < history_count && l < howMuch; i++) { test = 0; for (int j = 0, k = 69; j < history[i].session.Count && k < recvBit.Count; j++, k++) { if (history[i].session[j] == recvBit[k]) { if (history[i].operation_ID[0] == recvBit[0] && history[i].operation_ID[1] == recvBit[1]) { test++; } } } if (test == 11) { if (recvBit[0] == false && recvBit[1] == false) { if (checkMultiply(history[i]) == false) { continue; } } historyToSend[l] = new HistoryClass(); for (int a = 0; a < historyToSend[l].operation_ID.Count; a++) //Kopia ID operacji { historyToSend[l].operation_ID[a] = history[i].operation_ID[a]; } for (int a = 0; a < historyToSend[l].variable_1.Count; a++) //Kopia zmiennej 1 { historyToSend[l].variable_1[a] = history[i].variable_1[a]; } for (int a = 0; a < historyToSend[l].variable_2.Count; a++) //Kopia zmiennej 2 { historyToSend[l].variable_2[a] = history[i].variable_2[a]; } for (int a = 0; a < historyToSend[l].result.Count; a++) //Kopia wyniku { historyToSend[l].result[a] = history[i].result[a]; } for (int a = 0; a < historyToSend[l].session.Count; a++) //Kopia sesji { historyToSend[l].session[a] = history[i].session[a]; } l++; //inkrementacja pozycji w tablicy do wysłania } } //Zakodowanie wiadomości do klienta BitArray sendSizeHistoryToTheClient = new BitArray(112, false); //Tablica posiadająca rozmiar danych temp[0] = howMuch; BitArray numbersOfStories = new BitArray(temp); //Tworzenie BItArray z wynikiem działania numbersOfStories = ReverseClass.reverseOrder(numbersOfStories); sendSizeHistoryToTheClient[2] = sendSizeHistoryToTheClient[4] = true; //Zakodowanie ilości plików w historii sendSizeHistoryToTheClient[63] = sendSizeHistoryToTheClient[65] = sendSizeHistoryToTheClient[67] = sendSizeHistoryToTheClient[68] = true; //Zakodowanie długości bitów for (int i = 69, j = 0; i < sendSizeHistoryToTheClient.Count - 11 && j < numbersOfStories.Count; i++, j++) { sendSizeHistoryToTheClient[i] = numbersOfStories[j]; } //Kodowanie sesji for (int i = 101, j = 69; i < sendSizeHistoryToTheClient.Count && j < recvBit.Count; i++, j++) { sendSizeHistoryToTheClient[i] = recvBit[j]; } byte[] sendByteToTheClient = new byte[sendSizeHistoryToTheClient.Count / 8]; //Stworzenie tablicy byte to wysylki sendByteToTheClient = CoddingClass.CoddingBitToByteArray(sendSizeHistoryToTheClient); SendClass.SendMessageToTheClient(sendByteToTheClient); BitArray sendHistoryFileToTheClient = new BitArray(176, false); //Wysyłane dane //Wysyłanie wiadomości do klineta byte[] FinishHistryToSend = new byte[176 / 8]; //Kodowanie długości bitów sendHistoryFileToTheClient[62] = sendHistoryFileToTheClient[63] = sendHistoryFileToTheClient[65] = sendHistoryFileToTheClient[67] = sendHistoryFileToTheClient[68] = true; for (int a = 0; a < howMuch; a++) { for (int i = 0, j = 0; i < 2 && j < historyToSend[a].operation_ID.Count; i++, j++) //ID OPERACJI { sendHistoryFileToTheClient[i] = historyToSend[a].operation_ID[j]; } for (int i = 69, j = 0; i < 101 && j < historyToSend[a].variable_1.Count; i++, j++) //VAL 1 { sendHistoryFileToTheClient[i] = historyToSend[a].variable_1[j]; } for (int i = 101, j = 0; i < 133 && j < historyToSend[a].variable_2.Count; i++, j++) //VAL 2 { sendHistoryFileToTheClient[i] = historyToSend[a].variable_2[j]; } for (int i = 133, j = 0; i < 165 && j < historyToSend[a].result.Count; i++, j++) //result { sendHistoryFileToTheClient[i] = historyToSend[a].result[j]; } for (int i = 165, j = 0; i < 176 && j < historyToSend[a].session.Count; i++, j++) //sesja { sendHistoryFileToTheClient[i] = historyToSend[a].session[j]; } sendHistoryFileToTheClient[2] = true; sendHistoryFileToTheClient[3] = false; sendHistoryFileToTheClient[4] = false; FinishHistryToSend = CoddingClass.CoddingBitToByteArray(sendHistoryFileToTheClient); SendClass.SendMessageToTheClient(FinishHistryToSend); } } } }