Пример #1
0
        public static ControllerBase CreateController(string server, int port, string username, string password)
        {
            PostToConsole($"Connecting to server {server}:{port}");
            var networkConnection = new TcpClient(server, port);

            var authRequest = new Message
            {
                MessageType = "auth-request",
                Authentication = new Authentication { Username = username, Password = password }
            };

            var stream = networkConnection.GetStream();

            var serializer = new XmlSerializer(typeof(Message));
            var writer = new StreamWriter(stream);
            var reader = new StreamReader(stream);

            SendMessage(writer, authRequest);

            var authResponse = GetMessage(reader);

            if (authResponse.MessageType != "auth-response")
            {
                PostToConsole($"Bad message from server : {authResponse.MessageType}");
                return null;
            }

            if (authResponse.Authentication.Result.ToLowerInvariant() != "ok")
            {
                PostToConsole($"Authentication Failed");
                return null;
            }

            PostToConsole($"Authenticated as {username}");
            PostToConsole("Waiting for Sim Start");

            var simStart = GetMessage(reader);

            while (simStart.MessageType != "sim-start")
            {
                PostToConsole($"Unexpected message from server : {simStart.MessageType}");
            }

            var sim = simStart.Simulation;

            ControllerBase result;

            switch (sim.Role.Name.ToLower())
            {
                case "car":
                    result = new CarController();
                    break;

                case "drone":
                    result = new DroneController();
                    break;

                case "motorcycle":
                    result = new MotorcycleController();
                    break;

                case "truck":
                    result = new TruckController();
                    break;

                default:
                    PostToConsole($"Bad role from server : {simStart.Simulation.Role.Name}");
                    return null;
            }

            result.SimulationId = sim.Id;
            result.TotalSteps = sim.Steps;
            result.Team = sim.Team;
            result.MaxBattery = sim.Role.MaxBattery;
            result.MaxLoad = sim.Role.MaxLoad;
            result.Speed = sim.Role.Speed;
            result.Tools = new List<Tool>(sim.Role.Tools);
            result.Products = new List<Product>(sim.Products);
            result.reader = reader;
            result.writer = writer;

            return result;
        }
Пример #2
0
 protected static void SendMessage(StreamWriter writer, Message message)
 {
     serializer.Serialize(writer, message);
     writer.Write("\0");
     writer.Flush();
 }
Пример #3
0
 protected virtual Messages.Action HandleMessage(Message requestAction)
 {
     PostToConsole($"Step {requestAction.Perception.Simulation.Step} : Action = skip");
     return new Messages.Action { Id = requestAction.Perception.Id, Type = "skip" };
 }
Пример #4
0
        public virtual void ProcessMessages()
        {
            Console.CancelKeyPress += OnCancelKeyPressed;

            while (!Cancelled)
            {
                var requestAction = GetMessage(reader);

                if (requestAction.MessageType.ToLower() != "request-action")
                {
                    PostToConsole($"Unexpected message type : {requestAction.MessageType}");
                    continue;
                }

                var response = new Message { MessageType = "action", Action = HandleMessage(requestAction) };

                SendMessage(writer, response);
            }
        }