public void Download(FileComponent comp) { Debug.Assert(comp != null); Entity entity = new Entity(); EntityIdleComponent idle = new EntityIdleComponent(); entity.Add(idle); EntityStoreComponent store = new EntityStoreComponent(comp.Owner); entity.Add(store); FileComponent file = new FileComponent(comp); entity.Add(file); TransferComponent transfer = new TransferComponent(); entity.Add(transfer); DownloadMachineComponent machine = new DownloadMachineComponent(); entity.Add<MachineComponent>(machine); #if DEBUG LogComponent log = new LogComponent(LogController); entity.Add(log); #endif Add(entity); }
public void Execute(HttpRequest httpRequest) { // Connect NetworkChannel channel = new NetworkChannel(Connection); // Data Entity entity = new Entity(null); entity.EntityShutdown += Listener; EntityIdleComponent idle = new EntityIdleComponent(); entity.Add(idle); SessionComponent session = new SessionComponent() { Key = Key.Parse(httpRequest.Data) }; entity.Add(session); SessionMap.Add(session.Id, entity); Server.Sessions = SessionMap.Count; // TODO: Use Diffie–Hellman // // Response HttpResponse httpResponse = new HttpResponse(session.Id) { Data = SecurityUtil.Token }; channel.Send(httpResponse); #if DEBUG LogComponent log = new LogComponent(Log.Controller); entity.Add(log); Log.Add(httpRequest, httpResponse); #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); }