/// <summary>
        /// HandleClient function.
        /// handles the client-server communication.
        /// </summary>
        /// <param name="client">specified client</param>
        /// <param name="clients">list of all current clients</param>
        public void HandleClient(TcpClient client, List <TcpClient> clients)
        {
            try
            {
                new Task(() =>
                {
                    try
                    {
                        while (!m_isStopped)
                        {
                            NetworkStream stream = client.GetStream();
                            BinaryReader reader  = new BinaryReader(stream);
                            BinaryWriter writer  = new BinaryWriter(stream);
                            string commandLine   = reader.ReadString();
                            Logging.Log("ClientHandler got command: " + commandLine, MessageTypeEnum.INFO);

                            CommandRecievedEventArgs commandRecievedEventArgs = JsonConvert.DeserializeObject <CommandRecievedEventArgs>(commandLine);
                            if (commandRecievedEventArgs.CommandID == (int)CommandEnum.CloseClient)
                            {
                                clients.Remove(client);
                                client.Close();
                                break;
                            }
                            Console.WriteLine("Got command: {0}", commandLine);
                            bool r;
                            string result = this.ImageController.ExecuteCommand((int)commandRecievedEventArgs.CommandID,
                                                                                commandRecievedEventArgs.Args, out r);
                            // string result = handleCommand(commandRecievedEventArgs);
                            Mutex.WaitOne();

                            writer.Write(result);
                            Mutex.ReleaseMutex();
                        }
                    }
                    catch (Exception ex)
                    {
                        clients.Remove(client);
                        Logging.Log(ex.ToString(), MessageTypeEnum.FAIL);
                        client.Close();
                    }
                }).Start();
            }
            catch (Exception ex)
            {
                Logging.Log(ex.ToString(), MessageTypeEnum.FAIL);
            }
        }
Beispiel #2
0
        public void SetConfigsAndLogs(TcpClient client)
        {
            // serialize command for settings
            var serializedConfig             = JsonConvert.SerializeObject(Configurations);
            CommandRecievedEventArgs command = new CommandRecievedEventArgs((int)CommandEnum.GetConfigCommand, new string[] { serializedConfig }, string.Empty);

            // send appconfig
            var serializedCmd = JsonConvert.SerializeObject(command);

            this.SendMessage(serializedCmd, client);

            // serialize command for logs
            var serializedLogs = JsonConvert.SerializeObject(this.loggingService.Logs);

            command = new CommandRecievedEventArgs((int)CommandEnum.GetListLogCommand, new string[] { serializedLogs }, string.Empty);

            // send logs
            serializedCmd = JsonConvert.SerializeObject(command);
            this.SendMessage(serializedCmd, client);
        }
Beispiel #3
0
        private Task HandleConnectionAsync(TcpClient client)
        {
            return(Task.Run(async() =>
            {
                using (NetworkStream stream = client.GetStream())
                {
                    bool running = true;
                    BinaryReader reader = new BinaryReader(stream);
                    string msg = string.Empty;
                    SetConfigsAndLogs(client);

                    while (running)
                    {
                        try
                        {
                            msg = reader.ReadString();
                            Console.WriteLine("msg: {0}", msg);
                            CommandRecievedEventArgs cmd = JsonConvert.DeserializeObject <CommandRecievedEventArgs>(msg);
                            Console.WriteLine("command is: {0}", cmd);
                            // client exit
                            if (cmd.CommandID == (int)CommandEnum.ExitCommand)
                            {
                                client.Close();
                                clients.Remove(client);
                                Console.WriteLine("Client removed");
                                break;
                            }
                            OnCommandRecieved?.Invoke(this, cmd);
                        }
                        catch (Exception ex)
                        {
                            running = false;
                            clients.Remove(client);
                            Console.WriteLine(ex.Message);
                        }
                    }
                    Console.WriteLine("closing client");
                }
            }));
        }
        /// <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);
        }
Beispiel #6
0
 /// <summary>
 /// Invoking the DirectoryCloseEvent closing the handlers
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 public void CloseHandler(object sender, CommandRecievedEventArgs e)
 {
     this.m_dirWatcher.Dispose();
     this.DirectoryClose.Invoke(this, new DirectoryCloseEventArgs(this.m_path, "closing handler"));
 }
Beispiel #7
0
 /// <summary>
 /// invoking the informHandlerClose of each handler
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="args"></param>
 public void ServiceStopped(object sender, CommandRecievedEventArgs args)
 {
     informHandlerClose.Invoke(this, args);
 }
Beispiel #8
0
 /// <summary>
 /// send the command to all folders that register to the commandRecived event.
 /// </summary>
 /// <param name="e"></param>
 public void SendCommand(CommandRecievedEventArgs e)
 {
     CommandRecieved?.Invoke(this, e);
 }
Beispiel #9
0
 public void updateAllClients(CommandRecievedEventArgs args)
 {
     this.serverIS.NotifyClients(args);
 }
 public void sendCmd(object sender, CommandRecievedEventArgs data)
 {
     CommandRecieved?.Invoke(this, data);
 }
 /// <summary>
 /// NotifyAboutNewLogEntry function.
 /// </summary>
 /// <param name="updateObj">CommandRecievedEventArgs obj</param>
 private void NotifyAboutNewLogEntry(CommandRecievedEventArgs updateObj)
 {
     NotifyAllClientsAboutUpdate(updateObj);
 }