Пример #1
0
        private void ExecuteThread(object obj)
        {
            try
            {
                // Data
                CommandState state = (CommandState)obj;

                // Request
                Entity               entity     = state.Target;
                TunnelComponent      tunnel     = entity.Get <TunnelComponent>();
                SearchRequestCommand cmdRequest = new SearchRequestCommand(entity, tunnel.Connection);
                string               jsonData   = cmdRequest.Execute(state.Data);
                if (string.IsNullOrEmpty(jsonData))
                {
                    return;
                }

                // Response
                entity = state.Source;
                tunnel = entity.Get <TunnelComponent>();
                SearchResponseCommand cmdResponse = new SearchResponseCommand(entity, tunnel.Connection);
                HttpCode code = cmdResponse.Execute(state.Id, jsonData);
                if (code == HttpCode.Ok)
                {
                    SearchListComponent download = entity.Get <SearchListComponent>();
                    download.Add(state.Target);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Пример #2
0
        // TODO: Eventually support multiple searches via a TreeView?
        //
        public void Search(string keyword, FileFilter filter = FileFilter.None)
        {
            if (MenuController.Entity == null)
            {
                return;
            }

            Clear();
            Start();

            try
            {
                // NOTE: No need to close the connection
                SearchRequestCommand command = new SearchRequestCommand(MenuController.Entity);
                command.Execute(keyword, filter);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Пример #3
0
        private void ListenState()
        {
            NetworkChannel channel = null;

            try
            {
                // Connect
                if (!Connected)
                {
                    State  = DemonState.Restart;
                    Status = DemonStatus.Info;
                    return;
                }

                // NOTE: No need to close the channel
                channel = new NetworkChannel(Connection);
                Status  = DemonStatus.Success;

                // Request
                HttpRequest httpRequest;
                channel.Receive(out httpRequest);
                if (httpRequest == null)
                {
                    Status = DemonStatus.Warning;
                    return;
                }

                // TODO: Version check
                //
                // Message
                SessionComponent session            = Owner.Get <SessionComponent>();
                string           decrypted          = session.Decrypt(httpRequest.Data);
                JsonPacket       jsonRequest        = JsonPacket.Parse(decrypted);
                JsonMessage      jsonRequestMessage = JsonMessage.Parse(jsonRequest.Message);
                JsonType         jsonType           = jsonRequestMessage.Type;

                switch (jsonType)
                {
                case JsonType.Ping:
                {
                    PingResponseCommand command = new PingResponseCommand(Owner, Connection);
                    command.Execute(httpRequest, jsonRequest);
                    break;
                }

                case JsonType.Search:
                {
                    JsonAction action = jsonRequestMessage.Action;

                    switch (action)
                    {
                    case JsonAction.Request:
                    {
                        SearchRequestCommand command = new SearchRequestCommand(Owner, Connection);
                        command.Execute(httpRequest, jsonRequest);
                        break;
                    }

                    case JsonAction.Response:
                    {
                        SearchResponseCommand command = new SearchResponseCommand(Owner, Connection);
                        List <FileComponent>  list    = command.Execute(httpRequest, jsonRequest);
                        OnListAdded(list);
                        break;
                    }

                    default:
                    {
                        channel.SendBadRequest();
                        break;
                    }
                    }

                    break;
                }

                case JsonType.Browse:
                {
                    BrowseResponseCommand command = new BrowseResponseCommand(Owner, Connection);
                    command.Execute(httpRequest, jsonRequest);
                    break;
                }

                case JsonType.Download:
                {
                    // TODO
                    //
                    DownloadResponseCommand command = new DownloadResponseCommand(Owner, Connection);                            // { Handler = UploadEvent };
                    command.Execute(httpRequest, jsonRequest);
                    break;
                }

                default:
                {
                    channel.SendBadRequest();
                    State  = DemonState.Restart;
                    Status = DemonStatus.Info;
                    return;
                }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Status = DemonStatus.Error;

                if (channel != null)
                {
                    try
                    {
                        channel.SendInternalServerError();
                    }
                    catch (Exception) { }
                }
            }
        }