Beispiel #1
0
        public JsonAction Execute(HttpRequest httpRequest, JsonPacket jsonRequest, SessionComponent session)
        {
            Clear();

            // Connect
            NetworkChannel channel = new NetworkChannel(Connection);

            // Request
            JsonAction jsonAction = JsonAction.None;
            JsonDownloadRequestMessage jsonRequestMessage = JsonDownloadRequestMessage.Parse(jsonRequest.Message);
            string jsonId = jsonRequestMessage.Id;

            if (jsonId != null)
            {
                // Data
                Entity entity = TransferMap.Get(jsonId);
                if (entity == null)
                {
                    channel.SendNotFound();
                    return(jsonAction);
                }

                ChunkComponent transfer = entity.Get <ChunkComponent>();
                if (transfer == null)
                {
                    channel.SendNotFound();
                    return(jsonAction);
                }

                JsonTransfer jsonTransfer = transfer.PopData();
                if (!transfer.Finished)
                {
                    jsonAction = JsonAction.Request;
                    entity.Update();
                }
                else
                {
                    TransferMap.Remove(jsonId);
                }

                string    jsonData  = null;
                JsonChunk jsonChunk = null;
                if (jsonTransfer != null)
                {
                    jsonData  = jsonTransfer.Data;
                    jsonChunk = jsonTransfer.Chunk;
                }

                // Response
                JsonDownloadResponseMessage jsonResponseMessage = new JsonDownloadResponseMessage(jsonId)
                {
                    Chunk = jsonChunk
                };
                JsonPacket jsonResponse = new JsonPacket(jsonResponseMessage, jsonData);

                HttpResponse httpResponse = new HttpResponse()
                {
                    Data = session.Encrypt(jsonResponse)
                };
                channel.Send(httpResponse);
#if DEBUG
                jsonResponse.Data = null;
                Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse);
#endif
            }
            else
            {
                // Data
                Entity entity = session.Owner;
                SearchListComponent search     = entity.Get <SearchListComponent>();
                JsonClient          jsonClient = jsonRequestMessage.Client;

                // Request
                if (jsonClient == null && search.Empty)
                {
                    channel.SendNotFound();
                    return(jsonAction);
                }

                // Response
                jsonId = SecurityUtil.CreateKeyString();
                JsonDownloadResponseMessage jsonResponseMessage = new JsonDownloadResponseMessage(jsonId);
                JsonPacket jsonResponse = new JsonPacket(jsonResponseMessage);

                HttpResponse httpResponse = new HttpResponse()
                {
                    Data = session.Encrypt(jsonResponse)
                };
                channel.Send(httpResponse);
#if DEBUG
                Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse);
#endif
                // Data
                entity = new Entity(jsonId);
                EntityIdleComponent idle = new EntityIdleComponent();
                entity.Add(idle);

                JsonChunk      jsonChunk = jsonRequestMessage.Chunk;
                ChunkComponent transfer  = new ChunkComponent(jsonChunk.Size, Options.ChunkSize, Options.MaxChunks);
                entity.Add(transfer);

                TransferMap.Add(entity);

                // Command
                string jsonData = jsonRequest.Data;
                jsonChunk = transfer.PopChunk();

                if (jsonClient == null)
                {
                    foreach (Entity e in search)
                    {
                        CommandState state = new CommandState()
                        {
                            Id = jsonId, Data = jsonData, Chunk = jsonChunk, Entity = e
                        };
                        Thread thread = new Thread(new ParameterizedThreadStart(ExecuteThread))
                        {
                            Priority = ThreadPriority.BelowNormal, IsBackground = true
                        };
                        thread.Start(state);
                    }
                }
                else
                {
                    Entity e = ClientMap.Get(jsonClient.Id);
                    if (e == null)
                    {
                        channel.SendNotFound();
                        return(jsonAction);
                    }

                    CommandState state = new CommandState()
                    {
                        Id = jsonId, Data = jsonData, Chunk = jsonChunk, Entity = e
                    };
                    Thread thread = new Thread(new ParameterizedThreadStart(ExecuteThread))
                    {
                        Priority = ThreadPriority.BelowNormal, IsBackground = true
                    };
                    thread.Start(state);
                }

                jsonAction = JsonAction.Request;
            }

            return(jsonAction);
        }
        public JsonAction Execute(HttpRequest httpRequest, JsonPacket jsonRequest, SessionComponent session)
        {
            Clear();

            // Connect
            NetworkChannel channel = new NetworkChannel(Connection);

            // Request
            JsonUploadRequestMessage jsonRequestMessage = JsonUploadRequestMessage.Parse(jsonRequest.Message);

            // Data
            string jsonId = jsonRequestMessage.Id;
            Entity entity = TransferMap.Get(jsonId);

            if (entity == null)
            {
                channel.SendNotFound();
                return(JsonAction.None);
            }

            JsonChunk      jsonChunk = jsonRequestMessage.Chunk;
            ChunkComponent transfer  = entity.Get <ChunkComponent>();

            if (jsonChunk != null)
            {
                JsonTransfer jsonTransfer = new JsonTransfer()
                {
                    Chunk = jsonChunk, Data = jsonRequest.Data
                };
                transfer.PushData(jsonTransfer);
            }

            if (!transfer.HasChunk())
            {
                channel.SendNotFound();
                return(JsonAction.None);
            }

            int delay = 0;

            jsonChunk = null;
            if (transfer.HasDataCapacity())
            {
                jsonChunk = transfer.PopChunk();
            }
            else
            {
                delay = (int)DemonTimeout.File.TotalMilliseconds;
            }

            // Response
            JsonUploadResponseMessage jsonResponseMessage = new JsonUploadResponseMessage(jsonId)
            {
                Chunk = jsonChunk, Delay = delay
            };
            JsonPacket jsonResponse = new JsonPacket(jsonResponseMessage);

            HttpResponse httpResponse = new HttpResponse()
            {
                Data = session.Encrypt(jsonResponse)
            };

            channel.Send(httpResponse);
#if DEBUG
            jsonRequest.Data = null;
            Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse);
#endif
            return(JsonAction.Request);
        }