Beispiel #1
0
        private void signalThread()
        {
            while (Running)
            {
                // Wait for a signal to be delivered
                int index = UnixSignal.WaitAny(signals, -1);

                Mono.Unix.Native.Signum signal = signals [index].Signum;

                log.Info("signal: " + signal.ToString());
                // Notify the main thread that a signal was received,
                // you can use things like:
                //    Application.Invoke () for Gtk#
                //    Control.Invoke on Windows.Forms
                //    Write to a pipe created with UnixPipes for server apps.
                //    Use an AutoResetEvent

                // For example, this works with Gtk#
                //Application.Invoke (delegate () { ReceivedSignal (signal);
                stop();
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            //Set up IO - (TODO, allow to be set in config file)
            var RingerPower      = new Raspberry.IO.GeneralPurpose.OutputPinConfiguration(Raspberry.IO.GeneralPurpose.ProcessorPin.Pin17);
            var RingerOscillator = new Raspberry.IO.GeneralPurpose.OutputPinConfiguration(Raspberry.IO.GeneralPurpose.ProcessorPin.Pin18);
            var HookSwitch       = new Raspberry.IO.GeneralPurpose.InputPinConfiguration(Raspberry.IO.GeneralPurpose.ProcessorPin.Pin22)
            {
                Reversed = true
            };
            var DialPulseSwitch = new Raspberry.IO.GeneralPurpose.InputPinConfiguration(Raspberry.IO.GeneralPurpose.ProcessorPin.Pin27);


            //Start Ringer and DialListener GPIO classes
            using (var ringer = new clsRinger(RingerPower, RingerOscillator))
            {
                using (var dialListener = new clsDialHookListener(HookSwitch, DialPulseSwitch))
                {
                    //Simple bell test function. 0 = UK ring, 1 = USA Ring
                    dialListener.NumberDialed += (uint NumberDialed) =>
                    {
                        Console.WriteLine("Number Dialed:{0}", NumberDialed);

                        if (NumberDialed == 0)
                        {
                            ringer.SetRingPattern(clsRinger.ringPattern_UK);
                            ringer.StartRing();
                        }
                        else if (NumberDialed == 9)
                        {
                            ringer.SetRingPattern(clsRinger.ringPattern_USA);
                            ringer.StartRing();
                        }
                    };

                    //Cancel the Ringer
                    dialListener.HookSwitchChange += (bool OnHook, uint Pulse) =>
                    {
                        if (!OnHook)
                        {
                            ringer.StopRing();
                        }
                    };


                    UnixSignal[] signals = new UnixSignal [] {
                        new UnixSignal(Mono.Unix.Native.Signum.SIGINT),
                        new UnixSignal(Mono.Unix.Native.Signum.SIGUSR1),
                    };

                    while (true)
                    {
                        int index = UnixSignal.WaitAny(signals, -1);                          //Wait for any Unix Signals

                        Mono.Unix.Native.Signum signal = signals [index].Signum;
                        Console.Write("SIGNAL:{0}", signal.ToString());
                        break;
                    }
                    ;
                }
            }
            Console.WriteLine("**end**");
        }
        public static void Main(string[] args)
        {
            bool   _shutDown = false;
            bool   _isMono;
            Thread signal_thread = null;

            _isMono = Type.GetType("Mono.Runtime") != null;

            if (_isMono)
            {
                //http://unixhelp.ed.ac.uk/CGI/man-cgi?signal+7
                UnixSignal[] signals;
                signals = new UnixSignal[] {
                    //new UnixSignal (Mono.Unix.Native.Signum.SIGHUP),
                    new UnixSignal(Mono.Unix.Native.Signum.SIGINT),
                    //new UnixSignal (Mono.Unix.Native.Signum.SIGQUIT),
                    new UnixSignal(Mono.Unix.Native.Signum.SIGABRT),
                    //new UnixSignal (Mono.Unix.Native.Signum.SIGKILL),
                    new UnixSignal(Mono.Unix.Native.Signum.SIGTERM),
                    //new UnixSignal (Mono.Unix.Native.Signum.SIGSTOP),
                    new UnixSignal(Mono.Unix.Native.Signum.SIGTSTP)
                };



                signal_thread = new Thread(delegate()
                {
                    while (!_shutDown)
                    {
                        // Wait for a signal to be delivered
                        int index = UnixSignal.WaitAny(signals, -1);
                        Mono.Unix.Native.Signum signal = signals[index].Signum;
                        Console.WriteLine("shutdown signal recieved {0}" + signal.ToString());
                        _shutDown = true;
                    }
                });
            }



            Uri elasticSearchUrl;
            int daysToKeep = 30;

            if (args.Length == 0)
            {
                elasticSearchUrl = new Uri(Environment.GetEnvironmentVariable("elasticSearchUrl"));
                string environmentVariable = Environment.GetEnvironmentVariable("daysToKeep");
                if (environmentVariable != null)
                {
                    daysToKeep = int.Parse(environmentVariable);
                }
            }
            else
            {
                elasticSearchUrl = new Uri(args[0]);
                if (args.Length > 1)
                {
                    daysToKeep = int.Parse(args[1]);
                }
            }

            IPAddress ip = GetExternalIP();


            TimeSpan wait;

            while (!_shutDown)
            {
                Console.WriteLine("\n\nstarting purge from IP {0} at {1}", ip, DateTime.UtcNow);
                try
                {
                    var mapping      = GetMappings(elasticSearchUrl);
                    var toDate       = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day).Subtract(TimeSpan.FromDays(daysToKeep));
                    var toDateString = GetDayOnlyString(toDate);


                    mapping.Indices.Sort();
                    foreach (var i in mapping.Indices)
                    {
                        Console.WriteLine("checking index {0}", i);
                        if (IndexExists(elasticSearchUrl, i))
                        {
                            var count = GetCount(elasticSearchUrl, i, toDateString);
                            if (count != 0)
                            {
                                Console.WriteLine("deleting {0} records", count);
                                Uri uri = new Uri(elasticSearchUrl, i + "/_query");

                                string range      = GetRangeString(toDateString);
                                var    queryBytes = Encoding.UTF8.GetBytes(range);
                                /*string responseText = */
                                Console.WriteLine("delete query: {0}", range);
                                GetResponseText(uri, "DELETE", queryBytes);

                                //{"ok":true,"_indices":{"logstash-2013.07.21":{"_shards":{"total":4,"successful":4,"failed":0}}}}
                            }
                            else
                            {
                                Console.WriteLine("index {0} has no matching records", i);
                            }

                            // then clean it up if empty
                            if (DeleteIndexIfEmpty(elasticSearchUrl, i))
                            {
                                Console.WriteLine("deleted emtpy index {0}", i);
                            }
                        }
                        else
                        {
                            Console.WriteLine("index {0} does not exist", i);
                        }
                    }
                    wait = TimeSpan.FromDays(1);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("ERROR:" + ex.ToString());
                    wait = TimeSpan.FromMinutes(1);
                }
                Console.WriteLine("\n\nwaiting {0} minutes until next purge", wait.TotalMinutes);
                Thread.Sleep(wait);
            }
            Console.WriteLine("shutting down");

            if (_isMono)
            {
                signal_thread.Join();
            }

            Console.WriteLine("shut down complete.");
        }