Exemplo n.º 1
0
        private void IsAlive()
        {
            lock (Locker)
            {
                if (_lastIsAliveRequestOn.AddMinutes(this.BeatPeriod) < DateTime.Now)
                {
                    var isAliveContext = new BeatContext(this, BeatRequest.V1Request);

                    this.Transfer.IsAlive(isAliveContext);

                    if (isAliveContext.Exception != null)
                    {
                        this.IsNetException = true;
                        this.ServiceIsNotAlive = true;
                        var anyLog = new AnyLog(Guid.NewGuid())
                        {
                            Req_Ontology = "IsAlive",
                            Req_Verb = string.Empty,
                            Req_ClientId = string.Empty,
                            Req_ClientType = string.Empty,
                            CreateOn = DateTime.Now,
                            Req_Description = string.Empty,
                            Req_EventSourceType = string.Empty,
                            Req_EventSubjectCode = string.Empty,
                            InfoFormat = "json",
                            Req_InfoId = string.Empty,
                            Req_InfoValue = string.Empty,
                            Req_UserName = string.Empty,
                            Req_IsDumb = false,
                            LocalEntityId = string.Empty,
                            CatalogCode = string.Empty,
                            Req_ReasonPhrase = Status.NodeException.ToName(),
                            ReceivedOn = DateTime.Now,
                            Req_MessageId = string.Empty,
                            Req_MessageType = string.Empty,
                            Req_QueryList = string.Empty,
                            Req_Status = (int)Status.NodeException,
                            Req_TimeStamp = DateTime.Now,
                            Req_Version = isAliveContext.Request.Version,
                            Res_InfoValue = string.Empty,
                            Res_Description = isAliveContext.Exception.Message,
                            Res_ReasonPhrase = Status.NodeException.ToName(),
                            Res_StateCode = (int)Status.NodeException
                        };
                        AcDomain.LoggingService.Log(anyLog);
                    }
                    else
                    {
                        this.ServiceIsNotAlive = !isAliveContext.Response.IsAlive;
                    }
                }
                _lastIsAliveRequestOn = DateTime.Now;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 命令响应。进入此方法标示者进入
        /// </summary>
        /// <returns></returns>
        public void Response(MessageContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            try
            {
                if (!context.Result.IsClosed)
                {
                    var requestType = context.Command.MessageType;
                    if (!context.IsValid)
                    {
                        if (!context.Result.ResultDataItems.Any(a => a.Key.Equals("Id", StringComparison.OrdinalIgnoreCase)))
                        {
                            context.Result.ResultDataItems.Add(new DataItem("Id", context.LocalEntityId));
                        }
                        if (context.Command is IEntity)
                        {
                            using (var cmdProviderAct = new WfAct(context.Host, context, context.Ontology.MessageProvider, "保存执行失败的命令"))
                            {
                                if (requestType == MessageType.Action || context.Command.CommandType == MessageTypeKind.Received)
                                {
                                    context.Ontology.MessageProvider.SaveCommand(context.Ontology, context.Command.ToExecuteFailing(context.Result));
                                }
                                else if (requestType == MessageType.Command)
                                {
                                    context.Ontology.MessageProvider.SaveCommand(context.Ontology, context.Command.ToUnaccepted(context.Result));
                                }
                            }
                            context.Ontology.MessageProvider.DeleteCommand(context.Command.CommandType, context.Ontology, context.Command.Id, context.Command.IsDumb);
                        }
                    }
                    else
                    {
                        if (!context.Result.ResultDataItems.Any(a => a.Key.Equals("Id", StringComparison.OrdinalIgnoreCase)))
                        {
                            context.Result.ResultDataItems.Add(new DataItem("Id", context.LocalEntityId));
                        }
                        if (Verb.Get.Equals(context.Command.Verb))
                        {
                            using (var act = new WfAct(context.Host, context, context.Ontology.EntityProvider, "填充get型命令的InfoValue字段"))
                            {
                                var selectElements = new OrderedElementSet();
                                foreach (var element in context.Ontology.Elements.Values.Where(a => a.Element.IsEnabled == 1))
                                {
                                    if (context.ClientAgent.GetElementPermission(element, Verb.Get) != AllowType.NotAllow)
                                    {
                                        selectElements.Add(element);
                                    }
                                }
                                IList<InfoItem> infoValues = context.Ontology.EntityProvider.Get(
                                    context.Ontology, context.LocalEntityId, selectElements);
                                context.Result.ResultDataItems = infoValues.Select(a => new DataItem(a.Key, a.Value)).ToList();
                            }
                        }
                        var nodeHost = context.Host.NodeHost;
                        // ApplyProcessingFilters 应用处理前过滤器
                        ProcessResult result = nodeHost.ApplyEdiMessageHandingFilters(context);
                        if (!result.IsSuccess)
                        {
                            context.Result.UpdateStatus(result.StateCode, result.Description);
                        }
                        if (!context.Result.IsClosed)
                        {
                            switch (requestType)
                            {
                                case MessageType.Action:
                                    HandleAction(context);
                                    break;
                                case MessageType.Command:
                                    HandleCommand(context);
                                    break;
                                case MessageType.Event:
                                    HandleEvent(context);
                                    break;
                                default:
                                    throw new AnycmdException("意外的请求类型" + context.Command.MessageType);
                            }

                            // ApplyProcessedFilters 应用处理后过滤器
                            result = nodeHost.ApplyEdiMessageHandledFilters(context);
                            if (!result.IsSuccess)
                            {
                                context.Result.UpdateStatus(result.StateCode, result.Description);
                            }
                        }
                    }
                }
                int stateCode = context.Result.Status;
                if (stateCode < 200)
                {
                    IInfoStringConverter converter;
                    if (!context.Host.NodeHost.InfoStringConverters.TryGetInfoStringConverter(context.Command.DataTuple.InfoFormat, out converter))
                    {
                        throw new AnycmdException("意外的信息格式" + context.Command.DataTuple.InfoFormat);
                    }
                    var anyLog = new AnyLog(Guid.NewGuid())
                    {
                        Req_Ontology = context.Ontology.Ontology.Code,
                        Req_Verb = context.Command.Verb.Code,
                        Req_ClientId = context.Command.ClientId,
                        Req_ClientType = context.Command.ClientType.ToName(),
                        CreateOn = context.Command.CreateOn,
                        Req_Description = context.Command.Description,
                        Req_EventSourceType = context.Command.EventSourceType,
                        Req_EventSubjectCode = context.Command.EventSubjectCode,
                        InfoFormat = context.Command.DataTuple.InfoFormat,
                        Req_InfoId = context.Command.DataTuple.IdItems.InfoString,
                        Req_InfoValue = context.Command.DataTuple.ValueItems.InfoString,
                        Req_UserName = context.Command.UserName,
                        Req_IsDumb = context.Command.IsDumb,
                        LocalEntityId = context.Command.LocalEntityId,
                        CatalogCode = context.Command.CatalogCode,
                        Req_ReasonPhrase = context.Command.ReasonPhrase,
                        ReceivedOn = context.Command.ReceivedOn,
                        Req_MessageId = context.Command.MessageId,
                        Req_MessageType = context.Command.MessageType.ToName(),
                        Req_QueryList = context.Command.DataTuple.QueryListString,
                        Req_Status = context.Command.Status,
                        Req_TimeStamp = context.Command.TimeStamp,
                        Req_Version = context.Command.Version,
                        Res_InfoValue = converter.ToInfoString(context.Result.ResultDataItems),
                        Res_Description = context.Result.Description,
                        Res_ReasonPhrase = context.Result.ReasonPhrase,
                        Res_StateCode = (int)context.Result.Status
                    };
                    context.Ontology.Host.LoggingService.Log(anyLog);
                }
            }
            catch (Exception ex)
            {
                context.Result.UpdateStatus(Status.InternalServerError, "服务器内部逻辑异常");
                context.Result.IsClosed = true;
                context.Exception = ex;
                throw;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        public virtual void Request(DistributeContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (context.Result.IsClosed)
            {
                return;
            }
            QueryResult result = context.Result;
            if (!CanRequest(context))
            {
                #region 请求条件不成立,不引发分发请求
                context.Ontology.MessageProvider.SaveCommand(context.Ontology, context.Command.ToDistributeFailing(result));
                if (context.Command is IEntity)
                {
                    context.Ontology.MessageProvider.DeleteCommand(MessageTypeKind.Distribute, context.Ontology, context.Command.Id, context.Command.IsDumb);
                }
                return;
                #endregion
            }
            else
            {
                #region 请求
                context.ClientAgent.Transfer.Transmit(context);

                if (context.Exception != null)
                {
                    context.ClientAgent.IsNetException = true;
                    context.Result.UpdateStatus(Status.InternalServerError, context.Exception.Message);
                    context.Ontology.Host.LoggingService.Error(context.Exception);
                }
                else
                {
                    var stateCode = result.Status;
                    if (stateCode < 200)
                    {
                        if (context.Responder == context.Ontology.Host.NodeHost.Nodes.ThisNode) return;
                        IInfoStringConverter converter;
                        if (!context.Ontology.Host.NodeHost.InfoStringConverters.TryGetInfoStringConverter(context.Command.DataTuple.InfoFormat, out converter))
                        {
                            throw new AnycmdException("意外的信息格式" + context.Command.DataTuple.InfoFormat);
                        }
                        var anyLog = new AnyLog(Guid.NewGuid())
                        {
                            Req_Ontology = context.Ontology.Ontology.Code,
                            Req_Verb = context.Command.Verb.Code,
                            Req_ClientId = context.Command.ClientId,
                            Req_ClientType = context.Command.ClientType.ToName(),
                            CreateOn = context.Command.CreateOn,
                            Req_Description = context.Command.Description,
                            Req_EventSourceType = context.Command.EventSourceType,
                            Req_EventSubjectCode = context.Command.EventSubjectCode,
                            InfoFormat = context.Command.DataTuple.InfoFormat,
                            Req_InfoId = context.Command.DataTuple.IdItems.InfoString,
                            Req_InfoValue = context.Command.DataTuple.ValueItems.InfoString,
                            Req_UserName = context.Command.UserName,
                            Req_IsDumb = context.Command.IsDumb,
                            LocalEntityId = context.Command.LocalEntityId,
                            CatalogCode = context.Command.CatalogCode,
                            Req_ReasonPhrase = context.Command.ReasonPhrase,
                            ReceivedOn = context.Command.ReceivedOn,
                            Req_MessageId = context.Command.MessageId,
                            Req_MessageType = context.Command.MessageType.ToName(),
                            Req_QueryList = context.Command.DataTuple.QueryListString,
                            Req_Status = context.Command.Status,
                            Req_TimeStamp = context.Command.TimeStamp,
                            Req_Version = context.Command.Version,
                            Res_InfoValue = converter.ToInfoString(context.Result.ResultDataItems),
                            Res_Description = context.Result.Description,
                            Res_ReasonPhrase = context.Result.ReasonPhrase,
                            Res_StateCode = (int)context.Result.Status
                        };
                        context.Ontology.Host.LoggingService.Log(anyLog);
                    }
                    else if (stateCode >= 500)
                    {
                        context.ClientAgent.ServiceIsNotAlive = true;
                    }
                    else
                    {
                        if (stateCode >= 200 && stateCode < 300)
                        {
                            if (context.Command.CommandType == MessageTypeKind.Distribute)
                            {
                                context.Ontology.MessageProvider.SaveCommand(context.Ontology, context.Command.ToDistributed(result));
                            }
                        }
                        else
                        {
                            context.Ontology.MessageProvider.SaveCommand(context.Ontology, context.Command.ToDistributeFailing(result));
                        }
                        if (context.Command is IEntity)
                        {
                            context.Ontology.MessageProvider.DeleteCommand(MessageTypeKind.Distribute, context.Ontology, context.Command.Id, context.Command.IsDumb);
                        }
                    }
                }
                #endregion
            }
        }