From 462036f74b202a6847858ac410c165a3af159cbc Mon Sep 17 00:00:00 2001 From: Christopher Date: Wed, 8 Jul 2020 01:53:10 +0200 Subject: [PATCH] add error handling --- src/Compress.cs | 55 -------------------- src/DataValue.cs | 102 ++++++++++++++++++++++++-------------- src/Storage/FileSystem.cs | 4 -- 3 files changed, 66 insertions(+), 95 deletions(-) delete mode 100644 src/Compress.cs diff --git a/src/Compress.cs b/src/Compress.cs deleted file mode 100644 index 5ef02ae..0000000 --- a/src/Compress.cs +++ /dev/null @@ -1,55 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.IO.Compression; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OpenSim.Modules.DataValue -{ - class Compress - { - public static void CopyTo(Stream src, Stream dest) - { - byte[] bytes = new byte[4096]; - - int cnt; - - while ((cnt = src.Read(bytes, 0, bytes.Length)) != 0) - { - dest.Write(bytes, 0, cnt); - } - } - - public static byte[] Zip(string str) - { - var bytes = Encoding.UTF8.GetBytes(str); - - using (var msi = new MemoryStream(bytes)) - using (var mso = new MemoryStream()) - { - using (var gs = new GZipStream(mso, CompressionMode.Compress)) - { - CopyTo(msi, gs); - } - - return mso.ToArray(); - } - } - - public static string Unzip(byte[] bytes) - { - using (var msi = new MemoryStream(bytes)) - using (var mso = new MemoryStream()) - { - using (var gs = new GZipStream(msi, CompressionMode.Decompress)) - { - CopyTo(gs, mso); - } - - return Encoding.UTF8.GetString(mso.ToArray()); - } - } - } -} diff --git a/src/DataValue.cs b/src/DataValue.cs index 912649b..889ce2a 100644 --- a/src/DataValue.cs +++ b/src/DataValue.cs @@ -119,22 +119,29 @@ namespace OpenSim.Modules.DataValue { if(m_storage != null) { - SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID); - StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key); + try + { + SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID); + StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key); - if (_element != null) - return _element.get(); + if (_element != null) + return _element.get(); - checkRateLimit(); + checkRateLimit(); - String _data = m_storage.get(_host.GroupID.ToString(), key); + String _data = m_storage.get(_host.GroupID.ToString(), key); - if (_data == null) + if (_data == null) + return ""; + + m_cache.Add(new StorageElement(_host.GroupID.ToString(), key, _data, m_storage)); + + return _data; + }catch(Exception _error) + { + m_log.Error("[" + Name + "] osGetDataValue: " + _error.Message); return ""; - - m_cache.Add(new StorageElement(_host.GroupID.ToString(), key, _data, m_storage)); - - return _data; + } } throw new Exception("No data Storage aviable."); @@ -145,19 +152,26 @@ namespace OpenSim.Modules.DataValue { if (m_storage != null) { - SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID); - StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key); - - if(_element != null) + try { - _element.save(value); - return; + SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID); + StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key); + + if (_element != null) + { + _element.save(value); + return; + } + + checkRateLimit(); + + m_cache.Add(new StorageElement(_host.GroupID.ToString(), key, value, m_storage)); + m_storage.save(_host.GroupID.ToString(), key, value); + } + catch (Exception _error) + { + m_log.Error("[" + Name + "] osSetDataValue: " + _error.Message); } - - checkRateLimit(); - - m_cache.Add(new StorageElement(_host.GroupID.ToString(), key, value, m_storage)); - m_storage.save(_host.GroupID.ToString(), key, value); } throw new Exception("No data Storage aviable."); @@ -167,15 +181,23 @@ namespace OpenSim.Modules.DataValue { if (m_storage != null) { - SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID); - StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key); + try + { + SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID); + StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key); - checkRateLimit(); + checkRateLimit(); - if (_element != null) - m_cache.Remove(_element); + if (_element != null) + m_cache.Remove(_element); + + m_storage.remove(_host.GroupID.ToString(), key); + } + catch (Exception _error) + { + m_log.Error("[" + Name + "] osDeleteDataValue: " + _error.Message); + } - m_storage.remove(_host.GroupID.ToString(), key); } throw new Exception("No data Storage aviable."); @@ -186,18 +208,26 @@ namespace OpenSim.Modules.DataValue { if (m_storage != null) { - SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID); - StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key); + try + { + SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID); + StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key); - if (_element != null) - return 1; + if (_element != null) + return 1; - checkRateLimit(); + checkRateLimit(); - if (m_storage.check(_host.GroupID.ToString(), key)) - return 1; + if (m_storage.check(_host.GroupID.ToString(), key)) + return 1; - return 0; + return 0; + } + catch (Exception _error) + { + m_log.Error("[" + Name + "] osCheckDataValue: " + _error.Message); + return 0; + } } throw new Exception("No data Storage aviable."); diff --git a/src/Storage/FileSystem.cs b/src/Storage/FileSystem.cs index 5cf8e31..4599ae4 100644 --- a/src/Storage/FileSystem.cs +++ b/src/Storage/FileSystem.cs @@ -19,7 +19,6 @@ namespace OpenSim.Modules.DataValue.Storage private Scene m_scene = null; - private bool m_enabledCompress = true; private string m_dataValueDirectory = "./ScriptDataValue"; public FileSystem(Scene scene, IConfig config) @@ -77,9 +76,6 @@ namespace OpenSim.Modules.DataValue.Storage string _storageKey = BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(ASCIIEncoding.ASCII.GetBytes(index.Trim().ToUpper()))).Replace("-", ""); - if (m_enabledCompress) - return m_dataValueDirectory + "/" + _nameSpace + "/" + _storageKey + ".gz"; - return m_dataValueDirectory + "/" + _nameSpace + "/" + _storageKey + ".txt"; } }