Exemplo n.º 1
0
 public ActionResult Save(Bam.Net.CoreServices.ApplicationRegistration.Dao.Subscription[] values)
 {
     try
     {
         SubscriptionCollection saver = new SubscriptionCollection();
         saver.AddRange(values);
         saver.Save();
         return(Json(new { Success = true, Message = "", Dao = "" }));
     }
     catch (Exception ex)
     {
         return(GetErrorResult(ex));
     }
 }
Exemplo n.º 2
0
        public async Task TransferSubscription(TransferType transferType, bool sendInitialValues)
        {
            const int kTestSubscriptions = 2;
            const int kDelay             = 2_000;

            // create test session and subscription
            var originSession = await ClientFixture.ConnectAsync(ServerUrl, SecurityPolicies.Basic256Sha256).ConfigureAwait(false);

            bool originSessionOpen = transferType == TransferType.KeepOpen;

            // create subscriptions
            var originSubscriptions        = new SubscriptionCollection();
            var originSubscriptionCounters = new int[kTestSubscriptions];
            var targetSubscriptionCounters = new int[kTestSubscriptions];

            for (int ii = 0; ii < kTestSubscriptions; ii++)
            {
                // create subscription with static monitored items
                var subscription = new Subscription(originSession.DefaultSubscription)
                {
                    PublishingInterval = 1_000,
                    PublishingEnabled  = true,
                };

                originSubscriptions.Add(subscription);
                originSession.AddSubscription(subscription);
                subscription.RepublishAfterTransfer = transferType == TransferType.DisconnectedRepublish;
                subscription.Create();

                // set defaults
                subscription.DefaultItem.DiscardOldest  = true;
                subscription.DefaultItem.QueueSize      = (ii == 0) ? 0U : 5;
                subscription.DefaultItem.MonitoringMode = MonitoringMode.Reporting;

                // create test set
                var namespaceUris = Session.NamespaceUris;
                var testSet       = new List <NodeId>();
                if (ii == 0)
                {
                    testSet.AddRange(GetTestSetStatic(namespaceUris));
                }
                else
                {
                    testSet.AddRange(GetTestSetSimulation(namespaceUris));
                }

                subscription.Handle = ii;
                var list = CreateMonitoredItemTestSet(subscription, testSet).ToList();
                list.ForEach(i => i.Notification += (MonitoredItem item, MonitoredItemNotificationEventArgs e) => {
                    originSubscriptionCounters[(int)subscription.Handle]++;
                    foreach (var value in item.DequeueValues())
                    {
                        TestContext.Out.WriteLine("Org:{0}: {1:20}, {2}, {3}, {4}", subscription.Id, item.DisplayName, value.Value, value.SourceTimestamp, value.StatusCode);
                    }
                });
                subscription.AddItems(list);
                subscription.ApplyChanges();
            }

            // settle
            await Task.Delay(kDelay).ConfigureAwait(false);

            // persist the subscription state
            var filePath = Path.GetTempFileName();

            // close session, do not delete subscription
            if (transferType != TransferType.KeepOpen)
            {
                originSession.DeleteSubscriptionsOnClose = false;
                originSession.Save(filePath);
                if (transferType == TransferType.CloseSession)
                {
                    // graceful close
                    originSession.Close();
                }
                else
                {
                    // force a socket dispose, to emulate network disconnect
                    // without closing session on server
                    originSession.TransportChannel.Dispose();
                }
            }

            // wait
            await Task.Delay(kDelay).ConfigureAwait(false);

            // close session, do not delete subscription
            if (transferType > TransferType.CloseSession)
            {
                originSession.Close();
            }

            // create target session
            var targetSession = await ClientFixture.ConnectAsync(ServerUrl, SecurityPolicies.Basic256Sha256).ConfigureAwait(false);

            // restore client state
            var transferSubscriptions = new SubscriptionCollection();

            if (transferType != TransferType.KeepOpen)
            {
                // load
                transferSubscriptions.AddRange(targetSession.Load(filePath));

                // hook notifications for log output
                int ii = 0;
                foreach (var subscription in transferSubscriptions)
                {
                    subscription.Handle = ii;
                    subscription.MonitoredItems.ToList().ForEach(i => i.Notification += (MonitoredItem item, MonitoredItemNotificationEventArgs e) => {
                        targetSubscriptionCounters[(int)subscription.Handle]++;
                        foreach (var value in item.DequeueValues())
                        {
                            TestContext.Out.WriteLine("Tra:{0}: {1:20}, {2}, {3}, {4}", subscription.Id, item.DisplayName, value.Value, value.SourceTimestamp, value.StatusCode);
                        }
                    });
                    ii++;
                }
            }
            else
            {
                transferSubscriptions.AddRange(originSubscriptions);
            }

            // wait
            await Task.Delay(kDelay).ConfigureAwait(false);

            // transfer restored subscriptions
            var result = targetSession.TransferSubscriptions(transferSubscriptions, sendInitialValues);

            Assert.IsTrue(result);

            // validate results
            for (int ii = 0; ii < transferSubscriptions.Count; ii++)
            {
                Assert.IsTrue(transferSubscriptions[ii].Created);
            }

            TestContext.Out.WriteLine("TargetSession is now SessionId={0}", targetSession.SessionId);

            // wait for some events
            await Task.Delay(kDelay).ConfigureAwait(false);

            // stop publishing
            foreach (var subscription in transferSubscriptions)
            {
                TestContext.Out.WriteLine("SetPublishingMode(false) for SessionId={0}, SubscriptionId={1}",
                                          subscription.Session.SessionId, subscription.Id);
                subscription.SetPublishingMode(false);
            }

            // validate expected counts
            for (int jj = 0; jj < kTestSubscriptions; jj++)
            {
                TestContext.Out.WriteLine("Subscription {0}: OriginCounts {1}, TargetCounts {2} ",
                                          jj, originSubscriptionCounters[jj], targetSubscriptionCounters[jj]);
                var monitoredItemCount  = transferSubscriptions[jj].MonitoredItemCount;
                var originExpectedCount = sendInitialValues && originSessionOpen ? monitoredItemCount * 2 : monitoredItemCount;
                var targetExpectedCount = sendInitialValues && !originSessionOpen ? monitoredItemCount : 0;
                if (jj == 0)
                {
                    // static nodes, expect only one set of changes, another one if send initial values was set
                    Assert.AreEqual(originExpectedCount, originSubscriptionCounters[jj]);
                    Assert.AreEqual(targetExpectedCount, targetSubscriptionCounters[jj]);
                }
                else
                {
                    // dynamic nodes, expect only one set of changes, another one if send initial values was set
                    Assert.LessOrEqual(originExpectedCount, originSubscriptionCounters[jj]);
                    Assert.LessOrEqual(targetExpectedCount, targetSubscriptionCounters[jj]);
                }
            }

            // close sessions
            targetSession.Close();
            if (originSessionOpen)
            {
                originSession.Close();
            }

            // cleanup
            File.Delete(filePath);
        }