/// <summary>
        /// Handles the command.
        /// </summary>
        /// <param name="cmd">The cmd<see cref="Command"/></param>
        /// <param name="streamId">The streamId<see cref="string"/></param>
        /// <param name="cloudId">The cloud identifier.</param>
        /// <returns>a task</returns>
        private async Task <GrpcResponse> HandleCommand(NetworkUser user, Command cmd, string streamId, string cloudId)
        {
            GrpcResponse result = Ok();

            switch (cmd.Topic)
            {
            case StaticCommandKeys.Connect:
                user = cmd.Data.FirstOrDefault()?.CastToModel <NetworkUser>();
                if (user == null)
                {
                    throw new Exception("Invalid connection data.");
                }

                return(await this.tokenObserver.Authenticate(user.LoginName, user.PasswordHash));

            case StaticCommandKeys.ServiceMetaData:
                ServiceMetaData metaData = cmd.Data.FirstOrDefault()?.CastToModel <ServiceMetaData>();
                if (metaData == null)
                {
                    throw new Exception("Invalid data for service meta data information.");
                }

                if (serviceMetaData.ContainsKey(metaData.ServiceAddress))
                {
                    ServiceMetaData s;
                    while (!serviceMetaData.TryRemove(metaData.ServiceAddress, out s))
                    {
                        await Task.Delay(1);
                    }
                }

                while (!serviceMetaData.TryAdd(metaData.ServiceAddress, metaData))
                {
                    await Task.Delay(1);
                }

                Log("Added service metadata for service " + streamId, LogLevel.Debug);
                break;

            case StaticCommandKeys.Subscribe:
                try
                {
                    SubscriptionMessage msg = cmd.Data.FirstOrDefault()?.CastToModel <SubscriptionMessage>();
                    if (msg == null)
                    {
                        throw new Exception("Invalid data for the subscription message.");
                    }

                    Log("Client subscribed to Topic: " + msg.Topic, LogLevel.Debug);
                    CommonSubscriptionHandler.SubscribeToTopic(streamId, msg.Topic.ToString());
                }
                catch (Exception ex)
                {
                    return(BadRequest(ex));
                }

                break;

            case StaticCommandKeys.Unsubscribe:
                try
                {
                    SubscriptionMessage msg = cmd.Data.FirstOrDefault()?.CastToModel <SubscriptionMessage>();
                    if (msg == null)
                    {
                        throw new Exception("Invalid data for the subscription message.");
                    }

                    Log("Client unsubscribed from Topic: " + msg.Topic, LogLevel.Debug);
                    CommonSubscriptionHandler.UnsubscribeFromTopic(streamId, msg.Topic.ToString());
                }
                catch (Exception ex)
                {
                    return(BadRequest(ex));
                }

                break;

            default:

                string topic = cmd.Topic;
                if (!string.IsNullOrEmpty(cmd.TargetId))
                {
                    topic = topic + "/" + cmd.TargetId;
                }

                CommonSubscriptionHandler.ForwardByTopic(cmd, topic);
                break;
            }

            return(result);
        }
示例#2
0
 /// <inheritdoc />
 public void ChunkReceived(Command cmd, string streamId, string token, Metadata header)
 {
     CommonSubscriptionHandler.ForwardByTopic(cmd, cmd.Topic);
 }