/// <summary>
        /// method to handle a received message
        /// </summary>
        /// <param name="sender">sender of the message</param>
        /// <param name="messageArgs">the message arguments</param>
        private void ReadRecivedMessage(object sender, MessageCommunicationEventArgs messageArgs)
        {
            string message = messageArgs.Message;
            ServerClientCommunicationCommand commCommand = ServerClientCommunicationCommand.FromJson(message);

            switch (commCommand.CommId)
            {
            case CommandEnum.LogCommand:
                string jsonLogs = commCommand.Args[0];
                List <MessageRecievedEventArgs> tmpList = JsonConvert.DeserializeObject <List <MessageRecievedEventArgs> >(jsonLogs);
                foreach (MessageRecievedEventArgs entry in tmpList)
                {
                    try
                    {
                        //moving the action to be handled in the UI thread
                        App.Current.Dispatcher.Invoke((Action) delegate
                        {
                            this.Logs.Insert(0, entry);
                        });
                    }
                    catch (Exception exc)
                    {
                        string msg = exc.Message;
                    }
                }
                break;

            default:
                break;
            }
        }
        /// <summary>
        /// method returns the logs from the service
        /// </summary>
        /// <param name="filterBy">string representing status to filter by</param>
        /// <returns>list of filtered logs</returns>
        public IEnumerable <LogMessage> GetLogs(string filterBy)
        {
            try
            {
                SynchTcpClientHandler commChannel = new SynchTcpClientHandler();

                bool filter = true;
                if (filterBy == null || filterBy == "")
                {
                    filter = false;
                }
                List <LogMessage> logs = new List <LogMessage>();
                filterBy = filterBy?.ToLower();

                ServerClientCommunicationCommand commCommand = new ServerClientCommunicationCommand(CommandEnum.LogCommand, null);
                string receivedMessage = commChannel.Send(commCommand.ToJson());
                ServerClientCommunicationCommand logsCommCommand = ServerClientCommunicationCommand.FromJson(receivedMessage);
                if (logsCommCommand.CommId == CommandEnum.LogCommand)
                {
                    string jsonLogs = logsCommCommand.Args[0];
                    List <MessageRecievedEventArgs> tmpList = JsonConvert.DeserializeObject <List <MessageRecievedEventArgs> >(jsonLogs);
                    foreach (MessageRecievedEventArgs entry in tmpList)
                    {
                        if (!filter)
                        {
                            logs.Add(new LogMessage()
                            {
                                Message = entry.Message, Status = entry.Status
                            });
                        }
                        else
                        if (entry.Status.ToString().ToLower() == filterBy)
                        {
                            logs.Add(new LogMessage()
                            {
                                Message = entry.Message, Status = entry.Status
                            });
                        }
                    }
                    return(logs);
                }
                else
                {
                    return(new List <LogMessage>());
                }
            }
            catch
            {
                return(new List <LogMessage>());
            }
        }
        /// <summary>
        /// method to handle received message
        /// </summary>
        /// <param name="sender">the sender of the message</param>
        /// <param name="messageArgs">the arguments of the message</param>
        private void ReadRecivedMessage(object sender, MessageCommunicationEventArgs messageArgs)
        {
            string message = messageArgs.Message;
            ServerClientCommunicationCommand commCommand = ServerClientCommunicationCommand.FromJson(message);

            switch (commCommand.CommId)
            {
            case CommandEnum.GetConfigCommand:
                this.InitializeConfigData(commCommand.Args[0]);
                break;

            case CommandEnum.CloseCommand:
                this.ExecuteRemoveDirectoryPath(commCommand.Args[0]);
                break;

            default:
                break;
            }
        }
Beispiel #4
0
        /// <summary>
        /// method that returns the config data from the service
        /// </summary>
        /// <returns>config data</returns>
        public ConfigData GetConfig()
        {
            try
            {
                ConfigData            cData       = new ConfigData();
                string[]              args        = new string[1];
                SynchTcpClientHandler commChannel = new SynchTcpClientHandler();
                this.commConfigCommand = new ServerClientCommunicationCommand(CommandEnum.GetConfigCommand, args);

                string message = commChannel.Send(this.commConfigCommand.ToJson());
                ServerClientCommunicationCommand commCommand = ServerClientCommunicationCommand.FromJson(message);
                if (commCommand.CommId == CommandEnum.GetConfigCommand)
                {
                    string  jsonData      = commCommand.Args[0];
                    JObject appConfigData = JObject.Parse(jsonData);
                    cData.OutputDirectory = (string)appConfigData["OutputDir"];
                    cData.SourceName      = (string)appConfigData["SourceName"];
                    cData.LogName         = (string)appConfigData["LogName"];
                    cData.ThumbnailSize   = (string)appConfigData["ThumbnailSize"];
                    string dirPathsListString = (string)appConfigData["dirPathsToManageListString"];
                    cData.DirectoryHandlerPaths = new List <string>(JsonConvert.DeserializeObject <List <string> >(dirPathsListString));
                    commChannel.Close();
                    return(cData);
                }
                else
                {
                    commChannel.Close();
                    return(new ConfigData()
                    {
                        LogName = "", SourceName = "", ThumbnailSize = "", OutputDirectory = "", DirectoryHandlerPaths = new List <string>()
                    });
                }
            }
            catch
            {
                return(new ConfigData()
                {
                    LogName = "", SourceName = "", ThumbnailSize = "", OutputDirectory = "", DirectoryHandlerPaths = new List <string>()
                });
            }
        }
        /// <summary>
        /// method for handeling message from communication channel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="message"> received message</param>
        private void HandleMessageFromServer(object sender, MessageCommunicationEventArgs message)
        {
            IClientHandler clientHandler = sender as IClientHandler;
            ServerClientCommunicationCommand commCommand = ServerClientCommunicationCommand.FromJson(message.Message);

            switch (commCommand.CommId)
            {
            case CommandEnum.GetConfigCommand:
            //asking for logs list
            case CommandEnum.LogCommand:
                bool     flag1;
                string   result       = this.m_controller.ExecuteCommand(commCommand.CommId, commCommand.Args, out flag1);
                string[] responseArr1 = new string[1];
                responseArr1[0] = result;
                ServerClientCommunicationCommand responseCommand = new ServerClientCommunicationCommand(commCommand.CommId, responseArr1);
                string responseJson = responseCommand.ToJson();
                //writing back the answer to the client request
                clientHandler?.WriteMessage(responseJson);
                break;

            //closing a directory handler request
            case CommandEnum.CloseCommand:
                bool   flag2;
                string pathRemoved = this.m_controller.ExecuteCommand(commCommand.CommId, commCommand.Args, out flag2);
                //writing to all of the connected clients the path of directory which's handler closed
                string[] responseArr2 = new string[1];
                responseArr2[0] = pathRemoved;
                this.m_serverChannel.BroadcastToClients(new ServerClientCommunicationCommand(CommandEnum.CloseCommand, responseArr2));
                break;

            case CommandEnum.PhotoTransferCommand:
                bool   flag3;
                string temp = this.m_controller.ExecuteCommand(commCommand.CommId, commCommand.Args, out flag3);
                break;

            default:
                clientHandler?.WriteMessage("Invalid command Id");
                break;
            }
        }