/// <summary> /// Responsible for setting up the web client to begin downloading a file from a given host as specified /// in the datagrid. It also specifies the where the downloaded file will be saved. /// </summary> /// <param name="fileName"></param> private void FtpDownloadFile(String fileName) { // Create the new WebClient and add the event handlers m_WebClient = new WebClient(); m_WebClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged); m_WebClient.DownloadFileCompleted += new AsyncCompletedEventHandler(webClient_DownloadFileCompleted); // Create the full URL including file name string ftphost = @"ftp://" + dataGridView1.Rows[m_VCUIndex].Cells[0].Value.ToString(); string ftpfullpath = ftphost + "/" + fileName; // Used when the amount of VCUs selected creates a scroll bar dataGridView1.Rows[m_VCUIndex].Selected = true; dataGridView1.FirstDisplayedScrollingRowIndex = m_VCUIndex; // reset all state parameters m_FTPTimeoutCount = 0; m_FTPDownloadEnded = false; m_FTPDownloadState = FTPDownloadState.IN_PROGRESS; // Change the cell to yellow indicating a download is in progress dataGridView1.Rows[m_VCUIndex].DefaultCellStyle.BackColor = Color.Yellow; dataGridView1.Rows[m_VCUIndex].Cells[1].Value = "In Progress..."; // Create the file name that will be stored string fileSavePath = @"C:\Ptu\Pcu\Data\Temp\"; // Verify the directory exists; it should as long as the PTU was installed on this machine if (!Directory.Exists(fileSavePath)) { Directory.CreateDirectory(fileSavePath); } // Strip directory from fileName... look for the last '/' in the path/filename String fileNameWithoutRemoteDir = fileName.Substring(fileName.LastIndexOf('/') + 1); // This is the path and name of the file that is downloaded and saved string fullyQualifiedFileName = fileSavePath + fileNameWithoutRemoteDir; // Start the download m_WebClient.DownloadFileAsync(new Uri(ftpfullpath), fullyQualifiedFileName); }
/// <summary> /// Cancels all remaining downloads from any additional selected VCUs /// </summary> /// <param name="errorOrCancel">Informs user whether error or user canceled.</param> private void CancelRemaining(string errorOrCancel) { dataGridView1.Rows[m_VCUIndex].Cells[1].Value = errorOrCancel; dataGridView1.Rows[m_VCUIndex].Cells[2].Value = "N/A"; dataGridView1.Rows[m_VCUIndex].DefaultCellStyle.BackColor = Color.OrangeRed; m_VCUIndex++; while (m_VCUIndex < m_VCUList.Count) { dataGridView1.Rows[m_VCUIndex].Cells[1].Value = "Canceled"; dataGridView1.Rows[m_VCUIndex].Cells[2].Value = "N/A"; dataGridView1.Rows[m_VCUIndex].DefaultCellStyle.BackColor = Color.OrangeRed; m_VCUIndex++; } m_FTPDownloadState = FTPDownloadState.IDLE; buttonCancelDownload.Enabled = false; buttonStartDownload.Enabled = true; buttonSelectVCU.Enabled = true; groupBoxRTDM.Enabled = true; groupBoxIELF.Enabled = true; toolStripStatusLabel2.Text = "Click \"Start FTP Download\" button to restart the FTP downloading."; }
/// <summary> /// Displays a custom message box informing the user of the error that was detected /// and presents the user with options based on when and where the error /// </summary> /// <param name="errorMessage">error message displayed to the user</param> /// <param name="userOrError">user canceled or error stopped the download</param> private void InvokeErrorMessageBox(string errorMessage, FTPStopped userOrError) { m_HandleDownloadError = true; m_FTPDownloadState = FTPDownloadState.HANDLE_ERROR; bool lastUrl = (m_VCUIndex + 1 >= m_VCUList.Count) ? true : false; // "using" required so that form is kept from being garbage collected until // the UserChoice is read using (HandleErrorForm errorForm = new HandleErrorForm(errorMessage, lastUrl)) { errorForm.StartPosition = FormStartPosition.CenterScreen; errorForm.ShowDialog(this); switch (errorForm.UserChoice) { case HandleErrorForm.UserChoiceEnum.CONTINUE_WITH_NEXT_URL: // display the proper info in the data grid on why the VCU download was stopped if (userOrError == FTPStopped.BY_USER) { dataGridView1.Rows[m_VCUIndex].Cells[1].Value = "Canceled"; } else { dataGridView1.Rows[m_VCUIndex].Cells[1].Value = "Error"; } dataGridView1.Rows[m_VCUIndex].Cells[2].Value = "N/A"; dataGridView1.Rows[m_VCUIndex].DefaultCellStyle.BackColor = Color.OrangeRed; m_FTPDownloadState = FTPDownloadState.CHECK_FOR_MORE; break; case HandleErrorForm.UserChoiceEnum.CANCEL_ALL_REMAINING: if (userOrError == FTPStopped.BY_USER) { CancelRemaining("Canceled"); } else { CancelRemaining("Error"); } m_FTPDownloadState = FTPDownloadState.IDLE; break; case HandleErrorForm.UserChoiceEnum.LAST_URL: // Handle the last URL in the list if (userOrError == FTPStopped.BY_USER) { dataGridView1.Rows[m_VCUIndex].Cells[1].Value = "Canceled"; } else { dataGridView1.Rows[m_VCUIndex].Cells[1].Value = "Error"; } dataGridView1.Rows[m_VCUIndex].Cells[2].Value = "N/A"; dataGridView1.Rows[m_VCUIndex].DefaultCellStyle.BackColor = Color.OrangeRed; m_FTPDownloadState = FTPDownloadState.IDLE; buttonCancelDownload.Enabled = false; buttonStartDownload.Enabled = true; buttonSelectVCU.Enabled = true; groupBoxRTDM.Enabled = true; groupBoxIELF.Enabled = true; toolStripStatusLabel2.Text = "Click \"Start FTP Download\" button to restart the FTP downloading."; break; } } }
/// <summary> /// TODO - handling this the old fashioned way and needs to be changed to /// an event driven state machine. /// </summary> private void ProcessFTPDownloads() { // Handle the case where there is a download error or user cancels the current download if (m_HandleDownloadError) { m_FTPDownloadState = FTPDownloadState.HANDLE_ERROR; m_HandleDownloadError = false; } switch (m_FTPDownloadState) { case FTPDownloadState.IDLE: case FTPDownloadState.HANDLE_ERROR: default: // wait here for the message box to be closed if (m_InitDownload) { m_FTPDownloadState = FTPDownloadState.INITIATE_DOWNLOAD; m_InitDownload = false; } break; case FTPDownloadState.INITIATE_DOWNLOAD: if (m_FilesToDownload.Count > 0) { string uri = dataGridView1.Rows[m_VCUIndex].Cells[0].Value.ToString(); m_NumBytesToDownload = GetFileSize(uri, m_FilesToDownload[0]); FtpDownloadFile(m_FilesToDownload[0]); // Remove the file from the queue m_FilesToDownload.RemoveAt(0); m_FTPDownloadState = FTPDownloadState.IN_PROGRESS; } break; case FTPDownloadState.IN_PROGRESS: MonitorDownloadProgress(); if ((m_FTPDownloadEnded) && (m_FilesToDownload.Count > 0)) { m_FTPDownloadState = FTPDownloadState.INITIATE_DOWNLOAD; m_FileCount++; toolStripStatusLabel2.Text = "\"" + m_FilesToDownload[0] + "\"" + " being downloaded from the selected VCU: " + m_FileCount.ToString() + " of " + m_FilesToDownloadCount.ToString(); } else if (m_FTPDownloadEnded) { m_FTPDownloadState = FTPDownloadState.DOWNLOAD_COMPLETE; } break; case FTPDownloadState.DOWNLOAD_COMPLETE: StringBuilder fileName = new StringBuilder(256); StringBuilder errorString = new StringBuilder(256); String toolStripText = "Downloading Complete... "; // Compile all of the RTDM data if (cBoxRTDMDownload.Checked) { Int32 errorCode = BuildViewableDanFile(fileName, errorString); if (errorCode == 0) { toolStripText += "RTDM OK... "; // Move the file to 1 directory above String origFileNameRtdm = fileName.ToString(); String newFileNameRtdm = origFileNameRtdm.Replace("Temp\\", ""); File.Move(origFileNameRtdm, newFileNameRtdm); // Clear all RTDM data if checked (upload clear file and VCU clears the files // when this file is discovered. VCU will then delete the file if (cBoxClearRTDMData.Checked) { UploadClearFile(RTDM_DRIVE_DIR + "/Clear.rtdm", "CLEAR RTDM"); } } else { toolStripText += "RTDM FAILED... "; toolStripStatusLabel2.Text = "File Downloading Complete... ERROR PRODUCING VIEWABLE RTDM FILE!!!"; timer1.Enabled = false; MessageBox.Show(errorString.ToString(), "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); timer1.Enabled = true; } } // Create the IELF file and corresponding CSV file if (cBoxIELFDownload.Checked) { Int32 errorCode = BuildDanFileName(fileName, errorString); String danFileName = fileName.ToString(); String newDanFileName = danFileName.Replace("Temp\\", ""); // Create new event file with the proper naming convention String origFileNameIelf = Path.GetDirectoryName(danFileName) + "\\ielf.flt"; String newFileNameIelf = Path.GetDirectoryName(newDanFileName) + "\\" + Path.GetFileNameWithoutExtension(newDanFileName) + ".flt"; newFileNameIelf = newFileNameIelf.Replace("rtdm", "iev_"); if (File.Exists(newFileNameIelf)) { File.Delete(newFileNameIelf); } File.Move(origFileNameIelf, newFileNameIelf); // Create a human readable CSV file from the binary file CreateIelfCsv c = new CreateIelfCsv(); string errorMsg = c.CreateCSVFile(newFileNameIelf); if (errorMsg != null) { toolStripText += "IELF FAILED"; MessageBox.Show(errorMsg, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { toolStripText += "IELF OK"; } // Clear all IELF data if checked (upload clear file and VCU clears the files // when this file is discovered. VCU will then delete the file if (cBoxClearIELFData.Checked) { UploadClearFile(IELF_DRIVE_DIR + "/Clear.ielf", "CLEAR IELF"); } } toolStripStatusLabel2.Text = toolStripText; // Delete all other files and "Temp" directory Directory.Delete(@"C:\Ptu\Pcu\Data\Temp\", true); // Poll and wait for directory deletion; this is only done to avoid conflicts // with a subsequent download from another VCU while (Directory.Exists(@"C:\Ptu\Pcu\Data\Temp\")) { System.Threading.Thread.Sleep(100); } m_FTPDownloadState = FTPDownloadState.CHECK_FOR_MORE; break; case FTPDownloadState.CHECK_FOR_MORE: // Are there more target VCUs to download from ? m_VCUIndex++; if (m_VCUIndex < m_VCUList.Count) { QueueUpFileDownloadList(); m_FTPDownloadState = FTPDownloadState.INITIATE_DOWNLOAD; } else { buttonCancelDownload.Enabled = false; buttonStartDownload.Enabled = true; buttonSelectVCU.Enabled = true; groupBoxRTDM.Enabled = true; groupBoxIELF.Enabled = true; m_FTPDownloadState = FTPDownloadState.IDLE; } break; } }