public static global::System.Xml.Schema.XmlSchemaComplexType GetTypedTableSchema(global::System.Xml.Schema.XmlSchemaSet xs) {
     global::System.Xml.Schema.XmlSchemaComplexType type = new global::System.Xml.Schema.XmlSchemaComplexType();
     global::System.Xml.Schema.XmlSchemaSequence sequence = new global::System.Xml.Schema.XmlSchemaSequence();
     ScheduledFTPFile ds = new ScheduledFTPFile();
     global::System.Xml.Schema.XmlSchemaAny any1 = new global::System.Xml.Schema.XmlSchemaAny();
     any1.Namespace = "http://www.w3.org/2001/XMLSchema";
     any1.MinOccurs = new decimal(0);
     any1.MaxOccurs = decimal.MaxValue;
     any1.ProcessContents = global::System.Xml.Schema.XmlSchemaContentProcessing.Lax;
     sequence.Items.Add(any1);
     global::System.Xml.Schema.XmlSchemaAny any2 = new global::System.Xml.Schema.XmlSchemaAny();
     any2.Namespace = "urn:schemas-microsoft-com:xml-diffgram-v1";
     any2.MinOccurs = new decimal(1);
     any2.ProcessContents = global::System.Xml.Schema.XmlSchemaContentProcessing.Lax;
     sequence.Items.Add(any2);
     global::System.Xml.Schema.XmlSchemaAttribute attribute1 = new global::System.Xml.Schema.XmlSchemaAttribute();
     attribute1.Name = "namespace";
     attribute1.FixedValue = ds.Namespace;
     type.Attributes.Add(attribute1);
     global::System.Xml.Schema.XmlSchemaAttribute attribute2 = new global::System.Xml.Schema.XmlSchemaAttribute();
     attribute2.Name = "tableTypeName";
     attribute2.FixedValue = "FileDownloadDataTable";
     type.Attributes.Add(attribute2);
     type.Particle = sequence;
     global::System.Xml.Schema.XmlSchema dsSchema = ds.GetSchemaSerializable();
     if (xs.Contains(dsSchema.TargetNamespace)) {
         global::System.IO.MemoryStream s1 = new global::System.IO.MemoryStream();
         global::System.IO.MemoryStream s2 = new global::System.IO.MemoryStream();
         try {
             global::System.Xml.Schema.XmlSchema schema = null;
             dsSchema.Write(s1);
             for (global::System.Collections.IEnumerator schemas = xs.Schemas(dsSchema.TargetNamespace).GetEnumerator(); schemas.MoveNext(); ) {
                 schema = ((global::System.Xml.Schema.XmlSchema)(schemas.Current));
                 s2.SetLength(0);
                 schema.Write(s2);
                 if ((s1.Length == s2.Length)) {
                     s1.Position = 0;
                     s2.Position = 0;
                     for (; ((s1.Position != s1.Length) 
                                 && (s1.ReadByte() == s2.ReadByte())); ) {
                         ;
                     }
                     if ((s1.Position == s1.Length)) {
                         return type;
                     }
                 }
             }
         }
         finally {
             if ((s1 != null)) {
                 s1.Close();
             }
             if ((s2 != null)) {
                 s2.Close();
             }
         }
     }
     xs.Add(dsSchema);
     return type;
 }
Beispiel #2
0
        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();
        }
 public static global::System.Xml.Schema.XmlSchemaComplexType GetTypedDataSetSchema(global::System.Xml.Schema.XmlSchemaSet xs) {
     ScheduledFTPFile ds = new ScheduledFTPFile();
     global::System.Xml.Schema.XmlSchemaComplexType type = new global::System.Xml.Schema.XmlSchemaComplexType();
     global::System.Xml.Schema.XmlSchemaSequence sequence = new global::System.Xml.Schema.XmlSchemaSequence();
     global::System.Xml.Schema.XmlSchemaAny any = new global::System.Xml.Schema.XmlSchemaAny();
     any.Namespace = ds.Namespace;
     sequence.Items.Add(any);
     type.Particle = sequence;
     global::System.Xml.Schema.XmlSchema dsSchema = ds.GetSchemaSerializable();
     if (xs.Contains(dsSchema.TargetNamespace)) {
         global::System.IO.MemoryStream s1 = new global::System.IO.MemoryStream();
         global::System.IO.MemoryStream s2 = new global::System.IO.MemoryStream();
         try {
             global::System.Xml.Schema.XmlSchema schema = null;
             dsSchema.Write(s1);
             for (global::System.Collections.IEnumerator schemas = xs.Schemas(dsSchema.TargetNamespace).GetEnumerator(); schemas.MoveNext(); ) {
                 schema = ((global::System.Xml.Schema.XmlSchema)(schemas.Current));
                 s2.SetLength(0);
                 schema.Write(s2);
                 if ((s1.Length == s2.Length)) {
                     s1.Position = 0;
                     s2.Position = 0;
                     for (; ((s1.Position != s1.Length) 
                                 && (s1.ReadByte() == s2.ReadByte())); ) {
                         ;
                     }
                     if ((s1.Position == s1.Length)) {
                         return type;
                     }
                 }
             }
         }
         finally {
             if ((s1 != null)) {
                 s1.Close();
             }
             if ((s2 != null)) {
                 s2.Close();
             }
         }
     }
     xs.Add(dsSchema);
     return type;
 }