Exemplo n.º 1
0
        /// <summary>
        /// Removes the handler.
        /// </summary>
        /// <param name="path">The path to handler to move.</param>
        public void RemoveHandler(String path)
        {
            this.handlerToClose = path;
            // bool stop = false;
            //Task task = new Task(() =>
            //{
            HandlerToClose h          = new HandlerToClose(path);
            String         jobject    = h.ToJSON();
            int            message    = (int)CommandEnum.CloseHandler;
            String         newMessage = message.ToString() + jobject;

            client.write(newMessage);
            //this.Listen();
        }
Exemplo n.º 2
0
        /// <summary>
        /// sends command to remove the handler specified
        /// </summary>
        /// <param name="handlerName">Name of the handler.</param>
        /// <returns></returns>
        public bool RemoveHandler(String handlerName)
        {
            this.waitForSuccess = true;
            HandlerToClose h          = new HandlerToClose(handlerName);
            String         jobject    = h.ToJSON();
            int            message    = (int)CommandEnum.CloseHandler;
            String         newMessage = message.ToString() + jobject;

            client.write(newMessage);
            while (!this.waitForSuccess)
            {
            }
            ;
            return(true);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Handles the client: adds it to the list of clients, sends it the log messages so far,
        /// reads command, send it to execute and sends it to writing the results.
        /// </summary>
        /// <param name="client">The client.</param>
        public void HandleClient(TcpClient client)
        {
            this.Clients.Add(client);
            //bool settingClient = true;
            if (!ClientStreamDictionary.ContainsKey(client))
            {
                // System.Diagnostics.Debugger.Launch();
                ClientStreamDictionary.Add(client, client.GetStream());
            }
            BinaryReader reader;
            bool         stop = false;

            new Task(() =>
            {
                while (!stop)
                {
                    // writer = new BinaryWriter(stream);
                    int commandNum = 0;
                    bool res;
                    String result = null;

                    // System.Diagnostics.Debugger.Launch();
                    bool hasValue = ClientStreamDictionary.TryGetValue(client, out NetworkStream stream);
                    if (hasValue)
                    {
                        hasValue = readerDictionary.TryGetValue(client, out BinaryReader r);
                        if (!hasValue)
                        {
                            reader = new BinaryReader(stream);
                            readerDictionary.Add(client, reader);
                        }
                        else
                        {
                            reader = r;
                        }
                        string commandLine = null;
                        try
                        {
                            commandLine = reader.ReadString();
                        }
                        catch (Exception)
                        {
                            continue;
                        }

                        Task.Delay(1000);
                        // Task.Delay(4000);
                        Console.WriteLine("Got command: {0}", commandLine);
                        //System.Diagnostics.Debugger.Launch();
                        Char c = commandLine[0];
                        // this.log.Log("command recieved: " + c, MessageTypeEnum.INFO);
                        try
                        {
                            commandNum = Int32.Parse(c.ToString());

                            /*
                             * if (commandNum==(int)CommandEnum.GetConfigCommand)
                             * {
                             *  for (int i = 0; i < this.logMessages.Count; ++i)
                             *  {
                             *      //System.Diagnostics.Debugger.Launch();
                             *      this.WriteToClient(this.logMessages[i]);
                             *  }
                             * }
                             */
                            if (commandNum == (int)CommandEnum.CloseHandler)
                            {
                                //System.Diagnostics.Debugger.Launch();
                                this.log.Log("close specific handler command recieved", MessageTypeEnum.INFO);
                                String handlerJObject = commandLine.Substring(1, commandLine.Length - 1);
                                this.log.Log("recieved: " + handlerJObject, MessageTypeEnum.INFO);
                                HandlerToClose h = HandlerToClose.FromJSON(handlerJObject);
                                this.CloseCommand?.Invoke(this, new DirectoryCloseEventArgs(h.Path, null));
                                //System.Diagnostics.Debugger.Launch();
                                res    = true;
                                result = ResultMessgeEnum.Success.ToString();
                                this.WriteToClient(result);
                                Task.Delay(100);
                                this.WriteToClient(handlerJObject);

                                // Task.Delay(100);
                            }
                            else if (commandNum == (int)CommandEnum.LogCommand)
                            {
                                for (int i = 0; i < this.logMessages.Count; ++i)
                                {
                                    //System.Diagnostics.Debugger.Launch();
                                    this.WriteToClient(this.logMessages[i]);
                                }
                                res    = true;
                                result = ResultMessgeEnum.Success.ToString();
                            }
                            else
                            {
                                this.log.Log("command number " + commandNum.ToString() + " recieved", MessageTypeEnum.INFO);
                                result = this.controller.ExecuteCommand(Int32.Parse(commandLine), null, out res);
                            }
                        }
                        catch (Exception)
                        {
                            res    = false;
                            result = ResultMessgeEnum.Fail.ToString();
                        }



                        this.WriteToClient(result);
                        //this.WriteToClient(result, 1);
                    }
                }
            }).Start();
        }
Exemplo n.º 4
0
        /// <summary>
        /// detemines what to do when got a meesage (either got feedback for the operation or got the
        /// primary settings of the app config).
        /// </summary>
        /// <param name="message">The message.</param>
        public void GotMeesage(string message)
        {
            Console.WriteLine("in settings view model, got: " + message);

            if (message.Equals(Infrastructure.Enums.ResultMessgeEnum.Success.ToString()) ||
                message.Contains(Infrastructure.Enums.ResultMessgeEnum.Success.ToString()))
            {
                if (this.handlerToClose != null)
                {
                    Console.WriteLine("in settings model, got: " + message + ". removing handler now");
                    //stop = true;
                    App.Current.Dispatcher.Invoke((Action) delegate // <--- HERE
                    {
                        this.handlers.Remove(this.handlerToClose);
                    });
                    NotifyPropertyChanged("Handlers");
                }
            }
            else if (message.Equals(Infrastructure.Enums.ResultMessgeEnum.Fail.ToString()))
            {
            }
            else if (this.configSet == false && message != null)
            {
                ImageServiceAppConfigItem initialConfig = ImageServiceAppConfigItem.FromJSON(message);
                if (initialConfig != null && !this.configSet)
                {
                    mut.WaitOne();
                    this.configSet = true;

                    Console.WriteLine(initialConfig.OutputFolder);
                    this.OutputDirectory = initialConfig.OutputFolder;
                    Console.WriteLine(initialConfig.LogName);
                    this.LogName = initialConfig.LogName;
                    Console.WriteLine(initialConfig.SourceName);
                    this.SourceName = initialConfig.SourceName;
                    Console.WriteLine(initialConfig.ThumbnailSize);
                    this.ThumbnailSize = initialConfig.ThumbnailSize;
                    App.Current.Dispatcher.Invoke((Action) delegate // <--- HERE
                    {
                        string[] folders = initialConfig.Handlers.Split(';');

                        foreach (String folder in folders)
                        {
                            this.handlers.Add(folder);
                        }
                    });
                    mut.ReleaseMutex();
                }
            }
            else
            {
                try
                {
                    Console.WriteLine("in settings model - got remove handler command, " +
                                      "command is: " + message);
                    HandlerToClose h = HandlerToClose.FromJSON(message);
                    if (handlers.Contains(h.Path))
                    {
                        App.Current.Dispatcher.Invoke((Action) delegate // <--- HERE
                        {
                            this.handlers.Remove(h.Path);
                        });
                        //NotifyPropertyChanged("Handlers");
                    }
                }
                catch (Exception)
                {
                }
            }
        }