private static void StopListener(StandingDataListener sdListener,
                                         PersistentObjectListener poListener,
                                         BundleListener bundleListener,
                                         PersistentObjectControlListener poControlListener,
                                         BundleControlListener bundleControlListener)
        {
            var listeningMode = ConfigurationManager.AppSettings[CONFIG_LISTENING_MODE];

            if (listeningMode.Contains("SD"))
            {
                sdListener.Stop();
                sdListener.WaitUntilStopped();
            }

            if (listeningMode.Contains("PO"))
            {
                poControlListener.Stop();
                poControlListener.WaitUntilStopped();

                poListener.Stop();
                poListener.WaitUntilStopped();
            }

            if (listeningMode.Contains("BUNDLE"))
            {
                bundleControlListener.Stop();
                bundleControlListener.WaitUntilStopped();

                bundleListener.Stop();
                bundleListener.WaitUntilStopped();
            }
        }
        private static void StartBackgroundWork(string sql,
                                                StandingDataListener sdListener,
                                                PersistentObjectListener poListener,
                                                BundleListener bundleListener,
                                                PersistentObjectControlListener poControlListener,
                                                BundleControlListener bundleControlListener)
        {
            var msmqCapacity = int.Parse(ConfigurationManager.AppSettings[CONFIG_MSMQ_CAPACITY]);
            var clientId     = new Neighborhood(sql).GetLocalClient();

            Console.WriteLine("Starting Background Work...");

            StartListener(sdListener, poListener, bundleListener, poControlListener, bundleControlListener);
            Kiss.StartJobs(sql, clientId, msmqCapacity);

            Console.WriteLine("Press any key to continue.");
            Console.ReadKey();
        }
        private static void StartListener(StandingDataListener sdListener,
                                          PersistentObjectListener poListener,
                                          BundleListener bundleListener,
                                          PersistentObjectControlListener poControlListener,
                                          BundleControlListener bundleControlListener)
        {
            var listeningMode = ConfigurationManager.AppSettings[CONFIG_LISTENING_MODE];

            if (listeningMode.Contains("SD-DTC"))
            {
                sdListener.Start(KissTransactionMode.DTC);
            }

            if (listeningMode.Contains("PO-DTC"))
            {
                poListener.Start(KissTransactionMode.DTC);
                poControlListener.Start(KissTransactionMode.MSMQ);
            }

            if (listeningMode.Contains("BUNDLE-DTC"))
            {
                bundleListener.Start(KissTransactionMode.DTC);
                bundleControlListener.Start(KissTransactionMode.MSMQ);
            }

            if (listeningMode.Contains("SD-MSMQ"))
            {
                sdListener.Start(KissTransactionMode.MSMQ);
            }

            if (listeningMode.Contains("PO-MSMQ"))
            {
                poListener.Start(KissTransactionMode.MSMQ);
                poControlListener.Start(KissTransactionMode.MSMQ);
            }

            if (listeningMode.Contains("BUNDLE-MSMQ"))
            {
                bundleListener.Start(KissTransactionMode.MSMQ);
                bundleControlListener.Start(KissTransactionMode.MSMQ);
            }
        }
        private static void StopBackgroundWork(bool interactive,
                                               StandingDataListener sdListener,
                                               PersistentObjectListener poListener,
                                               BundleListener bundleListener,
                                               PersistentObjectControlListener poControlListener,
                                               BundleControlListener bundleControlListener)
        {
            if (interactive)
            {
                Console.WriteLine("Stopping Background Work...");
            }

            Kiss.StopJobs();
            StopListener(sdListener, poListener, bundleListener, poControlListener, bundleControlListener);

            if (interactive)
            {
                Console.WriteLine("Press any key to continue.");
                Console.ReadKey();
            }
        }
        private static void Main(string[] args)
        {
            var sql           = args.Length > 0 ? args[0] : "SQLEXPRESS";
            var runZoraClient = true;

            string[] lastAllBundleIds = new string[0],
            lastAllPoIds = new string[0],
            lastAllSdIds = new string[0];
            var msmqCapacity          = int.Parse(ConfigurationManager.AppSettings[CONFIG_MSMQ_CAPACITY]);
            var sdListener            = new StandingDataListener(sql, msmqCapacity);
            var poListener            = new PersistentObjectListener(sql, msmqCapacity);
            var bundleListener        = new BundleListener(sql, msmqCapacity);
            var poControlListener     = new PersistentObjectControlListener(sql, msmqCapacity);
            var bundleControlListener = new BundleControlListener(sql, msmqCapacity);

            do
            {
                Console.Clear();

                Console.WriteLine("DPAG KISS PoC Zora Client '{0} / {1} / {2}'",
                                  sql, new Neighborhood(sql).GetLocalClient(), Process.GetCurrentProcess().Id);

                Console.WriteLine();

                Console.WriteLine("a. Register PC");
                Console.WriteLine("b. Deregister PC");
                Console.WriteLine("c. Install MSMQ");
                Console.WriteLine("d. Uninstall MSMQ");
                Console.WriteLine("e. Start Background Work");
                Console.WriteLine("f. Stop Background Work");
                Console.WriteLine("g. Distribute Bundles");
                Console.WriteLine("h. Distribute Persistent Objects");
                Console.WriteLine("i. Recover Bundles");
                Console.WriteLine("j. Recover Persistent Objects");
                Console.WriteLine("k. Remove Persistent Objects");
                Console.WriteLine("l. Transmit Bundles");
                Console.WriteLine("w. Clear Report Cache");
                Console.WriteLine("x. Report Bundles");
                Console.WriteLine("y. Report Persistent Objects");
                Console.WriteLine("z. Report Standing Data");

                Console.WriteLine();
                Console.WriteLine("0. Exit Application");
                Console.WriteLine();

                try
                {
                    switch (Console.ReadKey(true).KeyChar)
                    {
                    case 'a':
                        RegisterPC(sql);
                        break;

                    case 'b':
                        DeregisterPC(sql);
                        break;

                    case 'c':
                        InstallMSMQ(sql);
                        break;

                    case 'd':
                        DeinstallMSMQ(sql);
                        break;

                    case 'e':
                        StartBackgroundWork(sql,
                                            sdListener, poListener, bundleListener,
                                            poControlListener, bundleControlListener);
                        break;

                    case 'f':
                        StopBackgroundWork(true,
                                           sdListener, poListener, bundleListener,
                                           poControlListener, bundleControlListener);
                        break;

                    case 'g':
                        DistributeBundles(sql);
                        break;

                    case 'h':
                        DistributePersistentObjects(sql);
                        break;

                    case 'i':
                        RecoverBundles(sql);
                        break;

                    case 'j':
                        RecoverPersistentObjects(sql);
                        break;

                    case 'k':
                        RemovePersistentObjects(sql);
                        break;

                    case 'l':
                        TransmitBundles(sql);
                        break;

                    case 'w':
                        ClearReportCache(ref lastAllBundleIds, ref lastAllPoIds, ref lastAllSdIds);
                        break;

                    case 'x':
                        ReportBundles(sql, ref lastAllBundleIds);
                        break;

                    case 'y':
                        ReportPersistentObjects(sql, ref lastAllPoIds);
                        break;

                    case 'z':
                        ReportStandingData(sql, ref lastAllSdIds);
                        break;

                    case '0':
                        runZoraClient = false;
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("\n\nCaught exception: {0}.\n\n", ex);
                    Console.WriteLine("Press any key to continue.");

                    Console.ReadKey();
                }
            } while (runZoraClient);

            try
            {
                StopBackgroundWork(false,
                                   sdListener, poListener, bundleListener,
                                   poControlListener, bundleControlListener);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Caught exception while stopping KISS jobs and MSMQ listeners: {0}.\n\n", ex);
                Console.WriteLine("Press any key to continue.");

                Console.ReadKey();
            }
        }