Esempio n. 1
0
        private async Task ProcessCommands()
        {
            while (true)
            {
                var command = await link.ReceiveCommand();

                switch (command.Name)
                {
                case "kick":
                    await link.SendCommand(new Command("-", "killing session " + command.Text, -1));

                    sessionStorage[command.Text]?.Kill();
                    break;

                case "kill":
                    await link.SendCommand(new Command("-", "killing node " + command.Text, -1));

                    sessionStorage[command.Text]?.Kill(true);
                    break;

                case "list":
                    foreach (var nick in sessionStorage.ListAlive())
                    {
                        await link.SendCommand(new Command("-", $"'{nick}': stable = {heartbeatStorage.IsStable(nick)}", -1));
                    }
                    break;

                case "hist":
                    var parts    = command.Text.Split('|');
                    var askWho   = parts[0];
                    var askAbout = parts[1];
                    var session  = sessionStorage[askWho];
                    if (session == null)
                    {
                        await link.SendCommand(new Command("-", "there's no session with " + askWho, -1));
                    }
                    else
                    {
                        var watch    = Stopwatch.StartNew();
                        var response = await session.SendCommandWithResponse(Commands.History, askAbout, TimeSpan.FromMinutes(9));

                        if (response == null)
                        {
                            await link.SendCommand(new Command("-", "timed out", -1));
                        }
                        else
                        {
                            await link.SendCommand(new Command("-", "took " + watch.Elapsed, -1));

                            foreach (var line in response)
                            {
                                await link.SendCommand(new Command("-", line, -1));
                            }
                        }
                    }
                    break;
                }
            }
        }
Esempio n. 2
0
        public async Task HandleCommand(Command command, Session session)
        {
            Group group;

            switch (command.Name)
            {
            case Commands.Heartbeat:
                if (!TrySetNick(command.Text) || !sessionStorage.TryRegister(nick, session))
                {
                    log.Info("Failed to set nick '{nick}' for client at {endpoint}. Nick is already in use. Killing session..",
                             command.Text, session.RemoteEndpoint);
                    await session.Kill(true);

                    return;
                }

                heartbeatStorage.RegisterHeartbeat(nick);
                session.SendResponse(command.Id, "OK");
                break;

            case Commands.End:
                heartbeatStorage.RemoveSession(nick);
                await session.Kill();

                break;

            case Commands.Say:
                group = Group.ExtractGroup(command.Text);

                //log.Info("Saying '{text}' to ({group})..", command.Text, group);

                foreach (var member in group)
                {
                    var memberSession = sessionStorage[member];
                    if (memberSession == null || !memberSession.IsAlive)
                    {
                        continue;
                    }

                    memberSession.SendCommand(Commands.Say, command.Text);
                }
                break;

            case Commands.History:
                group = Group.ExtractGroup(command.Text);
                group = group.Add(nick);

                log.Info("The history of group ({group}) was requested. Collecting..", group);

                var responses = new List <Response>();
                foreach (var member in group)
                {
                    var memberSession = sessionStorage[member];
                    if (memberSession == null || !memberSession.IsAlive)
                    {
                        continue;
                    }

                    var response = await memberSession.SendCommandWithResponse(Commands.History, group.ToString(), HistoryRequestTimeout);

                    responses.Add(response);
                }

                log.Info("Sending collected and merged history of group ({group}) back..", group);

                var mergedResponse = HistoryMerger.Merge(responses);
                session.SendResponse(command.Id, mergedResponse);
                break;

            case Commands.List:
                session.SendResponse(command.Id, new Response(sessionStorage.ListAlive().Where(s => heartbeatStorage.IsStable(s))));
                break;
            }
        }