Example #1
0
        public Station(
            Constants.StationId stationType,
            ITransmitter transmitter,
            int bufferSize,
            int timeoutInMs,
            bool detectionOnly,
            FileStream inputFileStream,
            FileStream outputFileStream,
            Constants.ShowFrameDelegate sendFrame)
        {
            this.StationId         = stationType;
            this.transmitter       = transmitter;
            this.BufferSize        = bufferSize;
            InputBuffer            = new Dictionary <int, Frame>();
            OutputBuffer           = new Dictionary <int, Frame>();
            this.TimeoutInMs       = timeoutInMs;
            this.CorrectionMode    = detectionOnly ? HammingHelper.Mode.DETECT : HammingHelper.Mode.CORRECT;
            this.inputFileStream   = inputFileStream;
            this.outputFileStream  = outputFileStream;
            this.sendFrameDelegate = sendFrame;
            StopExecution          = false;
            ExecutionStopped       = new ManualResetEvent(false);

            // Initialize constants
            int frameSizeBeforeHamming = HammingHelper.GetDataSize(Constants.FrameSize * 8) / 8;
            int realBitCount           = HammingHelper.GetTotalSize(frameSizeBeforeHamming * 8);

            EncodedFramePadding = Constants.FrameSize * 8 - realBitCount;
            int frameHeaderSize = Frame.HeaderSize();

            DataSizeInFrame = frameSizeBeforeHamming - frameHeaderSize;
            MaxSequence     = (UInt16)(bufferSize * 2 - 1);

            // Initialize fields
            FirstFrameSent = 0;
            NextFrameToSendSequenceNumber = 0;
            NextFrameToReceive            = 0;

            // Since frames cannot use MaxSequence as a sequence number, we use it for LastFrameSequenceNumberForNak.
            // This is meant to ensure that no Frame matches this field if there was no Nak sent.
            LastFrameSequenceNumberForNak = MaxSequence;

            // Initialise ack timer logic
            AckTimer = new System.Timers.Timer(AckTimeout);
            // When the timer ends, we need to inform the program that we need to send an Ack now. Also stop the timer.
            AckTimer.Elapsed += (sender, e) => { sendAck = true; AckTimer.Stop(); Console.WriteLine("ack timer elapsed"); };
            sendAck           = false;

            HighPriorityFrames = new Queue <Frame>();
            TimeoutTimers      = new ConcurrentDictionary <UInt16, System.Timers.Timer>();
        }
Example #2
0
 public void TestGetDataSize()
 {
     Assert.AreEqual(8, HammingHelper.GetDataSize(13));
     Assert.AreEqual(16, HammingHelper.GetDataSize(26));
 }