Inheritance: IDisposable
        public void Start(object argsObject)
        {
            PlcParameter plcParameter = argsObject as PlcParameter;
            try
            {
                WoopsaAdsServer serverConnection = new WoopsaAdsServer(plcParameter.adsNetId);
                WoopsaProperty propertyIsConnected;
                WoopsaObject plc = null;
                bool isWorking = false;
                bool shouldShutDown = false;

                lock (_rootWoopsaObject)
                {
                    DiagnosticWindow.AddPlcStatus(new PlcStatus(plcParameter.name, false, "Starting up"));
                    propertyIsConnected = new WoopsaProperty(_rootWoopsaObject, "IsAlive" + plcParameter.name, WoopsaValueType.Logical, (property) => serverConnection.isAdsConnected);
                }

                while (!shouldShutDown)
                {
                    if (serverConnection.IsHeartBeatAlive())
                    {
                        serverConnection.isAdsConnected = true;
                        if (!serverConnection.isHierarchieLoaded)
                        {
                            if (plc != null)
                            {
                                plc.Dispose();
                            }
                            lock (_rootWoopsaObject)
                            {
                                plc = new WoopsaObject(_rootWoopsaObject, plcParameter.name);
                            }
                            serverConnection.loadHierarchy(plc);
                        }
                        if (!isWorking)
                        {
                            isWorking = true;
                            DiagnosticWindow.AddToDebug(Thread.CurrentThread.Name + " thread  : working...");
                            lock (_rootWoopsaObject)
                            {
                                DiagnosticWindow.PlcStatusChange(plc.Name, isWorking, "Working");
                            }
                        }
                    }
                    else
                    {
                        serverConnection.isAdsConnected = false;
                        serverConnection.isHierarchieLoaded = false;
                        if (plc != null)
                        {
                            plc.Dispose();
                        }
                        DiagnosticWindow.AddToDebug(Thread.CurrentThread.Name + " - Error connection ... Try again");
                        isWorking = false;
                        lock (_rootWoopsaObject)
                        {
                            DiagnosticWindow.PlcStatusChange(plcParameter.name, isWorking, "Error");
                        }
                    }
                    Thread.Sleep(500);
                    lock (plcParameterList)
                    {
                        shouldShutDown = _shouldStop;
                    }
                }
                serverConnection.Dispose();
            }
            catch (SocketException e)
            {
                // A SocketException is caused by an application already listening on a port in 90% of cases
                // Applications known to use port 80:
                //  - On Windows 10, IIS is on by default on some configurations. Disable it here:
                //    http://stackoverflow.com/questions/30758894/apache-server-xampp-doesnt-run-on-windows-10-port-80
                //  - IIS
                //  - Apache
                //  - Nginx
                //  - Skype

                DiagnosticWindow.AddToDebug("Error: Could not start Woopsa Server. Most likely because an application is already listening on port 80.");
                DiagnosticWindow.AddToDebug("Known culprits:");
                DiagnosticWindow.AddToDebug(" - On Windows 10, IIS is on by default on some configurations.");
                DiagnosticWindow.AddToDebug(" - Skype");
                DiagnosticWindow.AddToDebug(" - Apache, nginx, etc.");
                DiagnosticWindow.AddToDebug("SocketException: " + e.Message);
                MessageBox.Show("SocketException ! : See diagnostic log for more information", "Er ror", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            catch (Exception ex)
            {
                Application.Current.Dispatcher.Invoke(
                    System.Windows.Threading.DispatcherPriority.Normal,
                    new Action<Exception>((exc) =>
                    {
                        throw new Exception("Exception from another Thread : " + Thread.CurrentThread.Name, exc);
                    }), ex);
            }
            DiagnosticWindow.AddToDebug(Thread.CurrentThread.Name + " thread: terminating gracefully.");
            lock (_rootWoopsaObject)
            {
                lock (App.appLock)
                {
                    if (App.isExiting)
                        return;
                }
                DiagnosticWindow.PlcStatusChange(plcParameter.name, false, "Stop");
            }
        }
Esempio n. 2
0
        public void Start(object argsObject)
        {
            PlcParameter plcParameter = argsObject as PlcParameter;

            try
            {
                WoopsaAdsServer serverConnection = new WoopsaAdsServer(plcParameter.adsNetId);
                WoopsaProperty  propertyIsConnected;
                WoopsaObject    plc            = null;
                bool            isWorking      = false;
                bool            shouldShutDown = false;

                lock (_rootWoopsaObject)
                {
                    DiagnosticWindow.AddPlcStatus(new PlcStatus(plcParameter.name, false, "Starting up"));
                    propertyIsConnected = new WoopsaProperty(_rootWoopsaObject, "IsAlive" + plcParameter.name, WoopsaValueType.Logical, (property) => serverConnection.isAdsConnected);
                }


                while (!shouldShutDown)
                {
                    if (serverConnection.IsHeartBeatAlive())
                    {
                        serverConnection.isAdsConnected = true;
                        if (!serverConnection.isHierarchieLoaded)
                        {
                            if (plc != null)
                            {
                                plc.Dispose();
                            }
                            lock (_rootWoopsaObject)
                            {
                                plc = new WoopsaObject(_rootWoopsaObject, plcParameter.name);
                            }
                            serverConnection.loadHierarchy(plc);
                        }
                        if (!isWorking)
                        {
                            isWorking = true;
                            DiagnosticWindow.AddToDebug(Thread.CurrentThread.Name + " thread  : working...");
                            lock (_rootWoopsaObject)
                            {
                                DiagnosticWindow.PlcStatusChange(plc.Name, isWorking, "Working");
                            }
                        }
                    }
                    else
                    {
                        serverConnection.isAdsConnected     = false;
                        serverConnection.isHierarchieLoaded = false;
                        if (plc != null)
                        {
                            plc.Dispose();
                        }
                        DiagnosticWindow.AddToDebug(Thread.CurrentThread.Name + " - Error connection ... Try again");
                        isWorking = false;
                        lock (_rootWoopsaObject)
                        {
                            DiagnosticWindow.PlcStatusChange(plcParameter.name, isWorking, "Error");
                        }
                    }
                    Thread.Sleep(500);
                    lock (plcParameterList)
                    {
                        shouldShutDown = _shouldStop;
                    }
                }
                serverConnection.Dispose();
            }
            catch (SocketException e)
            {
                // A SocketException is caused by an application already listening on a port in 90% of cases
                // Applications known to use port 80:
                //  - On Windows 10, IIS is on by default on some configurations. Disable it here:
                //    http://stackoverflow.com/questions/30758894/apache-server-xampp-doesnt-run-on-windows-10-port-80
                //  - IIS
                //  - Apache
                //  - Nginx
                //  - Skype

                DiagnosticWindow.AddToDebug("Error: Could not start Woopsa Server. Most likely because an application is already listening on port 80.");
                DiagnosticWindow.AddToDebug("Known culprits:");
                DiagnosticWindow.AddToDebug(" - On Windows 10, IIS is on by default on some configurations.");
                DiagnosticWindow.AddToDebug(" - Skype");
                DiagnosticWindow.AddToDebug(" - Apache, nginx, etc.");
                DiagnosticWindow.AddToDebug("SocketException: " + e.Message);
                MessageBox.Show("SocketException ! : See diagnostic log for more information", "Er ror", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            catch (Exception ex)
            {
                Application.Current.Dispatcher.Invoke(
                    System.Windows.Threading.DispatcherPriority.Normal,
                    new Action <Exception>((exc) =>
                {
                    throw new Exception("Exception from another Thread : " + Thread.CurrentThread.Name, exc);
                }), ex);
            }
            DiagnosticWindow.AddToDebug(Thread.CurrentThread.Name + " thread: terminating gracefully.");
            lock (_rootWoopsaObject)
            {
                lock (App.appLock)
                {
                    if (App.isExiting)
                    {
                        return;
                    }
                }
                DiagnosticWindow.PlcStatusChange(plcParameter.name, false, "Stop");
            }
        }