Exemple #1
0
 /// <summary>
 /// handling the situation of closing the directory
 /// </summary>
 public void HandleClosing()
 {
     string message;
     try
     {
         //make each watcher stop filtering and watching
         foreach (FileSystemWatcher watcher in this.m_dirWatcher)
         {
             watcher.EnableRaisingEvents = false;
         }
         //log that this directory is closed
         message = m_path + "is no longer watched";
         DirectoryCloseEventArgs closing = new DirectoryCloseEventArgs(m_path, message);
         DirectoryClose?.Invoke(this, closing);
     }
     catch (Exception e)
     {
         //couldnt close the directory - log it
         message = "Error while trying to close " + m_path;
         m_logging.Log(message, MessageTypeEnum.INFO);
     }
     finally
     {
         //unsubscribe the "onCreated" func from the event
         foreach (FileSystemWatcher watch in this.m_dirWatcher)
         {
             watch.Created -= new FileSystemEventHandler(OnCreated);
         }
     }
 }
 /// <summary>
 /// The function closes the handler
 /// </summary>
 /// <param name="logMessage">the message that will be shown when closing the handler</param>
 public void CloseHandler(string logMessage)
 {
     m_dirWatcher.EnableRaisingEvents = false;
     m_dirWatcher.Created            -= OnFileCreated;
     m_dirWatcher.Dispose();
     DirectoryClose.Invoke(this, new DirectoryCloseEventArgs(m_path, logMessage));
 }
 /// <summary>
 /// when close command recieved
 /// </summary>
 public void OnClose()
 {
     // unsubscribe to event
     this.m_dirWatcher.Created -= OnCreated;
     // envoke DirectoryClose event
     DirectoryClose?.Invoke(this, new DirectoryCloseEventArgs(m_path, "close directory"));
 }
        /*********************************************************************/

        public void closeHandler(string path)
        {
            //– close FileSystemWatcher and invoke onClose event
            watcher.EnableRaisingEvents = false;
            DirectoryClose.Invoke(this, new DirectoryCloseEventArgs(path, "Directory Closed"));
            //DirectoryClose -= new FileSystemWatcher(OnCommandRecieved);
        }
Exemple #5
0
        public void StartHandleDirectory(string dirPath)
        {
            // If The Directory Doesn't Exists
            if (!Directory.Exists(dirPath))
            {
                DirectoryClose?.Invoke(this, new DirectoryCloseEventArgs(dirPath,
                                                                         String.Format(MessageInfrastructure.ERROR_DirNotFound, dirPath)));
                return;             // Finishing the Task Of Listening to the file
            }

            m_path = dirPath;       // Storing the Path of Directory

            m_dirWatcher      = new FileSystemWatcher();
            m_dirWatcher.Path = dirPath;

            /* Watch for changes in LastAccess and LastWrite times, and
             *  the renaming of files or directories. */
            m_dirWatcher.NotifyFilter = NotifyFilters.LastWrite
                                        | NotifyFilters.FileName | NotifyFilters.DirectoryName;
            m_dirWatcher.EnableRaisingEvents = true;             // Setting that the Watcher is active for Events

            m_dirWatcher.Created += new FileSystemEventHandler(OnChanged);

            // m_dirWatcher.Filter = "*.bmp;*.jpg;*.gif;*.png";               // Setting the Filter

            m_logging.Log(String.Format(MessageInfrastructure.INFO_StartDirHandler, dirPath), MessageTypeEnum.INFO);
        }
        /// <summary>
        /// The event occur upon receiving a command.
        /// </summary>
        /// <param name="sender"></param> The command sender (invoker).
        /// <param name="e"></param> The argumetns.
        public void OnCommandRecieved(object sender, CommandReceivedEventArgs e)
        {
            string msg;
            bool   result = true;

            if (e.CommandID == CommandEnum.CloseCommand)
            {
                if (e.RequestDirPath != dirPath)
                {
                    return;
                }
                msg = "Directory " + dirPath + " is closing";
                dirWatcher.EnableRaisingEvents = false;
                DirectoryClose?.Invoke(this, new DirectoryCloseEventArgs(e.RequestDirPath, msg));
                //logger.Log(msg, MessageTypeEnum.INFO);
            }
            else
            {
                if (!IsSubFile(e.RequestDirPath))
                {
                    return;
                }
                msg = controller.ExecuteCommand(e.CommandID, e.Args, out result);
            }
            logger.Log(msg, GetMessageType(result));
        }
Exemple #7
0
        /// <summary>
        /// When a command comes- checks what kind it is: if closing- its closes the service,
        /// otherwise its calls to ExecuteCommand and writes to the log sucsess\failure
        /// </summary>
        /// <param name="sender">the object called to the event</param>
        /// <param name="e">the event args required for this event</param>
        public void OnCommandRecieved(object sender, CommandRecievedEventArgs e)
        {
            bool   result;
            string message;

            if (e.CommandID == (int)CommandEnum.CloseCommand)
            {
                if (e.RequestDirPath.Equals("*") || e.RequestDirPath.Equals(m_path))
                {
                    m_dirWatcher.EnableRaisingEvents = false;
                    DirectoryCloseEventArgs dirCloseArgs = new DirectoryCloseEventArgs(m_path, "CLOSE");
                    DirectoryClose?.Invoke(this, dirCloseArgs);
                }
                return;
            }
            else
            {
                if (m_path == null || !e.RequestDirPath.Contains(m_path))
                {
                    return;
                }

                message = m_controller.ExecuteCommand(e.CommandID, e.Args, out result);
            }

            // write to the log
            if (result)
            {
                m_logging.Log(message, MessageTypeEnum.INFO);
            }
            else
            {
                m_logging.Log(message, MessageTypeEnum.FAIL);
            }
        }
Exemple #8
0
 public void OnCloseService(object send, DirectoryCloseEventArgs e)
 {
     m_logging.Log("closing handler for directory: " + m_path, MessageTypeEnum.INFO);
     m_dirWatcher.EnableRaisingEvents = false;
     m_dirWatcher.Dispose();
     DirectoryClose?.Invoke(this, e);
 }
Exemple #9
0
 /// <summary>
 /// This method is called when command is received.
 /// </summary>
 /// <param name="sender">The command sender.</param>
 /// <param name="e">The command's arguments.</param>
 public void OnCommandRecieved(object sender, CommandRecievedEventArgs e) // The Event that will be activated upon new Command
 {                                                                        //checking if our path is the one the server intended to handle the command
     if (e.RequestDirPath.Equals(m_path) || e.RequestDirPath.Equals("*")) // Equals compares content
     {
         // if CloseCommand
         if (e.CommandID == (int)CommandEnum.CloseCommand) // == better for numbers
         {
             m_dirWatcher.EnableRaisingEvents = false;     // stop watching.
             string exitMsg = "Stoped handling the directory in path: " + m_path;
             DirectoryCloseEventArgs directoryCloseEventArgs = new DirectoryCloseEventArgs(m_path, exitMsg);
             DirectoryClose?.Invoke(this, directoryCloseEventArgs);
         }
         else // if other command
         {
             bool   success;
             string msg = m_controller.ExecuteCommand(e.CommandID, e.Args, out success);
             if (success)
             {
                 m_logging.Log(msg, MessageTypeEnum.INFO);
             }
             else
             {
                 m_logging.Log(msg, MessageTypeEnum.FAIL);
             }
         }
     }
 }
Exemple #10
0
        public void OnCommandRecieved(object sender, CommandRecievedEventArgs e)
        {
            Task task = new Task(() =>
            {
                bool success;           // Indication if the Command Was Successful
                // If The Command is directed to the given Path
                if (e.RequestDirPath.Equals("*") || e.RequestDirPath.Equals(m_path))
                {
                    // If The User Wishes to close the file
                    if (e.CommandID == (int)CommandEnum.CloseCommand)
                    {
                        if (m_dirWatcher != null)
                        {
                            m_dirWatcher.EnableRaisingEvents = false;
                        }
                        DirectoryClose?.Invoke(this, new DirectoryCloseEventArgs(m_path,
                                                                                 String.Format(MessageInfrastructure.INFO_CloseCommand, m_path)));
                        // Invoking the Server Close
                    }
                    // Logging the Command Being Recieved
                    m_logging.Log(String.Format(MessageInfrastructure.INFO_RecievedCommand, e.CommandID,
                                                String.Join(",", e.Args)), Logging.Modal.MessageTypeEnum.INFO);

                    // Execute command
                    string result = m_controller.ExecuteCommand(e.CommandID, e.Args, out success);

                    MessageTypeEnum type = (success) ? MessageTypeEnum.INFO : MessageTypeEnum.FAIL;

                    m_logging.Log(String.Format(MessageInfrastructure.INFO_SuccessfulCommand, e.CommandID,
                                                String.Join(",", e.Args), result), type);
                }
            });

            task.Start();
        }
Exemple #11
0
        /// <summary>
        /// This function is invoked when filewatcher has an error
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnFileWatcherError(object sender, ErrorEventArgs e)
        {
            m_logging.Log(DateTime.Now.ToString() + " Error Detected In Handler Of Directory " + m_path, MessageTypeEnum.FAIL);
            DirectoryCloseEventArgs args = new DirectoryCloseEventArgs(m_path, e.GetException().ToString());

            DirectoryClose?.Invoke(this, args);
            CloseHandler();
        }
Exemple #12
0
        /// <summary>
        /// EndHandle function.
        /// stop handle directory.
        /// </summary>
        public void EndHandle()
        {
            //disable watcher
            this.m_dirWatcher.EnableRaisingEvents = false;
            //raising DirectoryClose event
            DirectoryCloseEventArgs args = new DirectoryCloseEventArgs(this.m_path, "closing directory: " + this.m_path);

            DirectoryClose?.Invoke(this, args);
        }
 ///</summary>
 ///the event that will be activated when service needs to be close
 ///// </summary>
 /// <param name="sender">the notifyer</param name>
 ///<pararm name="e">arguments of the command</param name>
 public void OnCloseSevice(object sender, CommandRecievedEventArgs e)
 {
     m_dirWatcher.EnableRaisingEvents = false;
     //ImageServer server = (ImageServer)sender;
     m_dirWatcher.Dispose();
     m_logging.Log("Handler closed " + m_path, MessageTypeEnum.INFO);
     //server.CloseService -= OnCommandRecieved;
     DirectoryClose?.Invoke(this, new DirectoryCloseEventArgs(m_path, "message"));
 }
        //Closing the handler, the watcher and the end of the service.
        public void HandlerClose()
        {
            foreach (FileSystemWatcher watcher in m_listWatchers)
            {
                watcher.EnableRaisingEvents = false;
                watcher.Dispose();
            }

            DirectoryClose?.Invoke(this, new DirectoryCloseEventArgs(m_path, "dir " + m_path + " directory closed"));
        }
Exemple #15
0
        /// <summary>
        /// Stop handling the given dir.
        /// </summary>
        public void StopHandleDirectory()
        {
            // Stop handling the directory and make sure FileSystemWatcher isn't watching anymore
            var stopDirectoryMessage = "Stopped handling folder:" + _pathToDir;
            var closeEventArgs       = new DirectoryCloseEventArgs(_pathToDir, stopDirectoryMessage);

            DirectoryClose?.Invoke(this, closeEventArgs);
            _watchers.Clear();
            // Log it to the logger
            _logger.Log("Stopped handling the directory " + _pathToDir, MessageTypeEnum.INFO);
        }
Exemple #16
0
        /// <summary>
        /// Ends the handler.
        /// </summary>
        private void EndHandler()
        {
            m_dirWatcher.EnableRaisingEvents = false;

            m_dirWatcher.Created -= new FileSystemEventHandler(Create);
            m_dirWatcher.Changed -= new FileSystemEventHandler(Create);

            DirectoryCloseEventArgs d = new DirectoryCloseEventArgs(m_path, "Closing directory" + m_path);

            DirectoryClose?.Invoke(this, d);
        }
 // The Event that will be activated upon new Command
 public void OnCommandRecieved(object sender, CommandRecievedEventArgs e)
 {
     if (e.RequestDirPath == this.m_path)
     {
         if (e.CommandID == (int)CommandEnum.CloseCommand)
         {
             OnCloseHandlerCommand();
             DirectoryClose?.Invoke(this, new DirectoryCloseEventArgs(this.m_path, "Closing Handler"));
             this.m_logging.Log("Closing handler for folder:" + this.m_path, MessageTypeEnum.INFO);
         }
     }
 }
Exemple #18
0
 /// <summary>
 /// Close file watcher (called before service ends
 /// or if for some reason we need to close the listening it beforehand).
 /// </summary>
 /// <param name="source"></param>
 /// <param name="args"></param>
 public void CloseFileWatcher(object source, DirectoryCloseEventArgs args)
 {
     if (args.DirectoryPath.Equals(this.directoryPath) || args.DirectoryPath.Equals("*"))
     {
         // set watchers' raising event status to false
         foreach (FileSystemWatcher fileSystemWatcher in this.directoryWatchers)
         {
             fileSystemWatcher.EnableRaisingEvents = false;
         }
         this.loggingService.Log(this, new MessageRecievedEventArgs(MessageTypeEnum.INFO, "Closing directory " + this.directoryPath));
         DirectoryClose.Invoke(this, new DirectoryCloseEventArgs(this.directoryPath, "Received handler-closing message"));
     }
 }
Exemple #19
0
 /// <summary>
 /// close this directory handler close FileSystemWatcher  and invoke the DirectoryClose event .
 /// </summary>
 public void CloseHandle()
 {
     try
     {
         m_dirWatcher.EnableRaisingEvents = false;
         m_dirWatcher.Created            -= new FileSystemEventHandler(OnCreated);
         DirectoryCloseEventArgs closeEventArgs = new DirectoryCloseEventArgs(m_path, "closing directory" + m_path);
         // we invoke to the server that it will know this directory was clossed: CommandRecieved -= this.OnCommandRecieved;
         DirectoryClose?.Invoke(this, closeEventArgs);
     } catch (Exception e) {
         m_logging.Log("couldnt close handler" + m_path + " " + e.Message, MessageTypeEnum.FAIL);
     }
 }
Exemple #20
0
 /// <summary>
 /// Closes the handler and commit to logger.
 /// </summary>
 /// <param name="sender">The sender.</param>
 /// <param name="e">The <see cref="DirectoryCloseEventArgs"/> instance containing the event data.</param>
 public void CloseHandler(object sender, DirectoryCloseEventArgs e)
 {
     try {
         m_dirWatcher.EnableRaisingEvents = false;
         DirectoryCloseEventArgs directoryCloseArgs = new DirectoryCloseEventArgs(m_path, "Closing handler for: " + m_path);
         DirectoryClose?.Invoke(this, directoryCloseArgs);
         m_logging.Log("Closing handler for: " + m_path, MessageTypeEnum.INFO);
     } catch (Exception e1) {
         m_logging.Log("Failed to closed handler for: " + m_path, MessageTypeEnum.INFO);
     } finally {
         m_dirWatcher.Created -= new FileSystemEventHandler(NewEvent);
     }
 }
 public void OnCloseHandler(object sender, DirectoryCloseEventArgs e)
 {
     try
     {
         this.m_watcher.EnableRaisingEvents = false;
         Watcher.Created -= new FileSystemEventHandler(OnCreated);
         DirectoryClose?.Invoke(this, new DirectoryCloseEventArgs(direcPath, "Directory " + this.direcPath + " closed"));
     }
     catch (Exception ex)
     {
         this.m_logging.Log("Error closing the handler: " + this.direcPath + "due to " + ex.Message, MessageTypeEnum.FAIL);
     }
 }
Exemple #22
0
        /// <summary>
        /// Called when [command recieved].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="CommandRecievedEventArgs"/> instance containing the event data.</param>
        public void OnCommandRecieved(object sender, CommandRecievedEventArgs e)
        {
            //debug.write("on command recived");
            bool   result;
            string message = m_controller.ExecuteCommand(e.CommandID, e.Args, out result);

            m_logging.Log(message, MessageTypeEnum.INFO);
            if ((string.Compare(message, "closeDirectory") == 0) || ((string.Compare(m_path, e.RequestDirPath) == 0)))
            {
                DirectoryCloseEventArgs close = new DirectoryCloseEventArgs(m_path, "directory has been closed");
                DirectoryClose.Invoke(this, close);
            }
            //return message;
        }
Exemple #23
0
        /// <summary>
        /// Stops listening for changes in the file, closes the handler.
        /// Param: Object sender, CommandRecievedArgs args.
        /// </summary>
        void CloseHandler(object sender, CommandRecievedEventArgs args)
        {
            DirectoryCloseEventArgs eventArgs;

            try
            {
                m_dirWatcher.EnableRaisingEvents = false;
                eventArgs = new DirectoryCloseEventArgs(m_path, " Handler closed");
            } catch (Exception e)
            {
                eventArgs = new DirectoryCloseEventArgs(m_path, " Problem with closeing handler");
                m_logging.Log(e.ToString(), Logging.Modal.MessageTypeEnum.FAIL);
            }
            DirectoryClose?.Invoke(this, eventArgs);
        }
        /// <summary>
        /// Handles the <see cref="E:CommandReceived" /> event.
        /// </summary>
        /// <param name="sender">The object which gave the command (the server).</param>
        /// <param name="e">The <see cref="CommandReceivedEventArgs"/> instance containing the event data.</param>
        public void OnCommandReceived(object sender, CommandReceivedEventArgs e)
        {
            // if the command is to close all handlers, or this particular one
            if (e.CommandID == 1 || (e.CommandID == 2 && e.RequestDirPath == m_path))
            {
                DirectoryClose?.Invoke(this, new DirectoryCloseEventArgs(m_path, "closed handler"));

                m_dirWatcher.Dispose();
                m_logging.Log("closing handler for " + m_path, MessageTypeEnum.INFO);
            }
            else
            {
                //empty for now, since only close commands can come from server
            }
        }
Exemple #25
0
 /// <summary>
 /// Raises the Close event.
 /// </summary>
 public void OnClose()
 {
     try
     {
         this.m_dirWatcher.EnableRaisingEvents = false;
         //   this.m_dirWatcher.Created -= new FileSystemEventHandler(OnCreated);
         m_dirWatcher.Dispose();
         DirectoryClose?.Invoke(this, new DirectoryCloseEventArgs(m_path, "close handler at path " + m_path));
         this.m_logging.Log("handler of " + this.m_path + " was closed", MessageTypeEnum.INFO);
     }
     catch (Exception exception)
     {
         this.m_logging.Log(exception.Data.ToString(), MessageTypeEnum.FAIL);
     }
 }
Exemple #26
0
 /// <summary>
 /// Stops the listening to the directory. invokes the DirectoryClose event and informs
 /// the ILogging.
 /// </summary>
 /// <param name="e"> the arguments for the event </param>
 void OnClose(DirectoryCloseEventArgs args)
 {
     try
     {
         if (path == args.DirectoryPath)
         {
             stopWatching(args);
             removeFromAppConfig(args.DirectoryPath);
             DirectoryClose?.Invoke(this, args);
         }
     }
     catch (Exception e)
     {
         logger.Log(e.Message, MessageTypeEnum.FAIL);
     }
 }
Exemple #27
0
        /// <summary>
        /// OnCommandRecieved of this handler actually tells the server that this handler is being closed
        /// and when the last handler is closed the server can finally stop.
        /// </summary>
        /// <param name="sender"> sender </param>
        /// <param name="e"> CommandRecievedEventArgs </param>
        public void OnCommandRecieved(object sender, CommandRecievedEventArgs e)
        {
            switch (e.CommandID)
            {
            case (int)CommandEnum.CloseHandlerCommand:
                this.m_logging.Log("closing Directory Handler of directory in path: " + this.m_path, MessageTypeEnum.INFO);
                DirectoryClose?.Invoke(this, new DirectoryCloseEventArgs(this.m_path, "closing"));
                for (int i = 0; i < 4; i++)
                {
                    this.m_dirWatchers[i].Dispose();
                }
                break;

            default:
                throw new ArgumentException();
            }
        }
Exemple #28
0
 /// <summary>
 /// closing the handler - closing m_dirWatcher & invoking DirectoryClose event.
 /// </summary>
 public void CloseHandler()
 {
     try
     {
         m_dirWatcher.EnableRaisingEvents = false;
         m_dirWatcher.Changed            -= OnChanged;
         m_dirWatcher.Created            -= OnChanged;
         m_dirWatcher.Deleted            -= OnChanged;
         m_dirWatcher.Dispose();
         DirectoryClose?.Invoke(this, new DirectoryCloseEventArgs(m_path,
                                                                  Messages.ClosedHandlerSuccessfully(m_path)));
         m_logging.Log(Messages.ClosedHandlerSuccessfully(m_path), MessageTypeEnum.INFO);
     } catch (Exception e)
     {
         m_logging.Log(Messages.FailedClosingHandler(e.Message), MessageTypeEnum.FAIL);
     }
 }
        public event EventHandler <DirectoryCloseEventArgs> DirectoryClose;    // The Event That Notifies that the Directory is being closed

        /// <summary>
        /// method handling commands being sent to the directory handler object
        /// </summary>
        /// <param name="sender">the sender of the command</param>
        /// <param name="e">arguments and info of the sent command</param>
        public void OnCommandRecieved(object sender, CommandRecievedEventArgs e)
        {
            m_logging.Log("In directory handler received command with id: " + e.CommandID, MessageTypeEnum.INFO);

            //right now with switch case because its a general command and not only close
            switch (e.CommandID)
            {
            //close command
            case CommandEnum.CloseCommand:
                this.CloseDirectory();
                DirectoryClose?.Invoke(this, new DirectoryCloseEventArgs(this.m_path, "closing"));
                break;

            default:
                throw new ArgumentException();
            }
        }
Exemple #30
0
        /// <summary>
        /// This function is called when an image is created
        /// it reports to the log and executes the command
        /// in the controller
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnCreated(object sender, FileSystemEventArgs e)
        {
            string[] args = { e.FullPath };
            bool     result;
            string   res;

            res = m_controller.ExecuteCommand((int)CommandEnum.NewFileCommand, args, out result);
            // if command was unsuccessful
            if (!result)
            {
                m_logging.Log(DateTime.Now.ToString() + " " + res, MessageTypeEnum.FAIL);
                DirectoryClose?.Invoke(this, new DirectoryCloseEventArgs(m_path, res));
                CloseHandler();
                return;
            }
            m_logging.Log(DateTime.Now.ToString() + " Image Created: " + res, MessageTypeEnum.INFO);
        }