示例#1
0
        /// <summary>
        /// This gets called whenever a command is recieved from
        /// </summary>
        /// <param name="pSender">The telnet server instance</param>
        /// <param name="pEventArgs">The event args, including the socket</param>
        private void TelnetCommandRecieved(object pSender, TelnetServerCommandEventArgs pEventArgs)
        {
            //A command has arrived from a telnet client.  Parse it.
            //Tokenize the command string
            string [] tmpCommandArguments = pEventArgs.Command.Split(' ');

            if (tmpCommandArguments.Length > 0)
            {
                switch (tmpCommandArguments[0].ToUpper())
                {
                case "ADD":
                    if (tmpCommandArguments.Length > 1)
                    {
                        System.Text.StringBuilder tmpSearchString = new System.Text.StringBuilder();

                        for (int tmpCounter = 1; tmpCounter < tmpCommandArguments.Length; tmpCounter++)
                        {
                            //Add back the spaces to make the search meaningful.
                            if (tmpSearchString.Length > 0)
                            {
                                tmpSearchString.Append(" ");
                            }

                            tmpSearchString.Append(tmpCommandArguments[tmpCounter].ToString());
                        }

                        _TelnetServer.SendData("Adding [" + tmpSearchString.ToString() + "] to the word list.", pEventArgs.Socket);
                        _TelnetServer.SendLineFeed(pEventArgs.Socket);

                        _WordList.AddWord(tmpSearchString.ToString());

                        _TelnetServer.SendData("There are now " + _WordList.Count + " items in the word list.", pEventArgs.Socket);
                        _TelnetServer.SendLineFeed(pEventArgs.Socket);
                    }
                    else
                    {
                        _TelnetServer.SendData("Use: ADD [word]", pEventArgs.Socket);
                        _TelnetServer.SendLineFeed(pEventArgs.Socket);
                    }
                    break;

                case "AL":
                case "ALP":
                case "ALPH":
                case "ALPHA":
                    if (tmpCommandArguments.Length > 1)
                    {
                        System.Text.StringBuilder tmpSearchString = new System.Text.StringBuilder();

                        for (int tmpCounter = 1; tmpCounter < tmpCommandArguments.Length; tmpCounter++)
                        {
                            //Add back the spaces to make the search meaningful.
                            if (tmpSearchString.Length > 0)
                            {
                                tmpSearchString.Append(" ");
                            }

                            tmpSearchString.Append(tmpCommandArguments[tmpCounter].ToString());
                        }

                        _TelnetServer.SendData("Generating Alphagram from [" + tmpSearchString.ToString() + "]", pEventArgs.Socket);
                        _TelnetServer.SendData(" resulting in [" + GenerateAlphagram(tmpSearchString.ToString()) + "]", pEventArgs.Socket);
                        _TelnetServer.SendLineFeed(pEventArgs.Socket);
                        _TelnetServer.SendLineFeed(pEventArgs.Socket);
                    }
                    else
                    {
                        _TelnetServer.SendData("Use: ALPHA [some text]", pEventArgs.Socket);
                        _TelnetServer.SendLineFeed(pEventArgs.Socket);
                    }
                    break;

                case "CO":
                case "COU":
                case "COUN":
                case "COUNT":
                    _TelnetServer.SendData("There are " + _WordList.Count + " items in the word list.", pEventArgs.Socket);
                    _TelnetServer.SendLineFeed(pEventArgs.Socket);
                    break;

                case "EXIT":
                    //Kill the socket
                    pEventArgs.Socket.Kill();
                    break;

                case "?":
                case "H":
                case "HE":
                case "HEL":
                case "HELP":
                    SendTelnetHelp(pEventArgs);
                    break;

                case "INFO":
                    _TelnetServer.SendData("         Linked List Information", pEventArgs.Socket);
                    _TelnetServer.SendLineFeed(pEventArgs.Socket);
                    _TelnetServer.SendData("-------------------------------------------", pEventArgs.Socket);
                    _TelnetServer.SendLineFeed(pEventArgs.Socket);
                    _TelnetServer.SendData("         List Count: " + _WordList.Count, pEventArgs.Socket);
                    _TelnetServer.SendLineFeed(pEventArgs.Socket);
                    if (_WordList.Count > 0)
                    {
                        _TelnetServer.SendData(String.Format("       Current Item: ({0}) {1} ... Alphagram[{2}]", _WordList.CurrentItemKey, _WordList.CurrentWord, _WordList.CurrentWordAlphagram), pEventArgs.Socket);
                        _TelnetServer.SendLineFeed(pEventArgs.Socket);
                    }
                    _TelnetServer.SendData("          Add Count: " + _WordList.StatisticAddCount, pEventArgs.Socket);
                    _TelnetServer.SendLineFeed(pEventArgs.Socket);
                    _TelnetServer.SendData("       Delete Count: " + _WordList.StatisticDeleteCount, pEventArgs.Socket);
                    _TelnetServer.SendLineFeed(pEventArgs.Socket);
                    _TelnetServer.SendData("       Search Count: " + _WordList.StatisticSearchCount, pEventArgs.Socket);
                    _TelnetServer.SendLineFeed(pEventArgs.Socket);
                    _TelnetServer.SendData(" Search Match Count: " + _WordList.StatisticSearchMatchCount, pEventArgs.Socket);
                    _TelnetServer.SendLineFeed(pEventArgs.Socket);
                    _TelnetServer.SendData("   Navigation Count: " + _WordList.StatisticNavigationCount, pEventArgs.Socket);
                    _TelnetServer.SendLineFeed(pEventArgs.Socket);
                    break;

                case "LS":
                case "LI":
                case "LIS":
                case "LIST":
                    if (tmpCommandArguments.Length > 1)
                    {
                        System.Text.StringBuilder tmpSearchString = new System.Text.StringBuilder();

                        for (int tmpCounter = 1; tmpCounter < tmpCommandArguments.Length; tmpCounter++)
                        {
                            //Add back the spaces to make the search meaningful.
                            if (tmpSearchString.Length > 0)
                            {
                                tmpSearchString.Append(" ");
                            }

                            tmpSearchString.Append(tmpCommandArguments[tmpCounter].ToString());
                        }

                        SendTelnetWordList(pEventArgs, tmpSearchString.ToString().Trim());
                    }
                    else
                    {
                        _TelnetServer.SendData("Use: LIST [option]", pEventArgs.Socket);
                        _TelnetServer.SendLineFeed(pEventArgs.Socket);
                        _TelnetServer.SendData("     If you want a full list, use 'LIST ALL'", pEventArgs.Socket);
                        _TelnetServer.SendLineFeed(pEventArgs.Socket);
                    }
                    break;

                case "LOAD":
                    System.Text.StringBuilder tmpFileName = new System.Text.StringBuilder();

                    if (tmpCommandArguments.Length < 2)
                    {
                        _TelnetServer.SendData("No File Specified; Using 'Scrabble.txt'", pEventArgs.Socket);
                        _TelnetServer.SendLineFeed(pEventArgs.Socket);

                        tmpFileName.Append("Scrabble.txt");
                    }
                    else
                    {
                        for (int tmpCounter = 1; tmpCounter < tmpCommandArguments.Length; tmpCounter++)
                        {
                            //Add back the spaces to make the search meaningful.
                            if (tmpFileName.Length > 0)
                            {
                                tmpFileName.Append(" ");
                            }

                            tmpFileName.Append(tmpCommandArguments[tmpCounter].ToString());
                        }
                    }

                    _TelnetServer.SendData("Loading [" + tmpFileName.ToString() + "] into the word list.", pEventArgs.Socket);
                    _TelnetServer.SendLineFeed(pEventArgs.Socket);

                    _WordList.LoadWordList(tmpFileName.ToString());

                    _TelnetServer.SendData("There are now " + _WordList.Count + " items in the word list.", pEventArgs.Socket);
                    _TelnetServer.SendLineFeed(pEventArgs.Socket);
                    break;

                case "QU":
                case "QUI":
                    _TelnetServer.SendData("You must fully type QUIT to close the program.", pEventArgs.Socket);
                    _TelnetServer.SendLineFeed(pEventArgs.Socket);
                    break;

                case "QUIT":
                    //Kill the socket, close the application
                    pEventArgs.Socket.Kill();
                    _KeepRunning = false;
                    break;

                case "SEARCH":
                    if (tmpCommandArguments.Length > 1)
                    {
                        System.Text.StringBuilder tmpSearchString = new System.Text.StringBuilder();

                        for (int tmpCounter = 1; tmpCounter < tmpCommandArguments.Length; tmpCounter++)
                        {
                            //Add back the spaces to make the search meaningful.
                            if (tmpSearchString.Length > 0)
                            {
                                tmpSearchString.Append(" ");
                            }

                            tmpSearchString.Append(tmpCommandArguments[tmpCounter].ToString());
                        }

                        _TelnetServer.SendData("Searching for word [" + tmpSearchString.ToString() + "] in the word list.", pEventArgs.Socket);
                        _TelnetServer.SendLineFeed(pEventArgs.Socket);

                        if (_WordList.FindFirstByWord(tmpSearchString.ToString()))
                        {
                            _TelnetServer.SendData("Found a match!", pEventArgs.Socket);
                            _TelnetServer.SendLineFeed(pEventArgs.Socket);
                            _TelnetServer.SendData(String.Format(" {0}. {1}          Alphagram[{2}]", _WordList.CurrentItemKey, _WordList.CurrentWord, _WordList.CurrentWordAlphagram), pEventArgs.Socket);
                            _TelnetServer.SendLineFeed(pEventArgs.Socket);
                        }
                        else
                        {
                            _TelnetServer.SendData("No matches!\n\r", pEventArgs.Socket);
                            _TelnetServer.SendLineFeed(pEventArgs.Socket);
                        }
                    }
                    else
                    {
                        _TelnetServer.SendData("Use: WORD [number]", pEventArgs.Socket);
                        _TelnetServer.SendLineFeed(pEventArgs.Socket);
                    }
                    break;

                case "SEARCHALPHA":
                    if (tmpCommandArguments.Length > 1)
                    {
                        System.Text.StringBuilder tmpSearchString = new System.Text.StringBuilder();

                        for (int tmpCounter = 1; tmpCounter < tmpCommandArguments.Length; tmpCounter++)
                        {
                            //Add back the spaces to make the search meaningful.
                            if (tmpSearchString.Length > 0)
                            {
                                tmpSearchString.Append(" ");
                            }

                            tmpSearchString.Append(tmpCommandArguments[tmpCounter].ToString());
                        }

                        SendTelnetAlphaWordList(pEventArgs, tmpSearchString.ToString().Trim());
                    }
                    else
                    {
                        _TelnetServer.SendData("Use: SEARCHALPHA [option]", pEventArgs.Socket);
                        _TelnetServer.SendLineFeed(pEventArgs.Socket);
                        _TelnetServer.SendData("     If you want a full list, use 'LIST ALL'", pEventArgs.Socket);
                        _TelnetServer.SendLineFeed(pEventArgs.Socket);
                    }
                    break;

                case "WORD":
                    if (tmpCommandArguments.Length > 1)
                    {
                        long tmpIndexToFind = Convert.ToInt32(tmpCommandArguments[1]);
                        _TelnetServer.SendData("Searching for word #[" + tmpIndexToFind.ToString() + "] in the word list.", pEventArgs.Socket);
                        _TelnetServer.SendLineFeed(pEventArgs.Socket);

                        if (_WordList.FindFirstByIndex(tmpIndexToFind))
                        {
                            _TelnetServer.SendData("Found a match!", pEventArgs.Socket);
                            _TelnetServer.SendLineFeed(pEventArgs.Socket);
                            _TelnetServer.SendData(String.Format(" {0}. {1}          Alphagram[{2}]", _WordList.CurrentItemKey, _WordList.CurrentWord, _WordList.CurrentWordAlphagram), pEventArgs.Socket);
                            _TelnetServer.SendLineFeed(pEventArgs.Socket);
                        }
                        else
                        {
                            _TelnetServer.SendData("No matches!\n\r", pEventArgs.Socket);
                            _TelnetServer.SendLineFeed(pEventArgs.Socket);
                        }
                    }
                    else
                    {
                        _TelnetServer.SendData("Use: WORD [number]", pEventArgs.Socket);
                        _TelnetServer.SendLineFeed(pEventArgs.Socket);
                    }
                    break;

                default:
                    //Do nothing but be a little helpful on default.
                    _TelnetServer.SendData("What are you doing, Dave.\n\rType 'help' for a list of commands.", pEventArgs.Socket);
                    _TelnetServer.SendLineFeed(pEventArgs.Socket);
                    break;
                }
            }
        }