Example #1
0
            private void PollServer(TrackedService server)
            {
                bool pollOk = true;

                string commandArg = null;

                if (server.ServiceType == ServiceType.Block)
                {
                    commandArg = "heartbeatB";
                }
                else if (server.ServiceType == ServiceType.Root)
                {
                    commandArg = "heartbeatR";
                }
                else if (server.ServiceType == ServiceType.Manager)
                {
                    commandArg = "heartbeatM";
                }
                else
                {
                    tracker.log.Error(String.Format("Don't know how to poll type {0}", server.ServiceType));
                    pollOk = false;
                }

                // Send the poll command to the server,
                IMessageProcessor p            = connector.Connect(server.ServiceAddress, ServiceType.Block);
                MessageStream     outputStream = new MessageStream();

                outputStream.AddMessage(new Message("poll", commandArg));
                IEnumerable <Message> inputStream = p.Process(outputStream);

                foreach (Message m in inputStream)
                {
                    // Any error with the poll means no status change,
                    if (m.HasError)
                    {
                        pollOk = false;
                    }
                }

                // If the poll is ok, set the status of the server to UP and remove from
                // the monitor list,
                if (pollOk)
                {
                    // The server status is set to 'STATUS_UP' if either the current state
                    // is 'DOWN CLIENT REPORT' or 'DOWN HEARTBEAT'
                    // Synchronize over 'servers_map' for safe alteration of the ref.
                    ServiceStatus oldStatus;
                    lock (monitoredServers) {
                        oldStatus = server.CurrentStatus;
                        if (oldStatus == ServiceStatus.DownClientReport ||
                            oldStatus == ServiceStatus.DownHeartbeat)
                        {
                            server.CurrentStatus = ServiceStatus.Up;
                        }
                        // Remove the server from the monitored_servers list.
                        monitoredServers.Remove(server);
                    }

                    if (tracker.log.IsInterestedIn(LogLevel.Information))
                    {
                        tracker.log.Info(String.Format("Poll ok. Status now UP for {0} {1}", server.ServiceAddress, server.ServiceType));
                    }

                    // Fire the event if the status changed,
                    try {
                        if (tracker.StatusChange != null)
                        {
                            tracker.StatusChange(this,
                                                 new ServiceStatusEventArgs(server.ServiceAddress, server.ServiceType, oldStatus,
                                                                            ServiceStatus.Up));
                        }
                    } catch (Exception e) {
                        // Catch any exception generated. Log it but don't terminate the
                        // thread.
                        tracker.log.Error("Exception in listener during poll", e);
                    }
                }
                else
                {
                    // Make sure the server status is set to 'DOWN HEARTBEAT' if the poll
                    // failed,
                    // Synchronize over 'servers_map' for safe alteration of the ref.
                    lock (monitoredServers) {
                        ServiceStatus sts = server.CurrentStatus;
                        if (sts == ServiceStatus.Up ||
                            sts == ServiceStatus.DownClientReport)
                        {
                            server.CurrentStatus = ServiceStatus.DownHeartbeat;
                        }
                    }
                }
            }