예제 #1
0
        private void MonitoredItem_Notification(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            try
            {
                if (e.NotificationValue == null || monitoredItem.Subscription.Session == null)
                {
                    return;
                }

                JsonEncoder encoder = new JsonEncoder(
                    monitoredItem.Subscription.Session.MessageContext, false);
                encoder.WriteNodeId("MonitoredItem", monitoredItem.ResolvedNodeId);
                e.NotificationValue.Encode(encoder);

                var json  = encoder.Close();
                var bytes = new UTF8Encoding(false).GetBytes(json);

                foreach (var publisher in m_publishers)
                {
                    try
                    {
                        publisher.Publish(new ArraySegment <byte>(bytes));
                    }
                    catch (Exception ex)
                    {
                        Utils.Trace(ex, "Failed to publish message, dropping....");
                    }
                }
            }
            catch (Exception exception)
            {
                Utils.Trace(exception, "Error processing monitored item notification.");
            }
        }
 public void JsonEncoder_Constructor_Streamwriter2()
 {
     using (var jsonEncoder = new JsonEncoder(m_context, false, false, m_memoryStream, true, StreamSize))
     {
         TestEncoding(jsonEncoder);
         int length = jsonEncoder.Close();
         var result = Encoding.UTF8.GetString(m_memoryStream.ToArray());
     }
 }
 public void JsonEncoder_Constructor_Recyclable_Streamwriter2()
 {
     using (var memoryStream = new Microsoft.IO.RecyclableMemoryStream(m_memoryManager))
         using (var jsonEncoder = new JsonEncoder(m_context, false, false, memoryStream, false, StreamSize))
         {
             TestEncoding(jsonEncoder);
             int length = jsonEncoder.Close();
             var result = Encoding.UTF8.GetString(m_memoryStream.ToArray());
         }
 }
예제 #4
0
        public string ToJson()
        {
            var encoder = new JsonEncoder(ServiceMessageContext.GlobalContext, true);

            encoder.WriteString("authority", AuthorityUrl);
            encoder.WriteString("grantType", GrantType);
            encoder.WriteString("tokenEndpoint", TokenEndpoint);
            encoder.WriteString("resource", ResourceId);
            encoder.WriteStringArray("scopes", Scopes);

            return(encoder.Close());
        }
        public void WhenServerUrisAreLessThanNamespaces_ShouldNotThrowAndMustReturnCorrectServerUris()
        {
            //arrange
            var namespaceTable = new NamespaceTable(new List <string> {
                Namespaces.OpcUa, "http://bar", "http://foo"
            });
            var expectedServerUri = "http://foobar";
            var serverUris        = new StringTable(new[] { Namespaces.OpcUa, expectedServerUri });
            var memoryStream      = new MemoryStream();
            var context           = new ServiceMessageContext {
                NamespaceUris = namespaceTable, ServerUris = serverUris
            };
            Encoding encoding = Encoding.UTF7; // setting to UTF7 because the BOM marker in UTF8 causes reading error
            // using jsonEncoder, this could have been any IEncoder
            var jsonEncoder = new JsonEncoder(context, true, new StreamWriter(memoryStream, encoding));

            var envelope = new SessionLessServiceMessage {
                NamespaceUris = context.NamespaceUris,
                ServerUris    = context.ServerUris,
                Message       = null
            };

            //act and validate it does not throw
            Assert.DoesNotThrow(() => {
                envelope.Encode(jsonEncoder);
            });
            jsonEncoder.Close();
            jsonEncoder.Dispose();

            //assert
            var buffer = memoryStream.ToArray();
            var result = encoding.GetString(buffer);

            var jObject = JObject.Parse(result);

            Assert.IsNotNull(jObject);
            var serverUrisToken = jObject["ServerUris"];

            Assert.IsNotNull(serverUrisToken);
            var serverUrisEncoded = serverUrisToken.ToObject <string[]>();

            Assert.IsNotNull(serverUrisEncoded);
            Assert.AreEqual(1, serverUrisEncoded.Length);
            Assert.Contains(expectedServerUri, serverUrisEncoded);
        }
예제 #6
0
        private void NotificationsCTRL_ItemsAdded(object sender, ListItemActionEventArgs e)
        {
            try
            {
                foreach (NotificationMessageListCtrl.ItemData item in e.Items)
                {
                    if (item.NotificationMessage == null || item.Subscription.Session == null)
                    {
                        return;
                    }

                    JsonEncoder encoder = new JsonEncoder(
                        item.Subscription.Session.MessageContext, false);

                    foreach (MonitoredItem monitoredItem in item.Subscription.MonitoredItems)
                    {
                        encoder.WriteNodeId("MonitoredItem", monitoredItem.ResolvedNodeId);
                        ((DataChangeNotification)((NotificationData)item.NotificationMessage.NotificationData[0].Body)).MonitoredItems[0].Encode(encoder);

                        string json = encoder.Close();
                        json = json.Replace("\\", "");
                        byte[] bytes = new UTF8Encoding(false).GetBytes(json);

                        foreach (var publisher in m_publishers)
                        {
                            try
                            {
                                publisher.Publish(new ArraySegment <byte>(bytes));
                                Utils.Trace(null, "Publishing: " + json);
                            }
                            catch (Exception ex)
                            {
                                Utils.Trace(ex, "Failed to publish message, dropping....");
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Utils.Trace(exception, "Error processing monitored item notification.");
            }
        }
예제 #7
0
        private void NotificationsCTRL_ItemsAdded(object sender, ListItemActionEventArgs e)
        {
            try
            {
                foreach (NotificationMessageListCtrl.ItemData item in e.Items)
                {
                    if (item.NotificationMessage == null || item.Subscription.Session == null)
                    {
                        return;
                    }

                    JsonEncoder encoder = new JsonEncoder(
                        item.Subscription.Session.MessageContext, false);

                    foreach (MonitoredItem monitoredItem in item.Subscription.MonitoredItems)
                    {
                        encoder.WriteNodeId("MonitoredItem", monitoredItem.ResolvedNodeId);
                        item.NotificationMessage.Encode(encoder);

                        var json = encoder.Close();
                        try
                        {
                            m_massTransitPublisher.Publish(json);
                            Utils.Trace(null, "Publishing: " + json);
                        }
                        catch (Exception ex)
                        {
                            Utils.Trace(ex, "Failed to publish message, dropping....");
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Utils.Trace(exception, "Error processing monitored item notification.");
            }
        }
        private void MonitoredItem_Notification(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            try
            {
                if (e.NotificationValue == null || monitoredItem.Subscription.Session == null)
                {
                    return;
                }

                JsonEncoder encoder = new JsonEncoder(
                    monitoredItem.Subscription.Session.MessageContext, false);
                encoder.WriteNodeId("MonitoredItem", monitoredItem.ResolvedNodeId);
                e.NotificationValue.Encode(encoder);

                var json = encoder.Close();
                var bytes = new UTF8Encoding(false).GetBytes(json);

                foreach (var publisher in m_publishers)
                {
                    try
                    {
                        publisher.Publish(new ArraySegment<byte>(bytes));
                    }
                    catch(Exception ex)
                    {
                        Utils.Trace(ex, "Failed to publish message, dropping....");
                    }
                }
            }
            catch (Exception exception)
            {
                Utils.Trace(exception, "Error processing monitored item notification.");
            }
        }
예제 #9
0
        private void NotificationsCTRL_ItemsAdded(object sender, ListItemActionEventArgs e)
        {
            try
            {
                foreach (NotificationMessageListCtrl.ItemData item in e.Items)
                {
                    if (item.NotificationMessage == null || item.Subscription.Session == null)
                    {
                        return;
                    }

                    JsonEncoder encoder = new JsonEncoder(
                        item.Subscription.Session.MessageContext, false);

                    foreach (MonitoredItem monitoredItem in item.Subscription.MonitoredItems)
                    {
                        encoder.WriteNodeId("MonitoredItem", monitoredItem.ResolvedNodeId);
                        item.NotificationMessage.Encode(encoder);

                        var json = encoder.Close();
                        try
                        {
                            m_massTransitPublisher.Publish(json);
                            Utils.Trace(null, "Publishing: " + json);
                        }
                        catch (Exception ex)
                        {
                            Utils.Trace(ex, "Failed to publish message, dropping....");
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Utils.Trace(exception, "Error processing monitored item notification.");
            }
        }
예제 #10
0
        private void NotificationsCTRL_ItemsAdded(object sender, ListItemActionEventArgs e)
        {
            try
            {
                foreach (NotificationMessageListCtrl.ItemData item in e.Items)
                {
                    if (item.NotificationMessage == null || item.Subscription.Session == null)
                    {
                        return;
                    }

                    JsonEncoder encoder = new JsonEncoder(
                        item.Subscription.Session.MessageContext, false);

                    foreach (MonitoredItem monitoredItem in item.Subscription.MonitoredItems)
                    {
                        encoder.WriteNodeId("MonitoredItem", monitoredItem.ResolvedNodeId);
                        ((DataChangeNotification)((NotificationData)item.NotificationMessage.NotificationData[0].Body)).MonitoredItems[0].Encode(encoder);

                        string json = encoder.Close();
                        json = json.Replace("\\", "");
                        byte[] bytes = new UTF8Encoding(false).GetBytes(json);

                        foreach (var publisher in m_publishers)
                        {
                            try
                            {
                                publisher.Publish(new ArraySegment<byte>(bytes));
                                Utils.Trace(null, "Publishing: " + json);
                            }
                            catch (Exception ex)
                            {
                                Utils.Trace(ex, "Failed to publish message, dropping....");
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Utils.Trace(exception, "Error processing monitored item notification.");
            }
        }