Beispiel #1
0
        /// <summary>
        /// This will wait for the SensorNetworkServer, and when it finds it, it will connect!
        /// This code was directly lifted from how this functionality works in the Teensy's source code.
        /// </summary>
        private void WaitForAndConnectToServer()
        {
            bool connected = false;

            // Wait for the SensorNetworkServer to be up
            while (!connected && CurrentlyRunning)
            {
                try
                {
                    Client       = new TcpClient(ClientIP, ClientPort);
                    ClientStream = Client.GetStream();
                    connected    = true;

                    // Ask the SensorNetworkServer for its initialization
                    byte[] askForInit = Encoding.ASCII.GetBytes("Send Sensor Configuration");
                    ClientStream.Write(askForInit, 0, askForInit.Length);
                    ClientStream.Flush();
                }
                catch
                {
                    logger.Info($"{Utilities.GetTimeStamp()}: SimulationSensorNetwork is waiting for the SensorNetworkServer.");
                    if (Client != null)
                    {
                        Client.Dispose();
                    }
                    if (ClientStream != null)
                    {
                        ClientStream.Dispose();
                    }
                }
            }
        }
Beispiel #2
0
        }                                        // To detect redundant calls

        protected virtual void Dispose(bool disposing)
        {
            if (!DisposedValue)
            {
                if (disposing)
                {
                    try
                    {
                        ClientStream.Close();
                        ClientStream.Dispose();

                        Client.Disconnect();
                        Client.Dispose();
                    }
                    finally
                    {
                        ConnectionInfo       = null;
                        AuthenticationMethod = null;
                        ClientStream         = null;
                        Client = null;
                    }
                }

                DisposedValue = true;
            }
        }
Beispiel #3
0
        private void ClientErrorOccurred(object sender, ExceptionEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine(e.Exception.Message);

            if (e.Exception is Renci.SshNet.Common.SshConnectionException)
            {
                try
                {
                    ClientStream.Close();
                    ClientStream.Dispose();
                }
                finally
                {
                    ClientStream = null;
                }

                try
                {
                    Client.Disconnect();
                    Client.Dispose();
                }
                finally
                {
                    ConnectionInfo       = null;
                    AuthenticationMethod = null;
                    Client = null;

                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsConnected"));
                }
            }
            else
            {
                throw new NotImplementedException();
            }
        }
Beispiel #4
0
        public void Dispose()
        {
            Close();

            (Client as IDisposable)?.Dispose();
            ClientStream?.Dispose();
        }
Beispiel #5
0
        public void Dispose()
        {
            Close();

            ((IDisposable)Client)?.Dispose();
            ClientStream?.Dispose();
        }
 public void CloseForm()
 {
     Cts.Cancel();
     Thread.Sleep(1000);
     ClientStream.Close();
     ClientStream.Dispose();
 }
Beispiel #7
0
 public void OnPipeDisposed()
 {
     if (ClientStream != null && ClientStream.IsConnected)
     {
         ClientStream?.Close();
     }
     ClientStream?.Close();
     ClientStream?.Dispose();
     ClientStream = null;
 }
Beispiel #8
0
 /// <summary>
 /// This is used to start the simulation Sensor Network. Calling this is equivalent to powering off the Teensy.
 /// </summary>
 public void EndSimulationSensorNetwork()
 {
     if (CurrentlyRunning)
     {
         CurrentlyRunning = false;
         if (Client != null)
         {
             Client.Dispose();
         }
         if (ClientStream != null)
         {
             ClientStream.Close();
             ClientStream.Dispose();
         }
         Server.Stop();
         if (ServerStream != null)
         {
             ServerStream.Close();
             ServerStream.Dispose();
         }
         SimulationSensorMonitoringThread.Join();
     }
 }
Beispiel #9
0
        private void DisconnectForced(string reason)
        {
            bool clientWasConnected = Client?.Connected == true;

            ReceivedLines.Clear();
            Client?.Dispose();
            ClientStream?.Dispose();
            ClientReader?.Dispose();
            ClientWriter?.Dispose();

            Client       = null;
            ClientStream = null;
            ClientReader = null;
            ClientWriter = null;

            Connected     = false;
            ReadLoopTask  = null;
            KeepAliveTask = null;

            if (clientWasConnected)
            {
                ConnectionClosed?.Invoke(this, new EventArgs <string>(reason));
            }
        }
Beispiel #10
0
        protected virtual void Dispose(bool disposing)
        {
            if (ClientStream != null)
            {
                ClientStream.Close();
                ClientStream.Dispose();
            }

            if (ServerStream != null)
            {
                ServerStream.Close();
                ServerStream.Dispose();
            }

            if (ClientTcpClient != null)
            {
                ClientTcpClient.Dispose();
            }

            if (ServerTcpClient != null)
            {
                ServerTcpClient.Dispose();
            }
        }
Beispiel #11
0
        private void SimulationSensorMonitor()
        {
            // First, we want to connect to the SensorNetworkServer
            if (CurrentlyRunning)
            {
                WaitForAndConnectToServer();
            }

            // Next, we want to request initialization and receive it
            byte[] receivedInit = new byte[0];
            if (CurrentlyRunning)
            {
                receivedInit = RequestAndAcquireSensorInitialization();
            }

            // At this point, we have the initialization and can initialize the sensors
            if (CurrentlyRunning)
            {
                InitializeSensors(receivedInit);
            }

            // Now we can grab the CSV data for ONLY the initialized sensors...
            if (CurrentlyRunning)
            {
                ReadFakeDataFromCSV();
            }

            // Keep track of the indexes for each data array, because we are only extracting a small subsection of each one.
            // We want to know what subsection we just got so we can get the next subsection in the next iteration
            int?elTempIdx = 0;
            int?azTempIdx = 0;
            int?elEncIdx  = 0;
            int?azEncIdx  = 0;
            int?elAccIdx  = 0;
            int?azAccIdx  = 0;
            int?cbAccIdx  = 0;

            // This will tell us if we are rebooting or not. We will only reboot if the connection is randomly terminated.
            bool reboot = false;

            // Now we enter the "super loop"
            while (CurrentlyRunning)
            {
                // Convert subarrays to bytes
                SimulationSubArrayData subArrays = BuildSubArrays(ref elTempIdx, ref azTempIdx, ref elEncIdx, ref azEncIdx, ref elAccIdx, ref azAccIdx, ref cbAccIdx);

                SensorStatuses statuses = new SensorStatuses
                {
                    // TODO: Write the values of each sensor status in here so it can get be encoded (issue #376)
                };

                byte[] dataToSend = ConvertDataArraysToBytes(
                    subArrays.ElevationAccl,
                    subArrays.AzimuthAccl,
                    subArrays.CounterBAccl,
                    subArrays.ElevationTemps,
                    subArrays.AzimuthTemps,
                    subArrays.ElevationEnc,
                    subArrays.AzimuthEnc,
                    statuses
                    );

                // We have to check for CurrentlyRunning down here because we don't know when the connection is going to be terminated, and
                // it could very well be in the middle of the loop.
                if (CurrentlyRunning)
                {
                    try
                    {
                        // Send arrays
                        ClientStream.Write(dataToSend, 0, dataToSend.Length);
                        Thread.Sleep(SensorNetworkConstants.DataSendingInterval);
                    }
                    // This will be reached if the connection is unexpectedly terminated (like it is during sensor reinitialization)
                    catch
                    {
                        CurrentlyRunning = false;
                        reboot           = true;
                    }
                }
            }

            // If the server disconnects, that triggers a reboot
            if (reboot)
            {
                CurrentlyRunning = true;
                Client.Dispose();
                ClientStream.Dispose();
                SimulationSensorMonitor();
            }
        }
Beispiel #12
0
 internal void Disconnect()
 {
     ClientStream.Dispose();
     ClientSocket.Dispose();
 }