diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim.GridInterfaces/Local/LocalAssetServer.cs | 208 |
1 files changed, 208 insertions, 0 deletions
diff --git a/OpenSim.GridInterfaces/Local/LocalAssetServer.cs b/OpenSim.GridInterfaces/Local/LocalAssetServer.cs new file mode 100644 index 0000000..6cd954a --- /dev/null +++ b/OpenSim.GridInterfaces/Local/LocalAssetServer.cs | |||
@@ -0,0 +1,208 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.Text; | ||
4 | using System.Threading; | ||
5 | using System.IO; | ||
6 | using OpenSim.Framework.Interfaces; | ||
7 | using OpenSim.Framework.Assets; | ||
8 | using OpenSim.Framework.Utilities; | ||
9 | using libsecondlife; | ||
10 | using Db4objects.Db4o; | ||
11 | using Db4objects.Db4o.Query; | ||
12 | |||
13 | namespace OpenSim.GridInterfaces.Local | ||
14 | { | ||
15 | public class LocalAssetPlugin : IAssetPlugin | ||
16 | { | ||
17 | public LocalAssetPlugin() | ||
18 | { | ||
19 | |||
20 | } | ||
21 | |||
22 | public IAssetServer GetAssetServer() | ||
23 | { | ||
24 | return (new LocalAssetServer()); | ||
25 | } | ||
26 | } | ||
27 | |||
28 | public class LocalAssetServer : IAssetServer | ||
29 | { | ||
30 | private IAssetReceiver _receiver; | ||
31 | private BlockingQueue<ARequest> _assetRequests; | ||
32 | private IObjectContainer db; | ||
33 | private Thread _localAssetServerThread; | ||
34 | |||
35 | public LocalAssetServer() | ||
36 | { | ||
37 | bool yapfile; | ||
38 | this._assetRequests = new BlockingQueue<ARequest>(); | ||
39 | yapfile = System.IO.File.Exists("assets.yap"); | ||
40 | |||
41 | OpenSim.Framework.Console.MainConsole.Instance.WriteLine("Local Asset Server class created"); | ||
42 | try | ||
43 | { | ||
44 | db = Db4oFactory.OpenFile("assets.yap"); | ||
45 | OpenSim.Framework.Console.MainConsole.Instance.WriteLine("Db4 Asset database creation"); | ||
46 | } | ||
47 | catch (Exception e) | ||
48 | { | ||
49 | db.Close(); | ||
50 | OpenSim.Framework.Console.MainConsole.Instance.WriteLine("Db4 Asset server :Constructor - Exception occured"); | ||
51 | OpenSim.Framework.Console.MainConsole.Instance.WriteLine(e.ToString()); | ||
52 | } | ||
53 | if (!yapfile) | ||
54 | { | ||
55 | this.SetUpAssetDatabase(); | ||
56 | } | ||
57 | this._localAssetServerThread = new Thread(new ThreadStart(RunRequests)); | ||
58 | this._localAssetServerThread.IsBackground = true; | ||
59 | this._localAssetServerThread.Start(); | ||
60 | |||
61 | } | ||
62 | |||
63 | public void SetReceiver(IAssetReceiver receiver) | ||
64 | { | ||
65 | this._receiver = receiver; | ||
66 | } | ||
67 | |||
68 | public void RequestAsset(LLUUID assetID, bool isTexture) | ||
69 | { | ||
70 | ARequest req = new ARequest(); | ||
71 | req.AssetID = assetID; | ||
72 | req.IsTexture = isTexture; | ||
73 | this._assetRequests.Enqueue(req); | ||
74 | } | ||
75 | |||
76 | public void UpdateAsset(AssetBase asset) | ||
77 | { | ||
78 | |||
79 | } | ||
80 | |||
81 | public void UploadNewAsset(AssetBase asset) | ||
82 | { | ||
83 | AssetStorage store = new AssetStorage(); | ||
84 | store.Data = asset.Data; | ||
85 | store.Name = asset.Name; | ||
86 | store.UUID = asset.FullID; | ||
87 | db.Set(store); | ||
88 | db.Commit(); | ||
89 | } | ||
90 | |||
91 | public void SetServerInfo(string ServerUrl, string ServerKey) | ||
92 | { | ||
93 | |||
94 | } | ||
95 | public void Close() | ||
96 | { | ||
97 | if (db != null) | ||
98 | { | ||
99 | Console.WriteLine("Closing local Asset server database"); | ||
100 | db.Close(); | ||
101 | } | ||
102 | } | ||
103 | |||
104 | private void RunRequests() | ||
105 | { | ||
106 | while (true) | ||
107 | { | ||
108 | byte[] idata = null; | ||
109 | bool found = false; | ||
110 | AssetStorage foundAsset = null; | ||
111 | ARequest req = this._assetRequests.Dequeue(); | ||
112 | IObjectSet result = db.Query(new AssetUUIDQuery(req.AssetID)); | ||
113 | if (result.Count > 0) | ||
114 | { | ||
115 | foundAsset = (AssetStorage)result.Next(); | ||
116 | found = true; | ||
117 | } | ||
118 | |||
119 | AssetBase asset = new AssetBase(); | ||
120 | if (found) | ||
121 | { | ||
122 | asset.FullID = foundAsset.UUID; | ||
123 | asset.Type = foundAsset.Type; | ||
124 | asset.InvType = foundAsset.Type; | ||
125 | asset.Name = foundAsset.Name; | ||
126 | idata = foundAsset.Data; | ||
127 | } | ||
128 | else | ||
129 | { | ||
130 | asset.FullID = LLUUID.Zero; | ||
131 | } | ||
132 | asset.Data = idata; | ||
133 | _receiver.AssetReceived(asset, req.IsTexture); | ||
134 | } | ||
135 | |||
136 | } | ||
137 | |||
138 | private void SetUpAssetDatabase() | ||
139 | { | ||
140 | Console.WriteLine("setting up Asset database"); | ||
141 | |||
142 | AssetBase Image = new AssetBase(); | ||
143 | Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000001"); | ||
144 | Image.Name = "test Texture"; | ||
145 | this.LoadAsset(Image, true, "testpic2.jp2"); | ||
146 | AssetStorage store = new AssetStorage(); | ||
147 | store.Data = Image.Data; | ||
148 | store.Name = Image.Name; | ||
149 | store.UUID = Image.FullID; | ||
150 | db.Set(store); | ||
151 | db.Commit(); | ||
152 | |||
153 | Image = new AssetBase(); | ||
154 | Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000002"); | ||
155 | Image.Name = "test Texture2"; | ||
156 | this.LoadAsset(Image, true, "map_base.jp2"); | ||
157 | store = new AssetStorage(); | ||
158 | store.Data = Image.Data; | ||
159 | store.Name = Image.Name; | ||
160 | store.UUID = Image.FullID; | ||
161 | db.Set(store); | ||
162 | db.Commit(); | ||
163 | |||
164 | Image = new AssetBase(); | ||
165 | Image.FullID = new LLUUID("00000000-0000-0000-5005-000000000005"); | ||
166 | Image.Name = "Prim Base Texture"; | ||
167 | this.LoadAsset(Image, true, "testpic2.jp2"); | ||
168 | store = new AssetStorage(); | ||
169 | store.Data = Image.Data; | ||
170 | store.Name = Image.Name; | ||
171 | store.UUID = Image.FullID; | ||
172 | db.Set(store); | ||
173 | db.Commit(); | ||
174 | |||
175 | Image = new AssetBase(); | ||
176 | Image.FullID = new LLUUID("66c41e39-38f9-f75a-024e-585989bfab73"); | ||
177 | Image.Name = "Shape"; | ||
178 | this.LoadAsset(Image, false, "base_shape.dat"); | ||
179 | store = new AssetStorage(); | ||
180 | store.Data = Image.Data; | ||
181 | store.Name = Image.Name; | ||
182 | store.UUID = Image.FullID; | ||
183 | db.Set(store); | ||
184 | db.Commit(); | ||
185 | |||
186 | |||
187 | } | ||
188 | |||
189 | private void LoadAsset(AssetBase info, bool image, string filename) | ||
190 | { | ||
191 | //should request Asset from storage manager | ||
192 | //but for now read from file | ||
193 | |||
194 | string dataPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "assets"); //+ folder; | ||
195 | string fileName = Path.Combine(dataPath, filename); | ||
196 | FileInfo fInfo = new FileInfo(fileName); | ||
197 | long numBytes = fInfo.Length; | ||
198 | FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); | ||
199 | byte[] idata = new byte[numBytes]; | ||
200 | BinaryReader br = new BinaryReader(fStream); | ||
201 | idata = br.ReadBytes((int)numBytes); | ||
202 | br.Close(); | ||
203 | fStream.Close(); | ||
204 | info.Data = idata; | ||
205 | //info.loaded=true; | ||
206 | } | ||
207 | } | ||
208 | } | ||