Пример #1
0
        public StatusResponseInfo GetStatus()
        {
            var stat = GetResponse(new GetStatusInfo());

            if (String.IsNullOrWhiteSpace(stat))
            {
                return new StatusResponseInfo {
                           BotName = "NO RESPONSE"
                }
            }
            ;
            StatusResponseInfo response;

            try
            {
                response = JsonConvert.DeserializeObject <StatusResponseInfo>(stat);
            }
            catch
            {
                response = new StatusResponseInfo {
                    Status = stat
                };
            }
            response.IP   = _ip;
            response.Port = _port;
            return(response);
        }
Пример #2
0
        public static void MonitorDebugBotStatus()
        {
            var conn = new TcpAdminConnection(BotConnectionInfo.DebugIP, BotConnectionInfo.DebugPort);

            while (true)
            {
                try
                {
                    _debugBotStat = conn.GetStatus();
                }
                catch (Exception e)
                {
                    _debugBotStat = new StatusResponseInfo {
                        BotName = e.Message
                    };
                }
                finally
                {
                    Thread.Sleep(800);
                }
            }
        }
Пример #3
0
        private static void StatusServerOnDataReceived(object sender, Message message)
        {
            try
            {
                var messages = message.MessageString.Split('\u0013');
                foreach (var msg in messages)
                {
                    if (String.IsNullOrWhiteSpace(msg) || String.IsNullOrWhiteSpace(msg.Replace("\0", "")))
                    {
                        continue;
                    }
                    dynamic m     = JsonConvert.DeserializeObject(msg);
                    string  t     = m.JType?.ToString();
                    var     nodes = Bot.Nodes.ToList();

                    if (t != null)
                    {
                        //Console.WriteLine(t);
                        switch (t)
                        {
                        case "GetStatusInfo":
                            //web api is requesting current status
                            //Console.WriteLine("Getting Status");
                            var status = new StatusResponseInfo
                            {
                                BotName = Bot.Me.Username,
                                MessagesProcPerSecond = Program.MessagesProcessed.FirstOrDefault(),
                                MessagesPerSecondOut  = Program.MessagesSent.FirstOrDefault(),
                                MessagesPerSecondIn   = Program.MessagesReceived.FirstOrDefault(),
                                MaxGames     = Program.MaxGames,
                                MaxGamesTime = Program.MaxTime,
                                Nodes        = nodes.Select(n => new NodeResponseInfo
                                {
                                    MessagesSent   = n.MessagesSent,
                                    ClientId       = n.ClientId,
                                    CurrentGames   = n.CurrentGames,
                                    CurrentPlayers = n.CurrentPlayers,
                                    Games          = n.Games.Select(x => new GameListInfo {
                                        GroupId = x.GroupId, GroupName = x.ChatGroup, NumPlayers = x.PlayerCount, PlayersAlive = x.Users.Count, State = x.State
                                    }).ToList(),
                                    ShuttingDown = n.ShuttingDown,
                                    Uptime       = n.Uptime,
                                    Version      = n.Version
                                }).ToList(),
                                NumGames   = nodes.Sum(x => x.CurrentGames),
                                NumPlayers = nodes.Sum(x => x.CurrentPlayers),
                                Uptime     = DateTime.UtcNow - Bot.StartTime,
                                Status     = Bot.CurrentStatus
                            };
                            message.Reply(JsonConvert.SerializeObject(status));
                            break;

                        case "GetNodeInfo":
                            var gni  = JsonConvert.DeserializeObject <GetNodeInfo>(msg);
                            var node = nodes.FirstOrDefault(x => x.ClientId == gni.ClientId);
                            if (node == null)
                            {
                                message.Reply("null");
                                return;
                            }
                            var nodeInfo = new NodeResponseInfo
                            {
                                MessagesSent   = node.MessagesSent,
                                ClientId       = node.ClientId,
                                CurrentGames   = node.CurrentGames,
                                CurrentPlayers = node.CurrentPlayers,
                                Games          = node.Games.Select(x => new GameListInfo {
                                    GroupId = x.GroupId, GroupName = x.ChatGroup, NumPlayers = x.PlayerCount, PlayersAlive = x.Users.Count, State = x.State
                                }).ToList(),
                                ShuttingDown = node.ShuttingDown,
                                Uptime       = node.Uptime,
                                Version      = node.Version
                            };
                            message.Reply(JsonConvert.SerializeObject(nodeInfo));
                            break;

                        case "GetGameInfo":
                            var ggi = JsonConvert.DeserializeObject <GetGameInfo>(msg);
                            //get the node

                            var gamenode = Bot.Nodes.FirstOrDefault(x => x.ClientId == ggi.ClientId);
                            var game     = gamenode?.GetGameInfo(ggi);

                            if (game == null)
                            {
                                message.Reply("null");
                                return;
                            }
                            var response = JsonConvert.SerializeObject(game);
                            using (var sw = new StreamWriter(Path.Combine(Bot.RootDirectory, "..\\tcpadmin.log"), true))
                            {
                                sw.WriteLine("Control Replying to GetGameInfo with:\n" + response + "\n\n");
                            }

                            message.Reply(response);
                            break;

                        case "StopNodeRequest":
                            var snr = JsonConvert.DeserializeObject <StopNodeRequest>(msg);
                            Bot.Nodes.FirstOrDefault(x => x.ClientId == snr.ClientId)?.ShutDown();
                            break;

                        default:
                            message.Reply("null");
                            break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                // ignored
                while (e.InnerException != null)
                {
                    e = e.InnerException;
                }
                //Console.WriteLine(e.Message);
                using (var sw = new StreamWriter(Path.Combine(Bot.RootDirectory, "..\\Logs\\tcperror.log"), true))
                    sw.WriteLine(e.Message + "\n" + e.StackTrace + "\n");
            }
            finally
            {
            }
        }