/// <summary>
 /// Connect Request, this function is used for establishing the connection
 /// </summary>
 /// <param name="remoteAddress">address of the remote unit</param>
 /// <returns>Success:
 /// Operation accomplished successfully
 /// NoConnection:
 /// Connection cannot be established.</returns>
 /// <exception cref="System.InvalidOperationException">Connected</exception>
 /// <exception cref="System.ArgumentException">Wrong address format type</exception>
 public TConnectReqRes ConnectReq(IAddress remoteAddress)
 {
     if (Connected)
     {
         throw new InvalidOperationException($"Operation {nameof(Connected)} is not allowed because the connection has been instantiated already");
     }
     if (!(remoteAddress.address is string))
     {
         throw new ArgumentException("Wrong address format type");
     }
     if (string.IsNullOrEmpty((string)remoteAddress.address))
     {
         return(TConnectReqRes.NoConnection);
     }
     m_Observable = new DataObservable((string)remoteAddress.address, m_TextReaderProtocolParameters, TraceSource);
     m_Observer   = m_Observable
                    .Buffer <IDataEntity>(TimeSpan.FromMilliseconds(m_TextReaderProtocolParameters.FileModificationNotificationTimeout), 1)
                    .Subscribe <IList <IDataEntity> >
                    (
         x => NotifyNewData(x),
         exception => ExceptionHandler(exception),
         () => { Connected = false; m_Observable.Dispose(); }
                    );
     Connected = true;
     return(TConnectReqRes.Success);
 }
Beispiel #2
0
        public void DataObservableTest()
        {
            Assert.IsTrue(File.Exists(_fileName));
            List <IDataEntity> _buffer = new List <IDataEntity>();
            Stopwatch          _watch  = Stopwatch.StartNew();
            TestTraceSource    _trace  = new TestTraceSource();

            using (DataObservable _dataSource = new DataObservable(_fileName, TestTextReaderProtocolParameters.InstanceData(), _trace))
            {
                Exception _exception         = null;
                int       _nextExecutedCount = 0;
                Assert.AreEqual <double>(1000, TestTextReaderProtocolParameters.InstanceData().DelayFileScan);
                using (IDisposable _client = _dataSource
                                             .Do <IDataEntity>(x => _nextExecutedCount++)
                                             .Subscribe(x => { _buffer.Add(x); _watch.Stop(); }, exception => _exception = exception))
                {
                    Assert.AreEqual <int>(0, _buffer.Count);
                    string[] _content = File.ReadAllLines(_fileName);
                    Assert.AreEqual <int>(2422, _content.Length);
                    File.WriteAllLines(_fileName, _content);
                    Thread.Sleep(2200);
                    Assert.IsNull(_exception, $"{_exception}");
                    Assert.AreEqual <int>(1, _trace.TraceBuffer.Count);
                    Console.WriteLine(_trace.TraceBuffer[0].ToString());
                    Assert.AreEqual <int>(1, _nextExecutedCount, $"Execution cout: {_nextExecutedCount}");
                    Assert.AreEqual <int>(1, _buffer.Count);
                    Assert.AreEqual <int>(39, _buffer[0].Tags.Length);
                    Assert.AreEqual <string>("09-12-16", _buffer[0].Tags[0]);
                    Assert.AreEqual <string>("09:24:02", _buffer[0].Tags[1]);
                    Assert.AreEqual <string>("", _buffer[0].Tags[38]);
                    Assert.IsTrue(_watch.ElapsedMilliseconds > 2000, $"Elapsed: {_watch.ElapsedMilliseconds}"); //1000nS for Buffer + 1000 for Delay
                    Console.WriteLine($"Time execution: {_watch.ElapsedMilliseconds}");
                }
            }
        }
        private bool m_DisposedValue = false; // To detect redundant calls

        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (m_DisposedValue)
            {
                return;
            }
            if (disposing)
            {
                m_Observer?.Dispose();
                m_Observable?.Dispose();
                m_Observer   = null;
                m_Observable = null;
            }
            m_DisposedValue = true;
        }