Exemple #1
0
        // main logic for processing messages received from client utilizing MessageEnums
        protected MessageEnums ProcessLine(string line)
        {
            string[] splitLine = line.Split(' ');
            if (splitLine.Length <= 0 || !Message.IsMessageEnum(splitLine[0]))
            {
                SendMessage(Message.error($"Incorrect message protocol '{line}'"));
                return(ERROR);
            }
            try
            {
                MessageEnums responseType = Message.GetMessageEnumFromString(splitLine[0]);
                switch (responseType)
                {
                case TRADER_NEW:
                    InitNewTrader();
                    return(TRADER_NEW);

                case TRADER_RECONNECTING:
                    if (!Program.IsRestarting)
                    {
                        throw new ServerNotRestartingException("Server is not restarting");
                    }
                    InitNewTrader(splitLine[1]);
                    return(TRADER_RECONNECTING);

                case TRADER_TRADE:
                    if (_trader == null)
                    {
                        throw new Exception("Trader Uninitialized");
                    }
                    if (Program.IsRestarting)
                    {
                        throw new Exception("Cannot trade whilst server is recovering");
                    }
                    return(TradeStock(splitLine[1]));

                default:
                    SendMessage(Message.error($"Invalid request '{line}'"));
                    return(ERROR);
                }
            }
            catch (IndexOutOfRangeException e) {
                SendMessage(Message.error($"Inavlid request '{line}' unexpected number of arguments"));
                return(ERROR);
            }
            catch (ServerNotRestartingException e)
            {
                SendMessage(Message.error($"{e.Message}"));
                return(ERROR);
            }
        }