public void CloseHandler(object sender, DirectoryCloseEventArgs e) { int flag = 0; foreach (IDirectoryHandler handler in handlers) { if (e.DirectoryPath.Equals("*") || handler.getPath().Equals(e.DirectoryPath)) { flag = 1; this.CommandRecieved -= handler.OnCommandRecieved; // this.m_logging.Log("Closing handler for " + e.DirectoryPath, MessageTypeEnum.INFO); handler.DirectoryClose -= CloseHandler; handler.StopHandleDirectory(e.DirectoryPath); this.m_logging.Log("Closed handler for " + e.DirectoryPath, MessageTypeEnum.INFO); string path = e.DirectoryPath; string[] args = { path }; //DataInfo info = new DataInfo(CommandEnum.CloseCommand, JsonConvert.SerializeObject(args)); MessageRecievedEventArgs info = new MessageRecievedEventArgs(MessageTypeEnum.WARNING, path); updareClientsHandlers(info); } } if (flag == 0) { MessageRecievedEventArgs info = new MessageRecievedEventArgs(MessageTypeEnum.FAIL, null); //sen.Invoke(this, info); sendClients(info); } }
/// <summary> /// When a command is being received, this function execute it via the IController, and /// then informs the ILogging. /// </summary> /// <param name="sender"> the object that sent the command </param> /// <param name="e"> the command's arguments </param> void IHandler.OnCommandReceived(object sender, CommandReceivedEventArgs e) { if (e.CommandID == CommandEnum.CloseCommand) { DirectoryCloseEventArgs args = new DirectoryCloseEventArgs(e.RequestDirPath, "Closing Handler: " + path); OnClose(args); } else if (e.CommandID == CommandEnum.CloseServerCommand) { DirectoryCloseEventArgs args = new DirectoryCloseEventArgs(path, "Server Closure " + path + " closed"); stopWatching(args); } else { // The Event that will be activated upon new Command string logMsg = controller.ExecuteCommand(e.CommandID, e.Args, out bool result); if (result) { logger.Log(logMsg, MessageTypeEnum.INFO); } else { logger.Log(logMsg, MessageTypeEnum.FAIL); } } }
public string Execute(string[] args, out bool result) { DirectoryCloseEventArgs e = new DirectoryCloseEventArgs(args[0], "Closing handler"); // invoke event notifying that a handler needs to close Closed?.Invoke(this, e); StringBuilder sb = new StringBuilder(); string[] handlers = ConfigurationManager.AppSettings.Get("Handler").Split(';'); foreach (string handler in handlers) { if (string.Compare(args[0], handler) != 0) { sb.Append(handler); sb.Append(";"); } } ConfigurationManager.AppSettings.Set("Handler", sb.ToString()); result = true; //return "Executed Close Command with arguments: " + args[0]; MessageRecievedEventArgs info = new MessageRecievedEventArgs(MessageTypeEnum.WARNING, args[0]); string parse = JsonConvert.SerializeObject(info); DataInfo commandSendArgs = new DataInfo(CommandEnum.CloseCommand, parse); string toJson = JsonConvert.SerializeObject(commandSendArgs); return(toJson); }
/** * the function close server after call to the close */ public void closeService(object sender, DirectoryCloseEventArgs e) { IDirectoryHandler h = (IDirectoryHandler)sender; CommandRecieved -= h.OnCommandRecieved; m_logging.Log("close handler: " + e.Message, MessageTypeEnum.INFO); }
public void OnCloseServer(object sender, DirectoryCloseEventArgs args) { IDirectoryHandler handler = (IDirectoryHandler)sender; onCommand -= handler.OnCommandRecieved; informHandlerClose -= handler.CloseHandler; handler.DirectoryClose -= OnCloseServer; }
/// <summary> /// remove the directory handle from the CommanedRecived event /// </summary> /// <param name="sender"> sender class</param> /// <param name="e"> arguments of Directory close</param> public void CloseDirectory(object sender, DirectoryCloseEventArgs e) { IDirectoryHandler d = (DirectoyHandler)sender; CommandRecieved -= d.OnCommandRecieved; d.DirectoryClose -= CloseDirectory; m_logging.Log(e.Message, MessageTypeEnum.INFO); }
/// <summary> /// Informs the clients that a Handler Directory has been removed form the watchlist /// </summary> /// <param name="sender">Object</param> /// <param name="args">DirectoryCloseEventArgs</param> public void NotifyClientsDirectoryClosed(object sender, DirectoryCloseEventArgs args) { clientsMutex.WaitOne(); foreach (TcpClient client in connectedClients) { StreamWriter writer = new StreamWriter(client.GetStream()); writer.WriteLine(CommandEnum.CloseCommand); writer.WriteLine(args.DirectoryPath); writer.WriteLine("<EOF>"); } clientsMutex.ReleaseMutex(); }
/// <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); } }
/// <summary> /// The Event that will be activated upon new Command /// </summary> /// <param name="sender">is the class that call this method</param> /// <param name="e">save all the arguments to the event </param> public void OnCommandRecieved(object sender, CommandRecievedEventArgs e) { //check if the command should execute on this Directory ("*" - for all directories) //remember - Close has no execute command if (e.RequestDirPath == dirPath || e.RequestDirPath == "*") { if (e.CommandID == (int)CommandStateEnum.CLOSE) //if Command is Close { DirectoryCloseEventArgs e1 = new DirectoryCloseEventArgs(dirPath, "Stop handle Dir: " + dirPath); StopHandleDirectory(); DirectoryClose?.Invoke(this, e1); } else {//use the controller to execute //for new File command taks the args[0]. string msg = c_imageController.ExecuteCommand(e.CommandID, e.Args, out bool result, out MessageTypeEnum type); //log write the result msg, with the returning type m_loggingModel.Log(msg, type); } } }
/** * run the correct command. */ public void OnCommandRecieved(object sender, CommandRecievedEventArgs e) { this.watcher.EnableRaisingEvents = true; //if you want to listen logging.Log("Hendler starts listening to: " + path, MessageTypeEnum.INFO); //add log msg to logging for start listening bool result; string resultMsg = null; MessageTypeEnum typeMsg = new MessageTypeEnum(); if (e.CommandID == (int)CommandEnum.CloseCommand) //when the command is close { this.watcher.EnableRaisingEvents = false; DirectoryCloseEventArgs closeArgs = new DirectoryCloseEventArgs(path, "Handler Closing: " + path); DirectoryClose?.Invoke(this, closeArgs); // and dirctoryClose.Invoke--> for the service logging.Log("Handler Closing: " + path, MessageTypeEnum.INFO); //update the log } //id dir path is * or my folder, if request == handler.path --> then go, else - return. if (!e.RequestDirPath.Equals(path) || e.RequestDirPath.EndsWith("*")) { resultMsg = controller.ExecuteCommand(e.CommandID, e.Args, out result); if (result == false) // if the command not found { StopHandleDirectory(e.RequestDirPath); typeMsg = MessageTypeEnum.FAIL; } else { typeMsg = MessageTypeEnum.INFO; } } logging.Log(resultMsg, typeMsg); //update the log //watcher.Created += new FileSystemEventHandler(OnChanged); watcher.Created += new FileSystemEventHandler(OnChanged); }
private void stopWatching(DirectoryCloseEventArgs args) { path = null; dirWatcher.EnableRaisingEvents = false; logger.Log(args.Message, MessageTypeEnum.INFO); }
private void OnServerClose(object sender, DirectoryCloseEventArgs e) { string[] args = { e.DirectoryPath }; this.cServer.SendCommandToAllClients((int)CommandsEnum.RemoveDirCommand, args); }
public void HandlerClosed(object sender, DirectoryCloseEventArgs e) { HandlerClosedEvent?.Invoke(sender, e); }