/// <summary> /// Periodically check the status of update processes and resend the update file if necessary /// </summary> private void PendingUpdates() { while (m_IsAliveThreadPendingUpdates) { m_mutexPendingUpdates.WaitOne(); var pendingUpdatesFinal = m_pendingUpdates; foreach (CUpdaterServicePending pendingCom in m_pendingUpdates) { //Obtenemos el estado de la actualización BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None); JBCUpdaterServiceClient serviceClient = new JBCUpdaterServiceClient(binding, pendingCom.endPoint); serviceClient.Open(); dc_EnumConstJBCdc_UpdateState state = serviceClient.StateUpdate(); serviceClient.Close(); // // Success operation // if (state == dc_EnumConstJBCdc_UpdateState.Finished) { pendingUpdatesFinal.Remove(pendingCom.endPoint.ToString()); // // Fail or Updating // } else { if (pendingCom.nTriesRemainingUpdate == 0) { pendingUpdatesFinal.Remove(pendingCom.endPoint.ToString()); //TODO send error message } else { pendingCom.DecrementRemainingUpdate(); pendingUpdatesFinal.Remove(pendingCom.endPoint.ToString()); pendingUpdatesFinal.Add(pendingCom, pendingCom.endPoint.ToString(), null, null); if (SendFile(pendingCom.endPoint, pendingCom.sUrlSendFile)) { SendInitUpdateCommand(pendingCom.endPoint); } else { pendingUpdatesFinal.Remove(pendingCom.endPoint.ToString()); //TODO send error message } } } } m_pendingUpdates = pendingUpdatesFinal; m_mutexPendingUpdates.Release(); Thread.Sleep(TIME_RETRY_UPDATE); } }
/// <summary> /// Send update file to windows service /// </summary> /// <param name="endpoint">Windows service's endpoint</param> /// <param name="pathSw">Update file path</param> /// <returns>True if the file is sent successful</returns> private bool SendFile(EndpointAddress endpoint, string pathSw) { bool bOk = false; //Open connection BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None); UriBuilder uri = new UriBuilder(endpoint.Uri.Scheme, endpoint.Uri.Host, endpoint.Uri.Port, "/JBCUpdaterSrv/service"); EndpointAddress updateEndpoint = new EndpointAddress(uri.ToString()); JBCUpdaterServiceClient serviceClient = new JBCUpdaterServiceClient(binding, updateEndpoint); serviceClient.Open(); //Input file int nSequence = 1; FileStream fileStream = new FileStream(pathSw, FileMode.Open, FileAccess.Read); BinaryReader binaryReader = new BinaryReader(fileStream); byte[] bytes = null; do { bytes = binaryReader.ReadBytes(CHUNK_SIZE); if (bytes.Length > 0) { int nTries = 3; bOk = false; while (nTries > 0 && !bOk) { bOk = serviceClient.ReceiveFile(nSequence, bytes) == nSequence; if (!bOk) { System.Threading.Thread.Sleep(3000); //Esperamos un tiempo antes de intentar volver a enviar el paquete } nTries--; } } if (!bOk) { break; } nSequence++; } while (bytes.Length > 0); //Close file and connection fileStream.Close(); serviceClient.Close(); if (!bOk) { LoggerModule.logger.Warn(System.Reflection.MethodInfo.GetCurrentMethod().Name + ". Error: can't send data"); } return(bOk); }
/// <summary> /// Send updater command to windows service /// </summary> /// <param name="ep">Windows service's endpoint</param> private void SendInitUpdateCommand(EndpointAddress ep) { BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None); JBCUpdaterServiceClient serviceClient = new JBCUpdaterServiceClient(binding, ep); serviceClient.Open(); serviceClient.InitUpdate(); serviceClient.Close(); }
/// <summary> /// Wait until the windows service is ready to accept updates /// </summary> /// <param name="ep">Windows service endpoint</param> /// <returns>True if the windows service is ready to accept updates</returns> private bool WaitReadyUpdater(EndpointAddress ep) { bool bReady = false; int nTries = WAIT_READY_UPDATER_TRIES; int nTimeWait = WAIT_READY_UPDATER_TIME; BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None); JBCUpdaterServiceClient serviceClient = new JBCUpdaterServiceClient(binding, ep); try { serviceClient.Open(); do { if (serviceClient.StateUpdate() == dc_EnumConstJBCdc_UpdateState.Updating) { nTries--; Thread.Sleep(nTimeWait); } else { bReady = true; } } while (nTries > 0 && !bReady); if (nTries == 0) { LoggerModule.logger.Warn(System.Reflection.MethodInfo.GetCurrentMethod().Name + ". Time exceded"); } } catch (Exception ex) { LoggerModule.logger.Error(System.Reflection.MethodInfo.GetCurrentMethod().Name + ". Error: " + ex.Message); } finally { serviceClient.Close(); } return(bReady); }