Esempio n. 1
0
        //
        //
        /// <summary>
        /// Requests that the current drop file be copied to specified directory/filename.
        /// (If either of these strings are string.Empty, then the current value is used;
        /// that is, if you want to save the current drop file named, say, "Drop.txt" into a directory
        /// called "C:\\Archive\\" with the same file name, call with arguments ("C:\\Archive\\",string.Empty)
        /// </summary>
        /// <param name="targetPath">Archival directory path (with trailing "\\")</param>
        /// <param name="targetFileName">Archival file name.</param>
        /// <returns></returns>
        public bool RequestCopyTo(string targetPath, string targetFileName)
        {
            DropQueueWriterEventArgs eventArgs = GetRequest(DropQueueWriterRequestType.CopyTo);

            eventArgs.Message.Append(targetFileName);
            eventArgs.Message2.Append(targetPath);
            return(this.HubEventEnqueue(eventArgs));
        }
Esempio n. 2
0
        }// ProcessCopyAllFiles()

        //
        //
        // *********************************************************************
        // ****                     Process MoveTo()                        ****
        // *********************************************************************
        private void ProcessMoveTo(DropQueueWriterEventArgs eventArgs)
        {
            string currentFilePath = string.Format("{0}{1}", m_PathName, m_FileName);
            // Set target path
            string targetPath;

            if (eventArgs.Message2.Length == 0)  //string.IsNullOrEmpty(eventArgs.Message2))
            {
                targetPath = m_PathName;         // keep original path.
            }
            else if (eventArgs.Message2[eventArgs.Message2.Length - 1].Equals('\\'))
            {
                targetPath = eventArgs.Message2.ToString();     // user provided a target path
            }
            else
            {
                targetPath = string.Format("{0}\\", eventArgs.Message2);
            }
            // Set target file name.
            string targetFileName;

            if (eventArgs.Message.Length == 0)   //string.IsNullOrEmpty(eventArgs.Message))
            {
                targetFileName = m_FileName;     // Keep original filename
            }
            else
            {
                targetFileName = eventArgs.Message.ToString();  // User provided filename also.
            }
            // Move the file now.
            try
            {
                // Create directory
                if (!System.IO.Directory.Exists(targetPath))
                {
                    System.IO.Directory.CreateDirectory(targetPath);
                }
                // Move file
                string outFilePath = string.Format("{0}{1}", targetPath, targetFileName);
                if (System.IO.File.Exists(currentFilePath))
                {
                    if (System.IO.File.Exists(outFilePath))
                    {
                        System.IO.File.Delete(outFilePath);                 // delete any existing file in backup output area
                    }
                    System.IO.File.Move(currentFilePath, outFilePath);      // stick current file there - for recovery purposes.
                }
            }
            catch (Exception e)
            {
                if (Log != null)
                {
                    Log.NewEntry(LogLevel.Major, "{1}: ProcessMoveTo failed.  Exception: {0}", e.Message, this.m_HubName);
                }
            }
            // Exit.
            m_Factory.Recycle(eventArgs);
        }//ProcessMoveFile()
Esempio n. 3
0
        //
        //
        #endregion//Properties


        #region Public Methods
        // *****************************************************************
        // ****                     Public Methods                      ****
        // *****************************************************************
        // Note: All these public methods, are assumed to be called by an outside thread.
        //
        //
        /// <summary>
        /// Users call this function to push a line to write onto queue.
        /// Note that the order that requests are received, they are processed.
        /// </summary>
        /// <param name="aLineToWrite"></param>
        /// <returns></returns>
        public bool RequestEnqueue(string aLineToWrite)
        {
            if (base.ListenState == WaitListenState.Stopping)
            {
                return(false);                               // Queue no longer accepting new requests.
            }
            DropQueueWriterEventArgs eventArgs = GetRequest(DropQueueWriterRequestType.WriteLine);

            eventArgs.Message.Append(aLineToWrite);
            this.HubEventEnqueue(eventArgs);                // doing it this way gives more work to hub thread, but maintains the ordering of all tasks.
            return(true);
        }// RequestEnqueue()
Esempio n. 4
0
        } // IsDirectoryExists

        //
        //
        //
        #endregion // private methods


        #region HubEventHandler and Processing
        // *********************************************************
        // ****             Hub Event Handler()                 ****
        // *********************************************************
        protected override void HubEventHandler(EventArgs[] eventArgList)
        {
            foreach (EventArgs eventArg in eventArgList)
            {
                Type eventType = eventArg.GetType();
                if (eventType == typeof(DropQueueWriterEventArgs))
                {
                    DropQueueWriterEventArgs e = (DropQueueWriterEventArgs)eventArg;
                    switch (e.Request)
                    {
                    case DropQueueWriterRequestType.WriteLine:
                        m_WriteQueue.Enqueue(e);                        // Store the entire eventArg containing message in queue (easier to recycle later).
                        break;

                    case DropQueueWriterRequestType.Stop:
                        ProcessFlushNow();
                        base.Stop();
                        break;

                    case DropQueueWriterRequestType.FlushNow:
                        ProcessFlushNow();
                        break;

                    case DropQueueWriterRequestType.CopyTo:
                        ProcessCopyTo(e);
                        break;

                    case DropQueueWriterRequestType.CopyAllFiles:
                        ProcessCopyAllFiles(e);
                        break;

                    case DropQueueWriterRequestType.MoveTo:
                        ProcessMoveTo(e);
                        break;

                    case DropQueueWriterRequestType.ChangeFileName:
                        ProcessTargetFileChange(e);
                        break;

                    default:
                        if (Log != null)
                        {
                            Log.NewEntry(LogLevel.Major, "{0}: Unknown request {1}.", this.m_HubName, e.Request);
                        }
                        break;
                    }//switch()
                }
            }
        }// HubEventHandler()
Esempio n. 5
0
        }//ProcessMoveFile()

        //
        //
        //
        //
        // *****************************************************************************
        // ****                 Process Target File Change()                        ****
        // *****************************************************************************
        private void ProcessTargetFileChange(DropQueueWriterEventArgs eventArgs)
        {
            // Flush the buffer, before we change the output target.
            if (m_WriteQueue.Count > 0)
            {
                ProcessFlushNow();
            }
            try
            {
                // Change target dir
                if (eventArgs.Message2.Length > 0) //(!string. IsNullOrEmpty(eventArgs.Message2))
                {                                  // User wants to change target directory.
                    if (IsDirectoryExists(eventArgs.Message2.ToString()))
                    {
                        m_PathName = eventArgs.Message2.ToString(); // todo: confirm the trailing \\
                    }
                    else if (Log != null)
                    {
                        Log.NewEntry(LogLevel.Major, "{1}: Failed to created new directory {0}", eventArgs.Message2, this.m_HubName);
                    }
                }
                if (eventArgs.Message.Length > 0)//(!string.IsNullOrEmpty(eventArgs.Message))
                {
                    m_FileName = eventArgs.Message.ToString();
                }
                // Check if file exists
                string filePath = string.Format("{0}{1}", m_PathName, m_FileName);
                if (!this.AppendToDropFile && System.IO.File.Exists(filePath))
                {
                    if (Log != null)
                    {
                        Log.NewEntry(LogLevel.Minor, "{1}: Deleting pre-existing drop file {0}", filePath, this.m_HubName);
                    }
                    System.IO.File.Delete(filePath);
                }
            }
            catch (Exception e)
            {
                if (Log != null)
                {
                    Log.NewEntry(LogLevel.Major, "{1}: ProcessTargetFileChange failed.  Exception: {0}", e.Message, this.m_HubName);
                }
            }
            // Exit
            m_Factory.Recycle(eventArgs);
        } // ProcessTargetFileChange()
Esempio n. 6
0
        }// ProcessCopyTo()

        //
        //
        //
        /// <summary>
        /// Copies all files in the local Path (that match a given filename pattern) to
        /// another path.
        /// </summary>
        /// <param name="eventArgs"></param>
        private void ProcessCopyAllFiles(DropQueueWriterEventArgs eventArgs)
        {
            // Create name for archival file.
            string archiveDirPath;

            if (eventArgs.Message2.Length == 0)
            {
                m_Factory.Recycle(eventArgs);
                return;                                                 // User must provide a target DirPath!
            }
            else
            {
                if (eventArgs.Message2[eventArgs.Message2.Length - 1].Equals('\\')) // .EndsWith("\\"))
                {
                    archiveDirPath = eventArgs.Message2.ToString();                 // user provided a target path
                }
                else
                {
                    archiveDirPath = string.Format("{0}\\", eventArgs.Message2);
                }
            }
            try
            {
                if (!IsDirectoryExists(archiveDirPath)) // Confirm target directory exists.
                {                                       // Cannot find/create target directory.
                    if (Log != null)
                    {
                        Log.NewEntry(LogLevel.Major, "{1}: Failed to copy all files to {0}.  Failed to find/create that directory.", archiveDirPath, this.m_HubName);
                    }
                    m_Factory.Recycle(eventArgs);
                    return;                                             // nothing we can do but quit.
                }
                // Get Local filenames to copy.
                List <string> fileNamesWeCopied = new List <string>();
                string[]      localFileNameArray;
                if (eventArgs.Message.Length > 0)                      // pattern provided in ".FileName" property
                {
                    localFileNameArray = System.IO.Directory.GetFiles(this.m_PathName, eventArgs.Message.ToString());
                }
                else
                {
                    localFileNameArray = System.IO.Directory.GetFiles(this.m_PathName);// no pattern provided.
                }
                foreach (string s in localFileNameArray)
                {
                    string fileName        = s.Substring(s.LastIndexOf('\\') + 1);                  // local file name.
                    string archiveFilePath = string.Format("{0}{1}", archiveDirPath, fileName);     // archive file path
                    try
                    {
                        if (System.IO.File.Exists(archiveFilePath))
                        {   // This file already exists in the output directory.
                            System.IO.FileInfo infoRemote = new System.IO.FileInfo(archiveFilePath);
                            System.IO.FileInfo infoLocal  = new System.IO.FileInfo(s);
                            if (infoLocal.Length > infoRemote.Length)
                            {   // The local file is bigger, so we better do the copy!
                                if (Log != null)
                                {
                                    Log.NewEntry(LogLevel.Major, "{1}.ProcessCopyAllFiles: Local copy of {0} is larger than remote, so we will copy to remote.", fileName, this.m_HubName);
                                }
                                System.IO.File.Copy(s, archiveFilePath, true);
                                fileNamesWeCopied.Add(fileName);
                            }
                        }
                        else
                        {   // File doesn't already exist, so just copy it.
                            System.IO.File.Copy(s, archiveFilePath, true);
                            fileNamesWeCopied.Add(fileName);
                        }
                    }
                    catch (Exception e)
                    {
                        if (Log != null)
                        {
                            Log.NewEntry(LogLevel.Major, "{1}.ProcessCopyAllFiles: Failed to copy file to {0}.  Exception {2}.", archiveDirPath, this.m_HubName, e.Message);
                        }
                    }
                }
                // Report our results
                if (Log != null && Log.BeginEntry(LogLevel.Minor))
                {
                    Log.AppendEntry("{1}.ProcessCopyAllFiles: Copied {0} files [", fileNamesWeCopied.Count, this.m_HubName);
                    foreach (string s in fileNamesWeCopied)
                    {
                        Log.AppendEntry(" {0}", s);
                    }
                    Log.AppendEntry("].");
                    Log.EndEntry();
                }
            }
            catch (Exception e)
            {
                if (Log != null)
                {
                    Log.NewEntry(LogLevel.Major, "{2}: Copying all files from {0} to {1} failed. Exception: {2}", this.FilePath, archiveDirPath, e.Message, this.m_HubName);
                }
            }
            // Exit
            m_Factory.Recycle(eventArgs);
        }// ProcessCopyAllFiles()
Esempio n. 7
0
        }// ProcessWriteNow().

        //
        //
        // *********************************************************************
        // ****                     Process CopyTo()                        ****
        // *********************************************************************
        /// <summary>
        ///
        /// </summary>
        private void ProcessCopyTo(DropQueueWriterEventArgs eventArgs)
        {
            // Create name for archival file.
            string archiveDirPath;
            string archivalFileName;

            if (eventArgs.Message2.Length == 0)             // Check if user didn't provided path to copy file to
            {
                archiveDirPath = m_PathName;                // keep the same old path, then.
            }
            else
            {                                               // User copying to new dir.
                if (eventArgs.Message2[eventArgs.Message2.Length - 1].Equals('\\'))
                {
                    archiveDirPath = eventArgs.Message2.ToString();
                }
                else
                {
                    eventArgs.Message2.Append("\\");
                    archiveDirPath = eventArgs.Message2.ToString();
                }
            }
            if (eventArgs.Message.Length == 0)              // If no fileName supplied, keep the current filename. //(string.IsNullOrEmpty(eventArgs.Message))
            {
                archivalFileName = m_FileName;
            }
            else
            {
                archivalFileName = eventArgs.Message.ToString();
            }

            // Confirm target directory exists.
            if (!IsDirectoryExists(archiveDirPath))
            {
                if (Log != null)                            // Cannot find/create target directory.  Just fail quietly.
                {
                    Log.NewEntry(LogLevel.Major, "{1}: Failed to copying drop to {0}.", archiveDirPath, this.m_HubName);
                }
            }
            else
            {
                // Copy current file to archival location.
                string currentFilePath = this.FilePath;
                if (System.IO.File.Exists(currentFilePath))
                {
                    string archiveFilePath = string.Format("{0}{1}", archiveDirPath, archivalFileName);
                    try
                    {
                        if (System.IO.File.Exists(archiveFilePath))
                        {
                            System.IO.FileInfo fileInfoArchive = new System.IO.FileInfo(archiveFilePath);
                            System.IO.FileInfo fileInfoCurrent = new System.IO.FileInfo(currentFilePath);
                            if (fileInfoCurrent.Length > fileInfoArchive.Length)
                            {
                                if (Log != null)
                                {
                                    Log.NewEntry(LogLevel.Major, "DropQueueWriter: Copying drop to archive directory {0}.", archiveFilePath);
                                }
                                System.IO.File.Copy(currentFilePath, archiveFilePath, true);
                            }
                            else if (Log != null)
                            {
                                Log.NewEntry(LogLevel.Major, "DropQueueWriter: File in archival area seems bigger, will not over-write it!");
                            }
                        }
                        else
                        {
                            if (Log != null)
                            {
                                Log.NewEntry(LogLevel.Major, "DropQueueWriter: Copying drop to archive directory {0}.", archiveFilePath);
                            }
                            System.IO.File.Copy(currentFilePath, archiveFilePath, true);
                        }
                        Log.NewEntry(LogLevel.Major, "DropQueueWriter: Copying drop successfully to {0}.", archiveFilePath);
                    }
                    catch (Exception ex)
                    {
                        if (Log != null)
                        {
                            Log.NewEntry(LogLevel.Major, "{2}: Failed to move drop to archive directory {0}. {1}", archiveFilePath, ex.Message, this.m_HubName);
                        }
                    }
                }
                else if (Log != null)
                {
                    Log.NewEntry(LogLevel.Major, "DropQueueWriter.ProcessCopyTo: File to copy does not exist {0}.", currentFilePath);
                }
            }// if archiveDirPath exists
            // Exit.
            m_Factory.Recycle(eventArgs);
        }// ProcessCopyTo()