コード例 #1
0
        /// <summary>
        /// 命令解包
        /// </summary>
        /// <param name="showError"></param>
        /// <param name="messages"></param>
        /// <returns></returns>
        public static ZeroResultData Unpack(this ZMessage messages, bool showError = false)
        {
            try
            {
                var description = messages[0].Read();
                if (description.Length == 0)
                {
                    if (showError)
                    {
                        ZeroTrace.WriteError("Unpack", "LaoutError",
                                             description.LinkToString(p => p.ToString("X2"), ""));
                    }
                    return(new ZeroResultData
                    {
                        State = ZeroOperatorStateType.FrameInvalid,
                        Message = "网络格式错误"
                    });
                }

                int end = description[0] + 1;
                if (end != messages.Count)
                {
                    if (showError)
                    {
                        ZeroTrace.WriteError("Unpack", "LaoutError",
                                             $"FrameSize{messages.Count}", description.LinkToString(p => p.ToString("X2"), ""));
                    }
                    return(new ZeroResultData
                    {
                        State = ZeroOperatorStateType.FrameInvalid,
                        Message = "网络格式错误"
                    });
                }

                var result = new ZeroResultData
                {
                    InteractiveSuccess = true,
                    State = (ZeroOperatorStateType)description[1]
                };
                for (int idx = 1; idx < end; idx++)
                {
                    result.Add(description[idx + 1], Encoding.UTF8.GetString(messages[idx].Read()));
                }

                return(result);
            }
            catch (Exception e)
            {
                ZeroTrace.WriteException("Unpack", e);
                return(new ZeroResultData
                {
                    State = ZeroOperatorStateType.LocalRecvError,
                    Exception = e
                });
            }
            finally
            {
                messages.Dispose();
            }
        }
コード例 #2
0
        /// <summary>
        ///     一次请求
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="desicription"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static ZeroResultData SendTo(this ZSocket socket, byte[] desicription, params string[] args)
        {
            var message = new ZMessage
            {
                new ZFrame(desicription)
            };

            if (args != null)
            {
                foreach (var arg in args)
                {
                    message.Add(new ZFrame((arg).ToZeroBytes()));
                }
            }
            if (!socket.SendTo(message))
            {
#if DEBUG
                ZeroTrace.WriteError("SendTo", /*error.Text,*/ socket.Connects.LinkToString(','),
                                     $"Socket Ptr:{socket.SocketPtr}");
#endif
                return(new ZeroResultData
                {
                    State = ZeroOperatorStateType.LocalRecvError,
                    ZmqError = socket.LastError
                });
            }
            return(new ZeroResultData
            {
                State = ZeroOperatorStateType.Ok,
                InteractiveSuccess = true
            });
        }
コード例 #3
0
        /// <summary>
        ///     读取配置
        /// </summary>
        /// <returns></returns>
        internal StationConfig LoadConfig(string stationName)
        {
            if (!ZeroApplication.ZerCenterIsRun)
            {
                ZeroTrace.WriteError("LoadConfig", "No ready");
                return(null);
            }
            var result = CallCommand("host", stationName);

            if (!result.InteractiveSuccess || result.State != ZeroOperatorStateType.Ok)
            {
                ZeroTrace.WriteError("LoadConfig", result);
                return(null);
            }

            var json = result.GetValue(ZeroFrameType.Status);

            if (json == null || json[0] != '{')
            {
                ZeroTrace.WriteError("LoadConfig", stationName, "not a json", json);
                return(null);
            }

            if (ZeroApplication.Config.UpdateConfig(stationName, json, out var config))
            {
                return(config);
            }
            return(null);
        }
コード例 #4
0
        /// <summary>
        ///     接收文本
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="showError">是否显示错误</param>
        /// <returns></returns>
        public static ZeroResultData ReceiveString(this ZSocket socket, bool showError = false)
        {
            ZMessage messages;

            try
            {
                if (!socket.Recv(out messages))
                {
                    if (showError)
                    {
                        ZeroTrace.WriteError("Receive", socket.Connects.LinkToString(','), socket.LastError.Text);
                    }
                    return(new ZeroResultData
                    {
                        State = ZeroOperatorStateType.LocalRecvError,
                        ZmqError = socket.LastError
                    });
                }
            }
            catch (Exception e)
            {
                ZeroTrace.WriteException("Receive", e, socket.Connects.LinkToString(','));
                return(new ZeroResultData
                {
                    State = ZeroOperatorStateType.LocalRecvError,
                    Exception = e
                });
            }
            return(Unpack(messages, showError));
        }
コード例 #5
0
        /// <summary>
        /// 尝试安装站点
        /// </summary>
        /// <param name="station"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public bool TryInstall(string station, string type)
        {
            if (ZeroApplication.Config.TryGetConfig(station, out _))
            {
                return(true);
            }
            ZeroTrace.WriteInfo(station, "No find,try install ...");
            var r = CallCommand("install", type, station, station, station);

            if (!r.InteractiveSuccess)
            {
                ZeroTrace.WriteError(station, "Test install failed");
                return(false);
            }

            if (r.State != ZeroOperatorStateType.Ok && r.TryGetValue(ZeroFrameType.Status, out var json))
            {
                ZeroApplication.Config.UpdateConfig(station, json, out _);
            }
            ZeroTrace.WriteInfo(station, "Is install ,try start it ...");
            r = CallCommand("start", station);
            if (!r.InteractiveSuccess && r.State != ZeroOperatorStateType.Ok && r.State != ZeroOperatorStateType.Runing)
            {
                ZeroTrace.WriteError(station, "Can't start station");
                return(false);
            }
            LoadConfig(station);
            ZeroTrace.WriteInfo(station, "Station runing");
            return(true);
        }
コード例 #6
0
ファイル: ZeroPublishExtend.cs プロジェクト: ym1100/MicroZero
 /// <summary>
 ///     发送广播
 /// </summary>
 /// <param name="description"></param>
 /// <param name="title"></param>
 /// <param name="content"></param>
 /// <param name="socket"></param>
 /// <returns></returns>
 public static bool Publish(this ZSocket socket, byte[] description, string title, byte[] content)
 {
     if (socket == null)
     {
         return(false);
     }
     try
     {
         if (!socket.SendTo(description,
                            title.ToZeroBytes(),
                            GlobalContext.RequestInfo.RequestId.ToZeroBytes(),
                            ZeroApplication.Config.RealName.ToZeroBytes(),
                            content))
         {
             ZeroTrace.WriteError("Pub", socket.LastError.Text, socket.Connects.LinkToString(','), title);
             return(false);
         }
         var result = socket.ReceiveString();
         return(result.InteractiveSuccess && result.State == ZeroOperatorStateType.Ok);
     }
     catch (Exception e)
     {
         ZeroTrace.WriteException("Pub", e, socket.Connects.LinkToString(','), $"Socket Ptr:{socket.SocketPtr}");
         return(false);
     }
 }
コード例 #7
0
 /// <summary>
 ///     发送广播
 /// </summary>
 /// <param name="title"></param>
 /// <param name="socket"></param>
 /// <returns></returns>
 public static bool Publish(this ZSocket socket, string title)
 {
     if (socket == null)
     {
         return(false);
     }
     try
     {
         if (!socket.SendTo(PubDescriptionEmpty,
                            ToZeroBytes(title),
                            ToZeroBytes(ApiContext.RequestContext.RequestId),
                            ToZeroBytes(ZeroApplication.Config.RealName))
             )
         {
             ZeroTrace.WriteError("Pub", socket.LastError.Text, socket.Connects.LinkToString(','), title);
             return(false);
         }
         var result = socket.ReceiveString();
         return(result.InteractiveSuccess && result.State == ZeroOperatorStateType.Ok);
     }
     catch (Exception e)
     {
         ZeroTrace.WriteException("Pub", e, socket.Connects.LinkToString(','), $"Socket Ptr:{socket.SocketPtr}");
         return(false);
     }
 }
コード例 #8
0
        /// <summary>
        ///     接收文本
        /// </summary>
        /// <param name="socket"></param>
        /// <returns></returns>
        public static ZeroResultData <byte[]> ReceiveUnknow(this ZSocket socket)
        {
            //tryCnt = 0;
            ZMessage messages;

            try
            {
                if (!socket.Recv(out messages))
                {
                    ZeroTrace.WriteError("Receive", socket.Connects.LinkToString(','), socket.LastError.Text);
                    return(new ZeroResultData <byte[]>
                    {
                        State = ZeroOperatorStateType.LocalRecvError,
                        ZmqError = socket.LastError
                    });
                }
            }
            catch (Exception e)
            {
                ZeroTrace.WriteException("Receive", e, socket.Connects.LinkToString(','));
                return(new ZeroResultData <byte[]>
                {
                    State = ZeroOperatorStateType.LocalRecvError,
                    Exception = e
                });
            }

            try
            {
                var result = new ZeroResultData <byte[]>
                {
                    InteractiveSuccess = true,
                    State = ZeroOperatorStateType.Ok
                };
                foreach (var frame in messages)
                {
                    result.Add(ZeroFrameType.BinaryValue, frame.Read());
                }

                return(result);
            }
            catch (Exception e)
            {
                ZeroTrace.WriteException("Receive", e, socket.Connects.LinkToString(','), $"FrameSize{messages.Count}, Socket Ptr:{ socket.SocketPtr}.");
                return(new ZeroResultData <byte[]>
                {
                    State = ZeroOperatorStateType.LocalRecvError,
                    Exception = e
                });
            }
            finally
            {
                messages.Dispose();
            }
        }
コード例 #9
0
 private static void center_closing(string content)
 {
     if (Interlocked.CompareExchange(ref ZeroApplication._appState, StationState.Closing, StationState.Run) == StationState.Run)
     {
         ZeroTrace.WriteError("center_close", content);
         ZeroApplication.RaiseEvent(ZeroNetEventType.CenterSystemClosing);
         ZeroApplication.ZerCenterStatus = ZeroCenterState.Closed;
         ZeroApplication.OnZeroEnd();
         ZeroApplication.ApplicationState = StationState.Failed;
     }
 }
コード例 #10
0
ファイル: IZeroObject.cs プロジェクト: ym1100/MicroZero
 /// <summary>
 ///     对象关闭时登记
 /// </summary>
 public static void OnObjectFailed(IZeroObject obj)
 {
     lock (ActiveObjects)
     {
         FailedObjects.Add(obj);
         ZeroTrace.WriteError(obj.Name, "Failed");
         if (ActiveObjects.Count + FailedObjects.Count == ZeroObjects.Count)
         {
             ActiveSemaphore.Release(); //发出完成信号
         }
     }
 }
コード例 #11
0
        /// <summary>
        ///     读取配置
        /// </summary>
        /// <returns></returns>
        public bool LoadAllConfig()
        {
            var result = CallCommand("host", "*");

            if (!result.InteractiveSuccess || result.State != ZeroOperatorStateType.Ok)
            {
                ZeroTrace.WriteError("LoadConfig", result);
                return(false);
            }
            if (!result.TryGetValue(ZeroFrameType.Status, out var json))
            {
                ZeroTrace.WriteError("LoadAllConfig", "Empty");
                return(false);
            }
            return(ZeroApplication.Config.FlushConfigs(json));
        }
コード例 #12
0
ファイル: ZeroApplication.cs プロジェクト: ym1100/MicroZero
        /// <summary>
        ///     命令行方式管理
        /// </summary>
        public static void CommandConsole()
        {
            while (true)
            {
                var cmd = Console.ReadLine();
                if (String.IsNullOrWhiteSpace(cmd))
                {
                    continue;
                }
                switch (cmd.Trim().ToLower())
                {
                case "quit":
                case "exit":
                    Shutdown();
                    return;

                case "start":
                    Start();
                    continue;
                }

                var words = cmd.Split(' ', '\t', '\r', '\n');
                if (words.Length == 0)
                {
                    Console.WriteLine("请输入正确命令");
                    continue;
                }

                var result = SystemManager.Instance.CallCommand(words);
                if (result.InteractiveSuccess)
                {
                    ZeroTrace.SystemLog("Console", result.TryGetValue(ZeroFrameType.Status, out var value)
                        ? value
                        : result.State.Text());
                }
                else
                {
                    ZeroTrace.WriteError("Console", result.TryGetValue(ZeroFrameType.Status, out var value)
                        ? value
                        : result.State.Text());
                }
            }
        }
コード例 #13
0
 /// <summary>
 /// 试着取配置
 /// </summary>
 /// <param name="stationName"></param>
 /// <param name="json"></param>
 /// <param name="config"></param>
 /// <returns></returns>
 public bool UpdateConfig(string stationName, string json, out StationConfig config)
 {
     if (stationName == null || string.IsNullOrEmpty(json) || json[0] != '{')
     {
         ZeroTrace.WriteError("UpdateConfig", "argument error", stationName, json);
         config = null;
         return(false);
     }
     try
     {
         config = JsonConvert.DeserializeObject <StationConfig>(json);
         AddStation(config);
         return(true);
     }
     catch (Exception e)
     {
         ZeroTrace.WriteException("UpdateConfig", e, stationName, json);
         config = null;
         return(false);
     }
 }
コード例 #14
0
        /// <summary>
        ///     下载文档
        /// </summary>
        /// <returns></returns>
        public bool LoadDocument(string name, out StationDocument doc)
        {
            ZeroResultData result;

            try
            {
                result = CallCommand("doc", name);
                if (!result.InteractiveSuccess || result.State != ZeroOperatorStateType.Ok)
                {
                    ZeroTrace.WriteError("LoadDocument", result);
                    doc = null;
                    return(false);
                }
            }
            catch (Exception e)
            {
                ZeroTrace.WriteException("LoadDocument", e);
                doc = null;
                return(false);
            }
            if (!result.TryGetValue(ZeroFrameType.Status, out var json))
            {
                ZeroTrace.WriteError("LoadDocument", "Empty");
                doc = null;
                return(false);
            }
            try
            {
                doc = JsonConvert.DeserializeObject <StationDocument>(json);
                return(true);
            }
            catch (Exception e)
            {
                ZeroTrace.WriteException("LoadDocument", e, json);
                doc = null;
                return(false);
            }
        }
コード例 #15
0
        /// <summary>
        ///     上传文档
        /// </summary>
        /// <returns></returns>
        public bool UploadDocument()
        {
            if (DocumentIsUpload)
            {
                return(true);
            }
            bool success = true;

            foreach (var doc in ZeroApplication.Config.Documents.Values)
            {
                if (!doc.IsLocal)
                {
                    continue;
                }
                var result = CallCommand("doc", doc.Name, JsonConvert.SerializeObject(doc));
                if (!result.InteractiveSuccess || result.State != ZeroOperatorStateType.Ok)
                {
                    ZeroTrace.WriteError("UploadDocument", result);
                    success = false;
                }
            }
            DocumentIsUpload = success;
            return(success);
        }
コード例 #16
0
        /// <summary>
        ///     接收字节
        /// </summary>
        /// <param name="socket"></param>
        /// <returns></returns>
        public static ZeroResultData <byte[]> Receive(this ZSocket socket)
        {
            ZMessage messages;

            try
            {
                if (!socket.Recv(out messages))
                {
                    return(new ZeroResultData <byte[]>
                    {
                        State = ZeroOperatorStateType.LocalRecvError,
                        ZmqError = socket.LastError
                    });
                }
            }
            catch (Exception e)
            {
                ZeroTrace.WriteException("Receive", e, socket.Connects.LinkToString(','), "Exception");
                return(new ZeroResultData <byte[]>
                {
                    State = ZeroOperatorStateType.LocalRecvError,
                    Exception = e
                });
            }

            try
            {
                var description = messages[0].Read();
                if (description.Length < 2)
                {
                    ZeroTrace.WriteError("Receive", "LaoutError", socket.Connects.LinkToString(','), description.LinkToString(p => p.ToString("X2"), ""));
                    return(new ZeroResultData <byte[]>
                    {
                        State = ZeroOperatorStateType.FrameInvalid,
                        Message = "网络格式错误"
                    });
                }

                int end = description[0] + 1;
                if (end != messages.Count)
                {
                    ZeroTrace.WriteError("Receive", "LaoutError", socket.Connects.LinkToString(','), $"FrameSize{messages.Count}", description.LinkToString(p => p.ToString("X2"), ""));
                    return(new ZeroResultData <byte[]>
                    {
                        State = ZeroOperatorStateType.FrameInvalid,
                        Message = "网络格式错误"
                    });
                }

                var result = new ZeroResultData <byte[]>
                {
                    InteractiveSuccess = true,
                    State = (ZeroOperatorStateType)description[1]
                };
                for (int idx = 1; idx < end; idx++)
                {
                    result.Add(description[idx + 1], messages[idx].Read());
                }

                return(result);
            }
            catch (Exception e)
            {
                ZeroTrace.WriteException("Receive", e, socket.Connects.LinkToString(','), $"FrameSize{messages.Count},Socket Ptr:{ socket.SocketPtr}.");
                return(new ZeroResultData <byte[]>
                {
                    State = ZeroOperatorStateType.LocalRecvError,
                    Exception = e
                });
            }
            finally
            {
                messages.Dispose();
            }
        }