예제 #1
0
        /// <summary>
        /// the function stops the handling of a specific directiory
        /// </summary>
        /// <param name= sender> the object that sent the request </param>
        /// <param name= e> the event that occured </param>
        public void CloseHandler(object sender, DirectoryCloseEventArgs e)
        {
            List <IDirectoryHandler> list = getHandlers();

            foreach (IDirectoryHandler handler in list)
            {
                if (e.DirectoryPath.Equals("*") || handler.GetPath().Equals(e.DirectoryPath))
                {
                    this.CommandRecieved -= handler.OnCommandRecieved;
                    this.logging.Log("Closing handler for " + e.DirectoryPath, MessageTypeEnum.INFO);
                    handler.DirectoryClose -= CloseHandler;
                    // delete handler
                    handler.StopHandleDirectory(e.DirectoryPath);
                    this.logging.Log("Closed handler for " + e.DirectoryPath, MessageTypeEnum.INFO);
                    string   path = e.DirectoryPath;
                    string[] args = { path };
                    // create info args
                    InfoEventArgs info = new InfoEventArgs((int)InfoEnums.CloseHandlerInfo, args);
                    // remove the handler from the app config handler list
                    ServiceInfo serviceInfo = ServiceInfo.CreateServiceInfo();
                    serviceInfo.RemoveHandler(e.DirectoryPath);
                    // notify all of the clients that the handler was closed
                    NotifyClients?.Invoke(this, info);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Activates NotifyClients event if a new log has arrived.
        /// </summary>
        /// <param name="sender">The sender object.</param>
        /// <param name="e">The recieved arguments.</param>
        private void NewLogCommand(object sender, MessageRecievedEventArgs e)
        {
            string jsonCommand = JsonConvert.SerializeObject(e);

            string[] arr = new string[1];
            arr[0] = jsonCommand;
            CommandRecievedEventArgs command = new CommandRecievedEventArgs((int)CommandEnum.NewLogCommand, arr, "");

            NotifyClients?.Invoke(command);
        }
예제 #3
0
        /// <summary>
        /// Write to log.
        /// </summary>
        /// <param name="message">Message to write.</param>
        /// <param name="type">Type of message.</param>
        public void Log(string message, MessageTypeEnum type)
        {
            logHistory.Add(new MessageRecievedEventArgs(type, message));
            MessageRecieved?.Invoke(this, new MessageRecievedEventArgs(type, message));

            string[] args = new string[2];
            args[0] = ((int)type).ToString();
            args[1] = message;
            ConfigurationRecieveEventArgs command =
                new ConfigurationRecieveEventArgs((int)ConfigurationEnum.NewLogMessageConfiguraton, args);

            NotifyClients?.Invoke(this, command);
        }
예제 #4
0
        public void Log(string message, MessageTypeEnum type)
        {
            MessageRecievedEventArgs msg = new MessageRecievedEventArgs();

            msg.Message = message;
            msg.Status  = type;
            MessageRecieved?.Invoke(this, msg);
            string[] args = new string[2];
            args[0] = type.ToString();
            args[1] = message;
            InfoEventArgs info = new InfoEventArgs((int)InfoEnums.LogInfo, args);

            NotifyClients?.Invoke(this, info);
        }
예제 #5
0
        /// <summary>
        /// Handles a GUI client. Reads commands, executes them and sends a command back if needed
        /// </summary>
        /// <param name="client">The GUI client.</param>
        /// <param name="clients">A list of GUI clients.</param>
        public void HandleClient(TcpClient client, List <TcpClient> clients)
        {
            new Task(() =>
            {
                try
                {
                    NetworkStream stream = client.GetStream();
                    BinaryReader reader  = new BinaryReader(stream);
                    BinaryWriter writer  = new BinaryWriter(stream);

                    while (client.Connected)
                    {
                        string command = reader.ReadString();
                        if (command == null)
                        {
                            continue;
                        }
                        CommandRecievedEventArgs commandRecievedEventArgs = JsonConvert.DeserializeObject <CommandRecievedEventArgs>(command);
                        m_logging.Log("HandleClient got the command: " + (CommandEnum)commandRecievedEventArgs.CommandID, MessageTypeEnum.INFO);
                        if (commandRecievedEventArgs.CommandID == (int)CommandEnum.ClientClosedCommand)
                        {
                            clients.Remove(client);
                            client.Close();
                            m_logging.Log("A client was removed ", MessageTypeEnum.INFO);
                            break;
                        }
                        else if (commandRecievedEventArgs.CommandID == (int)CommandEnum.CloseCommand)
                        {
                            m_imageServer.makeEvent(commandRecievedEventArgs);
                            if (m_imageServer.Handlers.Contains(commandRecievedEventArgs.RequestDirPath))
                            {
                                m_imageServer.Handlers.Remove(commandRecievedEventArgs.RequestDirPath);
                            }
                            Thread.Sleep(100);
                            string[] arr = new string[1];
                            arr[0]       = commandRecievedEventArgs.RequestDirPath;
                            CommandRecievedEventArgs command2 = new CommandRecievedEventArgs((int)CommandEnum.CloseCommand, arr, "");
                            NotifyClients?.Invoke(command2);
                            continue;
                        }
                        else if (commandRecievedEventArgs.CommandID == (int)CommandEnum.GetConfigCommand)
                        {
                            string handlers = "";
                            foreach (string handler in m_imageServer.Handlers)
                            {
                                handlers += handler + ";";
                            }
                            if (handlers != "")
                            {
                                handlers.TrimEnd(';');
                            }
                            commandRecievedEventArgs.Args[0] = handlers;
                        }
                        bool success;
                        string msg = m_controller.ExecuteCommand(commandRecievedEventArgs.CommandID, commandRecievedEventArgs.Args, out success);
                        if (success)
                        {
                            m_logging.Log("Success executing command: " + (CommandEnum)commandRecievedEventArgs.CommandID, MessageTypeEnum.INFO);
                        }
                        else
                        {
                            m_logging.Log(msg, MessageTypeEnum.FAIL);
                        }
                        lock (writeLock)
                        {
                            writer.Write(msg);
                        }
                    }
                }
                catch (Exception exc)
                {
                    clients.Remove(client);
                    client.Close();
                    m_logging.Log(exc.ToString(), MessageTypeEnum.FAIL);
                }
            }).Start();
        }
예제 #6
0
 /// <summary>
 /// Invokes event.
 /// </summary>
 /// <param name="message">Message to send to all clients.</param>
 public void InvokeEvent(ConfigurationRecieveEventArgs message)
 {
     NotifyClients?.Invoke(this, message);
 }