diff --git a/AIS/bsmd.AIS2Service/AISClass.cs b/AIS/bsmd.AIS2Service/AISClass.cs index 5c9ae54c..a7bee616 100644 --- a/AIS/bsmd.AIS2Service/AISClass.cs +++ b/AIS/bsmd.AIS2Service/AISClass.cs @@ -165,7 +165,7 @@ namespace bsmd.AIS2Service break; default: - _log.InfoFormat("No decoder for AIS class {0}", (AISType)type); + // _log.InfoFormat("No decoder for AIS class {0}", (AISType)type); break; } diff --git a/AIS/bsmd.AIS2Service/AISDecoder.cs b/AIS/bsmd.AIS2Service/AISDecoder.cs index 03132233..d1be367c 100644 --- a/AIS/bsmd.AIS2Service/AISDecoder.cs +++ b/AIS/bsmd.AIS2Service/AISDecoder.cs @@ -122,7 +122,8 @@ namespace bsmd.AIS2Service } else { - _log.WarnFormat("failed to decode AIS data: {0}", aisStatus); + if(aisStatus != AISClass.Status.UNSUPPORTED) + _log.WarnFormat("failed to decode AIS data: {0}", aisStatus); } } diff --git a/AIS/bsmd.AIS2Service/SerialTCPReader.cs b/AIS/bsmd.AIS2Service/SerialTCPReader.cs index 065b6e53..bdfd6f5c 100644 --- a/AIS/bsmd.AIS2Service/SerialTCPReader.cs +++ b/AIS/bsmd.AIS2Service/SerialTCPReader.cs @@ -19,6 +19,9 @@ namespace bsmd.AIS2Service private TcpClient tcpSocket; private Thread _thread; + private const int sleepRetry = 30000; + private const int maxRetries = 3; + private static readonly ILog _log = LogManager.GetLogger(typeof(SerialTCPReader)); @@ -29,31 +32,52 @@ namespace bsmd.AIS2Service private void ReadData() { - try + + while (!_stopFlag) { - while (!_stopFlag) + if (this.tcpSocket == null || !this.tcpSocket.Connected) { - if (this.tcpSocket == null || !this.tcpSocket.Connected) this.Connect(); - - foreach(string line in ReadLines(this.tcpSocket.GetStream(), Encoding.ASCII)) + if (!this.Connect()) + { + _log.Error("Cannot connect to datasource, stopping service"); + this.FatalErrorOccurred?.Invoke(this, new EventArgs()); + } + } + + try + { + foreach (string line in ReadLines(this.tcpSocket.GetStream(), Encoding.ASCII)) { _inputQueue.Enqueue(line); if (_stopFlag) return; - } + } + } + catch (Exception ex) + { + _log.WarnFormat("Exception occurred on serial reader: {0}", ex.Message); } } - - catch(Exception ex) - { - _log.ErrorFormat("Something bad has happened: {0}", ex.Message); - this.FatalErrorOccurred?.Invoke(this, new EventArgs()); - } } - private void Connect() - { - this.tcpSocket = new TcpClient(_host, (int)_port); - _log.InfoFormat("TCP stream connected ({0}:{1})", _host, _port); + private bool Connect() + { + int retryCounter = 0; + while (retryCounter < maxRetries) + { + try + { + this.tcpSocket = new TcpClient(_host, (int)_port); + _log.InfoFormat("TCP stream connected ({0}:{1})", _host, _port); + return true; + } + catch (Exception ex) + { + retryCounter++; + _log.WarnFormat("connect failed: {0}, retrying {1}", ex.Message, retryCounter); + Thread.Sleep(sleepRetry); + } + } + return false; } private static IEnumerable ReadLines(Stream source, Encoding encoding)