public void Execute(HttpRequest httpRequest, JsonPacket jsonRequest, SessionComponent session) { // Connect NetworkChannel channel = new NetworkChannel(Connection); // Request JsonBrowseRequestMessage jsonRequestMessage = JsonBrowseRequestMessage.Parse(jsonRequest.Message); JsonClient jsonClient = jsonRequestMessage.Client; if (jsonClient == null) { channel.SendNotFound(); return; } Entity entity = ClientMap.Get(jsonClient.Id); if (entity == null) { channel.SendNotFound(); return; } // Command TunnelComponent tunnel = entity.Get <TunnelComponent>(); BrowseRequestCommand command = new BrowseRequestCommand(entity, tunnel.Connection); string jsonData = command.Execute(jsonRequest.Data); if (string.IsNullOrEmpty(jsonData)) { channel.SendNotFound(); return; } // Response JsonBrowseResponseMessage jsonResponseMessage = new JsonBrowseResponseMessage(); JsonPacket jsonResponse = new JsonPacket(jsonResponseMessage, jsonData); HttpResponse httpResponse = new HttpResponse() { Data = session.Encrypt(jsonResponse) }; channel.Send(httpResponse); #if DEBUG Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse); #endif }
public void Execute(HttpRequest httpRequest, JsonPacket jsonRequest) { Clear(); // Connect NetworkChannel channel = new NetworkChannel(Connection); // Request JsonSearchRequestMessage jsonRequestMessage = JsonSearchRequestMessage.Parse(jsonRequest.Message); JsonSearchRequestData jsonRequestData = JsonSearchRequestData.Parse(Group.Decrypt(jsonRequest.Data)); JsonSearch jsonSearch = jsonRequestData.Search; // Data List <FileComponent> compFiles = FileMap.Search(jsonSearch); if (compFiles.Count == 0) { channel.SendNotFound(); return; } List <JsonFile> jsonFiles = new List <JsonFile>(); foreach (FileComponent file in compFiles) { JsonFile jsonFile = (JsonFile)file; jsonFiles.Add(jsonFile); } // Response JsonSearchResponseMessage jsonResponseMessage = new JsonSearchResponseMessage(); JsonSearchResponseData jsonResponseData = new JsonSearchResponseData() { Files = jsonFiles }; JsonPacket jsonResponse = new JsonPacket(jsonResponseMessage, Group.Encrypt(jsonResponseData)); HttpResponse httpResponse = new HttpResponse() { Data = Session.Encrypt(jsonResponse) }; channel.Send(httpResponse); #if DEBUG Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse); #endif }
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); }
public void Execute(HttpRequest httpRequest, JsonPacket jsonRequest, SessionComponent session) { // Connect NetworkChannel channel = new NetworkChannel(Connection); // Request JsonGroupRequestMessage jsonRequestMessage = JsonGroupRequestMessage.Parse(jsonRequest.Message); // Data Entity entity = session.Owner; GroupComponent group = entity.Get <GroupComponent>(); EntityList entities = GroupList.Get(group.Id); if (entities == null) { channel.SendNotFound(); return; } List <JsonClient> jsonClients = new List <JsonClient>(); lock (entities) { foreach (Entity e in entities) { #if !DEBUG if (entity.Id.Equals(e.Id)) { continue; } #endif ClientComponent c = e.Get <ClientComponent>(); JsonClient jsonClient = (JsonClient)c; jsonClients.Add(jsonClient); } } if (jsonClients.Count == 0) { channel.SendNotFound(); return; } jsonClients.Sort(); // Response JsonGroupResponseMessage jsonResponseMessage = new JsonGroupResponseMessage() { Clients = jsonClients }; 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 }
public void Execute(HttpRequest httpRequest, JsonPacket jsonRequest, SessionComponent session) { Clear(); // Connect NetworkChannel channel = new NetworkChannel(Connection); // Request JsonSearchRequestMessage jsonRequestMessage = JsonSearchRequestMessage.Parse(jsonRequest.Message); // Data Entity entity = session.Owner; GroupComponent group = entity.Get <GroupComponent>(); EntityList entities = GroupList.Get(group.Id); if (entities == null) { channel.SendNotFound(); return; } // Response string jsonId = SecurityUtil.CreateKeyString(); JsonSearchResponseMessage jsonResponseMessage = new JsonSearchResponseMessage(jsonId); JsonPacket jsonResponse = new JsonPacket(jsonResponseMessage); HttpResponse httpResponse = new HttpResponse() { Data = session.Encrypt(jsonResponse) }; channel.Send(httpResponse); // Optimization: Keep track of the search list for faster downloads SearchListComponent download = entity.Get <SearchListComponent>(); download.Clear(); // Command lock (entities) { foreach (Entity e in entities) { // Do not search yourself if (entity.Id.Equals(e.Id)) { continue; } TunnelComponent tunnel = e.Get <TunnelComponent>(); if (!tunnel.Connected) { continue; } CommandState state = new CommandState() { Id = jsonId, Data = jsonRequest.Data, Source = entity, Target = e }; Thread thread = new Thread(new ParameterizedThreadStart(ExecuteThread)) { Priority = ThreadPriority.BelowNormal, IsBackground = true }; thread.Start(state); } } #if DEBUG Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse); #endif }
public void Execute(HttpRequest httpRequest, JsonPacket jsonRequest) { Clear(); // Connect NetworkChannel channel = new NetworkChannel(Connection); // Request JsonBrowseRequestMessage jsonRequestMessage = JsonBrowseRequestMessage.Parse(jsonRequest.Message); JsonBrowseRequestData jsonRequestData = JsonBrowseRequestData.Parse(Group.Decrypt(jsonRequest.Data)); JsonFile jsonFolder = (jsonRequestData != null) ? jsonRequestData.Folder : null; // Data List <JsonFile> folders = new List <JsonFile>(); List <JsonFile> files = new List <JsonFile>(); if (jsonFolder != null) { FileComponent file = FolderMap.Get(jsonFolder.Id); if (file == null) { channel.SendNotFound(); return; } // Folders foreach (FileComponent f in file.Folders) { JsonFile json = (JsonFile)f; folders.Add(json); } // Files foreach (FileComponent f in file.Files) { JsonFile json = (JsonFile)f; files.Add(json); } } else { // Folders FolderListComponent shared = Entity.Get <FolderListComponent>(); foreach (FileComponent f in shared) { JsonFile json = (JsonFile)f; folders.Add(json); } } // Response JsonBrowseResponseMessage jsonResponseMessage = new JsonBrowseResponseMessage(); JsonBrowseResponseData jsonResponseData = new JsonBrowseResponseData() { Files = files, Folders = folders }; JsonPacket jsonResponse = new JsonPacket(jsonResponseMessage, Group.Encrypt(jsonResponseData)); HttpResponse httpResponse = new HttpResponse() { Data = Session.Encrypt(jsonResponse) }; channel.Send(httpResponse); #if DEBUG Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse); #endif }
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); }