clockwerk-opensim – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | vero | 1 | /* |
2 | * Copyright (c) Contributors, http://opensimulator.org/ |
||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions are met: |
||
7 | * * Redistributions of source code must retain the above copyright |
||
8 | * notice, this list of conditions and the following disclaimer. |
||
9 | * * Redistributions in binary form must reproduce the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer in the |
||
11 | * documentation and/or other materials provided with the distribution. |
||
12 | * * Neither the name of the OpenSimulator Project nor the |
||
13 | * names of its contributors may be used to endorse or promote products |
||
14 | * derived from this software without specific prior written permission. |
||
15 | * |
||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY |
||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY |
||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
26 | */ |
||
27 | |||
28 | using System; |
||
29 | using System.IO; |
||
30 | using System.Reflection; |
||
31 | using log4net; |
||
32 | using OpenMetaverse; |
||
33 | using OpenSim.Framework; |
||
34 | using OpenSim.Region.Framework.Interfaces; |
||
35 | using OpenSim.Region.Framework.Scenes; |
||
36 | using OpenSim.Services.Interfaces; |
||
37 | using PermissionMask = OpenSim.Framework.PermissionMask; |
||
38 | |||
39 | namespace OpenSim.Region.CoreModules.Agent.AssetTransaction |
||
40 | { |
||
41 | public class AssetXferUploader |
||
42 | { |
||
43 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
||
44 | |||
45 | /// <summary> |
||
46 | /// Upload state. |
||
47 | /// </summary> |
||
48 | /// <remarks> |
||
49 | /// New -> Uploading -> Complete |
||
50 | /// </remarks> |
||
51 | private enum UploadState |
||
52 | { |
||
53 | New, |
||
54 | Uploading, |
||
55 | Complete |
||
56 | } |
||
57 | |||
58 | /// <summary> |
||
59 | /// Reference to the object that holds this uploader. Used to remove ourselves from it's list if we |
||
60 | /// are performing a delayed update. |
||
61 | /// </summary> |
||
62 | AgentAssetTransactions m_transactions; |
||
63 | |||
64 | private UploadState m_uploadState = UploadState.New; |
||
65 | |||
66 | private AssetBase m_asset; |
||
67 | private UUID InventFolder = UUID.Zero; |
||
68 | private sbyte invType = 0; |
||
69 | |||
70 | private bool m_createItem; |
||
71 | private uint m_createItemCallback; |
||
72 | |||
73 | private bool m_updateItem; |
||
74 | private InventoryItemBase m_updateItemData; |
||
75 | |||
76 | private bool m_updateTaskItem; |
||
77 | private TaskInventoryItem m_updateTaskItemData; |
||
78 | |||
79 | private string m_description = String.Empty; |
||
80 | private bool m_dumpAssetToFile; |
||
81 | private string m_name = String.Empty; |
||
82 | // private bool m_storeLocal; |
||
83 | private uint nextPerm = 0; |
||
84 | private IClientAPI ourClient; |
||
85 | |||
86 | private UUID m_transactionID; |
||
87 | |||
88 | private sbyte type = 0; |
||
89 | private byte wearableType = 0; |
||
90 | public ulong XferID; |
||
91 | private Scene m_Scene; |
||
92 | |||
93 | /// <summary> |
||
94 | /// AssetXferUploader constructor |
||
95 | /// </summary> |
||
96 | /// <param name='transactions'>/param> |
||
97 | /// <param name='scene'></param> |
||
98 | /// <param name='transactionID'></param> |
||
99 | /// <param name='dumpAssetToFile'> |
||
100 | /// If true then when the asset is uploaded it is dumped to a file with the format |
||
101 | /// String.Format("{6}_{7}_{0:d2}{1:d2}{2:d2}_{3:d2}{4:d2}{5:d2}.dat", |
||
102 | /// now.Year, now.Month, now.Day, now.Hour, now.Minute, |
||
103 | /// now.Second, m_asset.Name, m_asset.Type); |
||
104 | /// for debugging purposes. |
||
105 | /// </param> |
||
106 | public AssetXferUploader( |
||
107 | AgentAssetTransactions transactions, Scene scene, UUID transactionID, bool dumpAssetToFile) |
||
108 | { |
||
109 | m_asset = new AssetBase(); |
||
110 | |||
111 | m_transactions = transactions; |
||
112 | m_transactionID = transactionID; |
||
113 | m_Scene = scene; |
||
114 | m_dumpAssetToFile = dumpAssetToFile; |
||
115 | } |
||
116 | |||
117 | /// <summary> |
||
118 | /// Process transfer data received from the client. |
||
119 | /// </summary> |
||
120 | /// <param name="xferID"></param> |
||
121 | /// <param name="packetID"></param> |
||
122 | /// <param name="data"></param> |
||
123 | /// <returns>True if the transfer is complete, false otherwise or if the xferID was not valid</returns> |
||
124 | public bool HandleXferPacket(ulong xferID, uint packetID, byte[] data) |
||
125 | { |
||
126 | // m_log.DebugFormat( |
||
127 | // "[ASSET XFER UPLOADER]: Received packet {0} for xfer {1} (data length {2})", |
||
128 | // packetID, xferID, data.Length); |
||
129 | |||
130 | if (XferID == xferID) |
||
131 | { |
||
132 | if (m_asset.Data.Length > 1) |
||
133 | { |
||
134 | byte[] destinationArray = new byte[m_asset.Data.Length + data.Length]; |
||
135 | Array.Copy(m_asset.Data, 0, destinationArray, 0, m_asset.Data.Length); |
||
136 | Array.Copy(data, 0, destinationArray, m_asset.Data.Length, data.Length); |
||
137 | m_asset.Data = destinationArray; |
||
138 | } |
||
139 | else |
||
140 | { |
||
141 | byte[] buffer2 = new byte[data.Length - 4]; |
||
142 | Array.Copy(data, 4, buffer2, 0, data.Length - 4); |
||
143 | m_asset.Data = buffer2; |
||
144 | } |
||
145 | |||
146 | ourClient.SendConfirmXfer(xferID, packetID); |
||
147 | |||
148 | if ((packetID & 0x80000000) != 0) |
||
149 | { |
||
150 | SendCompleteMessage(); |
||
151 | return true; |
||
152 | } |
||
153 | } |
||
154 | |||
155 | return false; |
||
156 | } |
||
157 | |||
158 | /// <summary> |
||
159 | /// Start asset transfer from the client |
||
160 | /// </summary> |
||
161 | /// <param name="remoteClient"></param> |
||
162 | /// <param name="assetID"></param> |
||
163 | /// <param name="transaction"></param> |
||
164 | /// <param name="type"></param> |
||
165 | /// <param name="data"> |
||
166 | /// Optional data. If present then the asset is created immediately with this data |
||
167 | /// rather than requesting an upload from the client. The data must be longer than 2 bytes. |
||
168 | /// </param> |
||
169 | /// <param name="storeLocal"></param> |
||
170 | /// <param name="tempFile"></param> |
||
171 | public void StartUpload( |
||
172 | IClientAPI remoteClient, UUID assetID, UUID transaction, sbyte type, byte[] data, bool storeLocal, |
||
173 | bool tempFile) |
||
174 | { |
||
175 | // m_log.DebugFormat( |
||
176 | // "[ASSET XFER UPLOADER]: Initialised xfer from {0}, asset {1}, transaction {2}, type {3}, storeLocal {4}, tempFile {5}, already received data length {6}", |
||
177 | // remoteClient.Name, assetID, transaction, type, storeLocal, tempFile, data.Length); |
||
178 | |||
179 | lock (this) |
||
180 | { |
||
181 | if (m_uploadState != UploadState.New) |
||
182 | { |
||
183 | m_log.WarnFormat( |
||
184 | "[ASSET XFER UPLOADER]: Tried to start upload of asset {0}, transaction {1} for {2} but this is already in state {3}. Aborting.", |
||
185 | assetID, transaction, remoteClient.Name, m_uploadState); |
||
186 | |||
187 | return; |
||
188 | } |
||
189 | |||
190 | m_uploadState = UploadState.Uploading; |
||
191 | } |
||
192 | |||
193 | ourClient = remoteClient; |
||
194 | |||
195 | m_asset.FullID = assetID; |
||
196 | m_asset.Type = type; |
||
197 | m_asset.CreatorID = remoteClient.AgentId.ToString(); |
||
198 | m_asset.Data = data; |
||
199 | m_asset.Local = storeLocal; |
||
200 | m_asset.Temporary = tempFile; |
||
201 | |||
202 | // m_storeLocal = storeLocal; |
||
203 | |||
204 | if (m_asset.Data.Length > 2) |
||
205 | { |
||
206 | SendCompleteMessage(); |
||
207 | } |
||
208 | else |
||
209 | { |
||
210 | RequestStartXfer(); |
||
211 | } |
||
212 | } |
||
213 | |||
214 | protected void RequestStartXfer() |
||
215 | { |
||
216 | XferID = Util.GetNextXferID(); |
||
217 | |||
218 | // m_log.DebugFormat( |
||
219 | // "[ASSET XFER UPLOADER]: Requesting Xfer of asset {0}, type {1}, transfer id {2} from {3}", |
||
220 | // m_asset.FullID, m_asset.Type, XferID, ourClient.Name); |
||
221 | |||
222 | ourClient.SendXferRequest(XferID, m_asset.Type, m_asset.FullID, 0, new byte[0]); |
||
223 | } |
||
224 | |||
225 | protected void SendCompleteMessage() |
||
226 | { |
||
227 | // We must lock in order to avoid a race with a separate thread dealing with an inventory item or create |
||
228 | // message from other client UDP. |
||
229 | lock (this) |
||
230 | { |
||
231 | m_uploadState = UploadState.Complete; |
||
232 | |||
233 | ourClient.SendAssetUploadCompleteMessage(m_asset.Type, true, m_asset.FullID); |
||
234 | |||
235 | if (m_createItem) |
||
236 | { |
||
237 | CompleteCreateItem(m_createItemCallback); |
||
238 | } |
||
239 | else if (m_updateItem) |
||
240 | { |
||
241 | CompleteItemUpdate(m_updateItemData); |
||
242 | } |
||
243 | else if (m_updateTaskItem) |
||
244 | { |
||
245 | CompleteTaskItemUpdate(m_updateTaskItemData); |
||
246 | } |
||
247 | // else if (m_storeLocal) |
||
248 | // { |
||
249 | // m_Scene.AssetService.Store(m_asset); |
||
250 | // } |
||
251 | } |
||
252 | |||
253 | m_log.DebugFormat( |
||
254 | "[ASSET XFER UPLOADER]: Uploaded asset {0} for transaction {1}", |
||
255 | m_asset.FullID, m_transactionID); |
||
256 | |||
257 | if (m_dumpAssetToFile) |
||
258 | { |
||
259 | DateTime now = DateTime.Now; |
||
260 | string filename = |
||
261 | String.Format("{6}_{7}_{0:d2}{1:d2}{2:d2}_{3:d2}{4:d2}{5:d2}.dat", |
||
262 | now.Year, now.Month, now.Day, now.Hour, now.Minute, |
||
263 | now.Second, m_asset.Name, m_asset.Type); |
||
264 | SaveAssetToFile(filename, m_asset.Data); |
||
265 | } |
||
266 | } |
||
267 | |||
268 | private void SaveAssetToFile(string filename, byte[] data) |
||
269 | { |
||
270 | string assetPath = "UserAssets"; |
||
271 | if (!Directory.Exists(assetPath)) |
||
272 | { |
||
273 | Directory.CreateDirectory(assetPath); |
||
274 | } |
||
275 | FileStream fs = File.Create(Path.Combine(assetPath, filename)); |
||
276 | BinaryWriter bw = new BinaryWriter(fs); |
||
277 | bw.Write(data); |
||
278 | bw.Close(); |
||
279 | fs.Close(); |
||
280 | } |
||
281 | |||
282 | public void RequestCreateInventoryItem(IClientAPI remoteClient, |
||
283 | UUID folderID, uint callbackID, |
||
284 | string description, string name, sbyte invType, |
||
285 | sbyte type, byte wearableType, uint nextOwnerMask) |
||
286 | { |
||
287 | InventFolder = folderID; |
||
288 | m_name = name; |
||
289 | m_description = description; |
||
290 | this.type = type; |
||
291 | this.invType = invType; |
||
292 | this.wearableType = wearableType; |
||
293 | nextPerm = nextOwnerMask; |
||
294 | m_asset.Name = name; |
||
295 | m_asset.Description = description; |
||
296 | m_asset.Type = type; |
||
297 | |||
298 | // We must lock to avoid a race with a separate thread uploading the asset. |
||
299 | lock (this) |
||
300 | { |
||
301 | if (m_uploadState == UploadState.Complete) |
||
302 | { |
||
303 | CompleteCreateItem(callbackID); |
||
304 | } |
||
305 | else |
||
306 | { |
||
307 | m_createItem = true; //set flag so the inventory item is created when upload is complete |
||
308 | m_createItemCallback = callbackID; |
||
309 | } |
||
310 | } |
||
311 | } |
||
312 | |||
313 | public void RequestUpdateInventoryItem(IClientAPI remoteClient, InventoryItemBase item) |
||
314 | { |
||
315 | // We must lock to avoid a race with a separate thread uploading the asset. |
||
316 | lock (this) |
||
317 | { |
||
318 | m_asset.Name = item.Name; |
||
319 | m_asset.Description = item.Description; |
||
320 | m_asset.Type = (sbyte)item.AssetType; |
||
321 | |||
322 | // We must always store the item at this point even if the asset hasn't finished uploading, in order |
||
323 | // to avoid a race condition when the appearance module retrieves the item to set the asset id in |
||
324 | // the AvatarAppearance structure. |
||
325 | item.AssetID = m_asset.FullID; |
||
326 | if (item.AssetID != UUID.Zero) |
||
327 | m_Scene.InventoryService.UpdateItem(item); |
||
328 | |||
329 | if (m_uploadState == UploadState.Complete) |
||
330 | { |
||
331 | CompleteItemUpdate(item); |
||
332 | } |
||
333 | else |
||
334 | { |
||
335 | // m_log.DebugFormat( |
||
336 | // "[ASSET XFER UPLOADER]: Holding update inventory item request {0} for {1} pending completion of asset xfer for transaction {2}", |
||
337 | // item.Name, remoteClient.Name, transactionID); |
||
338 | |||
339 | m_updateItem = true; |
||
340 | m_updateItemData = item; |
||
341 | } |
||
342 | } |
||
343 | } |
||
344 | |||
345 | public void RequestUpdateTaskInventoryItem(IClientAPI remoteClient, TaskInventoryItem taskItem) |
||
346 | { |
||
347 | // We must lock to avoid a race with a separate thread uploading the asset. |
||
348 | lock (this) |
||
349 | { |
||
350 | m_asset.Name = taskItem.Name; |
||
351 | m_asset.Description = taskItem.Description; |
||
352 | m_asset.Type = (sbyte)taskItem.Type; |
||
353 | taskItem.AssetID = m_asset.FullID; |
||
354 | |||
355 | if (m_uploadState == UploadState.Complete) |
||
356 | { |
||
357 | CompleteTaskItemUpdate(taskItem); |
||
358 | } |
||
359 | else |
||
360 | { |
||
361 | m_updateTaskItem = true; |
||
362 | m_updateTaskItemData = taskItem; |
||
363 | } |
||
364 | } |
||
365 | } |
||
366 | |||
367 | /// <summary> |
||
368 | /// Store the asset for the given item when it has been uploaded. |
||
369 | /// </summary> |
||
370 | /// <param name="item"></param> |
||
371 | private void CompleteItemUpdate(InventoryItemBase item) |
||
372 | { |
||
373 | // m_log.DebugFormat( |
||
374 | // "[ASSET XFER UPLOADER]: Storing asset {0} for earlier item update for {1} for {2}", |
||
375 | // m_asset.FullID, item.Name, ourClient.Name); |
||
376 | |||
377 | m_Scene.AssetService.Store(m_asset); |
||
378 | |||
379 | m_transactions.RemoveXferUploader(m_transactionID); |
||
380 | |||
381 | m_Scene.EventManager.TriggerOnNewInventoryItemUploadComplete(ourClient.AgentId, (AssetType)type, m_asset.FullID, m_asset.Name, 0); |
||
382 | } |
||
383 | |||
384 | /// <summary> |
||
385 | /// Store the asset for the given task item when it has been uploaded. |
||
386 | /// </summary> |
||
387 | /// <param name="taskItem"></param> |
||
388 | private void CompleteTaskItemUpdate(TaskInventoryItem taskItem) |
||
389 | { |
||
390 | // m_log.DebugFormat( |
||
391 | // "[ASSET XFER UPLOADER]: Storing asset {0} for earlier task item update for {1} for {2}", |
||
392 | // m_asset.FullID, taskItem.Name, ourClient.Name); |
||
393 | |||
394 | m_Scene.AssetService.Store(m_asset); |
||
395 | |||
396 | m_transactions.RemoveXferUploader(m_transactionID); |
||
397 | } |
||
398 | |||
399 | private void CompleteCreateItem(uint callbackID) |
||
400 | { |
||
401 | m_Scene.AssetService.Store(m_asset); |
||
402 | |||
403 | InventoryItemBase item = new InventoryItemBase(); |
||
404 | item.Owner = ourClient.AgentId; |
||
405 | item.CreatorId = ourClient.AgentId.ToString(); |
||
406 | item.ID = UUID.Random(); |
||
407 | item.AssetID = m_asset.FullID; |
||
408 | item.Description = m_description; |
||
409 | item.Name = m_name; |
||
410 | item.AssetType = type; |
||
411 | item.InvType = invType; |
||
412 | item.Folder = InventFolder; |
||
413 | item.BasePermissions = (uint)(PermissionMask.All | PermissionMask.Export); |
||
414 | item.CurrentPermissions = item.BasePermissions; |
||
415 | item.GroupPermissions=0; |
||
416 | item.EveryOnePermissions=0; |
||
417 | item.NextPermissions = nextPerm; |
||
418 | item.Flags = (uint) wearableType; |
||
419 | item.CreationDate = Util.UnixTimeSinceEpoch(); |
||
420 | |||
421 | if (m_Scene.AddInventoryItem(item)) |
||
422 | ourClient.SendInventoryItemCreateUpdate(item, callbackID); |
||
423 | else |
||
424 | ourClient.SendAlertMessage("Unable to create inventory item"); |
||
425 | |||
426 | m_transactions.RemoveXferUploader(m_transactionID); |
||
427 | } |
||
428 | |||
429 | } |
||
430 | } |