Beispiel #1
0
        /// <summary>
        ///     进入系统侦听
        /// </summary>
        public static void RunMonitor()
        {
            var timeout = new TimeSpan(0, 0, 1);

            try
            {
                StationProgram.WriteLine("System Monitor Runing...");
                var subscriber = new SubscriberSocket();
                subscriber.Options.Identity          = StationProgram.Config.StationName.ToAsciiBytes();
                subscriber.Options.ReconnectInterval = new TimeSpan(0, 0, 0, 0, 200);
                subscriber.Connect(StationProgram.ZeroMonitorAddress);
                subscriber.Subscribe("");

                while (StationProgram.State == StationState.Run)
                {
                    if (!subscriber.TryReceiveFrameString(timeout, out var result))
                    {
                        continue;
                    }
                    OnMessagePush(result);
                }
            }
            catch (Exception e)
            {
                StationProgram.WriteLine(e.Message);
                LogRecorder.Exception(e);
            }
            if (StationProgram.State == StationState.Run)
            {
                Task.Factory.StartNew(RunMonitor);
            }
        }
Beispiel #2
0
        private static void station_join(string station, string content)
        {
            if (String.IsNullOrEmpty(content))
            {
                return;
            }
            StationConfig cfg;

            try
            {
                cfg = JsonConvert.DeserializeObject <StationConfig>(content);
            }
            catch (Exception e)
            {
                LogRecorder.Exception(e);
                return;
            }
            cfg.State = StationState.Run;
            if (StationProgram.Configs.ContainsKey(station))
            {
                StationProgram.Configs[station] = cfg;
            }
            else
            {
                StationProgram.Configs.Add(station, cfg);
            }
            if (StationProgram.Stations.ContainsKey(station))
            {
                StationProgram.Stations[station].Config = cfg;
                StationProgram.WriteLine($"{station} is join");
                StationEvent?.Invoke(cfg, new StationEventArgument("station_join", cfg));
                ZeroStation.Run(StationProgram.Stations[station]);
            }
        }
Beispiel #3
0
 /// <summary>
 /// 注册方法
 /// </summary>
 /// <param name="name"></param>
 /// <param name="action"></param>
 public void RegistAction(string name, ApiAction action)
 {
     if (!_apiActions.ContainsKey(name))
     {
         action.Name = name;
         _apiActions.Add(name, action);
         StationProgram.WriteLine($"{StationName}:{name} is registed");
     }
 }
Beispiel #4
0
 /// <summary>
 /// Ö´ÐÐ
 /// </summary>
 /// <param name="station"></param>
 public static void Run(ZeroStation station)
 {
     station.Close();
     station.Config = StationProgram.GetConfig(station.StationName);
     if (station.Config == null)
     {
         StationProgram.WriteLine($"{station.StationName} not find,try install...");
         StationProgram.InstallApiStation(station.StationName);
         return;
     }
     station.Run();
 }
Beispiel #5
0
 private static void system_stop(string content)
 {
     StationEvent?.Invoke(null, new StationEventArgument("system_stop", null));
     StationProgram.WriteLine(content);
     foreach (var sta in StationProgram.Stations.Values)
     {
         StationProgram.WriteLine($"Close {sta.StationName}");
         sta.Close();
         StationEvent?.Invoke(sta, new StationEventArgument("station_closing", sta.Config));
     }
     StationProgram.Configs.Clear();
 }
Beispiel #6
0
        private static void system_start(string content)
        {
            StationProgram.WriteLine(content);
            StationProgram.Configs.Clear();

            StationEvent?.Invoke(null, new StationEventArgument("system_start", null));
            foreach (var sta in StationProgram.Stations.Values)
            {
                StationProgram.WriteLine($"Restart {sta.StationName}");
                ZeroStation.Run(sta);
                StationEvent?.Invoke(sta, new StationEventArgument("station_join", sta.Config));
            }
        }
Beispiel #7
0
        private static void station_left(string station)
        {
            if (StationProgram.Configs.TryGetValue(station, out var cfg))
            {
                cfg.State = StationState.Closed;
            }
            if (StationProgram.Stations.ContainsKey(station))
            {
                StationProgram.WriteLine($"{station} is left");
                StationProgram.Stations[station].Close();
            }

            StationEvent?.Invoke(cfg, new StationEventArgument("station_left", cfg));
        }
Beispiel #8
0
        private static void station_resume(string station, string content)
        {
            if (StationProgram.Configs.TryGetValue(station, out var cfg))
            {
                cfg.State = StationState.Run;
            }
            if (StationProgram.Stations.ContainsKey(station))
            {
                StationProgram.WriteLine($"{station} is resume");
                ZeroStation.Run(StationProgram.Stations[station]);
            }

            StationEvent?.Invoke(cfg, new StationEventArgument("station_resume", cfg));
        }
Beispiel #9
0
        /// <summary>
        /// 命令轮询
        /// </summary>
        /// <returns></returns>
        private void PollTask()
        {
            _socket.SendString("*", "*");
            _inPoll = true;
            StationProgram.WriteLine($"【{StationName}】poll start");
            //var timeout = new TimeSpan(0, 0, 5);
            try
            {
                while (RunState == StationState.Run)
                {
                    //if (!socket.Poll(timeout))
                    //    continue;
                    //if (!socket.HasIn)
                    //    continue;
                    //string caller = socket.ReceiveFrameString();
                    //string command = socket.ReceiveFrameString();
                    //string argument = socket.ReceiveFrameString();
                    //string response = ExecCommand(command, argument);
                    //socket.SendMoreFrame(caller);
                    if (!_socket.ReceiveString(out var arg, 0))
                    {
                        continue;
                    }
                    _items.Push(arg);
                }
            }
            catch (Exception e)
            {
                StationProgram.WriteLine($"【{StationName}】poll error{e.Message}...");
                LogRecorder.Exception(e);
                RunState = StationState.Failed;
            }
            _inPoll = false;
            StationProgram.WriteLine($"【{StationName}】poll stop");

            _items.Save(CacheFileName);
            CloseSocket();
        }
Beispiel #10
0
        /// <summary>
        /// 心跳
        /// </summary>
        /// <returns></returns>
        private void HeartbeatTask()
        {
            _inHeart = true;
            var errorCount = 0;

            StationProgram.WriteLine($"【{StationName}】heartbeat start");
            try
            {
                while (RunState == StationState.Run)
                {
                    Thread.Sleep(5000);
                    var result = Config.HeartAddress.RequestNet("@", RealName);
                    if (string.IsNullOrWhiteSpace(result))
                    {
                        if (++errorCount > 3)
                        {
                            RunState = StationState.Failed;
                        }
                        StationProgram.WriteLine($"【{StationName}】heartbeat error{errorCount}...");
                    }
                    else if (errorCount > 0)
                    {
                        errorCount = 0;
                        StationProgram.WriteLine($"【{StationName}】heartbeat resume...");
                    }
                }
                //退出
                Config.HeartAddress.RequestNet("-", RealName);
            }
            catch (Exception e)
            {
                StationProgram.WriteLine($"【{StationName}】heartbeat error{e.Message}...");
                RunState = StationState.Failed;
                LogRecorder.Exception(e);
            }
            StationProgram.WriteLine($"【{StationName}】heartbeat stop");
            _inHeart = false;
        }
Beispiel #11
0
        /// <summary>
        /// 命令轮询
        /// </summary>
        /// <returns></returns>
        private void OnStop()
        {
            if (StationProgram.State != StationState.Run)
            {
                return;
            }
            bool restart;

            lock (this)
            {
                restart = RunState == StationState.Failed || RunState == StationState.Start;
            }
            if (!restart)
            {
                return;
            }
            StationProgram.WriteLine($"【{StationName}】restart...");
            if (Run())
            {
                return;
            }
            Thread.Sleep(1000);
            OnStop();
        }
Beispiel #12
0
        /// <inheritdoc />
        /// <summary>
        /// 命令轮询
        /// </summary>
        /// <returns></returns>
        public sealed override bool Run()
        {
            lock (this)
            {
                if (RunState != StationState.Run)
                {
                    RunState = StationState.Start;
                }
                if (_socket != null)
                {
                    return(false);
                }
            }
            _inPoll  = false;
            _inHeart = false;

            _socket = new RequestSocket();
            try
            {
                _socket.Options.Identity          = RealName.ToAsciiBytes();
                _socket.Options.ReconnectInterval = new TimeSpan(0, 0, 0, 0, 200);
                _socket.Connect(Config.InnerAddress);
            }
            catch (Exception e)
            {
                LogRecorder.Exception(e);
                RunState = StationState.Failed;
                CloseSocket();
                StationProgram.WriteLine($"【{StationName}】connect error =>{e.Message}");
                return(false);
            }
            try
            {
                var word = _socket.Request("@", JsonConvert.SerializeObject(Config));
                if (word != "wecome")
                {
                    CloseSocket();
                    RunState = StationState.Failed;
                    StationProgram.WriteLine($"【{StationName}】proto error");
                    return(false);
                }
            }
            catch (Exception e)
            {
                LogRecorder.Exception(e);
                RunState = StationState.Failed;
                CloseSocket();
                StationProgram.WriteLine($"【{StationName}】request error =>{e.Message}");
                return(false);
            }

            RunState = StationState.Run;
            _items   = MulitToOneQueue <List <string> > .Load(CacheFileName);

            Task.Factory.StartNew(CommandTask);

            var task1 = Task.Factory.StartNew(PollTask);

            task1.ContinueWith(OnTaskStop);

            var task2 = Task.Factory.StartNew(HeartbeatTask);

            task2.ContinueWith(OnTaskStop);

            while (!_inHeart || !_inPoll)
            {
                Thread.Sleep(50);
            }
            StationProgram.WriteLine($"【{StationName}:{RealName}】runing...");
            return(true);
        }