public HttpCode Execute(string jsonId, FileComponent file, string clientId = null) { Clear(); // Connect NetworkChannel channel = new NetworkChannel(Connection); // Request JsonChunk jsonChunk = new JsonChunk() { Size = file.Size }; JsonClient jsonClient = (clientId != null) ? new JsonClient(clientId) : null; JsonDownloadRequestMessage jsonRequestMessage = new JsonDownloadRequestMessage(jsonId) { Chunk = jsonChunk, Client = jsonClient }; JsonFile jsonFile = (JsonFile)file; JsonDownloadRequestData jsonRequestData = new JsonDownloadRequestData() { File = jsonFile }; JsonPacket jsonRequest = new JsonPacket(jsonRequestMessage, Group.Encrypt(jsonRequestData)); HttpRequest httpRequest = new HttpRequest(Session.Id) { Data = Session.Encrypt(jsonRequest) }; channel.Send(httpRequest); // Response HttpResponse httpResponse; channel.Receive(out httpResponse); Code = httpResponse.Code; if (httpResponse.Ok) { JsonPacket jsonResponse = JsonPacket.Parse(Session.Decrypt(httpResponse.Data)); JsonDownloadResponseMessage jsonResponseMessage = JsonDownloadResponseMessage.Parse(jsonResponse.Message); Chunk = jsonResponseMessage.Chunk; Id = jsonResponseMessage.Id; JsonDownloadResponseData jsonResponseData = JsonDownloadResponseData.Parse(Group.Decrypt(jsonResponse.Data)); if (jsonResponseData != null) { Data = jsonResponseData.Data; } #if DEBUG jsonResponse.Data = null; Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse); #endif } return(httpResponse.Code); }
public void Execute(HttpRequest httpRequest, JsonPacket jsonRequest) { Clear(); // Connect NetworkChannel channel = new NetworkChannel(Connection); // Request JsonDownloadRequestMessage jsonRequestMessage = JsonDownloadRequestMessage.Parse(jsonRequest.Message); JsonChunk jsonChunk = jsonRequestMessage.Chunk; string jsonId = jsonRequestMessage.Id; JsonDownloadRequestData jsonRequestData = JsonDownloadRequestData.Parse(Group.Decrypt(jsonRequest.Data)); JsonFile jsonFile = jsonRequestData.File; // Data FileComponent file = FileMap.Get(jsonFile.Id); if (file == null) { channel.SendNotFound(); return; } // Controller //UploadData uploadData = new UploadData(fileData, Client) { Id = jsonId, Chunk = jsonChunk }; //OnUpload(uploadData); // Response JsonDownloadResponseMessage jsonResponseMessage = new JsonDownloadResponseMessage(); 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 }
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); }