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);
        }
Exemple #2
0
        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
        }