private double throttle; // throttle (between 0 and 1), the fraction of maximum torque commanded

        #endregion Fields

        #region Constructors

        public DynamicsSimVehicle(SimVehicleState vehicleState, ObjectDirectory od)
        {
            this.simVehicleState = vehicleState;

            InitializeState();

            Connect(od);
        }
        public static void InitComm()
        {
            // set the machine name from the config
            machineName = Properties.Settings.Default.MachineName.Trim().ToUpper();

            // configure remoting
            RemotingConfiguration.Configure("net.xml", false);

            // get the name service
            WellKnownServiceTypeEntry[] wkst = RemotingConfiguration.GetRegisteredWellKnownServiceTypes();
            objectDirectory = (ObjectDirectory)Activator.GetObject(typeof(ObjectDirectory), wkst[0].ObjectUri);

            // get the channel factory
            channelFactory = (IChannelFactory)objectDirectory.Resolve("ChannelFactory");
        }
        /// <summary>
        /// Registers with the correct services
        /// </summary>
        public void Register()
        {
            try
            {
                // "Activate" the NameService singleton.
                objectDirectory = (ObjectDirectory)Activator.GetObject(typeof(ObjectDirectory), wkst[0].ObjectUri);

                // Retreive the directory of messaging channels
                IChannelFactory channelFactory = (IChannelFactory)objectDirectory.Resolve("ChannelFactory");

                // Retreive the Messaging Service channels we want to push to
                simulationMessageChannel = channelFactory.GetChannel("SimulationMessageChannel", ChannelMode.UdpMulticast);

                // Rebind the Simulator as the simulator remote facade server
                objectDirectory.Rebind(this, "SimulationServer");

                // Notify user of success
                SimulatorOutput.WriteLine("Connection to Name Service and Registration of Simulation Server as Simulator Facade Successful");

                // let clients know the sim is alive
                simulationMessageChannel.PublishUnreliably(SimulationMessage.Alive);
            }
            catch (Exception e)
            {
                SimulatorOutput.WriteLine("Error Registering With Remoting Services: " + e.ToString());
            }
        }
        /// <summary>
        /// Registers with the correct services
        /// </summary>
        public void Register()
        {
            // "Activate" the NameService singleton.
            objectDirectory = (ObjectDirectory)Activator.GetObject(typeof(ObjectDirectory), wkst[0].ObjectUri);

            // Retreive the directory of messaging channels
            channelFactory = (IChannelFactory)objectDirectory.Resolve("ChannelFactory");

            // Notify user of success
            RemoraOutput.WriteLine("Connection to Name Service Successful", OutputType.Remora);

            // get simulation if supposed to
            if (global::RemoraAdvanced.Properties.Settings.Default.SimMode)
            {
                this.simulatorFacade = (SimulatorFacade)objectDirectory.Resolve("SimulationServer");

                // Notify user of success
                RemoraOutput.WriteLine("Connection to Simulation Service Successful", OutputType.Remora);
            }
        }
        /// <summary>
        /// Initializes communications with the outside world by means of remoting
        /// </summary>
        private void InitializeRemotingCommunications()
        {
            // Read the configuration file.
            RemotingConfiguration.Configure("..\\..\\App.config", false);
            WellKnownServiceTypeEntry[] wkst = RemotingConfiguration.GetRegisteredWellKnownServiceTypes();

            // "Activate" the NameService singleton.
            objectDirectory = (ObjectDirectory)Activator.GetObject(typeof(ObjectDirectory), wkst[0].ObjectUri);

            // Bind the operational test faceade
            objectDirectory.Rebind(this, "OperationalTestFacade");

            // Retreive the directory of messaging channels
            IChannelFactory channelFactory = (IChannelFactory)objectDirectory.Resolve("ChannelFactory");

            // Retreive the Messaging Service channels we want to listen to
            rndfChannel = channelFactory.GetChannel("RndfNetworkChannel", ChannelMode.Bytestream);
            mdfChannel = channelFactory.GetChannel("MdfChannel", ChannelMode.Bytestream);
            positionChannel = channelFactory.GetChannel("PositionChannel", ChannelMode.Bytestream);
            testStringChannel = channelFactory.GetChannel("TestStringChannel", ChannelMode.Vanilla);

            // Create a channel listeners and listen on wanted channels
            channelListener = new MessagingListener();
            rndfToken = rndfChannel.Subscribe(channelListener);
            mdfToken = mdfChannel.Subscribe(channelListener);
            //positionToken = positionChannel.Subscribe(channelListener);
            testStringToken = testStringChannel.Subscribe(new StringListener());

            // Show that the navigation system is ready and waiting for input
            Console.WriteLine("   > Remoting Communication Initialized");
        }
        /// <summary>
        /// Attempts to register with the simulation
        /// </summary>
        public void AttemptSimulationConnection()
        {
            // notify
            Console.WriteLine(DateTime.Now.ToString() + ": Attempting Connection To Simulation Server");

            try
            {
                // "Activate" the NameService singleton.
                objectDirectory = (ObjectDirectory)Activator.GetObject(typeof(ObjectDirectory), wkst[0].ObjectUri);

                // Resolve the SimulationServer as the simulator remote facade server
                this.client.SimulationServer = (SimulatorFacade)objectDirectory.Resolve("SimulationServer");

                // create the dynamics sim facade
                this.client.DynamicsVehicle = new DynamicsSimVehicle(new SimVehicleState(), this.objectDirectory);

                // register ourselves
                bool success = this.client.SimulationServer.Register(this.client.Name());

                // notify
                if(success)
                    Console.WriteLine(DateTime.Now.ToString() + ": Attempt Succeeded");
                else
                    Console.WriteLine(DateTime.Now.ToString() + ": Registration Failed in Simulation");
            }
            catch (Exception e)
            {
                // notify
                Console.WriteLine(DateTime.Now.ToString() + ": Attempt Failed" + "\n" + e.ToString());
            }
        }
        /// <summary>
        /// Registers with the correct services
        /// </summary>
        public void Register()
        {
            try
            {
                // "Activate" the NameService singleton.
                objectDirectory = (ObjectDirectory)Activator.GetObject(typeof(ObjectDirectory), wkst[0].ObjectUri);

                // Retreive the directory of messaging channels
                IChannelFactory channelFactory = (IChannelFactory)objectDirectory.Resolve("ChannelFactory");

                // Retreive the Messaging Service channels we want to interact with
                if (simulationMessageChannel != null)
                    simulationMessageChannel.Dispose();
                simulationMessageChannel = channelFactory.GetChannel("SimulationMessageChannel", ChannelMode.UdpMulticast);

                // Listen to channels we wish to listen to
                simulationMessageChannelToken = simulationMessageChannel.Subscribe(this.messagingListener);

                // Rebind the Simulator as the simulator remote facade server
                objectDirectory.Rebind(this.client, this.client.Name());

                // get vehicle state channel
                if (vehicleStateChannel != null)
                    vehicleStateChannel.Dispose();
                vehicleStateChannel = channelFactory.GetChannel("ArbiterSceneEstimatorPositionChannel_" + SimulatorClient.MachineName, ChannelMode.Bytestream);

                // get observed obstacle channel
                if (observedObstacleChannel != null)
                    observedObstacleChannel.Dispose();
                observedObstacleChannel = channelFactory.GetChannel("ObservedObstacleChannel_" + SimulatorClient.MachineName, ChannelMode.Bytestream);

                // get observed vehicle channel
                if (observedVehicleChannel != null)
                    observedVehicleChannel.Dispose();
                observedVehicleChannel = channelFactory.GetChannel("ObservedVehicleChannel_" + SimulatorClient.MachineName, ChannelMode.Bytestream);

                // get vehicle speed channel
                if (vehicleSpeedChannel != null)
                    vehicleSpeedChannel.Dispose();
                vehicleSpeedChannel = channelFactory.GetChannel("VehicleSpeedChannel_" + SimulatorClient.MachineName, ChannelMode.Bytestream);

                // get the local road estimate channel
                if (localRoadChannel != null)
                    localRoadChannel.Dispose();
                localRoadChannel = channelFactory.GetChannel(LocalRoadEstimate.ChannelName + "_" + SimulatorClient.MachineName, ChannelMode.Bytestream);

                // Notify user of success
                Console.WriteLine(DateTime.Now.ToString() + ": Connection to Name Service and Registration of Simulation Client Successful");
            }
            catch (Exception e)
            {
                // notify
                Console.WriteLine(DateTime.Now.ToString() + ": Error Registering With Remoting Services: " + e.ToString());
            }
        }
        private void Connect(ObjectDirectory od)
        {
            // get the channel for the sim state
            IChannelFactory channelFactory = (IChannelFactory)od.Resolve("ChannelFactory");
            operationalStateChannel = channelFactory.GetChannel("OperationalSimState_" + SimulatorClient.MachineName, ChannelMode.Bytestream);

            // publish ourself to the object directory
            od.Rebind(this, "DynamicsSim_" + SimulatorClient.MachineName);
        }
        /// <summary>
        /// Initializes communications with the outside world by means of remoting
        /// </summary>
        public void InitializeRemotingCommunications()
        {
            // try to shut down in case we have already been active
            this.ShutDown();

            try
            {
                // Read the configuration file.
                RemotingConfiguration.Configure("Remora.exe.config", false);
                WellKnownServiceTypeEntry[] wkst = RemotingConfiguration.GetRegisteredWellKnownServiceTypes();

                // "Activate" the NameService singleton.
                objectDirectory = (ObjectDirectory)Activator.GetObject(typeof(ObjectDirectory), wkst[0].ObjectUri);

                // Retreive the directory of messaging channels
                IChannelFactory channelFactory = (IChannelFactory)objectDirectory.Resolve("ChannelFactory");

                // Retreive the Messaging Service channels we want to listen to
                vehicleStateChannel = channelFactory.GetChannel("PositionChannel", ChannelMode.UdpMulticast);
                observedObstacleChannel = channelFactory.GetChannel("ObservedObstacleChannel", ChannelMode.UdpMulticast);
                observedVehicleChannel = channelFactory.GetChannel("ObservedVehicleChannel", ChannelMode.UdpMulticast);
                arbiterInformationChannel = channelFactory.GetChannel("ArbiterInformationChannel", ChannelMode.UdpMulticast);
                carModeChannel = channelFactory.GetChannel("CarMode", ChannelMode.UdpMulticast);
                fakeVehicleChannel = channelFactory.GetChannel("FakeVehicleChannel", ChannelMode.UdpMulticast);

                // Create a channel listeners and listen on wanted channels
                channelListener = new MessagingListener();
                vehicleStateChannelToken = vehicleStateChannel.Subscribe(channelListener);
                observedObstacleChannel.Subscribe(channelListener);
                observedVehicleChannel.Subscribe(channelListener);
                arbiterInformationChannelToken = arbiterInformationChannel.Subscribe(channelListener);
                carModeChannelToken = carModeChannel.Subscribe(channelListener);
                fakeVehicleChannelToken = fakeVehicleChannel.Subscribe(channelListener);

            // set that remoting has been successfully initialize
            this.RemotingInitialized = true;
            RemoraOutput.WriteLine("Successfully initialized communications");
            }
            catch (Exception e)
            {
                RemoraOutput.WriteLine(e.ToString() + "\n\n" + "Could not initialize communications, attempting to reconnect");

                try
                {
                    WellKnownServiceTypeEntry[] wkst = RemotingConfiguration.GetRegisteredWellKnownServiceTypes();

                    // "Activate" the NameService singleton.
                    objectDirectory = (ObjectDirectory)Activator.GetObject(typeof(ObjectDirectory), wkst[0].ObjectUri);

                    // Retreive the directory of messaging channels
                    IChannelFactory channelFactory = (IChannelFactory)objectDirectory.Resolve("ChannelFactory");

                    // Retreive the Messaging Service channels we want to listen to
                    vehicleStateChannel = channelFactory.GetChannel("PositionChannel", ChannelMode.UdpMulticast);
                    observedObstacleChannel = channelFactory.GetChannel("ObservedObstacleChannel", ChannelMode.UdpMulticast);
                    observedVehicleChannel = channelFactory.GetChannel("ObservedVehicleChannel", ChannelMode.UdpMulticast);
                    arbiterInformationChannel = channelFactory.GetChannel("ArbiterInformationChannel", ChannelMode.UdpMulticast);
                    carModeChannel = channelFactory.GetChannel("CarMode", ChannelMode.UdpMulticast);
                    fakeVehicleChannel = channelFactory.GetChannel("FakeVehicleChannel", ChannelMode.UdpMulticast);

                    // Create a channel listeners and listen on wanted channels
                    channelListener = new MessagingListener();
                    vehicleStateChannelToken = vehicleStateChannel.Subscribe(channelListener);
                    observedObstacleChannel.Subscribe(channelListener);
                    observedVehicleChannel.Subscribe(channelListener);
                    arbiterInformationChannelToken = arbiterInformationChannel.Subscribe(channelListener);
                    carModeChannelToken = carModeChannel.Subscribe(channelListener);
                    fakeVehicleChannelToken = fakeVehicleChannel.Subscribe(channelListener);

                    // set that remoting has been successfully initialize
                    this.RemotingInitialized = true;
                    RemoraOutput.WriteLine("Successfully initialized communications");
                }
                catch (Exception e1)
                {
                    RemoraOutput.WriteLine(e1.ToString() + "\n\n" + "Could not reinitialize nameservice communications");

                    try
                    {
                        // Retreive the directory of messaging channels
                        IChannelFactory channelFactory = (IChannelFactory)objectDirectory.Resolve("ChannelFactory");

                        // Retreive the Messaging Service channels we want to listen to
                        vehicleStateChannel = channelFactory.GetChannel("PositionChannel", ChannelMode.UdpMulticast);
                        observedObstacleChannel = channelFactory.GetChannel("ObservedObstacleChannel", ChannelMode.UdpMulticast);
                        observedVehicleChannel = channelFactory.GetChannel("ObservedVehicleChannel", ChannelMode.UdpMulticast);
                        arbiterInformationChannel = channelFactory.GetChannel("ArbiterInformationChannel", ChannelMode.UdpMulticast);
                        carModeChannel = channelFactory.GetChannel("CarMode", ChannelMode.UdpMulticast);
                        fakeVehicleChannel = channelFactory.GetChannel("FakeVehicleChannel", ChannelMode.UdpMulticast);

                        // Create a channel listeners and listen on wanted channels
                        channelListener = new MessagingListener();
                        vehicleStateChannelToken = vehicleStateChannel.Subscribe(channelListener);
                        observedObstacleChannel.Subscribe(channelListener);
                        observedVehicleChannel.Subscribe(channelListener);
                        arbiterInformationChannelToken = arbiterInformationChannel.Subscribe(channelListener);
                        carModeChannelToken = carModeChannel.Subscribe(channelListener);
                        fakeVehicleChannelToken = fakeVehicleChannel.Subscribe(channelListener);

                        // set that remoting has been successfully initialize
                        this.RemotingInitialized = true;
                        RemoraOutput.WriteLine("Successfully initialized communications");
                    }
                    catch (Exception e2)
                    {
                        RemoraOutput.WriteLine(e2.ToString() + "\n\n" + "Could not reinitialize messaging communications");
                    }
                }
            }
        }
        public static void ConfigureRemoting(string server)
        {
            RemotingConfiguration.Configure("net.xml", false);

            if (!string.IsNullOrEmpty(server)) {
                string uri = "tcp://" + server + ":12345/ObjectDirectory";
                od = (ObjectDirectory)Activator.GetObject(typeof(ObjectDirectory), uri);
            }
            else {
                WellKnownServiceTypeEntry[] wkst = RemotingConfiguration.GetRegisteredWellKnownServiceTypes();
                // "Activate" the NameService singleton.
                od = (ObjectDirectory)Activator.GetObject(typeof(ObjectDirectory), wkst[0].ObjectUri);
            }
        }
        /// <summary>
        /// Configures remoting
        /// </summary>
        public void Configure()
        {
            // configure
            RemotingConfiguration.Configure("VehicleSeparationDisplay.exe.config", false);
            wkst = RemotingConfiguration.GetRegisteredWellKnownServiceTypes();

            // "Activate" the NameService singleton.
            objectDirectory = (ObjectDirectory)Activator.GetObject(typeof(ObjectDirectory), wkst[0].ObjectUri);

            // Retreive the directory of messaging channels
            channelFactory = (IChannelFactory)objectDirectory.Resolve("ChannelFactory");

            // Notify user of success
            Console.WriteLine("Connection to Name Service Successful");

            // get information channel
            arbiterInformationChannel = channelFactory.GetChannel("ArbiterInformationChannel" + this.RemotingSuffix, ChannelMode.UdpMulticast);
            arbiterInformationChannelToken = arbiterInformationChannel.Subscribe(messagingListener);
        }