diff options
author | Tleiades Hax | 2007-10-26 11:46:27 +0000 |
---|---|---|
committer | Tleiades Hax | 2007-10-26 11:46:27 +0000 |
commit | 5e7dba726896fcb84882b53952651742926e6efb (patch) | |
tree | a396337e721912fd954e8a66e26d16c375d7bab4 /OpenSim/Grid/AssetServer | |
parent | add my set-eol-style script. Can be run on Linux (diff) | |
download | opensim-SC-5e7dba726896fcb84882b53952651742926e6efb.zip opensim-SC-5e7dba726896fcb84882b53952651742926e6efb.tar.gz opensim-SC-5e7dba726896fcb84882b53952651742926e6efb.tar.bz2 opensim-SC-5e7dba726896fcb84882b53952651742926e6efb.tar.xz |
Very early first implementation of grid based assets.
Run this on a major grid, and weep
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Grid/AssetServer/Main.cs | 170 | ||||
-rw-r--r-- | OpenSim/Grid/AssetServer/RestService.cs | 109 |
2 files changed, 217 insertions, 62 deletions
diff --git a/OpenSim/Grid/AssetServer/Main.cs b/OpenSim/Grid/AssetServer/Main.cs index 9f1f9a2..dabacd4 100644 --- a/OpenSim/Grid/AssetServer/Main.cs +++ b/OpenSim/Grid/AssetServer/Main.cs | |||
@@ -28,15 +28,18 @@ | |||
28 | 28 | ||
29 | using System; | 29 | using System; |
30 | using System.IO; | 30 | using System.IO; |
31 | using System.Reflection; | ||
31 | 32 | ||
32 | using libsecondlife; | 33 | using libsecondlife; |
34 | using Nini.Config; | ||
35 | |||
36 | using OpenSim.Framework.Types; | ||
33 | using OpenSim.Framework.Communications.Cache; | 37 | using OpenSim.Framework.Communications.Cache; |
34 | using OpenSim.Framework.Configuration; | 38 | using OpenSim.Framework.Configuration; |
35 | using OpenSim.Framework.Console; | 39 | using OpenSim.Framework.Console; |
36 | using OpenSim.Framework.Servers; | ||
37 | using OpenSim.Framework.Configuration; | ||
38 | using OpenSim.Framework.Interfaces; | 40 | using OpenSim.Framework.Interfaces; |
39 | using OpenSim.Framework.Utilities; | 41 | using OpenSim.Framework.Utilities; |
42 | using OpenSim.Framework.Servers; | ||
40 | 43 | ||
41 | /* | 44 | /* |
42 | using System.Text; | 45 | using System.Text; |
@@ -57,7 +60,7 @@ namespace OpenSim.Grid.AssetServer | |||
57 | public static OpenAsset_Main assetserver; | 60 | public static OpenAsset_Main assetserver; |
58 | 61 | ||
59 | private LogBase m_console; | 62 | private LogBase m_console; |
60 | private IAssetServer m_assetServer; | 63 | private IAssetProvider m_assetProvider; |
61 | 64 | ||
62 | [STAThread] | 65 | [STAThread] |
63 | public static void Main(string[] args) | 66 | public static void Main(string[] args) |
@@ -97,11 +100,14 @@ namespace OpenSim.Grid.AssetServer | |||
97 | m_console.Verbose("ASSET", "Setting up asset DB"); | 100 | m_console.Verbose("ASSET", "Setting up asset DB"); |
98 | setupDB(m_config); | 101 | setupDB(m_config); |
99 | 102 | ||
103 | m_console.Verbose("ASSET", "Loading default asset set.."); | ||
104 | LoadDefaultAssets(); | ||
105 | |||
100 | m_console.Verbose("ASSET", "Starting HTTP process"); | 106 | m_console.Verbose("ASSET", "Starting HTTP process"); |
101 | BaseHttpServer httpServer = new BaseHttpServer((int)m_config.HttpPort); | 107 | BaseHttpServer httpServer = new BaseHttpServer((int)m_config.HttpPort); |
102 | 108 | ||
103 | httpServer.AddStreamHandler(new GetAssetStreamHandler(this)); | 109 | httpServer.AddStreamHandler(new GetAssetStreamHandler(this, m_assetProvider)); |
104 | httpServer.AddStreamHandler(new PostAssetStreamHandler( this )); | 110 | httpServer.AddStreamHandler(new PostAssetStreamHandler(this, m_assetProvider)); |
105 | 111 | ||
106 | httpServer.Start(); | 112 | httpServer.Start(); |
107 | } | 113 | } |
@@ -111,14 +117,49 @@ namespace OpenSim.Grid.AssetServer | |||
111 | return null; | 117 | return null; |
112 | } | 118 | } |
113 | 119 | ||
120 | |||
121 | public IAssetProvider LoadDatabasePlugin(string FileName) | ||
122 | { | ||
123 | MainLog.Instance.Verbose("ASSET SERVER", "LoadDatabasePlugin: Attempting to load " + FileName); | ||
124 | Assembly pluginAssembly = Assembly.LoadFrom(FileName); | ||
125 | IAssetProvider assetPlugin = null; | ||
126 | foreach (Type pluginType in pluginAssembly.GetTypes()) | ||
127 | { | ||
128 | if (!pluginType.IsAbstract) | ||
129 | { | ||
130 | Type typeInterface = pluginType.GetInterface("IAssetProvider", true); | ||
131 | |||
132 | if (typeInterface != null) | ||
133 | { | ||
134 | IAssetProvider plug = (IAssetProvider)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString())); | ||
135 | assetPlugin = plug; | ||
136 | assetPlugin.Initialise(); | ||
137 | |||
138 | MainLog.Instance.Verbose("ASSET SERVER", "Added " + assetPlugin.Name + " " + assetPlugin.Version); | ||
139 | break; | ||
140 | } | ||
141 | |||
142 | typeInterface = null; | ||
143 | } | ||
144 | } | ||
145 | |||
146 | pluginAssembly = null; | ||
147 | return assetPlugin; | ||
148 | } | ||
149 | |||
114 | public void setupDB(AssetConfig config) | 150 | public void setupDB(AssetConfig config) |
115 | { | 151 | { |
116 | try | 152 | try |
117 | { | 153 | { |
118 | SQLAssetServer assetServer = new SQLAssetServer(config.DatabaseProvider ); | 154 | m_assetProvider = LoadDatabasePlugin(config.DatabaseProvider); |
119 | assetServer.LoadDefaultAssets(); | 155 | if (m_assetProvider == null) |
156 | { | ||
157 | MainLog.Instance.Error("ASSET", "Failed to load a database plugin, server halting"); | ||
158 | Environment.Exit(-1); | ||
159 | } | ||
160 | // assetServer.LoadDefaultAssets(); | ||
120 | 161 | ||
121 | m_assetServer = assetServer; | 162 | // m_assetServer = assetServer; |
122 | } | 163 | } |
123 | catch (Exception e) | 164 | catch (Exception e) |
124 | { | 165 | { |
@@ -127,84 +168,89 @@ namespace OpenSim.Grid.AssetServer | |||
127 | } | 168 | } |
128 | } | 169 | } |
129 | 170 | ||
130 | public void RunCmd(string cmd, string[] cmdparams) | 171 | public void LoadAsset(AssetBase info, bool image, string filename) |
131 | { | 172 | { |
132 | switch (cmd) | 173 | //should request Asset from storage manager |
133 | { | 174 | //but for now read from file |
134 | case "help": | ||
135 | m_console.Notice("shutdown - shutdown this asset server (USE CAUTION!)"); | ||
136 | break; | ||
137 | |||
138 | case "shutdown": | ||
139 | m_console.Close(); | ||
140 | Environment.Exit(0); | ||
141 | break; | ||
142 | } | ||
143 | } | ||
144 | 175 | ||
145 | public void Show(string ShowWhat) | 176 | string dataPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "assets"); //+ folder; |
146 | { | 177 | string fileName = Path.Combine(dataPath, filename); |
178 | FileInfo fInfo = new FileInfo(fileName); | ||
179 | long numBytes = fInfo.Length; | ||
180 | FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); | ||
181 | byte[] idata = new byte[numBytes]; | ||
182 | BinaryReader br = new BinaryReader(fStream); | ||
183 | idata = br.ReadBytes((int)numBytes); | ||
184 | br.Close(); | ||
185 | fStream.Close(); | ||
186 | info.Data = idata; | ||
187 | //info.loaded=true; | ||
147 | } | 188 | } |
148 | } | ||
149 | 189 | ||
150 | public class GetAssetStreamHandler : BaseStreamHandler | 190 | public AssetBase CreateAsset(string assetIdStr, string name, string filename, bool isImage) |
151 | { | ||
152 | OpenAsset_Main m_assetManager; | ||
153 | |||
154 | override public byte[] Handle(string path, Stream request) | ||
155 | { | 191 | { |
156 | string param = GetParam(path); | 192 | AssetBase asset = new AssetBase( |
193 | new LLUUID(assetIdStr), | ||
194 | name | ||
195 | ); | ||
157 | 196 | ||
158 | byte[] assetdata = m_assetManager.GetAssetData(new LLUUID(param), false); | 197 | if (!String.IsNullOrEmpty(filename)) |
159 | if (assetdata != null) | ||
160 | { | 198 | { |
161 | return assetdata; | 199 | MainLog.Instance.Verbose("ASSETS", "Loading: [{0}][{1}]", name, filename); |
200 | |||
201 | LoadAsset(asset, isImage, filename); | ||
162 | } | 202 | } |
163 | else | 203 | else |
164 | { | 204 | { |
165 | return new byte[]{}; | 205 | MainLog.Instance.Verbose("ASSETS", "Instantiated: [{0}]", name); |
166 | } | 206 | } |
167 | } | ||
168 | 207 | ||
169 | public GetAssetStreamHandler(OpenAsset_Main assetManager) : base("/assets/", "GET") | 208 | return asset; |
170 | { | ||
171 | m_assetManager = assetManager; | ||
172 | } | 209 | } |
173 | } | ||
174 | |||
175 | public class PostAssetStreamHandler : BaseStreamHandler | ||
176 | { | ||
177 | OpenAsset_Main m_assetManager; | ||
178 | 210 | ||
179 | override public byte[] Handle(string path, Stream request) | 211 | public void LoadDefaultAssets() |
180 | { | 212 | { |
181 | string param = GetParam(path); | 213 | string filePath = Path.Combine(Util.configDir(), "OpenSimAssetSet.xml"); |
182 | LLUUID assetId = new LLUUID(param); | 214 | if (File.Exists(filePath)) |
183 | byte[] txBuffer = new byte[4096]; | ||
184 | |||
185 | using( BinaryReader binReader = new BinaryReader( request ) ) | ||
186 | { | 215 | { |
187 | using (MemoryStream memoryStream = new MemoryStream(4096)) | 216 | XmlConfigSource source = new XmlConfigSource(filePath); |
217 | |||
218 | for (int i = 0; i < source.Configs.Count; i++) | ||
188 | { | 219 | { |
189 | int count; | 220 | string assetIdStr = source.Configs[i].GetString("assetID", LLUUID.Random().ToStringHyphenated()); |
190 | while ((count = binReader.Read(txBuffer, 0, 4096)) > 0) | 221 | string name = source.Configs[i].GetString("name", ""); |
191 | { | 222 | sbyte type = (sbyte)source.Configs[i].GetInt("assetType", 0); |
192 | memoryStream.Write(txBuffer, 0, count); | 223 | sbyte invType = (sbyte)source.Configs[i].GetInt("inventoryType", 0); |
193 | } | 224 | string fileName = source.Configs[i].GetString("fileName", ""); |
225 | |||
226 | AssetBase newAsset = CreateAsset(assetIdStr, name, fileName, false); | ||
194 | 227 | ||
195 | byte[] assetData = memoryStream.ToArray(); | 228 | newAsset.Type = type; |
229 | newAsset.InvType = invType; | ||
196 | 230 | ||
197 | // m_assetManager.CreateAsset(assetId, assetData); | 231 | m_assetProvider.CreateAsset(newAsset); |
198 | } | 232 | } |
199 | } | 233 | } |
200 | |||
201 | return new byte[]{}; | ||
202 | } | 234 | } |
203 | 235 | ||
204 | public PostAssetStreamHandler( OpenAsset_Main assetManager ) | 236 | |
205 | : base("/assets/", "POST") | 237 | public void RunCmd(string cmd, string[] cmdparams) |
238 | { | ||
239 | switch (cmd) | ||
240 | { | ||
241 | case "help": | ||
242 | m_console.Notice("shutdown - shutdown this asset server (USE CAUTION!)"); | ||
243 | break; | ||
244 | |||
245 | case "shutdown": | ||
246 | m_console.Close(); | ||
247 | Environment.Exit(0); | ||
248 | break; | ||
249 | } | ||
250 | } | ||
251 | |||
252 | public void Show(string ShowWhat) | ||
206 | { | 253 | { |
207 | m_assetManager = assetManager; | ||
208 | } | 254 | } |
209 | } | 255 | } |
210 | } | 256 | } |
diff --git a/OpenSim/Grid/AssetServer/RestService.cs b/OpenSim/Grid/AssetServer/RestService.cs new file mode 100644 index 0000000..88a1668 --- /dev/null +++ b/OpenSim/Grid/AssetServer/RestService.cs | |||
@@ -0,0 +1,109 @@ | |||
1 | using System; | ||
2 | using System.IO; | ||
3 | using System.Xml; | ||
4 | using System.Xml.Serialization; | ||
5 | using System.Text; | ||
6 | |||
7 | using libsecondlife; | ||
8 | using OpenSim.Framework.Types; | ||
9 | using OpenSim.Framework.Servers; | ||
10 | using OpenSim.Framework.Interfaces; | ||
11 | using OpenSim.Framework.Console; | ||
12 | |||
13 | namespace OpenSim.Grid.AssetServer | ||
14 | { | ||
15 | public class GetAssetStreamHandler : BaseStreamHandler | ||
16 | { | ||
17 | OpenAsset_Main m_assetManager; | ||
18 | IAssetProvider m_assetProvider; | ||
19 | |||
20 | override public byte[] Handle(string path, Stream request) | ||
21 | { | ||
22 | string param = GetParam(path); | ||
23 | byte[] result = new byte[] { }; | ||
24 | try { | ||
25 | |||
26 | string[] p = param.Split(new char[] { '/', '?', '&' }, StringSplitOptions.RemoveEmptyEntries); | ||
27 | |||
28 | if (p.Length > 0) | ||
29 | { | ||
30 | LLUUID assetID; | ||
31 | bool isTexture = false; | ||
32 | LLUUID.TryParse(p[0], out assetID); | ||
33 | if (p.Length > 1) | ||
34 | { | ||
35 | if (string.Compare(p[1], "texture", true) == 0) | ||
36 | isTexture = true; | ||
37 | } | ||
38 | |||
39 | |||
40 | AssetBase asset = m_assetProvider.FetchAsset(assetID); | ||
41 | if (asset != null) | ||
42 | { | ||
43 | MainLog.Instance.Debug("REST", "GET:/asset found {0}, {1}", assetID, asset.Name); | ||
44 | |||
45 | XmlSerializer xs = new XmlSerializer(typeof(AssetBase)); | ||
46 | MemoryStream ms = new MemoryStream(); | ||
47 | XmlTextWriter xw = new XmlTextWriter(ms, Encoding.UTF8); | ||
48 | xw.Formatting = Formatting.Indented; | ||
49 | xs.Serialize(xw, asset); | ||
50 | xw.Flush(); | ||
51 | |||
52 | ms.Seek(0, SeekOrigin.Begin); | ||
53 | StreamReader sr = new StreamReader(ms); | ||
54 | |||
55 | result = ms.GetBuffer(); | ||
56 | Array.Resize<byte>(ref result, (int)ms.Length); | ||
57 | } | ||
58 | else | ||
59 | { | ||
60 | MainLog.Instance.Verbose("REST", "GET:/asset failed to find {0}", assetID); | ||
61 | } | ||
62 | } | ||
63 | } | ||
64 | catch (Exception e) | ||
65 | { | ||
66 | MainLog.Instance.Error(e.ToString()); | ||
67 | } | ||
68 | return result; | ||
69 | } | ||
70 | |||
71 | public GetAssetStreamHandler(OpenAsset_Main assetManager, IAssetProvider assetProvider) | ||
72 | : base("GET", "/assets" ) | ||
73 | { | ||
74 | m_assetManager = assetManager; | ||
75 | m_assetProvider = assetProvider; | ||
76 | } | ||
77 | } | ||
78 | |||
79 | public class PostAssetStreamHandler : BaseStreamHandler | ||
80 | { | ||
81 | OpenAsset_Main m_assetManager; | ||
82 | IAssetProvider m_assetProvider; | ||
83 | |||
84 | override public byte[] Handle(string path, Stream request) | ||
85 | { | ||
86 | string param = GetParam(path); | ||
87 | |||
88 | LLUUID assetId; | ||
89 | if(param.Length > 0) | ||
90 | LLUUID.TryParse(param, out assetId); | ||
91 | byte[] txBuffer = new byte[4096]; | ||
92 | |||
93 | XmlSerializer xs = new XmlSerializer(typeof(AssetBase)); | ||
94 | AssetBase asset = (AssetBase)xs.Deserialize(request); | ||
95 | |||
96 | MainLog.Instance.Verbose("REST", "StoreAndCommitAsset {0}", asset.FullID); | ||
97 | m_assetProvider.CreateAsset(asset); | ||
98 | |||
99 | return new byte[] { }; | ||
100 | } | ||
101 | |||
102 | public PostAssetStreamHandler(OpenAsset_Main assetManager, IAssetProvider assetProvider) | ||
103 | : base("POST", "/assets") | ||
104 | { | ||
105 | m_assetManager = assetManager; | ||
106 | m_assetProvider = assetProvider; | ||
107 | } | ||
108 | } | ||
109 | } | ||