Example #1
0
        public static void WriteToConsoleArray(string info, BitArray array)
        {
            lock (LockObject)
            {
                Console.Write(info + " : ");

                if (array.Length == 1)
                {
                    if (array[0] == true)
                    {
                        Console.Write("1");
                    }
                    else
                    {
                        Console.Write("0");
                    }
                }

                else
                {
                    string str = FrameHelper.BitArrayToString(array);

                    Console.Write(str);
                }

                Console.WriteLine();
            }
        }
Example #2
0
        public void SplitDataOnFragments()
        {
            const int frameSizeInBits = 16;

            BitArray data = GenerateSomeData();

            BitArray[] dataFragments;

            if (data.Length % frameSizeInBits == 0)
            {
                dataFragments = new BitArray[data.Length / frameSizeInBits];
            }

            else if (data.Length < frameSizeInBits)
            {
                dataFragments = new BitArray[1];
            }

            //when data.Length % frameSize == 0 && data.Length > frameSize
            else
            {
                dataFragments = new BitArray[(data.Length / frameSizeInBits) + 1];
            }

            for (int fragmentInd = 0; fragmentInd < dataFragments.Length; fragmentInd++)
            {
                if (data.Length % frameSizeInBits == 0)
                {
                    dataFragments[fragmentInd] = new BitArray(frameSizeInBits);
                }
                else
                {
                    int newFrameSize = dataFragments.Length % frameSizeInBits;
                    dataFragments[fragmentInd] = new BitArray(newFrameSize);
                }
            }

            for (int fragmentInd = 0; fragmentInd < dataFragments.Length; fragmentInd++)
            {
                BitArrayCopy(data, fragmentInd * frameSizeInBits, dataFragments[fragmentInd]);
            }

            _sentFrames = new BitArray[dataFragments.Length];

            for (int frameInd = 0; frameInd < _sentFrames.Length; frameInd++)
            {
                _sentFrames[frameInd] = FrameHelper.FillFrame(dataFragments[frameInd], frameInd);
            }
        }
Example #3
0
        public static bool ValidData(BitArray frame)
        {
            BitArray receivedData   = FrameHelper.GetFrameData(frame);
            BitArray expectedParity = GetExpectedFrameParity(frame);
            BitArray receivedParity = CalculateFrameParity(receivedData);

            for (int i = 0; i < receivedParity.Length; i++)
            {
                if (receivedParity[i] != expectedParity[i])
                {
                    return(false);
                }
            }

            return(true);
        }
Example #4
0
        public void SendReceiptToFirstSt(Object obj)
        {
            Stopwatch framesReceptionWatch = new Stopwatch();

            _postReceipt = (SecondStPostToFirstStWt)obj;
            _sentReceipt = new BitArray(1);

            framesReceptionWatch.Start();
            _signalFromSecondBuffer.WaitOne();
            framesReceptionWatch.Stop();
            TimeSpan framesReceptionTime = framesReceptionWatch.Elapsed;

            if (framesReceptionTime.Milliseconds > 40)
            {
                _sentReceipt[0] = false;
                ConsoleHelper.WriteToConsole("станция 2", "отправляю REJ-запрос");
            }

            else
            {
                _sentReceipt[0] = true;
                for (int i = 0; i < _receivedFrames.Length; i++)
                {
                    Random rnd = new Random();

                    bool dataIsValid = (rnd.Next(1, 20) % 6) != 0;

                    if (dataIsValid == true)
                    {
                        ConsoleHelper.WriteToConsoleArray("станция 2 полученный кадр №" + FrameHelper.getIntFromBitArray(FrameHelper.GetBinaryFrameNumber(_receivedFrames[i])),
                                                          FrameHelper.GetFrameData(_receivedFrames[i]));

                        string fileRow = FrameHelper.BitArrayToString(FrameHelper.GetFrameData(_receivedFrames[i]));
                        WriteDataToStFile(fileRow, true);
                    }
                    else
                    {
                        ConsoleHelper.WriteToConsole("станция 2 полученный кадр №" + FrameHelper.getIntFromBitArray(FrameHelper.GetBinaryFrameNumber(_receivedFrames[i])),
                                                     "данные повреждены");
                        ConsoleHelper.WriteToConsole("станция 2", "отправляю REJ-запрос");
                        frameIsMissed = true;
                        //clear file data
                        WriteDataToStFile("", false);
                        _signalToFirstSt.Release();
                        break;
                    }
                }
            }

            _postReceipt(_sentReceipt);

            if (frameIsMissed == true)
            {
                _signalFromSecondBuffer.WaitOne();
                for (int i = 0; i < _receivedFrames.Length; i++)
                {
                    ConsoleHelper.WriteToConsoleArray("станция 2 полученный кадр №" + FrameHelper.getIntFromBitArray(FrameHelper.GetBinaryFrameNumber(_receivedFrames[i])),
                                                      FrameHelper.GetFrameData(_receivedFrames[i]));

                    string fileRow = FrameHelper.BitArrayToString(FrameHelper.GetFrameData(_receivedFrames[i]));
                    WriteDataToStFile(fileRow, true);
                }
            }

            ConsoleHelper.WriteToConsole("станция 2", "отправил RR-ответ станции 1");
            _signalToFirstSt.Release();

            Stopwatch framesReceptionWatch2 = new Stopwatch();

            _sentReceipt = new BitArray(1);

            framesReceptionWatch2.Start();

            _signalFromSecondBuffer.WaitOne();
            framesReceptionWatch2.Stop();
            TimeSpan framesReceptionTime2 = framesReceptionWatch2.Elapsed;

            if (framesReceptionTime2.Milliseconds > 40)
            {
                _sentReceipt[0] = false;
                ConsoleHelper.WriteToConsole("станция 2", "Данные не получены");
            }

            else
            {
                _sentReceipt[0] = true;
                for (int i = 0; i < _receivedFrames.Length; i++)
                {
                    if (ValidData(_receivedFrames[i]) == true)
                    {
                        ConsoleHelper.WriteToConsoleArray("станция 2 полученный кадр №" + FrameHelper.getIntFromBitArray(FrameHelper.GetBinaryFrameNumber(_receivedFrames[i])),
                                                          FrameHelper.GetFrameData(_receivedFrames[i]));

                        string fileRow = FrameHelper.BitArrayToString(FrameHelper.GetFrameData(_receivedFrames[i]));
                        WriteDataToStFile(fileRow, true);
                    }
                    else
                    {
                        ConsoleHelper.WriteToConsole("станция 2 полученный кадр №" + FrameHelper.getIntFromBitArray(FrameHelper.GetBinaryFrameNumber(_receivedFrames[i])),
                                                     "данные повреждены");
                    }
                }
            }

            _postReceipt(_sentReceipt);
            ConsoleHelper.WriteToConsole("станция 2", "отправил RNR-ответ станции 1");

            _signalToFirstSt.Release();
        }
Example #5
0
        public void SendReceiptToSecondSt(Object obj)
        {
            Stopwatch framesReceptionWatch = new Stopwatch();

            _postReceipt = (FirstStPostToSecondStWt)obj;
            _sentReceipt = new BitArray(1);

            framesReceptionWatch.Start();
            _signalFromFirstBuffer.WaitOne();
            framesReceptionWatch.Stop();
            TimeSpan framesReceptionTime = framesReceptionWatch.Elapsed;

            if (framesReceptionTime.Milliseconds > 40)
            {
                _sentReceipt[0] = false;
                ConsoleHelper.WriteToConsole("станция 1", "Данные не получены");
            }

            else
            {
                _sentReceipt[0] = true;
                for (int i = 0; i < _receivedFrames.Length; i++)
                {
                    if (ValidData(_receivedFrames[i]) == true)
                    {
                        ConsoleHelper.WriteToConsoleArray("станция 1 полученный кадр №" + FrameHelper.getIntFromBitArray(FrameHelper.GetBinaryFrameNumber(_receivedFrames[i])),
                                                          FrameHelper.GetFrameData(_receivedFrames[i]));

                        string fileRow = FrameHelper.BitArrayToString(FrameHelper.GetFrameData(_receivedFrames[i]));
                        WriteDataToStFile(fileRow, true);
                    }
                    else
                    {
                        ConsoleHelper.WriteToConsole("станция 1 полученный кадр №" + FrameHelper.getIntFromBitArray(FrameHelper.GetBinaryFrameNumber(_receivedFrames[i])), "данные повреждены");
                    }
                }
            }

            _postReceipt(_sentReceipt);
            ConsoleHelper.WriteToConsole("станция 1", "отправил RR-ответ станции 2");
            _signalToSecondSt.Release();

            Stopwatch framesReceptionWatch2 = new Stopwatch();

            _sentReceipt = new BitArray(1);
            framesReceptionWatch2.Start();
            _signalFromFirstBuffer.WaitOne();
            framesReceptionWatch2.Stop();
            TimeSpan framesReceptionTime2 = framesReceptionWatch2.Elapsed;


            if (framesReceptionTime2.Milliseconds > 40)
            {
                _sentReceipt[0] = false;
                ConsoleHelper.WriteToConsole("станция 1", "Данные не получены");
            }

            else
            {
                _sentReceipt[0] = true;
                for (int i = 0; i < _receivedFrames.Length; i++)
                {
                    if (ValidData(_receivedFrames[i]) == true)
                    {
                        ConsoleHelper.WriteToConsoleArray("станция 1 полученный кадр №" + FrameHelper.getIntFromBitArray(FrameHelper.GetBinaryFrameNumber(_receivedFrames[i])),
                                                          FrameHelper.GetFrameData(_receivedFrames[i]));

                        string fileRow = FrameHelper.BitArrayToString(FrameHelper.GetFrameData(_receivedFrames[i]));
                        WriteDataToStFile(fileRow, true);
                    }
                    else
                    {
                        ConsoleHelper.WriteToConsole("станция 1 полученный кадр №" + FrameHelper.getIntFromBitArray(FrameHelper.GetBinaryFrameNumber(_receivedFrames[i])), "данные повреждены");
                    }
                }
            }

            _postReceipt(_sentReceipt);
            ConsoleHelper.WriteToConsole("станция 1", "отправил RNR-ответ станции 2");
            _signalToSecondSt.Release();
        }