PageRenderTime 334ms CodeModel.GetById 161ms app.highlight 16ms RepoModel.GetById 155ms app.codeStats 0ms

/indra/newview/llassetuploadqueue.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 211 lines | 154 code | 25 blank | 32 comment | 11 complexity | f2f1b8aacac2a93fac1c71f729e71c36 MD5 | raw file
  1/** 
  2 * @file llassetupload.cpp
  3 * @brief Serializes asset upload request
  4 *
  5 * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#include "llviewerprecompiledheaders.h"
 28
 29#include "llassetuploadqueue.h"
 30#include "llviewerregion.h"
 31#include "llviewerobjectlist.h"
 32
 33#include "llassetuploadresponders.h"
 34#include "llsd.h"
 35#include <iostream>
 36
 37class LLAssetUploadChainResponder : public LLUpdateTaskInventoryResponder
 38{
 39public:
 40	
 41	LLAssetUploadChainResponder(const LLSD& post_data,
 42								const std::string& file_name,
 43								const LLUUID& queue_id,
 44								U8* data, 
 45								U32 data_size,
 46								std::string script_name,
 47								LLAssetUploadQueueSupplier *supplier) :
 48		LLUpdateTaskInventoryResponder(post_data, file_name, queue_id, LLAssetType::AT_LSL_TEXT),
 49		mSupplier(supplier),
 50		mData(data),
 51		mDataSize(data_size),
 52		mScriptName(script_name)
 53	{
 54 	}
 55
 56	virtual ~LLAssetUploadChainResponder() 
 57   	{
 58   		if(mSupplier)
 59   		{
 60   			LLAssetUploadQueue *queue = mSupplier->get();
 61   			if (queue)
 62   			{
 63   				// Give ownership of supplier back to queue.
 64   				queue->mSupplier = mSupplier;
 65   				mSupplier = NULL;
 66   			}
 67   		}
 68   		delete mSupplier;
 69		delete mData;
 70   	}
 71	
 72	virtual void error(U32 statusNum, const std::string& reason)
 73   	{
 74		llwarns << "Error: " << reason << llendl;
 75		LLUpdateTaskInventoryResponder::error(statusNum, reason);
 76   		LLAssetUploadQueue *queue = mSupplier->get();
 77   		if (queue)
 78		{
 79   			queue->request(&mSupplier);
 80   		}
 81   	}
 82
 83	virtual void result(const LLSD& content)
 84   	{
 85		LLUpdateTaskInventoryResponder::result(content);
 86   		LLAssetUploadQueue *queue = mSupplier->get();
 87   		if (queue)
 88   		{
 89   			// Responder is reused across 2 phase upload,
 90   			// so only start next upload after 2nd phase complete.
 91   			std::string state = content["state"];
 92   			if(state == "complete")
 93   			{
 94   				queue->request(&mSupplier);
 95   			}
 96   		}	
 97   	}
 98	
 99	virtual void uploadUpload(const LLSD& content)
100	{
101		std::string uploader = content["uploader"];
102
103		mSupplier->log(std::string("Compiling " + mScriptName).c_str());
104		llinfos << "Compiling " << llendl;
105
106		// postRaw takes ownership of mData and will delete it.
107		LLHTTPClient::postRaw(uploader, mData, mDataSize, this);
108		mData = NULL;
109		mDataSize = 0;
110	}
111
112	virtual void uploadComplete(const LLSD& content)
113	{
114		// Bytecode save completed
115		if (content["compiled"])
116		{
117			mSupplier->log("Compilation succeeded");
118			llinfos << "Compiled!" << llendl;
119		}
120		else
121		{
122			LLSD compile_errors = content["errors"];
123			for(LLSD::array_const_iterator line	= compile_errors.beginArray();
124				line < compile_errors.endArray(); line++)
125			{
126				mSupplier->log(line->asString());
127				llinfos << content["errors"] << llendl;
128			}
129		}
130		LLUpdateTaskInventoryResponder::uploadComplete(content);
131	}
132
133	LLAssetUploadQueueSupplier *mSupplier;
134	U8* mData;
135	U32 mDataSize;
136	std::string mScriptName;
137};
138
139
140LLAssetUploadQueue::LLAssetUploadQueue(LLAssetUploadQueueSupplier *supplier) :
141	mSupplier(supplier)
142{
143}
144
145//virtual 
146LLAssetUploadQueue::~LLAssetUploadQueue()
147{
148	delete mSupplier;
149}
150
151// Takes ownership of supplier.
152void LLAssetUploadQueue::request(LLAssetUploadQueueSupplier** supplier)
153{
154	if (mQueue.empty())
155		return;
156
157	UploadData data = mQueue.front();
158	mQueue.pop_front();
159	
160	LLSD body;
161	body["task_id"] = data.mTaskId;
162	body["item_id"] = data.mItemId;
163	body["is_script_running"] = data.mIsRunning;
164	body["target"] = data.mIsTargetMono? "mono" : "lsl2";
165
166	std::string url = "";
167	LLViewerObject* object = gObjectList.findObject(data.mTaskId);
168	if (object)
169	{
170		url = object->getRegion()->getCapability("UpdateScriptTask");
171		LLHTTPClient::post(url, body,
172							new LLAssetUploadChainResponder(
173								body, data.mFilename, data.mQueueId, 
174								data.mData, data.mDataSize, data.mScriptName, *supplier));
175	}
176
177	*supplier = NULL;
178}
179
180void LLAssetUploadQueue::queue(const std::string& filename,
181							   const LLUUID& task_id,
182							   const LLUUID& item_id,
183							   BOOL is_running, 
184							   BOOL is_target_mono, 
185							   const LLUUID& queue_id,
186							   U8* script_data,
187							   U32 data_size,
188							   std::string script_name)
189{
190	UploadData data;
191	data.mTaskId = task_id;
192	data.mItemId = item_id;
193	data.mIsRunning = is_running;
194	data.mIsTargetMono = is_target_mono;
195	data.mQueueId = queue_id;
196	data.mFilename = filename;
197	data.mData = script_data;
198	data.mDataSize = data_size;
199	data.mScriptName = script_name;
200			
201	mQueue.push_back(data);
202
203	if(mSupplier)
204	{
205		request(&mSupplier);
206	}	
207}
208
209LLAssetUploadQueueSupplier::~LLAssetUploadQueueSupplier()
210{
211}