/// <summary> /// Poziva se kad stigne nesto sto nije od ovog gore navedenog /// </summary> /// <param name="e"></param> protected void OnUnknownReceived(ProtocolReceiverEventArgs e) { log.Debug("Protocol:OnUnknownReceived"); try { if (UnknownReceived != null) UnknownReceived(this, e); } catch (Exception ex) { log.Error(ex.ToString()); throw; } }
/// <summary> /// Handler koji se poziva kad stigne poruka koja nije niti ENQ, niti ACK, NAck ... /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void UnknownReceiveHandler(object sender, ProtocolReceiverEventArgs e) { try { log.Debug("IdleState:UnknownReceiveHandler"); ((Protocol)sender).AckTimer.Stop(); ((Protocol)sender).AckTimer.Elapsed -= _timerHandler; if (_message_sent == true) { ((Protocol)sender).AckTimer.Interval = Timeout.UnknownTimeout; _timerHandler = new ElapsedEventHandler(bind<object, ElapsedEventArgs, Protocol>(TimeoutUnknownHandler, (Protocol)sender)); ((Protocol)sender).AckTimer.Elapsed += _timerHandler; ((Protocol)sender).AckTimer.Start(); } } catch (Exception ex) { log.Error(ex.ToString()); throw; } }
/// <summary> /// Handler koji se poziva kad stigne ENQ od uredjaja /// Buduci da uredjaj uvijek ima prednost trebali bi mu odgovorit s ACK-om /// Ukoliko mi vec pokusavamo poslat poruku onda bi tu poruku trebali spremit i poslat je poslije /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void EnqReceiveHandler(object sender, ProtocolReceiverEventArgs e) { try { log.Debug("IdleState:EnqReceiveHandler"); ((Protocol)sender).AckTimer.Stop(); ((Protocol)sender).AckTimer.Elapsed -= _timerHandler; if (((Protocol)sender).Entity == Entity.Cobas && _message_sent == true) { ((Protocol)sender).AckTimer.Interval = Timeout.EnqTimeOut; _timerHandler = new ElapsedEventHandler(bind<object, ElapsedEventArgs, Protocol>(TimeoutEnqHandler, (Protocol)sender)); ((Protocol)sender).AckTimer.Elapsed += _timerHandler; ((Protocol)sender).AckTimer.Start(); } else { if (_message_sent == true) { _pending_messages.Add(_message); ((Protocol)sender).AckTimer.Stop(); ((Protocol)sender).AckTimer.Elapsed -= _timerHandler; } else { ((Protocol)sender).ChangeState(Receiver); } ((Protocol)sender).SendAck(); CleanState(); } } catch (Exception ex) { log.Error(ex.ToString()); throw; } }
/// <summary> /// Handler koji se poziva kad stigne ACK od uredaja. /// U idle state-u to znaci da je uredja poslao ACK na nas ENQ /// Mi onda stopiramo timer, prelazimo u stanje sender i saljemo poruku /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AckReceiveHandler(object sender, ProtocolReceiverEventArgs e) { try { log.Debug("IdleState:AckReceiveHandler"); ((Protocol)sender).AckTimer.Stop(); ((Protocol)sender).AckTimer.Elapsed -= _timerHandler; if (_message_sent == true) { ((Protocol)sender).ChangeState(Sender); ((Protocol)sender).State.Send(((Protocol)sender), _message); CleanState(); } } catch (Exception ex) { log.Error(ex.ToString()); throw; } }
/// <summary> /// Ovaj handler se pozove kada stigne frame. /// Trenutno jedini nacin da znam da li je nesto sto je stiglo na seriju/ulazni queue frame /// je da gledam da li je prvi karakter STX /// Ako je stigo frame onda provjeravam: /// - checksum /// - da li je frame number ispravan (ocekivani frame nuber) /// - da li sadrzi ETB ili ETX karakter, ovori da li je ovo middle frame ili end frame /// Ukoliko sve ovo prodje onda sastavljam frameove (ako ih ima vise) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FrameReceiveHandler(object sender, ProtocolReceiverEventArgs e) { try { log.Debug("ReceiverSate:FrameReceiveHandler"); string frame_string = e.Received; Frame frame = Frame.Create(frame_string); if (frame.Checksum != Frame.CalculateChecksum(frame).ToString("X2")) { log.WarnFormat("FrameReceiveHandler:Invalid checksum: {0} {1}", frame.Checksum, Frame.CalculateChecksum(frame).ToString("X2")); ((Protocol)sender).SendNack(); } else if(_expected_frame_number != frame.FrameNumber) { log.WarnFormat("FrameReceiveHandler:Invalid frame number: {0} {1}", frame.FrameNumber, _expected_frame_number); ((Protocol)sender).SendNack(); } else if (frame.End != SpecialCharacters.ETX && frame.End != SpecialCharacters.ETB) { log.Warn("FrameReceiveHandler:end char is missing"); ((Protocol)sender).SendNack(); } else { if (frame.End == SpecialCharacters.ETB) { _astm_message.Append(frame.Data); ((Protocol)sender).SendAck(); } else if (frame.End == SpecialCharacters.ETX) { _astm_message.Append(frame.Data); _message_received = true; ((Protocol)sender).SendAck(); } _expected_frame_number = ((_expected_frame_number + 1) % CobasFrame.MaxSequenceCount); } } catch (Exception ex) { log.Error(ex.ToString()); throw; } }
/// <summary> /// Uredja salje EOT kad je zavrsio sa slanjem, to znaci da je poslo sve frameove i ovo je /// dobar trenutak za kreirat poruku /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void EotReceiveHandler(object sender, ProtocolReceiverEventArgs e) { try { log.Debug("ReceiverSate:EotReceiveHandler"); ((Protocol)sender).ChangeState(Idle); if (_message_received == true) { string tmp = _astm_message.ToString(); Message message = ((Protocol)sender).Decoder.DecodeMessage(_astm_message.ToString()); if (((Protocol)sender).MessageEvents[message.GetType().Name] != null) ((Protocol)sender).MessageEvents[message.GetType().Name](new MessageReceiveEventArgs() { Flat_Message = _astm_message.ToString(), Message = message }); } foreach (string pending_message in TransmissionState._pending_messages) { Message msg = ((Protocol)sender).Decoder.DecodeMessage(pending_message); ICommand cmd = new TestRequestBatchCommand((OrderTestRequestBatch)msg, true); ((Protocol)sender).InputCommand.Add(cmd); ((Protocol)sender)._waitHandle.Set(); } TransmissionState.ClearPendingMessages(); CleanState(); } catch (DecoderException dex) { CleanState(); log.Error(dex.ToString()); smtpLog.Error(dex.ToString()); Debugging.SendAttachment(Debugging.CollectFiles()); } catch (Exception ex) { log.Error(ex.ToString()); throw; } }