/// <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);
                }
            }
        }
        /// <summary>
        /// the function executes the get config command
        /// </summary>
        /// <param name= args> the commands arguments </param>
        /// <param name= result> the result of the function, if succeeded or not </param>
        /// <return> returns the app config info as a string </return>
        public string Execute(string[] args, out bool result)
        {
            // gets the instance of the app config info
            ServiceInfo info = ServiceInfo.CreateServiceInfo();

            result = true;
            string handlers = String.Join(",", info.Handlers);

            string[] answer = (info.OutputDir + "," + info.SourceName + "," + info.LogName + "," + info.ThumbnailSize + "," + handlers).Split(',');
            // return the info converted to Json ready to be sent to client
            InfoEventArgs infoArgs = new InfoEventArgs((int)EnumTranslator.CommandToInfo((int)CommandEnum.GetConfigCommand), answer);

            return(JsonConvert.SerializeObject(infoArgs));
        }
Beispiel #3
0
        public void HandleClient(TcpClient client, object locker)
        {
            stream = client.GetStream();
            // as long as the client is connected
            while (client.Connected)
            {
                while (true)
                {
                    byte[] thisByte = new byte[1] {
                        0
                    };
                    List <byte> currBytes = new List <byte>();

                    try
                    {
                        while (thisByte[0] != (byte)'\n')
                        {
                            this.stream.Read(thisByte, 0, 1);
                            if (thisByte[0] != (byte)'\n')
                            {
                                currBytes.Add(thisByte[0]);
                            }
                        }

                        // convert to the size of the picture to int
                        string picStr = Encoding.ASCII.GetString(currBytes.ToArray(), 0, currBytes.ToArray().Length);
                        int    picSize;
                        bool   successful = int.TryParse(picStr, out picSize);
                        if (!successful)
                        {
                            continue;
                        }

                        // if the string is End\n we reached the end of the current picture
                        if (picStr.Equals("End\n"))
                        {
                            break;
                        }

                        // get the name of the picture
                        thisByte[0] = 0;
                        currBytes   = new List <byte>();
                        while (!this.stream.DataAvailable)
                        {
                        }
                        while (thisByte[0] != (byte)'\n')
                        {
                            this.stream.Read(thisByte, 0, 1);
                            if (thisByte[0] != (byte)'\n' &&
                                thisByte[0] != 0)
                            {
                                currBytes.Add(thisByte[0]);
                            }
                        }
                        // convert to string
                        string picName = Encoding.ASCII.GetString(currBytes.ToArray(), 0, currBytes.ToArray().Length);

                        // get the picture
                        byte[] bytes          = new byte[picSize];
                        int    bytesReadFirst = stream.Read(bytes, 0, bytes.Length);
                        int    tempBytes      = bytesReadFirst;
                        while (tempBytes < bytes.Length)
                        {
                            tempBytes += stream.Read(bytes, tempBytes, bytes.Length - tempBytes);
                        }


                        ServiceInfo info = ServiceInfo.CreateServiceInfo();
                        // save the image
                        string directory = info.Handlers[0];
                        File.WriteAllBytes(Path.Combine(directory, picName), bytes);
                        logging.Log("Saved image from Application client", MessageTypeEnum.INFO);
                    }
                    catch (Exception e)
                    {
                        logging.Log("Error reading from Application client. Exiting client handler",
                                    MessageTypeEnum.FAIL);
                        break;
                    }
                }
            }
            stream.Close();
            client.Close();
        }