/// <summary>
        /// constructor
        /// </summary>
        public RemoteNotification(ApnSettings apnSettings)
        {
            m_DeviceInfoByLogin = new Dictionary <string, DeviceInfo>();
            ///Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "M4_PUSH_Production.p12")
            pushBrokerIOS = new PushBroker();
            var appleCertIOS = File.ReadAllBytes(apnSettings.CertPathIOS);

            pushBrokerIOS.RegisterAppleService(new ApplePushChannelSettings(apnSettings.IsProductionIOS, appleCertIOS, apnSettings.CertPasswordIOS)); // Extension method
            pushBrokerOSX = new PushBroker();
            var appleCertOSX = File.ReadAllBytes(apnSettings.CertPathOSX);

            pushBrokerOSX.RegisterAppleService(new ApplePushChannelSettings(apnSettings.IsProductionOSX, appleCertOSX, apnSettings.CertPasswordOSX, true)); // Extension method
        }
        private void OnStart_Click(object sender, EventArgs e)
        {
            Dictionary <string, object> aAdditionalParameters = new Dictionary <string, object>();

            if (m_MessageProcessor != null)
            {
                MessageBox.Show("Server already started", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);

                return;
            }
            DataServer.Program.gLogger.WriteToLog_Info("Starting...", null);
            if (m_Authentication == null)
            {
                string aConnectionString = ConfigurationManager.ConnectionStrings["DataServer"].ConnectionString;

                m_Authentication = new Authentication(DataServer.Program.gLogger);
                m_Authentication.Init(aConnectionString);
            }
            aAdditionalParameters["sql connection string"] = m_Authentication.ConnectionStr;

            if (m_RemoteNotification == null)
            {
                // Read APN settings

                String apnConfFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, APN_SETTINGS_FILE);

                if (File.Exists(apnConfFilePath))
                {
                    String[] confLines = null;
                    try
                    {
                        confLines = File.ReadAllLines(apnConfFilePath);
                    }
                    catch (Exception ex)
                    {
                        DataServer.Program.gLogger.WriteToLog("APN conf file reading.", ex);
                    }

                    // 1-st line must be absolute path to iOS cert file.
                    // 2-nd lines must be iOS cert password.
                    // 3-rd line must be boolean value if this is production environment for iOS.
                    // 4-th line must be absolute path to OS X cert file.
                    // 5-th lines must be OS X cert password.
                    // 5-th line must be boolean value if this is production environment for OS X.
                    if (confLines != null && confLines.Length <= 6)
                    {
                        String  certPathIOS                = confLines[0];
                        String  certPasswordIOS            = confLines[1];
                        Boolean isProductionEnvironmentIOS = false;
                        Boolean.TryParse(confLines[2], out isProductionEnvironmentIOS);

                        String  certPathOSX                = confLines[3];
                        String  certPasswordOSX            = confLines[4];
                        Boolean isProductionEnvironmentOSX = false;
                        Boolean.TryParse(confLines[5], out isProductionEnvironmentOSX);

                        if (File.Exists(certPathIOS) && File.Exists(certPathOSX))
                        {
                            var apnSettings = new ApnSettings()
                            {
                                CertPathIOS     = certPathIOS,
                                CertPasswordIOS = certPasswordIOS,
                                IsProductionIOS = isProductionEnvironmentIOS,
                                CertPathOSX     = certPathOSX,
                                CertPasswordOSX = certPasswordOSX,
                                IsProductionOSX = isProductionEnvironmentOSX
                            };
                            m_RemoteNotification = new RemoteNotification(apnSettings);
                        }
                        else
                        {
                            DataServer.Program.gLogger.WriteToLog_Warning("APN cert file doesn't exists.", null);
                        }
                    }
                    else
                    {
                        DataServer.Program.gLogger.WriteToLog_Warning("Invalid APN conf file format.", null);
                    }
                }
                else
                {
                    DataServer.Program.gLogger.WriteToLog_Info("APN conf file path: " + apnConfFilePath, null);
                    DataServer.Program.gLogger.WriteToLog_Warning("No APN config file was found.", null);
                }
            }

            List <DataFeedItem>        aInitializedFeeders            = new List <DataFeedItem>();
            List <ConnServiceHostItem> aInitializedConnectionServices = new List <ConnServiceHostItem>();

            lblState.Text = "Starting";
            lblState.Refresh();
            EnableControls(false);
            SaveChanges();
            foreach (DataFeedItem item in m_DataFeeds)
            {
                item.Error = string.Empty;
                if (item.Enabled)
                {
                    if (item.Parameters == null && item.DataFeed != null)
                    {
                        item.Parameters = item.DataFeed.DefaultSettings;
                    }
                    aInitializedFeeders.Add(item);
                }
                else
                {
                    item.State = "Disabled";
                }
            }
            foreach (ConnServiceHostItem item in m_ConnServiceHosts)
            {
                item.Error = string.Empty;
                if (item.Enabled)
                {
                    if (item.Parameters == null && item.Host != null)
                    {
                        item.Parameters = item.Host.DefaultSettings;
                    }
                    aInitializedConnectionServices.Add(item);
                }
                else
                {
                    item.State = "Disabled";
                }
            }
            dgvDataFeeds.Refresh();
            dgvConnServiceHosts.Refresh();
            // initialize message router
            if (DataServer.MessageRouter.gMessageRouter == null)
            {
                DataServer.MessageRouter.gMessageRouter = new MessageRouter();

                DataServer.MessageRouter.gMessageRouter.Init(m_Authentication, m_RemoteNotification);
                DataServer.MessageRouter.gMessageRouter.AddedSession   += OnAddedSession_MsgRouter;
                DataServer.MessageRouter.gMessageRouter.RemovedSession += OnRemovedSession_MsgRouter;
            }
            lock (DataServer.MessageRouter.gMessageRouter)
            {
                foreach (DataFeedItem item in aInitializedFeeders)
                {
                    try
                    {
                        item.State = "Starting...";
                        item.DataFeed.Start(item.Parameters, m_DataFeedFolder);
                        item.State = "Started";
                    }
                    catch (Exception ex)
                    {
                        item.State = "Failed";
                        item.Error = ex.Message;
                        DataServer.Program.gLogger.WriteToLog_Warning(String.Format("{0} feeder failed to start.", item.Name), ex);
                    }
                }
                foreach (ConnServiceHostItem item in aInitializedConnectionServices)
                {
                    try
                    {
                        item.State = "Starting...";
                        item.Host.Start(item.Parameters);
                        item.State = "Started";
                    }
                    catch (Exception ex)
                    {
                        item.State = "Failed";
                        item.Error = ex.Message;
                        DataServer.Program.gLogger.WriteToLog_Warning(String.Format("{0} connection service failed to start.", item.Name), ex);
                    }
                }
            }

            m_MessageProcessor = new MessageProcessor();
            m_MessageProcessor.Start(aInitializedFeeders, aInitializedConnectionServices, aAdditionalParameters);

            dgvDataFeeds.Refresh();
            dgvConnServiceHosts.Refresh();

            lblState.Text   = "Started";
            btnStop.Enabled = true;
            this.timer1.Start();
            DataServer.Program.gLogger.WriteToLog_Info("Started.", null);
        }