PageRenderTime 64ms CodeModel.GetById 22ms app.highlight 37ms RepoModel.GetById 1ms app.codeStats 1ms

/indra/llmessage/lltransfertargetvfile.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 219 lines | 157 code | 28 blank | 34 comment | 15 complexity | de3db9f1d35015e1229f92b93f092ec1 MD5 | raw file
  1/** 
  2 * @file lltransfertargetvfile.cpp
  3 * @brief Transfer system for receiving a vfile.
  4 *
  5 * $LicenseInfo:firstyear=2006&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 "linden_common.h"
 28
 29#include "lltransfertargetvfile.h"
 30
 31#include "lldatapacker.h"
 32#include "llerror.h"
 33#include "llvfile.h"
 34
 35//static
 36void LLTransferTargetVFile::updateQueue(bool shutdown)
 37{
 38}
 39
 40
 41LLTransferTargetParamsVFile::LLTransferTargetParamsVFile() :
 42	LLTransferTargetParams(LLTTT_VFILE),
 43	mAssetType(LLAssetType::AT_NONE),
 44	mCompleteCallback(NULL),
 45	mUserDatap(NULL),
 46	mErrCode(0)
 47{
 48}
 49
 50void LLTransferTargetParamsVFile::setAsset(
 51	const LLUUID& asset_id,
 52	LLAssetType::EType asset_type)
 53{
 54	mAssetID = asset_id;
 55	mAssetType = asset_type;
 56}
 57
 58void LLTransferTargetParamsVFile::setCallback(LLTTVFCompleteCallback cb, void *user_data)
 59{
 60	mCompleteCallback = cb;
 61	mUserDatap = user_data;
 62}
 63
 64bool LLTransferTargetParamsVFile::unpackParams(LLDataPacker& dp)
 65{
 66	// if the source provided a new key, assign that to the asset id.
 67	if(dp.hasNext())
 68	{
 69		LLUUID dummy_id;
 70		dp.unpackUUID(dummy_id, "AgentID");
 71		dp.unpackUUID(dummy_id, "SessionID");
 72		dp.unpackUUID(dummy_id, "OwnerID");
 73		dp.unpackUUID(dummy_id, "TaskID");
 74		dp.unpackUUID(dummy_id, "ItemID");
 75		dp.unpackUUID(mAssetID, "AssetID");
 76		S32 dummy_type;
 77		dp.unpackS32(dummy_type, "AssetType");
 78	}
 79
 80	// if we never got an asset id, this will always fail.
 81	if(mAssetID.isNull())
 82	{
 83		return false;
 84	}
 85	return true;
 86}
 87
 88
 89LLTransferTargetVFile::LLTransferTargetVFile(
 90	const LLUUID& uuid,
 91	LLTransferSourceType src_type) :
 92	LLTransferTarget(LLTTT_VFILE, uuid, src_type),
 93	mNeedsCreate(TRUE)
 94{
 95	mTempID.generate();
 96}
 97
 98
 99LLTransferTargetVFile::~LLTransferTargetVFile()
100{
101}
102
103
104// virtual
105bool LLTransferTargetVFile::unpackParams(LLDataPacker& dp)
106{
107	if(LLTST_SIM_INV_ITEM == mSourceType)
108	{
109		return mParams.unpackParams(dp);
110	}
111	return true;
112}
113
114void LLTransferTargetVFile::applyParams(const LLTransferTargetParams &params)
115{
116	if (params.getType() != mType)
117	{
118		llwarns << "Target parameter type doesn't match!" << llendl;
119		return;
120	}
121	
122	mParams = (LLTransferTargetParamsVFile &)params;
123}
124
125
126LLTSCode LLTransferTargetVFile::dataCallback(const S32 packet_id, U8 *in_datap, const S32 in_size)
127{
128	//llinfos << "LLTransferTargetFile::dataCallback" << llendl;
129	//llinfos << "Packet: " << packet_id << llendl;
130
131	LLVFile vf(gAssetStorage->mVFS, mTempID, mParams.getAssetType(), LLVFile::APPEND);
132	if (mNeedsCreate)
133	{
134		vf.setMaxSize(mSize);
135		mNeedsCreate = FALSE;
136	}
137
138	if (!in_size)
139	{
140		return LLTS_OK;
141	}
142
143	if (!vf.write(in_datap, in_size))
144	{
145		llwarns << "Failure in LLTransferTargetVFile::dataCallback!" << llendl;
146		return LLTS_ERROR;
147	}
148	return LLTS_OK;
149}
150
151
152void LLTransferTargetVFile::completionCallback(const LLTSCode status)
153{
154	//llinfos << "LLTransferTargetVFile::completionCallback" << llendl;
155
156	if (!gAssetStorage)
157	{
158		llwarns << "Aborting vfile transfer after asset storage shut down!" << llendl;
159		return;
160	}
161	
162	// Still need to gracefully handle error conditions.
163	S32 err_code = 0;
164	switch (status)
165	{
166	  case LLTS_DONE:
167		if (!mNeedsCreate)
168		{
169			LLVFile file(gAssetStorage->mVFS, mTempID, mParams.getAssetType(), LLVFile::WRITE);
170			if (!file.rename(mParams.getAssetID(), mParams.getAssetType()))
171			{
172				llerrs << "LLTransferTargetVFile: rename failed" << llendl;
173			}
174		}
175		err_code = LL_ERR_NOERR;
176		lldebugs << "LLTransferTargetVFile::completionCallback for "
177			 << mParams.getAssetID() << ","
178			 << LLAssetType::lookup(mParams.getAssetType())
179			 << " with temp id " << mTempID << llendl;
180		break;
181	  case LLTS_ERROR:
182	  case LLTS_ABORT:
183	  case LLTS_UNKNOWN_SOURCE:
184	  default:
185	  {
186		  // We're aborting this transfer, we don't want to keep this file.
187		  llwarns << "Aborting vfile transfer for " << mParams.getAssetID() << llendl;
188		  LLVFile vf(gAssetStorage->mVFS, mTempID, mParams.getAssetType(), LLVFile::APPEND);
189		  vf.remove();
190	  }
191	  break;
192	}
193
194	switch (status)
195	{
196	case LLTS_DONE:
197		err_code = LL_ERR_NOERR;
198		break;
199	case LLTS_UNKNOWN_SOURCE:
200		err_code = LL_ERR_ASSET_REQUEST_NOT_IN_DATABASE;
201		break;
202	case LLTS_INSUFFICIENT_PERMISSIONS:
203		err_code = LL_ERR_INSUFFICIENT_PERMISSIONS;
204		break;
205	case LLTS_ERROR:
206	case LLTS_ABORT:
207	default:
208		err_code = LL_ERR_ASSET_REQUEST_FAILED;
209		break;
210	}
211	if (mParams.mCompleteCallback)
212	{
213		mParams.mCompleteCallback(err_code,
214								  mParams.getAssetID(),
215								  mParams.getAssetType(),
216								  mParams.mUserDatap,
217								  LL_EXSTAT_NONE);
218	}
219}