void ThreadProc() { _stopEvent.Reset(); string myPath = System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().Location); string myConfig = myPath + @"\applicationConfig.xml"; ApplicationSettings mySettings = new ScheduledFtpService.ApplicationSettings(); string SourceDirectoryPath; string FailedDirectoryPath; string ManagementServerAddress; string ManagementServerPassword; if (!File.Exists(myConfig)) { Log.WriteLog(myConfig + " does not exist. Creating one."); ClearAndWriteSettingsRow(mySettings); mySettings.WriteXml(myConfig); } mySettings.ReadXml(myConfig); ApplicationSettings.SettingsRow settingsRow; if (mySettings.Settings.Count == 0) { Log.WriteLog("There are no settings in " + myConfig); ClearAndWriteSettingsRow(mySettings); } settingsRow = mySettings.Settings[0]; if (settingsRow.SourceDirectory.Length == 0) { Log.WriteLog("SourceDirectory in " + myConfig + " is empty. Exiting."); _done = true; return; } ManagementServerAddress = settingsRow.ManagementServerAddress; if (ManagementServerAddress.Length == 0) { Log.WriteLog("ManagementServerAddress in " + myConfig + " is empty. Exiting."); _done = true; return; } if (settingsRow.IsIdEncrypted) ManagementServerPassword = CarverLab.Utility.Crypto.DecryptFromBase64String(settingsRow.ManagementServerId); else ManagementServerPassword = settingsRow.ManagementServerId; SourceDirectoryPath = settingsRow.SourceDirectory; FailedDirectoryPath = Path.Combine(SourceDirectoryPath, "Failed"); DirectoryInfo SourceDirectoryInfo = TryGetDirectory(SourceDirectoryPath); if (SourceDirectoryInfo == null) return; DirectoryInfo FailedDirectoryInfo = TryGetDirectory(FailedDirectoryPath); if (FailedDirectoryInfo == null) return; int TransferRetryMaxCount = mySettings.Settings[0].TransferRetryMaxCount; if (TransferRetryMaxCount == 0) { TransferRetryMaxCount = 3; } HybridDictionary WaitingFileList = new HybridDictionary(); Session ftpSession = null; while (!_done) { try { FileInfo[] FileInfoList = SourceDirectoryInfo.GetFiles("*.xml"); SortedList SortedFileInfoList = new SortedList( new FileInfoDateComparer(FileInfoDateComparer.DateCompareType.CreationTime)); foreach (FileInfo fi in FileInfoList) { if (!SortedFileInfoList.Contains(fi)) SortedFileInfoList.Add(fi,fi); else // this should never happen, but it did! what?!??! Log.WriteLog("For some reason, " + fi.FullName + " showed up twice in the directory!"); } foreach (DictionaryEntry entry in SortedFileInfoList) { if (_done) break; FileInfo xmlFileInfo = entry.Value as FileInfo; ScheduledFTPFile transferFile = new ScheduledFTPFile(); ScheduledFTPFile.FileUploadRow row; try { transferFile.ReadXml(xmlFileInfo.FullName); } catch (System.Exception e) { Log.WriteLog(xmlFileInfo.FullName + " Exception: " + e.Message); OnFireUploadedEvent(xmlFileInfo.FullName, false); continue; } row = transferFile.FileUpload[0]; string wmvPath = row.FilePath; if (!File.Exists(wmvPath)) { Log.WriteLog(wmvPath + " does not exist. Moving to Failed directory."); try { File.Move(xmlFileInfo.FullName, FailedDirectoryInfo.FullName + "\\" + xmlFileInfo.Name); } catch (System.Exception e) { Log.WriteLog("Could not move " + xmlFileInfo.FullName + ": " + e.Message); } OnFireUploadedEvent(xmlFileInfo.FullName, false); continue; } // open the file and see if it is in use... if it is, continue and try again... // and keep trying until WME lets go of the file... sheesh try { Log.WriteLog("Checking to see if " + wmvPath + " is still held by the encoder..."); FileStream fileCheck = new FileStream(wmvPath, FileMode.Open, FileAccess.ReadWrite); fileCheck.Close(); Log.WriteLog(wmvPath + " is free to upload."); } catch (System.Exception e) { if (e.Message.IndexOf("because it is being used") != -1) { Log.WriteLog("Yep, " + wmvPath + " is in use. Will try again... and again."); continue; } // just try again anyway... Log.WriteLog("Error: " + wmvPath + " error: " + e.Message); continue; } if (ftpSession != null) { Log.WriteLog("*** ERROR *** : ftpSession variable should be null!"); if (ftpSession.IsConnected) ftpSession.Close(); ftpSession = null; //throw new ApplicationException("ftpSession should be null. There must be a serious problem."); } ftpSession = new Session(); ftpSession.Server = row.ServerAddress; ftpSession.Port = row.ServerPort; string user, pass; if (row.EncryptedCredentials) { user = Crypto.DecryptFromBase64String(row.User); pass = Crypto.DecryptFromBase64String(row.Password); } else { user = row.User; pass = row.Password; } try { Log.WriteLog("Connecting to ftp server..."); ftpSession.Connect(user, pass); } catch (Exception ftpex) { Log.WriteLog("Could not connect to " + row.ServerAddress + ":" + row.ServerPort + ftpex.Message); OnFireUploadedEvent(xmlFileInfo.FullName, false); continue; } FtpDirectory dir; Log.WriteLog("Setting ftp current directory to " + row.Directory); if (row.Directory.Length == 0 || row.Directory == ".") { dir = ftpSession.CurrentDirectory; Log.WriteLog("Current ftp directory: " + dir.FullName); } else { Log.Verbose("Subdirectories:"); foreach (FtpDirectory dd in ftpSession.CurrentDirectory.SubDirectories) { Log.Verbose(string.Format("Name: {0}; Full name: {1};", dd.Name, dd.FullName)); } dir = ftpSession.CurrentDirectory.FindSubdirectory(row.Directory, true); if (dir == null) { Log.WriteLog("Subdirectory " + row.Directory + " doesn't exist on the server."); File.Move(xmlFileInfo.FullName, FailedDirectoryInfo.FullName + "\\" + xmlFileInfo.Name); OnFireUploadedEvent(xmlFileInfo.FullName, false); continue; } Log.WriteLog("Uploading to ftp directory: " + dir.FullName); } try { Log.WriteLog("Opening file " + wmvPath); Stream localStream = File.OpenRead(wmvPath); Log.WriteLog("Creating ftp stream."); string wmvName = Path.GetFileName(wmvPath); Stream remoteStream = dir.CreateFileStream(wmvName); int readed; byte[] uploadBuffer = new byte[4096]; Log.WriteLog("Transferring file " + wmvName); while ((readed = localStream.Read(uploadBuffer,0,4096)) != 0) { if (_done) break; remoteStream.Write(uploadBuffer, 0, 4096); } if (_done) break; remoteStream.Close(); localStream.Close(); ftpSession.Close(); ftpSession = null; Log.WriteLog("Transfer complete on file " + xmlFileInfo.FullName); Log.WriteLog("Connecting to Oyster system at " + ManagementServerAddress); OysterClassLibrary.Oyster oyster = new OysterClassLibrary.Oyster( ManagementServerAddress, ManagementServerPassword); OysterClassLibrary.Recording rec = oyster.GetRecordingByName(wmvName); if (rec == null) { throw new ApplicationException(wmvName + " is not a valid recording on the server."); } rec.IsReady = true; Log.WriteLog("Recording is ready: " + wmvName); xmlFileInfo.Delete(); Log.WriteLog("Deleted : " + xmlFileInfo.Name); System.IO.File.Delete(wmvPath); Log.WriteLog("Deleted : " + wmvName); OnFireUploadedEvent(xmlFileInfo.FullName, true); } catch (System.Exception upex) { Log.WriteLog("Transfer failed: " + xmlFileInfo.FullName + ": " + upex.Message); TryFileMove(xmlFileInfo.FullName, Path.Combine( FailedDirectoryInfo.FullName, xmlFileInfo.Name)); OnFireUploadedEvent(xmlFileInfo.FullName, false); continue; } } System.Threading.Thread.Sleep(1000); } catch (System.Exception mainex) { Log.WriteLog("Exception in main loop: " + mainex.ToString()); if (ftpSession != null) { if (ftpSession.IsConnected) { ftpSession.Close(); } ftpSession = null; } } } _stopEvent.Set(); }