public void SendSyncRequestCycler() { try { while (!m_shouldVanish) { if (!m_shouldStop && stopwatch.ElapsedMilliseconds > TIMER_INTERVAL) { //if validation fails if (m_auth.IsXMPPConnected()) { CompanyFileValidator companyFileValidator = new CompanyFileValidator(m_auth); //the send interval calculation should plus the check time (will be fixed) try { m_auth.CompanyFileValidated = companyFileValidator.CheckCompanyMarker(); } catch (System.Net.WebException ex) { //Http connection lost LogHelper.Error(ex); MessageBox.Show(ex.Message); Thread logoutThread = new Thread(this.Logout); logoutThread.Start(); return; } if (!m_auth.CompanyFileValidated) { using (NotPassCompanyFileValidationDialog dialog = new NotPassCompanyFileValidationDialog(m_auth)) { dialog.ShowDialog(); } if (m_auth.Authorized && !m_auth.CompanyFileValidated) { //In case the user click on the "close" button of the dialog // m_auth.Logout(); this will make the two threads lock each other Thread logoutThread = new Thread(this.Logout); logoutThread.Start(); return; } else if (m_auth.Authorized && m_auth.CompanyFileValidated) { //not likely to happen any way MessageBox.Show("Validated"); LogHelper.Debug("Validated"); //StartPingAndSyncRequest(); } } //below will assume the company marker is validated, or it won't hit here. if (m_auth.NeverSynced) { m_auth.NeedsFullSync = true; m_auth.AgreedToFullSync = (int)FullSyncConfirmed.NA; //never synced, it doesn't need confirmation from user /* if (syncMarkerValidator.MarkSync() == (int)Status.SUCCESS) m_auth.NeverSynced = false; else { m_auth.NeverSynced = true; MessageBox.Show("Mark sync failed."); LogHelper.Debug("Mark sync failed."); } */ m_auth.SyncValidated = true; } else { SyncMarkerValidator syncMarkerValidator = new SyncMarkerValidator(m_auth); try { m_auth.SyncValidated = syncMarkerValidator.CheckSyncMarker(); } catch (System.Net.WebException ex) { //Http connection lost LogHelper.Error(ex); MessageBox.Show(ex.Message); Thread logoutThread = new Thread(this.Logout); logoutThread.Start(); return; } m_auth.NeedsFullSync = m_auth.SyncValidated?false:true; } if (!m_auth.SyncValidated) { m_auth.NeedsFullSync = true; m_auth.AgreedToFullSync = (int)FullSyncConfirmed.No; using (AgreeToFullSyncDialog dialog = new AgreeToFullSyncDialog(m_auth)) { dialog.ShowDialog(); } if (m_auth.AgreedToFullSync == (int)FullSyncConfirmed.No) { //In case the user click on the "close" button of the dialog MessageBox.Show("No full sync, no choice. Have to log out."); LogHelper.Debug("No full sync, no choice. Have to log out."); // m_auth.Logout(); this will make the two threads lock each other Thread logoutThread = new Thread(this.Logout); logoutThread.Start(); return; } else { string xmlResponse = m_auth.SendSyncRequest(m_auth.NeedsFullSync); //m_auth.NeedsFullSync = true if (xmlResponse != null || xmlResponse.Length != 0) { //once get confirmation from server, stop timer to wait for the sync done signal RestartTimer(); PauseTimer(); //if no confirmation is received, go on cycling } } } else { //m_auth.NeedsFullSync = false; string xmlResponse = m_auth.SendSyncRequest(m_auth.NeedsFullSync); //m_auth.NeedsFullSync = false if (xmlResponse != null || xmlResponse.Length != 0) { //once get confirmation from server, stop timer to wait for the sync done signal RestartTimer(); PauseTimer(); //if no confirmation is received, go on cycling } } } else { MessageBox.Show("XMPP connection failed. Will log out"); //m_auth.Logout(); } } Thread.Sleep(CHECK_INTERVAL); } } catch (Exception ex) { LogHelper.Error(ex); } }
//This method will encapsulated in a thread and complete the potential validation and start the ping and sync req threads public void ValidateCompanyMarker() { //must wait till the xmpp connection established CompanyFileValidator companyFileValidator = new CompanyFileValidator(this); bool isTimeout = true; int count = 0; int checkInterval = 100; while (count < XMPP_CONNECT_TIMEOUT) { if (m_xmppCH != null && !m_xmppCH.IsOnline) { count += 100; Thread.Sleep(checkInterval); } else { isTimeout = false; break; } } if (isTimeout) { LogHelper.Debug("XMPP Connection timeout"); //companyFileValidator.IsValidated = false; MessageBox.Show("XMPP Connection timeout! Will log out"); Thread logoutThread = new Thread(this.LogoutWrapper); logoutThread.Start(); return; } if (m_xmppCH != null && m_xmppCH.IsOnline) { if (m_isNewAccount) { //mark the file if this account is new if (companyFileValidator.MarkCompanyFile() == (int)Status.SUCCESS) { m_isCompanyFileValidated = true; } else { m_isCompanyFileValidated = false; } } //if the company file has just been marked, no need to validate it else if (!m_isCompanyFileValidated) { try { m_isCompanyFileValidated = companyFileValidator.CheckCompanyMarker(); } catch (System.Net.WebException ex) { //Http connection lost LogHelper.Error(ex); MessageBox.Show(ex.Message); Thread logoutThread = new Thread(this.LogoutWrapper); logoutThread.Start(); return; } } if (m_isCompanyFileValidated) { //Once validated //start ping thread and sending sync request MessageBox.Show("Validated"); LogHelper.Debug("Validated"); StartPingAndSyncRequest(); } else { using (NotPassCompanyFileValidationDialog dialog = new NotPassCompanyFileValidationDialog(this)) { dialog.ShowDialog(); } if (!m_isCompanyFileValidated) { //In case the user click on the "close" button of the dialog Thread logoutThread = new Thread(this.LogoutWrapper); logoutThread.Start(); return; } else if (m_isAuthorized && m_isCompanyFileValidated) { //not likely to happen any way MessageBox.Show("Validated"); LogHelper.Debug("Validated"); StartPingAndSyncRequest(); } } } else { MessageBox.Show("XMPP offline. Can't validate"); LogHelper.Debug("XMPP offline. Can't validate"); } }