Ejemplo n.º 1
0
        public void TestWoopsaServerAuthentication()
        {
            TestObjectServerAuthentification objectServer = new TestObjectServerAuthentification();

            using (WoopsaServer server = new WoopsaServer(objectServer))
            {
                server.Authenticator = new SimpleAuthenticator("TestRealm",
                                                               (sender, e) => { e.IsAuthenticated = e.Username == "woopsa"; });

                using (WoopsaClient client = new WoopsaClient("http://localhost/woopsa"))
                {
                    const string TestUserName = "******";
                    client.Username = TestUserName;
                    WoopsaBoundClientObject root          = client.CreateBoundRoot();
                    WoopsaProperty          propertyVotes = root.Properties.ByName("Votes");
                    propertyVotes.Value = 5;
                    Assert.AreEqual(objectServer.Votes, 5);
                    Assert.AreEqual((int)propertyVotes.Value, 5);
                    WoopsaProperty propertyCurrentUserName = root.Properties.ByName(nameof(TestObjectServerAuthentification.CurrentUserName));
                    Assert.AreEqual(propertyCurrentUserName.Value, TestUserName);
                    client.Username = "******";
                    bool authenticationCheckOk;
                    try
                    {
                        propertyVotes.Value   = 5;
                        authenticationCheckOk = false;
                    }
                    catch
                    {
                        authenticationCheckOk = true;
                    }
                    Assert.IsTrue(authenticationCheckOk);
                }
            }
        }
Ejemplo n.º 2
0
        public void TestWoopsaProtocolUnboundClient()
        {
            TestObjectServer objectServer = new TestObjectServer();

            using (WoopsaClient client = new WoopsaClient("http://localhost/woopsa"))
            {
                WoopsaUnboundClientObject root         = client.CreateUnboundRoot("root");
                WoopsaProperty            propertyVote = root.GetProperty("Votes", WoopsaValueType.Integer, false);
                using (WoopsaServer server = new WoopsaServer(objectServer))
                {
                    propertyVote.Value = new WoopsaValue(123);
                    Assert.AreEqual(objectServer.Votes, 123);
                    var result = propertyVote.Value;
                    Assert.AreEqual(result.ToInt64(), 123);
                }
            }
        }
        public void TestWoopsaObjects()
        {
            WoopsaRoot   root    = new WoopsaRoot();
            WoopsaObject tunnel1 = new WoopsaObject(root, "Tunnel1");

            Assert.AreEqual(root.Items.Count(), 1);
            WoopsaObject tunnel2 = new WoopsaObject(root, "Tunnel2");

            Assert.AreEqual(root.Items.Count(), 2);
            WoopsaObject coMessung1 = new WoopsaObject(tunnel1, "CoMessung1");

            Assert.AreEqual(coMessung1.GetPath(), "/Tunnel1/CoMessung1");

            WoopsaProperty property1 = new WoopsaProperty(coMessung1, "Level", WoopsaValueType.Real,
                                                          (sender) => 1040.0);
            int            property2Value = 0;
            WoopsaProperty property2      = new WoopsaProperty(coMessung1, "Variation", WoopsaValueType.Real,
                                                               (sender) => property2Value, (sender, value) => property2Value = value.ToInt32());

            Assert.AreEqual(coMessung1.Properties.Count(), 2);
            Assert.AreEqual(coMessung1.Properties.First().Value.ToDouble(), 1040.0);
            coMessung1.Properties.ByName("Variation").Value = 45;
            Assert.AreEqual(coMessung1.Properties.ByName("Variation").Value.ToInt32(), 45);
            Assert.AreEqual(coMessung1.Properties.ByName("Variation").Value.ToString(), "45");
            (coMessung1.ByName("Variation") as IWoopsaProperty).Value = (WoopsaValue)36;
            Assert.AreEqual(coMessung1.Properties.ByName("Variation").Value.ToInt32(), 36);
            coMessung1.Properties["Variation"].Value = 5;
            Assert.AreEqual(property2Value, 5);
            int variation = coMessung1.Properties["Variation"].Value;

            Assert.AreEqual(variation, 5);
            WoopsaMethod method1 = new WoopsaMethod(coMessung1, "Calibrate", WoopsaValueType.Null,
                                                    new WoopsaMethodArgumentInfo[] {
                new WoopsaMethodArgumentInfo("minLevel", WoopsaValueType.Real),
                new WoopsaMethodArgumentInfo("maxLevel", WoopsaValueType.Real)
            },
                                                    Calibrate);
            IWoopsaValue result = method1.Invoke(1.1, 5.5);

            Assert.AreEqual(result, WoopsaValue.Null);
            Assert.AreEqual(_minLevel, 1.1);
            Assert.AreEqual(_maxLevel, 5.5);
        }
Ejemplo n.º 4
0
        public void TestWoopsaObjects()
        {
            WoopsaRoot root = new WoopsaRoot();
            WoopsaObject tunnel1 = new WoopsaObject(root, "Tunnel1");

            Assert.AreEqual(root.Items.Count(), 1);
            WoopsaObject tunnel2 = new WoopsaObject(root, "Tunnel2");
            Assert.AreEqual(root.Items.Count(), 2);
            WoopsaObject coMessung1 = new WoopsaObject(tunnel1, "CoMessung1");
            Assert.AreEqual(coMessung1.GetPath(), "/Tunnel1/CoMessung1");

            WoopsaProperty property1 = new WoopsaProperty(coMessung1, "Level", WoopsaValueType.Real,
                (sender) => 1040.0);
            int property2Value = 0;
            WoopsaProperty property2 = new WoopsaProperty(coMessung1, "Variation", WoopsaValueType.Real,
                (sender) => property2Value, (sender, value) => property2Value = value.ToInt32());

            Assert.AreEqual(coMessung1.Properties.Count(), 2);
            Assert.AreEqual(coMessung1.Properties.First().Value.ToDouble(), 1040.0);
            coMessung1.Properties.ByName("Variation").Value = 45;
            Assert.AreEqual(coMessung1.Properties.ByName("Variation").Value.ToInt32(), 45);
            Assert.AreEqual(coMessung1.Properties.ByName("Variation").Value.ToString(), "45");
            (coMessung1.ByName("Variation") as IWoopsaProperty).Value = (WoopsaValue)36;
            Assert.AreEqual(coMessung1.Properties.ByName("Variation").Value.ToInt32(), 36);
            coMessung1.Properties["Variation"].Value = 5;
            Assert.AreEqual(property2Value, 5);
            int variation = coMessung1.Properties["Variation"].Value;
            Assert.AreEqual(variation, 5);
            WoopsaMethod method1 = new WoopsaMethod(coMessung1, "Calibrate", WoopsaValueType.Null,
                new WoopsaMethodArgumentInfo[] {
                    new WoopsaMethodArgumentInfo("minLevel", WoopsaValueType.Real),
                    new WoopsaMethodArgumentInfo("maxLevel", WoopsaValueType.Real)
                },
                Calibrate);
            IWoopsaValue result = method1.Invoke(1.1, 5.5);
            Assert.AreEqual(result, WoopsaValue.Null);
            Assert.AreEqual(_minLevel, 1.1);
            Assert.AreEqual(_maxLevel, 5.5);
        }
        public void TestWoopsaClientSubscriptionChannelNoRemoteSubscriptionService()
        {
            bool           isValueChanged = false;
            WoopsaObject   objectServer   = new WoopsaObject(null, "");
            int            votes          = 0;
            WoopsaProperty propertyVotes  = new WoopsaProperty(objectServer, "Votes", WoopsaValueType.Integer, (p) => votes,
                                                               (p, value) => { votes = value.ToInt32(); });

            using (WoopsaServer server = new WoopsaServer((IWoopsaContainer)objectServer, TestingPort))
            {
                using (WoopsaClient client = new WoopsaClient(TestingUrl))
                {
                    WoopsaBoundClientObject  root         = client.CreateBoundRoot();
                    WoopsaClientSubscription subscription = root.Subscribe(nameof(TestObjectServer.Votes),
                                                                           (sender, e) => { isValueChanged = true; },
                                                                           TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(20));
                    votes = 2;
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    while ((!isValueChanged) && (watch.Elapsed < TimeSpan.FromSeconds(2000)))
                    {
                        Thread.Sleep(10);
                    }
                    if (isValueChanged)
                    {
                        Console.WriteLine("Notification after {0} ms", watch.Elapsed.TotalMilliseconds);
                    }
                    else
                    {
                        Console.WriteLine("No notification received");
                    }
                    subscription.Unsubscribe();
                    Assert.AreEqual(true, isValueChanged);
                }
            }
        }
Ejemplo n.º 6
0
 public void TestWoopsaClientSubscriptionChannelNoRemoteSubscriptionService()
 {
     bool isValueChanged = false;
     WoopsaObject objectServer = new WoopsaObject(null, "");
     int Votes = 0;
     WoopsaProperty propertyVotes = new WoopsaProperty(objectServer, "Votes", WoopsaValueType.Integer, (p) => Votes,
         (p, value) => { Votes = value.ToInt32(); });
     using (WoopsaServer server = new WoopsaServer((IWoopsaContainer)objectServer))
     {
         using (WoopsaClient client = new WoopsaClient("http://localhost/woopsa"))
         {
             WoopsaBoundClientObject root = client.CreateBoundRoot();
             WoopsaClientSubscription subscription = root.Subscribe(nameof(TestObjectServer.Votes),
                 (sender, e) => { isValueChanged = true; },
                 TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(20));
             Votes = 2;
             Stopwatch watch = new Stopwatch();
             watch.Start();
             while ((!isValueChanged) && (watch.Elapsed < TimeSpan.FromSeconds(2000)))
                 Thread.Sleep(10);
             if (isValueChanged)
                 Console.WriteLine("Notification after {0} ms", watch.Elapsed.TotalMilliseconds);
             else
                 Console.WriteLine("No notification received");
             subscription.Unsubscribe();
             Assert.AreEqual(true, isValueChanged);
         }
     }
 }
Ejemplo n.º 7
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");
            }
        }
Ejemplo n.º 8
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");
            }
        }