/// <summary>
        /// 处理事件。Event类型的命令是面向主题和状态码编程的。
        /// </summary>
        /// <returns></returns>
        private static void HandleEvent(MessageContext context)
        {
            EventSourceType eventSourceType;

            context.Command.EventSourceType.TryParse(out eventSourceType);
            switch (eventSourceType)
            {
            case EventSourceType.Command:
            {
                #region 命令事件
                // 如果是审计节点的审计事件
                if (context.Command.EventSubjectCode.Equals(EventSubjectCode.StateCodeChanged_Audit) &&
                    (context.ClientAgent == context.Host.NodeHost.Nodes.ThisNode))
                {
                    #region 审计节点事件
                    var localEvent = context.Ontology.MessageProvider.GetCommand(MessageTypeKind.LocalEvent,
                                                                                 context.Ontology, new Guid(context.Command.MessageId));
                    if (localEvent == null)
                    {
                        context.Result.UpdateStatus(Status.NotExist, "给定的MessageID标识的服务端事件不存在。");
                        return;
                    }
                    if (!Verb.Create.Equals(localEvent.Verb))
                    {
                        long total;
                        var  localEvents = context.Ontology.MessageProvider.GetPlistCommands(MessageTypeKind.LocalEvent, context.Ontology, null, null, null, localEvent.LocalEntityId, 0, 1, "CreateOn", "asc", out total);
                        if (localEvents[0].Id != localEvent.Id)
                        {
                            context.Result.UpdateStatus(Status.InvalidCommandTicks, "请按照时间顺序审核命令");
                            return;
                        }
                    }
                    using (var act = new WfAct(context.Host, context, context.Ontology.MessageProvider, "审核完成"))
                    {
                        var ctx = new MessageContext(context.Host, localEvent);
                        if (context.Command.Status == (int)Status.AuditApproved)
                        {
                            localEvent.IsDumb = context.Command.IsDumb;
                            #region 执行
                            using (var sqlAct = new WfAct(context.Host, ctx, ctx.Ontology.EntityProvider, "执行命令"))
                            {
                                // 注意:执行的是本地事件,而不是请求事件
                                var actionCode = ctx.Command.Verb;
                                var r          = ctx.Ontology.EntityProvider.ExecuteCommand(ctx.ToDbCommand());
                                context.Result.UpdateStatus(r.StateCode, r.Description);
                                ctx.Result.UpdateStatus(r.StateCode, r.Description);

                                if (r.IsSuccess)
                                {
                                    ctx.Ontology.MessageProvider.SaveCommand(ctx.Ontology, ctx.Command.ToExecuted(context.Result));

                                    var commandFactory = context.Host.NodeHost.MessageProducer;
                                    using (var factoryAct = new WfAct(context.Host, ctx, commandFactory, "生产命令"))
                                    {
                                        var products = commandFactory.Produce(new MessageTuple(ctx, GetTuple(ctx)));
                                        // 保存生产的待分发命令
                                        if (products != null && products.Count != 0)
                                        {
                                            ctx.Ontology.MessageProvider.SaveCommands(ctx.Ontology, products.ToArray());
                                        }
                                    }
                                }
                                else
                                {
                                    ctx.Ontology.MessageProvider.SaveCommand(ctx.Ontology, ctx.Command.ToExecuteFailing(context.Result));
                                }
                            }
                            #endregion
                        }
                        else
                        {
                            using (var cmdProviderAct = new WfAct(context.Host, ctx, ctx.Ontology.MessageProvider, "保存执行失败的命令"))
                            {
                                ctx.Ontology.MessageProvider.SaveCommand(ctx.Ontology, ctx.Command.ToExecuteFailing(context.Result));
                            }
                            context.Result.UpdateStatus(Status.AuditUnapproved, "审计未通过");
                        }
                        ctx.Ontology.MessageProvider.DeleteCommand(MessageTypeKind.LocalEvent, ctx.Ontology, localEvent.Id, context.Command.IsDumb);
                    }
                    #endregion
                }
                else
                {
                    var distrubutedCommand = context.Ontology.MessageProvider.GetCommand(MessageTypeKind.Distributed,
                                                                                         context.Ontology, new Guid(context.Command.MessageId));
                    if (distrubutedCommand == null)
                    {
                        context.Result.UpdateStatus(Status.NotExist, "给定的MessageID标识的服务端命令不存在。服务端认为没有向您的节点分发过MessageID为" + context.Command.MessageId + "的命令");
                    }
                    else
                    {
                        using (var act = new WfAct(context.Host, context, context.Ontology.MessageProvider, context.Result.Description))
                        {
                            context.Ontology.MessageProvider.SaveCommand(context.Ontology, context.Command.ToClientEvent(context.Result));
                        }
                        context.Result.UpdateStatus(Status.ReceiveOk, "接收成功");
                    }
                }
                break;
                #endregion
            }

            case EventSourceType.Entity:
            {
                #region 实体事件
                context.Result.UpdateStatus(Status.Nonsupport, "暂不支持客户端实体事件");
                break;
                #endregion
            }

            default:
                context.Exception = new GeneralException("意外的事件源类型");
                throw context.Exception;
            }
        }
Exemple #2
0
        /// <summary>
        /// 处理事件。Event类型的命令是面向主题和状态码编程的。
        /// </summary>
        /// <returns></returns>
        private static void HandleEvent(MessageContext context)
        {
            EventSourceType eventSourceType;
            context.Command.EventSourceType.TryParse(out eventSourceType);
            switch (eventSourceType)
            {
                case EventSourceType.Command:
                    {
                        #region 命令事件
                        // 如果是审计节点的审计事件
                        if (context.Command.EventSubjectCode.Equals(EventSubjectCode.StateCodeChanged_Audit)
                                && (context.ClientAgent == context.Host.NodeHost.Nodes.ThisNode))
                        {
                            #region 审计节点事件
                            var localEvent = context.Ontology.MessageProvider.GetCommand(MessageTypeKind.LocalEvent,
                                context.Ontology, new Guid(context.Command.MessageId));
                            if (localEvent == null)
                            {
                                context.Result.UpdateStatus(Status.NotExist, "给定的MessageID标识的服务端事件不存在。");
                                return;
                            }
                            if (!Verb.Create.Equals(localEvent.Verb))
                            {
                                long total;
                                var localEvents = context.Ontology.MessageProvider.GetPlistCommands(MessageTypeKind.LocalEvent, context.Ontology, null, null, null, localEvent.LocalEntityId, 0, 1, "CreateOn", "asc", out total);
                                if (localEvents[0].Id != localEvent.Id)
                                {
                                    context.Result.UpdateStatus(Status.InvalidCommandTicks, "请按照时间顺序审核命令");
                                    return;
                                }
                            }
                            using (var act = new WfAct(context.Host, context, context.Ontology.MessageProvider, "审核完成"))
                            {
                                var ctx = new MessageContext(context.Host, localEvent);
                                if (context.Command.Status == (int)Status.AuditApproved)
                                {
                                    localEvent.IsDumb = context.Command.IsDumb;
                                    #region 执行
                                    using (var sqlAct = new WfAct(context.Host, ctx, ctx.Ontology.EntityProvider, "执行命令"))
                                    {
                                        // 注意:执行的是本地事件,而不是请求事件
                                        var actionCode = ctx.Command.Verb;
                                        var r = ctx.Ontology.EntityProvider.ExecuteCommand(ctx.ToDbCommand());
                                        context.Result.UpdateStatus(r.StateCode, r.Description);
                                        ctx.Result.UpdateStatus(r.StateCode, r.Description);

                                        if (r.IsSuccess)
                                        {
                                            ctx.Ontology.MessageProvider.SaveCommand(ctx.Ontology, ctx.Command.ToExecuted(context.Result));

                                            var commandFactory = context.Host.NodeHost.MessageProducer;
                                            using (var factoryAct = new WfAct(context.Host, ctx, commandFactory, "生产命令"))
                                            {
                                                var products = commandFactory.Produce(new MessageTuple(ctx, GetTuple(ctx)));
                                                // 保存生产的待分发命令
                                                if (products != null && products.Count != 0)
                                                {
                                                    ctx.Ontology.MessageProvider.SaveCommands(ctx.Ontology, products.ToArray());
                                                }
                                            }
                                        }
                                        else
                                        {
                                            ctx.Ontology.MessageProvider.SaveCommand(ctx.Ontology, ctx.Command.ToExecuteFailing(context.Result));
                                        }
                                    }
                                    #endregion
                                }
                                else
                                {
                                    using (var cmdProviderAct = new WfAct(context.Host, ctx, ctx.Ontology.MessageProvider, "保存执行失败的命令"))
                                    {
                                        ctx.Ontology.MessageProvider.SaveCommand(ctx.Ontology, ctx.Command.ToExecuteFailing(context.Result));
                                    }
                                    context.Result.UpdateStatus(Status.AuditUnapproved, "审计未通过");
                                }
                                ctx.Ontology.MessageProvider.DeleteCommand(MessageTypeKind.LocalEvent, ctx.Ontology, localEvent.Id, context.Command.IsDumb);
                            }
                            #endregion
                        }
                        else
                        {
                            var distrubutedCommand = context.Ontology.MessageProvider.GetCommand(MessageTypeKind.Distributed,
                                context.Ontology, new Guid(context.Command.MessageId));
                            if (distrubutedCommand == null)
                            {
                                context.Result.UpdateStatus(Status.NotExist, "给定的MessageID标识的服务端命令不存在。服务端认为没有向您的节点分发过MessageID为" + context.Command.MessageId + "的命令");
                            }
                            else
                            {
                                using (var act = new WfAct(context.Host, context, context.Ontology.MessageProvider, context.Result.Description))
                                {
                                    context.Ontology.MessageProvider.SaveCommand(context.Ontology, context.Command.ToClientEvent(context.Result));
                                }
                                context.Result.UpdateStatus(Status.ReceiveOk, "接收成功");
                            }
                        }
                        break;
                        #endregion
                    }
                case EventSourceType.Entity:
                    {
                        #region 实体事件
                        context.Result.UpdateStatus(Status.Nonsupport, "暂不支持客户端实体事件");
                        break;
                        #endregion
                    }
                default:
                    context.Exception = new AnycmdException("意外的事件源类型");
                    throw context.Exception;
            }
        }
 /// <summary>
 /// 执行。Action类型的命令是立即执行的。
 /// <remarks>
 /// 该执行不会从成功接收的命令表中删除已执行的记录。
 /// </remarks>
 /// </summary>
 /// <exception cref="GeneralException">
 /// 当当前命令不是成功接收的命令时或者当前命令的动作码是get或head时引发
 /// </exception>
 /// <returns></returns>
 private static void HandleAction(MessageContext context)
 {
     if (context.NeedAudit)
     {
         PublishAuditEvent(context);
         return;
     }
     context.Result.UpdateStatus(Status.ExecuteOk, "执行成功");
     if (Verb.Get.Equals(context.Command.Verb) ||
         Verb.Head.Equals(context.Command.Verb))
     {
         return;
     }
     #region 执行
     using (var act = new WfAct(context.Host, context, context.Ontology.EntityProvider, context.Ontology.EntityProvider.Description))
     {
         using (var sqlAct = new WfAct(context.Host, context, context.Ontology.EntityProvider, "执行命令"))
         {
             Verb          actionCode = context.Command.Verb;
             ProcessResult r          = context.Ontology.EntityProvider.ExecuteCommand(context.ToDbCommand());
             context.Result.UpdateStatus(r.StateCode, r.Description);
             // 执行成功
             if (r.IsSuccess)
             {
                 using (var cmdProviderAct = new WfAct(context.Host, context, context.Ontology.MessageProvider, "保存成功执行的命令和由于命令执行而生产的待分发命令"))
                 {
                     #region 如果是正在接收的命令
                     if (!(context.Command is IEntity <Guid>))
                     {
                         context.Ontology.MessageProvider.SaveCommand(context.Ontology, context.Command.ToExecuted(context.Result));
                     }
                     #endregion
                     #region 如果是以前接收的命令
                     else if (context.Command is IEntity <Guid> )
                     {
                         context.Ontology.MessageProvider.SaveCommand(context.Ontology, context.Command.ToExecuted(context.Result));
                         context.Ontology.MessageProvider.DeleteCommand(MessageTypeKind.Received, context.Ontology, context.Command.Id, context.Command.IsDumb);
                     }
                     #endregion
                     #region 建造待分发命令
                     var commandFactory = context.Host.NodeHost.MessageProducer;
                     using (var factoryAct = new WfAct(context.Host, context, commandFactory, "命令工厂建造待分发命令"))
                     {
                         var products = commandFactory.Produce(new MessageTuple(context, GetTuple(context)));
                         // 保存生产的待分发命令
                         if (products != null && products.Count != 0)
                         {
                             context.Ontology.MessageProvider.SaveCommands(context.Ontology, products.ToArray());
                         }
                     }
                     #endregion
                 }
             }
             // 执行失败
             else
             {
                 using (var cmdProviderAct = new WfAct(context.Host, context, context.Ontology.MessageProvider, "保存执行失败的命令"))
                 {
                     context.Ontology.MessageProvider.SaveCommand(context.Ontology, context.Command.ToExecuteFailing(context.Result));
                     if (context.Command is IEntity <Guid> )
                     {
                         context.Ontology.MessageProvider.DeleteCommand(MessageTypeKind.Received, context.Ontology, context.Command.Id, context.Command.IsDumb);
                     }
                 }
             }
         }
     }
     #endregion
 }
Exemple #4
0
 /// <summary>
 /// 执行。Action类型的命令是立即执行的。
 /// <remarks>
 /// 该执行不会从成功接收的命令表中删除已执行的记录。
 /// </remarks>
 /// </summary>
 /// <exception cref="AnycmdException">
 /// 当当前命令不是成功接收的命令时或者当前命令的动作码是get或head时引发
 /// </exception>
 /// <returns></returns>
 private static void HandleAction(MessageContext context)
 {
     if (context.NeedAudit)
     {
         PublishAuditEvent(context);
         return;
     }
     context.Result.UpdateStatus(Status.ExecuteOk, "执行成功");
     if (Verb.Get.Equals(context.Command.Verb)
         || Verb.Head.Equals(context.Command.Verb))
     {
         return;
     }
     #region 执行
     using (var act = new WfAct(context.Host, context, context.Ontology.EntityProvider, context.Ontology.EntityProvider.Description))
     {
         using (var sqlAct = new WfAct(context.Host, context, context.Ontology.EntityProvider, "执行命令"))
         {
             Verb actionCode = context.Command.Verb;
             ProcessResult r = context.Ontology.EntityProvider.ExecuteCommand(context.ToDbCommand());
             context.Result.UpdateStatus(r.StateCode, r.Description);
             // 执行成功
             if (r.IsSuccess)
             {
                 using (var cmdProviderAct = new WfAct(context.Host, context, context.Ontology.MessageProvider, "保存成功执行的命令和由于命令执行而生产的待分发命令"))
                 {
                     #region 如果是正在接收的命令
                     if (!(context.Command is IEntity))
                     {
                         context.Ontology.MessageProvider.SaveCommand(context.Ontology, context.Command.ToExecuted(context.Result));
                     }
                     #endregion
                     #region 如果是以前接收的命令
                     else if (context.Command is IEntity)
                     {
                         context.Ontology.MessageProvider.SaveCommand(context.Ontology, context.Command.ToExecuted(context.Result));
                         context.Ontology.MessageProvider.DeleteCommand(MessageTypeKind.Received, context.Ontology, context.Command.Id, context.Command.IsDumb);
                     }
                     #endregion
                     #region 建造待分发命令
                     var commandFactory = context.Host.NodeHost.MessageProducer;
                     using (var factoryAct = new WfAct(context.Host, context, commandFactory, "命令工厂建造待分发命令"))
                     {
                         var products = commandFactory.Produce(new MessageTuple(context, GetTuple(context)));
                         // 保存生产的待分发命令
                         if (products != null && products.Count != 0)
                         {
                             context.Ontology.MessageProvider.SaveCommands(context.Ontology, products.ToArray());
                         }
                     }
                     #endregion
                 }
             }
             // 执行失败
             else
             {
                 using (var cmdProviderAct = new WfAct(context.Host, context, context.Ontology.MessageProvider, "保存执行失败的命令"))
                 {
                     context.Ontology.MessageProvider.SaveCommand(context.Ontology, context.Command.ToExecuteFailing(context.Result));
                     if (context.Command is IEntity)
                     {
                         context.Ontology.MessageProvider.DeleteCommand(MessageTypeKind.Received, context.Ontology, context.Command.Id, context.Command.IsDumb);
                     }
                 }
             }
         }
     }
     #endregion
 }