Exemplo n.º 1
0
        private object GetState()
        {
            try
            {
                Agent.State response = Config.State;
                Logger.Info(String.Format(CultureInfo.InvariantCulture, "Agent state: {0}", response.ToString()));

                if (BaseMessage.Settings != null)
                {
                    response.SetValue("agent_id", BaseMessage.Settings["agent_id"]);
                    response.SetValue("vm", BaseMessage.Settings["vm"]);
                }

                response.SetValue("job_state", GetJobState());
                response.SetValue("bosh_protocol", "1"); // TODO: response["bosh_protocol"] = Bosh::Agent::BOSH_PROTOCOL
                NtpMessage ntpMessage = Ntp.GetNtpOffset();
                response.SetValue("ntp", JToken.FromObject(ntpMessage));

                return(response.ToHash());
            }
            catch (StateException e)
            {
                throw new MessageHandlerException(e.Message, e);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the NTP offset using the default time server.
        /// </summary>
        /// <returns></returns>
        public static NtpMessage GetNtpOffset()
        {
            try
            {
                StringBuilder output = new StringBuilder();
                using (Process w32tm = new Process())
                {
                    ProcessStartInfo info = new ProcessStartInfo();
                    info.Arguments = "/query /status /verbose";
                    info.FileName  = "w32tm";
                    info.RedirectStandardOutput = true;
                    info.RedirectStandardInput  = true;
                    info.CreateNoWindow         = true;
                    info.UseShellExecute        = false;
                    w32tm.StartInfo             = info;
                    w32tm.EnableRaisingEvents   = true;
                    w32tm.OutputDataReceived   += new DataReceivedEventHandler(
                        delegate(object sender, DataReceivedEventArgs e)
                    {
                        output.Append(e.Data);
                    }
                        );
                    w32tm.Start(); w32tm.BeginOutputReadLine();
                    w32tm.WaitForExit();
                    w32tm.CancelOutputRead();
                }

                int exitCode = 0;
                if (int.TryParse(Regex.Match(output.ToString(), @"Last Sync Error:\s\d*", RegexOptions.None).Value.Replace("Last Sync Error:", "").Trim(), out exitCode))
                {
                    if (exitCode == 0)
                    {
                        double offset;
                        if (double.TryParse(Regex.Match(output.ToString(), @"Phase Offset:\s\d*.\d*", RegexOptions.None).Value.Replace("Phase Offset:", "").Trim(), out offset))
                        {
                            NtpMessage currentNtp = new NtpMessage();
                            currentNtp.Offset    = offset.ToString(CultureInfo.InvariantCulture);
                            currentNtp.Timestamp = DateTime.Now.ToString("dd MMM HH:mm:ss", CultureInfo.InvariantCulture);
                            return(currentNtp);
                        }
                    }
                }
                return(new NtpMessage()
                {
                    Message = "bad ntp server"
                });
            }
            catch (Exception ex)
            {
                Logger.Warning("Could not get NTP offset: {0}", ex.ToString());
                return(new NtpMessage()
                {
                    Message = "bad ntp server"
                });
            }
        }
Exemplo n.º 3
0
        public void NtpMessageConstructorSetsVersionModeAndTransmitTimestamp()
        {
            byte version      = 3;
            var  mode         = Mode.Client;
            var  transmitTime = new DateTime(2016, 12, 11, 10, 9, 8, 7, DateTimeKind.Utc);

            var transmitTimestamp = new Timestamp(transmitTime);
            var message           = new NtpMessage(version, mode, transmitTimestamp);

            Assert.AreEqual(version, message.VersionNumber);
            Assert.AreEqual(mode, message.Mode);
            Assert.AreEqual(transmitTime, message.TransmitTimestamp.ToDateTime());
        }