diff options
Diffstat (limited to 'OpenSim/Region/CoreModules/Framework/InventoryAccess/HGAssetMapper.cs')
-rw-r--r-- | OpenSim/Region/CoreModules/Framework/InventoryAccess/HGAssetMapper.cs | 200 |
1 files changed, 200 insertions, 0 deletions
diff --git a/OpenSim/Region/CoreModules/Framework/InventoryAccess/HGAssetMapper.cs b/OpenSim/Region/CoreModules/Framework/InventoryAccess/HGAssetMapper.cs new file mode 100644 index 0000000..664f38d --- /dev/null +++ b/OpenSim/Region/CoreModules/Framework/InventoryAccess/HGAssetMapper.cs | |||
@@ -0,0 +1,200 @@ | |||
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.Collections.Generic; | ||
30 | using System.Reflection; | ||
31 | using System.Threading; | ||
32 | using log4net; | ||
33 | using OpenMetaverse; | ||
34 | using OpenSim.Framework; | ||
35 | |||
36 | using OpenSim.Region.Framework.Scenes; | ||
37 | using OpenSim.Region.Framework.Scenes.Serialization; | ||
38 | using OpenSim.Region.Framework.Interfaces; | ||
39 | using OpenSim.Services.Interfaces; | ||
40 | |||
41 | //using HyperGrid.Framework; | ||
42 | //using OpenSim.Region.Communications.Hypergrid; | ||
43 | |||
44 | namespace OpenSim.Region.CoreModules.Framework.InventoryAccess | ||
45 | { | ||
46 | public class HGAssetMapper | ||
47 | { | ||
48 | #region Fields | ||
49 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
50 | |||
51 | // This maps between inventory server urls and inventory server clients | ||
52 | // private Dictionary<string, InventoryClient> m_inventoryServers = new Dictionary<string, InventoryClient>(); | ||
53 | |||
54 | private Scene m_scene; | ||
55 | |||
56 | #endregion | ||
57 | |||
58 | #region Constructor | ||
59 | |||
60 | public HGAssetMapper(Scene scene) | ||
61 | { | ||
62 | m_scene = scene; | ||
63 | } | ||
64 | |||
65 | #endregion | ||
66 | |||
67 | #region Internal functions | ||
68 | |||
69 | public AssetBase FetchAsset(string url, UUID assetID) | ||
70 | { | ||
71 | AssetBase asset = m_scene.AssetService.Get(url + "/" + assetID.ToString()); | ||
72 | |||
73 | if (asset != null) | ||
74 | { | ||
75 | m_log.DebugFormat("[HG ASSET MAPPER]: Copied asset {0} from {1} to local asset server. ", asset.ID, url); | ||
76 | return asset; | ||
77 | } | ||
78 | return null; | ||
79 | } | ||
80 | |||
81 | public bool PostAsset(string url, AssetBase asset) | ||
82 | { | ||
83 | if (asset != null) | ||
84 | { | ||
85 | // See long comment in AssetCache.AddAsset | ||
86 | if (!asset.Temporary || asset.Local) | ||
87 | { | ||
88 | // We need to copy the asset into a new asset, because | ||
89 | // we need to set its ID to be URL+UUID, so that the | ||
90 | // HGAssetService dispatches it to the remote grid. | ||
91 | // It's not pretty, but the best that can be done while | ||
92 | // not having a global naming infrastructure | ||
93 | AssetBase asset1 = new AssetBase(asset.FullID, asset.Name, asset.Type, asset.Metadata.CreatorID); | ||
94 | Copy(asset, asset1); | ||
95 | try | ||
96 | { | ||
97 | asset1.ID = url + "/" + asset.ID; | ||
98 | } | ||
99 | catch | ||
100 | { | ||
101 | m_log.Warn("[HG ASSET MAPPER]: Oops."); | ||
102 | } | ||
103 | |||
104 | m_scene.AssetService.Store(asset1); | ||
105 | m_log.DebugFormat("[HG ASSET MAPPER]: Posted copy of asset {0} from local asset server to {1}", asset1.ID, url); | ||
106 | } | ||
107 | return true; | ||
108 | } | ||
109 | else | ||
110 | m_log.Warn("[HG ASSET MAPPER]: Tried to post asset to remote server, but asset not in local cache."); | ||
111 | |||
112 | return false; | ||
113 | } | ||
114 | |||
115 | private void Copy(AssetBase from, AssetBase to) | ||
116 | { | ||
117 | to.Data = from.Data; | ||
118 | to.Description = from.Description; | ||
119 | to.FullID = from.FullID; | ||
120 | to.ID = from.ID; | ||
121 | to.Local = from.Local; | ||
122 | to.Name = from.Name; | ||
123 | to.Temporary = from.Temporary; | ||
124 | to.Type = from.Type; | ||
125 | |||
126 | } | ||
127 | |||
128 | // TODO: unused | ||
129 | // private void Dump(Dictionary<UUID, bool> lst) | ||
130 | // { | ||
131 | // m_log.Debug("XXX -------- UUID DUMP ------- XXX"); | ||
132 | // foreach (KeyValuePair<UUID, bool> kvp in lst) | ||
133 | // m_log.Debug(" >> " + kvp.Key + " (texture? " + kvp.Value + ")"); | ||
134 | // m_log.Debug("XXX -------- UUID DUMP ------- XXX"); | ||
135 | // } | ||
136 | |||
137 | #endregion | ||
138 | |||
139 | |||
140 | #region Public interface | ||
141 | |||
142 | public void Get(UUID assetID, UUID ownerID, string userAssetURL) | ||
143 | { | ||
144 | // Get the item from the remote asset server onto the local AssetCache | ||
145 | // and place an entry in m_assetMap | ||
146 | |||
147 | m_log.Debug("[HG ASSET MAPPER]: Fetching object " + assetID + " from asset server " + userAssetURL); | ||
148 | AssetBase asset = FetchAsset(userAssetURL, assetID); | ||
149 | |||
150 | if (asset != null) | ||
151 | { | ||
152 | // OK, now fetch the inside. | ||
153 | Dictionary<UUID, int> ids = new Dictionary<UUID, int>(); | ||
154 | HGUuidGatherer uuidGatherer = new HGUuidGatherer(this, m_scene.AssetService, userAssetURL); | ||
155 | uuidGatherer.GatherAssetUuids(asset.FullID, (AssetType)asset.Type, ids); | ||
156 | foreach (UUID uuid in ids.Keys) | ||
157 | FetchAsset(userAssetURL, uuid); | ||
158 | |||
159 | m_log.DebugFormat("[HG ASSET MAPPER]: Successfully fetched asset {0} from asset server {1}", asset.ID, userAssetURL); | ||
160 | |||
161 | } | ||
162 | else | ||
163 | m_log.Warn("[HG ASSET MAPPER]: Could not fetch asset from remote asset server " + userAssetURL); | ||
164 | } | ||
165 | |||
166 | |||
167 | public void Post(UUID assetID, UUID ownerID, string userAssetURL) | ||
168 | { | ||
169 | // Post the item from the local AssetCache onto the remote asset server | ||
170 | // and place an entry in m_assetMap | ||
171 | |||
172 | m_log.Debug("[HG ASSET MAPPER]: Posting object " + assetID + " to asset server " + userAssetURL); | ||
173 | AssetBase asset = m_scene.AssetService.Get(assetID.ToString()); | ||
174 | if (asset != null) | ||
175 | { | ||
176 | Dictionary<UUID, int> ids = new Dictionary<UUID, int>(); | ||
177 | HGUuidGatherer uuidGatherer = new HGUuidGatherer(this, m_scene.AssetService, string.Empty); | ||
178 | uuidGatherer.GatherAssetUuids(asset.FullID, (AssetType)asset.Type, ids); | ||
179 | foreach (UUID uuid in ids.Keys) | ||
180 | { | ||
181 | asset = m_scene.AssetService.Get(uuid.ToString()); | ||
182 | if (asset == null) | ||
183 | m_log.DebugFormat("[HG ASSET MAPPER]: Could not find asset {0}", uuid); | ||
184 | else | ||
185 | PostAsset(userAssetURL, asset); | ||
186 | } | ||
187 | |||
188 | // maybe all pieces got there... | ||
189 | m_log.DebugFormat("[HG ASSET MAPPER]: Successfully posted item {0} to asset server {1}", assetID, userAssetURL); | ||
190 | |||
191 | } | ||
192 | else | ||
193 | m_log.DebugFormat("[HG ASSET MAPPER]: Something wrong with asset {0}, it could not be found", assetID); | ||
194 | |||
195 | } | ||
196 | |||
197 | #endregion | ||
198 | |||
199 | } | ||
200 | } | ||