示例#1
0
        public void TestWoopsaProtocolPerformance()
        {
            TestObjectServer objectServer = new TestObjectServer();

            using (WoopsaServer server = new WoopsaServer(objectServer))
            {
                using (WoopsaClient client = new WoopsaClient("http://localhost/woopsa"))
                {
                    WoopsaBoundClientObject root     = client.CreateBoundRoot();
                    IWoopsaProperty         property = root.Properties.ByName("Votes");
                    property.Value = new WoopsaValue(0);
                    int       n     = property.Value.ToInt32();
                    Stopwatch watch = new Stopwatch();
                    watch.Start();

                    for (int i = 0; i < 100; i++)
                    {
                        property.Value = new WoopsaValue(i);
                        Assert.AreEqual(objectServer.Votes, i);
                        var result = property.Value;
                        Assert.AreEqual(result.ToInt64(), i);
                    }
                    TimeSpan duration = watch.Elapsed;
                    Assert.IsTrue(duration < TimeSpan.FromMilliseconds(200));
                }
            }
        }
        public void TestWoopsaClientSubscriptionChannel()
        {
            bool             isValueChanged = false;
            TestObjectServer objectServer   = new TestObjectServer();

            using (WoopsaServer server = new WoopsaServer(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));
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    while ((!isValueChanged) && (watch.Elapsed < TimeSpan.FromSeconds(20)))
                    {
                        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);
                }
            }
        }
示例#3
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);
                }
            }
        }
示例#4
0
        public void TestWoopsaProtocol()
        {
            TestObjectServer objectServer = new TestObjectServer();

            using (WoopsaServer server = new WoopsaServer(objectServer))
            {
                using (WoopsaClient client = new WoopsaClient("http://localhost/woopsa"))
                {
                    WoopsaBoundClientObject root = client.CreateBoundRoot();
                    root.Properties.ByName("Votes").Value = new WoopsaValue(11);
                    Assert.AreEqual(objectServer.Votes, 11);
                    var result = root.Properties.ByName("Votes").Value;
                    Assert.AreEqual(11, result.ToInt64());
                    result = root.Methods.ByName(nameof(TestObjectServer.IncrementVotes)).
                             Invoke(5);
                    Assert.AreEqual(16, root.Properties.ByName("Votes").Value.ToInt64());
                    Assert.AreEqual(WoopsaValueType.Null, result.Type);
                    NameValueCollection args = new NameValueCollection();
                    args.Add("count", "8");
                    result = client.ClientProtocol.Invoke("/" + nameof(TestObjectServer.IncrementVotes),
                                                          args);
                    Assert.AreEqual(24, root.Properties.ByName("Votes").Value.ToInt64());
                    Assert.AreEqual(WoopsaValueType.Null, result.Type);
                }
            }
        }
示例#5
0
        public void TestWoopsaProtocolPerformance()
        {
            TestObjectServer objectServer = new TestObjectServer();

            using (WoopsaServer server = new WoopsaServer(objectServer, TestingPort))
            {
                using (WoopsaClient client = new WoopsaClient(TestingUrl))
                {
                    WoopsaBoundClientObject root     = client.CreateBoundRoot();
                    IWoopsaProperty         property = root.Properties.ByName("Votes");
                    property.Value = new WoopsaValue(0);
                    int       n     = property.Value.ToInt32();
                    Stopwatch watch = new Stopwatch();
                    watch.Start();

                    for (int i = 0; i < 100; i++)
                    {
                        property.Value = new WoopsaValue(i);
                        Assert.AreEqual(objectServer.Votes, i);
                        var result = property.Value;
                        Assert.AreEqual(result.ToInt64(), i);
                    }
                    TimeSpan duration = watch.Elapsed;
                    Assert.IsTrue(duration < TimeSpan.FromMilliseconds(200), $"Duration takes ${duration.Milliseconds}ms, instead of 200ms");
                    Console.WriteLine($"Duration takes ${duration.Milliseconds}ms");
                }
            }
        }
示例#6
0
        public void TestWoopsaClientSubscriptionToProperty()
        {
            bool             isValueChanged = false;
            TestObjectServer objectServer   = new TestObjectServer();

            using (WoopsaServer server = new WoopsaServer(objectServer))
            {
                using (WoopsaClient client = new WoopsaClient("http://localhost/woopsa"))
                {
                    WoopsaBoundClientObject  root          = client.CreateBoundRoot();
                    WoopsaClientProperty     propertyVotes = root.Properties.ByName("Votes") as WoopsaClientProperty;
                    WoopsaClientSubscription subscription  = propertyVotes.Subscribe((sender, e) => { isValueChanged = true; },
                                                                                     TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(20));
                    objectServer.Votes = 2;
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    while ((!isValueChanged) && (watch.Elapsed < TimeSpan.FromSeconds(20)))
                    {
                        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);
                }
            }
        }
        public void TestWoopsaClientSubscriptionToProperty()
        {
            bool             isVotesChanged       = false;
            bool             isStringValueChanged = false;
            TestObjectServer objectServer         = new TestObjectServer();

            using (WoopsaServer server = new WoopsaServer(objectServer, TestingPort))
            {
                using (WoopsaClient client = new WoopsaClient(TestingUrl))
                {
                    int    newVotes                        = 0;
                    string newStringValue                  = string.Empty;
                    WoopsaBoundClientObject  root          = client.CreateBoundRoot();
                    WoopsaClientProperty     propertyVotes = root.Properties.ByName("Votes") as WoopsaClientProperty;
                    WoopsaClientSubscription subscription  = propertyVotes.Subscribe((sender, e) =>
                    {
                        newVotes       = e.Notification.Value;
                        isVotesChanged = true;
                    },
                                                                                     TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(20));

                    WoopsaClientProperty     propertyString = root.Properties.ByName("StringValue") as WoopsaClientProperty;
                    WoopsaClientSubscription subscription2  = propertyString.Subscribe((sender, e) =>
                    {
                        var t                = client.ClientProtocol.Read("Votes");
                        newStringValue       = e.Notification.Value;
                        isStringValueChanged = true;
                    },
                                                                                       TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(20));

                    objectServer.Votes       = 2;
                    objectServer.StringValue = "Test";
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    while ((!isVotesChanged || !isStringValueChanged) && (watch.Elapsed < TimeSpan.FromSeconds(20)))
                    {
                        Thread.Sleep(10);
                    }
                    if (isVotesChanged)
                    {
                        Console.WriteLine("Notification after {0} ms", watch.Elapsed.TotalMilliseconds);
                    }
                    else
                    {
                        Console.WriteLine("No notification received");
                    }
                    subscription.Unsubscribe();
                    Assert.AreEqual(true, isVotesChanged);
                    Assert.AreEqual(true, isStringValueChanged);

                    Assert.AreEqual(2, newVotes);
                    Assert.AreEqual("Test", newStringValue);
                }
            }
        }
        public void TestWoopsaClientSubscriptionChannel5000SubscriptionsObservableCollection()
        {
            const int objectsCount       = 5000;
            int       totalNotifications = 0;
            ObservableCollection <ManySubscriptionTestObject> list =
                new ObservableCollection <ManySubscriptionTestObject>();

            for (int i = 0; i < objectsCount; i++)
            {
                list.Add(new ManySubscriptionTestObject()
                {
                    Trigger = i
                });
            }
            using (WoopsaServer server = new WoopsaServer(new WoopsaObjectAdapter(null, "list", list, null, null,
                                                                                  WoopsaObjectAdapterOptions.None, WoopsaVisibility.DefaultIsVisible | WoopsaVisibility.IEnumerableObject), TestingPort))
            {
                using (WoopsaClient client = new WoopsaClient(TestingUrl, null, objectsCount))
                {
                    WoopsaBoundClientObject root = client.CreateBoundRoot();
                    for (int i = 0; i < list.Count; i++)
                    {
                        int index = i;
                        WoopsaClientSubscription subscription = root.Subscribe(
                            WoopsaUtils.CombinePath(
                                WoopsaObjectAdapter.EnumerableItemDefaultName(i),
                                nameof(ManySubscriptionTestObject.Trigger)),
                            (sender, e) =>
                        {
                            list[index].HasNotified = true;
                            totalNotifications++;
                        },
                            TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(200));
                    }
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    while ((totalNotifications < objectsCount) && (watch.Elapsed < TimeSpan.FromSeconds(500)))
                    {
                        Thread.Sleep(10);
                    }
                    if (totalNotifications == objectsCount)
                    {
                        Console.WriteLine("All notification after {0} ms", watch.Elapsed.TotalMilliseconds);
                    }
                    else
                    {
                        Console.WriteLine("{0} notification received, {1} expected", totalNotifications, objectsCount);
                    }
                    Assert.AreEqual(objectsCount, totalNotifications);
                }
            }
        }
        static void Main(string[] args)
        {
            var          root   = new WoopsaObject(null, "Gateway.IoT");
            WoopsaClient client = new WoopsaClient("http://192.168.1.66/woopsa", root);

            client.Username = "******";
            client.Password = "******";

            client.CreateBoundRoot("device_garage");

            var woopsaServer = new WoopsaServer(root, 10443);

            woopsaServer.Authenticator = new SimpleAuthenticator("Raspberry",
                                                                 (sender, e) => e.IsAuthenticated = e.Username == "admin" && e.Password == "admin");
        }
示例#10
0
        public void TestWoopsaProtocolRootContainer()
        {
            WoopsaRoot          serverRoot   = new WoopsaRoot();
            TestObjectServer    objectServer = new TestObjectServer();
            WoopsaObjectAdapter adapter      = new WoopsaObjectAdapter(serverRoot, "TestObject", objectServer);

            using (WoopsaServer server = new WoopsaServer(serverRoot))
            {
                using (WoopsaClient client = new WoopsaClient("http://localhost/woopsa"))
                {
                    WoopsaBoundClientObject root = client.CreateBoundRoot();
                    (root.Items.ByName("TestObject") as WoopsaObject).Properties.ByName("Votes").Value = 17;
                    Assert.AreEqual(objectServer.Votes, 17);
                }
            }
        }
        public void TestWoopsaClientSubscriptionDisappearingProperty()
        {
            bool       isValueChanged = false;
            MainClass  objectServer   = new MainClass();
            InnerClass inner          = new InnerClass();

            objectServer.Inner = inner;
            using (WoopsaServer server = new WoopsaServer(objectServer, TestingPort))
            {
                using (WoopsaClient client = new WoopsaClient(TestingUrl))
                {
                    WoopsaBoundClientObject  root         = client.CreateBoundRoot();
                    WoopsaObject             Inner        = root.Items.ByName(nameof(MainClass.Inner)) as WoopsaObject;
                    WoopsaClientProperty     propertyInfo = Inner.Properties.ByName(nameof(InnerClass.Info)) as WoopsaClientProperty;
                    WoopsaClientSubscription subscription = propertyInfo.Subscribe(
                        (sender, e) =>
                    {
                        isValueChanged = true;
                    },
                        TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(20));
                    inner.Info = "Test";
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    while ((!isValueChanged) && (watch.Elapsed < TimeSpan.FromSeconds(20)))
                    {
                        Thread.Sleep(10);
                    }
                    if (isValueChanged)
                    {
                        Console.WriteLine("Notification after {0} ms", watch.Elapsed.TotalMilliseconds);
                    }
                    else
                    {
                        Console.WriteLine("No notification received");
                    }
                    isValueChanged     = false;
                    objectServer.Inner = new BaseInnerClass();
                    //                    objectServer.Inner = new object();
                    while ((!isValueChanged) && (watch.Elapsed < TimeSpan.FromSeconds(20)))
                    {
                        Thread.Sleep(10);
                    }
                    subscription.Unsubscribe();
                    Assert.AreEqual(true, isValueChanged);
                }
            }
        }
        public void TestWoopsaClientSubscriptionChannelUnexistingItem()
        {
            TestObjectServer objectServer = new TestObjectServer();

            using (WoopsaServer server = new WoopsaServer(objectServer, TestingPort))
            {
                using (WoopsaClient client = new WoopsaClient(TestingUrl))
                {
                    WoopsaBoundClientObject root = client.CreateBoundRoot();
                    try
                    {
                        WoopsaClientSubscription sub = root.Subscribe("ThisDoesNotExistInTheServer",
                                                                      (sender, e) => { },
                                                                      TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(20));
                        Assert.Fail();
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }
示例#13
0
        static void Main(string[] args)
        {
            Console.WriteLine(" *** Welcome to the Woopsa Demo Client! *** ");
            Console.WriteLine(" Note: read the source code to understand what's happening behind the scenes!");
            Console.WriteLine("");

            string serverUrl;

            if (File.Exists("url.config"))
            {
                serverUrl = File.ReadAllText("url.config");
                Console.WriteLine("Using url.config");
            }
            else
            {
                Console.Write("Please enter the Woopsa server URL or leave blank for default (http://localhost/woopsa): ");
                serverUrl = Console.ReadLine();
                if (serverUrl == "")
                {
                    serverUrl = "http://localhost/woopsa";
                }
            }

            WoopsaClient client = new WoopsaClient(serverUrl);

            Console.WriteLine("Woopsa client created on URL: {0}", serverUrl);

            WoopsaBoundClientObject root = client.CreateBoundRoot();

            ExploreItem(root);

            // Leave the DOS window open
            Console.WriteLine("Press any key to exit...");
            Console.ReadLine();
            client.Dispose();
        }
        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);
                }
            }
        }
        public void TestWoopsaWaitNotification()
        {
            TestObjectServer objectServer = new TestObjectServer();

            using (WoopsaServer server = new WoopsaServer(objectServer, TestingPort))
            {
                using (WoopsaClient client = new WoopsaClient(TestingUrl))
                {
                    WoopsaBoundClientObject root = client.CreateBoundRoot();
                    // Just to show how to see all items
                    foreach (var item in root.Items)
                    {
                        Console.WriteLine("Item = " + item.Name);
                        if (item.Name == "SubscriptionService")
                        {
                            Console.WriteLine("Trouvé");
                        }
                    }

                    // create a subscription object
                    WoopsaObject subscription = root.Items.ByNameOrNull("SubscriptionService") as WoopsaObject;
                    if (subscription != null)
                    {
                        int          result = 0;
                        WoopsaMethod methodCreateScubscriptionChannel = subscription.Methods.ByNameOrNull("CreateSubscriptionChannel");
                        if (methodCreateScubscriptionChannel != null)
                        {
                            // call the method "CreateSubscriptionChannel" on the server
                            result = methodCreateScubscriptionChannel.Invoke(1000);   // define the queue size
                        }
                        int channel = result;

                        WoopsaMethod methodRegisterScubscription = subscription.Methods.ByNameOrNull("RegisterSubscription");
                        if (methodRegisterScubscription != null)
                        {
                            // call the method "registerScubscription" on the server
                            result = methodRegisterScubscription.Invoke(channel, WoopsaValue.WoopsaRelativeLink("/Votes"), 0.01, 0.01);
                        }
                        int subscriptionNbr = result;

                        WoopsaJsonData jData;
                        WoopsaMethod   methodWaitNotification = subscription.Methods.ByNameOrNull("WaitNotification");
                        if (methodWaitNotification != null)
                        {
                            Stopwatch watch = new Stopwatch();
                            watch.Start();
                            // call the method "WaitNotification" on the server
                            Thread.Sleep(100);
                            jData = methodWaitNotification.Invoke(channel, 0).JsonData;
                            Assert.IsTrue(jData.Length > 0);
                            int lastNotification;
                            lastNotification = jData[0]["Id"];
                            Assert.AreEqual(lastNotification, 1);
                            // Get notifications again
                            Thread.Sleep(100);
                            jData = methodWaitNotification.Invoke(channel, 0).JsonData;
                            Assert.IsTrue(jData.Length > 0);
                            lastNotification = jData[0]["Id"];
                            Assert.AreEqual(lastNotification, 1);
                        }
                    }
                }
            }
        }