Exemplo n.º 1
0
        private void Channel_OnOpen(object sender, ChannelOpenEventArgs e)
        {
            try
            {
                session.IsAuthenticated = Channel.IsAuthenticated;
                if (session.IsAuthenticated)
                {
                    IdentityDecoder decoder = new IdentityDecoder(session.Config.IdentityClaimType, context,
                                                                  session.Config.Indexes);
                    session.Identity = decoder.Id;
                    session.Indexes  = decoder.Indexes;

                    UserAuditRecord record = new UserAuditRecord(Channel.Id, session.Identity,
                                                                 session.Config.IdentityClaimType, Channel.TypeId, "MQTT", "Granted", DateTime.UtcNow);
                    userAuditor?.WriteAuditRecordAsync(record).Ignore();
                }

                adapter            = new OrleansAdapter(session.Identity, Channel.TypeId, "MQTT", graphManager, logger);
                adapter.OnObserve += Adapter_OnObserve;
            }
            catch (Exception ex)
            {
                logger?.LogErrorAsync(ex, $"MQTT adapter Channel_OnOpen error on channel '{Channel.Id}'.").GetAwaiter();
                OnError?.Invoke(this, new ProtocolAdapterErrorEventArgs(Channel.Id, ex));
            }
        }
Exemplo n.º 2
0
        private void Channel_OnOpen(object sender, ChannelOpenEventArgs e)
        {
            if (!Channel.IsAuthenticated)
            {
                OnError?.Invoke(this,
                                new ProtocolAdapterErrorEventArgs(Channel.Id,
                                                                  new SecurityException("Not authenticated on WSN channel")));
                Channel.CloseAsync().Ignore();
                return;
            }

            adapter            = new OrleansAdapter(identity, "WebSocket", "WSN", graphManager);
            adapter.OnObserve += Adapter_OnObserve;

            if (subscriptions != null)
            {
                foreach (var sub in subscriptions)
                {
                    SubscriptionMetadata metadata = new SubscriptionMetadata
                    {
                        Identity    = identity,
                        Indexes     = localIndexes,
                        IsEphemeral = true
                    };

                    adapter.SubscribeAsync(resource, metadata).GetAwaiter();
                }
            }
        }
Exemplo n.º 3
0
 public CoapRequestDispatcher(CoapSession session, IChannel channel)
 {
     this.channel       = channel;
     this.session       = session;
     auditor            = AuditFactory.CreateSingleton().GetAuditor(AuditType.Message);
     coapObserved       = new Dictionary <string, byte[]>();
     coapUnobserved     = new HashSet <string>();
     adapter            = new OrleansAdapter(session.Identity, channel.TypeId, "CoAP");
     adapter.OnObserve += Adapter_OnObserve;
     LoadDurablesAsync().LogExceptions();
 }
 public CoapRequestDispatcher(CoapSession session, IChannel channel, PiraeusConfig config, GraphManager graphManager, ILog logger = null)
 {
     this.channel       = channel;
     this.session       = session;
     this.config        = config;
     this.graphManager  = graphManager;
     this.logger        = logger;
     auditor            = AuditFactory.CreateSingleton().GetAuditor(AuditType.Message);
     coapObserved       = new Dictionary <string, byte[]>();
     coapUnobserved     = new HashSet <string>();
     adapter            = new OrleansAdapter(session.Identity, channel.TypeId, "CoAP", graphManager, logger);
     adapter.OnObserve += Adapter_OnObserve;
     LoadDurablesAsync().LogExceptions(logger);
 }
Exemplo n.º 5
0
        public RestProtocolAdapter(PiraeusConfig config, GraphManager graphManager, IChannel channel,
                                   HttpContext context, ILog logger = null)
        {
            this.config  = config;
            this.channel = channel;
            this.logger  = logger;
            method       = context.Request.Method.ToUpperInvariant();
            messageUri   = new MessageUri(context.Request);

            IdentityDecoder decoder =
                new IdentityDecoder(config.ClientIdentityNameClaimType, context, config.GetClientIndexes());

            identity = decoder.Id;
            indexes  = decoder.Indexes;
            adapter  = new OrleansAdapter(identity, channel.TypeId, "REST", graphManager, logger);
            if (method == "GET")
            {
                adapter.OnObserve += Adapter_OnObserve;
            }

            protocolType  = ProtocolType.REST;
            contentType   = messageUri.ContentType;
            resource      = messageUri.Resource;
            subscriptions = messageUri.Subscriptions;

            auditFactory = AuditFactory.CreateSingleton();
            if (config.AuditConnectionString != null &&
                config.AuditConnectionString.Contains("DefaultEndpointsProtocol"))
            {
                auditFactory.Add(new AzureTableAuditor(config.AuditConnectionString, "messageaudit"),
                                 AuditType.Message);
                auditFactory.Add(new AzureTableAuditor(config.AuditConnectionString, "useraudit"), AuditType.User);
            }
            else if (config.AuditConnectionString != null)
            {
                auditFactory.Add(new FileAuditor(config.AuditConnectionString), AuditType.Message);
                auditFactory.Add(new FileAuditor(config.AuditConnectionString), AuditType.User);
            }

            messageAuditor = auditFactory.GetAuditor(AuditType.Message);
            userAuditor    = auditFactory.GetAuditor(AuditType.User);
        }
Exemplo n.º 6
0
        private void Channel_OnOpen(object sender, ChannelOpenEventArgs e)
        {
            if (!Channel.IsAuthenticated)
            {
                OnError?.Invoke(this, new ProtocolAdapterErrorEventArgs(Channel.Id, new SecurityException("Not authenticated on WSN channel")));
                Channel.CloseAsync().Ignore(); //shutdown channel immediately
                return;
            }

            //string uriString = HttpUtility.HtmlDecode(UriHelper.GetEncodedUrl(context.Request));

            //MessageUri uri = new MessageUri(uriString);
            //IdentityDecoder decoder = new IdentityDecoder(config.ClientIdentityNameClaimType, context, config.GetClientIndexes());
            //identity = decoder.Id;
            ////resource = uri.Resource;
            ////indexes = uri.Indexes == null ? null : new List<KeyValuePair<string, string>>(uri.Indexes);
            //var localIndexes = decoder.Indexes;

            adapter            = new OrleansAdapter(identity, "WebSocket", "WSN", graphManager);
            adapter.OnObserve += Adapter_OnObserve;

            if (subscriptions != null)
            {
                foreach (var sub in subscriptions)
                {
                    //subscribe
                    SubscriptionMetadata metadata = new SubscriptionMetadata()
                    {
                        Identity    = identity,
                        Indexes     = localIndexes,
                        IsEphemeral = true
                    };

                    adapter.SubscribeAsync(resource, metadata).GetAwaiter();
                    //SubscribeAsync(sub, metadata).GetAwaiter();
                }
            }
        }
Exemplo n.º 7
0
        private void Channel_OnOpen(object sender, ChannelOpenEventArgs e)
        {
            if (!Channel.IsAuthenticated)  //requires channel authentication
            {
                OnError?.Invoke(this, new ProtocolAdapterErrorEventArgs(Channel.Id, new SecurityException("Not authenticated.")));
                Channel.CloseAsync().Ignore();
                return;
            }

            if (e.Message.Method != HttpMethod.Post && e.Message.Method != HttpMethod.Get)
            {
                Channel.CloseAsync().Ignore();
                OnError?.Invoke(this, new ProtocolAdapterErrorEventArgs(Channel.Id, new SecurityException("Rest protocol adapter requires GET or POST only.")));
            }

            MessageUri      uri     = new MessageUri(e.Message);
            IdentityDecoder decoder = new IdentityDecoder(config.ClientIdentityNameClaimType, context, config.GetClientIndexes());

            identity = decoder.Id;

            adapter            = new OrleansAdapter(decoder.Id, "HTTP", "REST");
            adapter.OnObserve += Adapter_OnObserve;
            HttpRequestMessage request = (HttpRequestMessage)e.Message;

            AuditRecord record = new UserAuditRecord(Channel.Id, identity, config.ClientIdentityNameClaimType, Channel.TypeId, String.Format("REST-{0}", request.Method.ToString()), "Granted", DateTime.UtcNow);

            userAuditor?.WriteAuditRecordAsync(record).Ignore();

            if (request.Method == HttpMethod.Get)
            {
                foreach (var item in uri.Subscriptions)
                {
                    Task t = Task.Factory.StartNew(async() =>
                    {
                        await SubscribeAsync(item, decoder.Id, decoder.Indexes);
                    });

                    t.LogExceptions();
                }
            }

            if (request.Method == HttpMethod.Post)
            {
                byte[] buffer = request.Content.ReadAsByteArrayAsync().Result;
                Task   t      = Task.Factory.StartNew(async() =>
                {
                    EventMetadata metadata = await GraphManager.GetPiSystemMetadataAsync(uri.Resource);
                    EventMessage message   = new EventMessage(uri.ContentType, uri.Resource, ProtocolType.REST, buffer, DateTime.UtcNow, metadata.Audit);

                    if (!string.IsNullOrEmpty(uri.CacheKey))
                    {
                        message.CacheKey = uri.CacheKey;
                    }

                    List <KeyValuePair <string, string> > indexList = uri.Indexes == null ? null : new List <KeyValuePair <string, string> >(uri.Indexes);

                    await PublishAsync(decoder.Id, message, indexList);
                    await Channel.CloseAsync();
                });

                t.LogExceptions();
            }
        }