aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/Util.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Framework/Util.cs')
-rw-r--r--OpenSim/Framework/Util.cs155
1 files changed, 144 insertions, 11 deletions
diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs
index 48f3f8b..96644ec 100644
--- a/OpenSim/Framework/Util.cs
+++ b/OpenSim/Framework/Util.cs
@@ -45,6 +45,7 @@ using System.Text.RegularExpressions;
45using System.Xml; 45using System.Xml;
46using System.Threading; 46using System.Threading;
47using log4net; 47using log4net;
48using log4net.Appender;
48using Nini.Config; 49using Nini.Config;
49using Nwc.XmlRpc; 50using Nwc.XmlRpc;
50using OpenMetaverse; 51using OpenMetaverse;
@@ -299,6 +300,25 @@ namespace OpenSim.Framework
299 x; 300 x;
300 } 301 }
301 302
303 // Clamp the maximum magnitude of a vector
304 public static Vector3 ClampV(Vector3 x, float max)
305 {
306 Vector3 ret = x;
307 float lenSq = x.LengthSquared();
308 if (lenSq > (max * max))
309 {
310 x = x / x.Length() * max;
311 }
312 return x;
313 }
314
315 // Inclusive, within range test (true if equal to the endpoints)
316 public static bool InRange<T>(T x, T min, T max)
317 where T : IComparable<T>
318 {
319 return x.CompareTo(max) <= 0 && x.CompareTo(min) >= 0;
320 }
321
302 public static uint GetNextXferID() 322 public static uint GetNextXferID()
303 { 323 {
304 uint id = 0; 324 uint id = 0;
@@ -809,9 +829,22 @@ namespace OpenSim.Framework
809 return "."; 829 return ".";
810 } 830 }
811 831
832 public static string logFile()
833 {
834 foreach (IAppender appender in LogManager.GetRepository().GetAppenders())
835 {
836 if (appender is FileAppender)
837 {
838 return ((FileAppender)appender).File;
839 }
840 }
841
842 return "./OpenSim.log";
843 }
844
812 public static string logDir() 845 public static string logDir()
813 { 846 {
814 return "."; 847 return Path.GetDirectoryName(logFile());
815 } 848 }
816 849
817 // From: http://coercedcode.blogspot.com/2008/03/c-generate-unique-filenames-within.html 850 // From: http://coercedcode.blogspot.com/2008/03/c-generate-unique-filenames-within.html
@@ -842,7 +875,7 @@ namespace OpenSim.Framework
842 return FileName; 875 return FileName;
843 } 876 }
844 877
845 // Nini (config) related Methods 878 #region Nini (config) related Methods
846 public static IConfigSource ConvertDataRowToXMLConfig(DataRow row, string fileName) 879 public static IConfigSource ConvertDataRowToXMLConfig(DataRow row, string fileName)
847 { 880 {
848 if (!File.Exists(fileName)) 881 if (!File.Exists(fileName))
@@ -865,6 +898,79 @@ namespace OpenSim.Framework
865 } 898 }
866 } 899 }
867 900
901 public static string GetConfigVarWithDefaultSection(IConfigSource config, string varname, string section)
902 {
903 // First, check the Startup section, the default section
904 IConfig cnf = config.Configs["Startup"];
905 if (cnf == null)
906 return string.Empty;
907 string val = cnf.GetString(varname, string.Empty);
908
909 // Then check for an overwrite of the default in the given section
910 if (!string.IsNullOrEmpty(section))
911 {
912 cnf = config.Configs[section];
913 if (cnf != null)
914 val = cnf.GetString(varname, val);
915 }
916
917 return val;
918 }
919
920 /// <summary>
921 /// Gets the value of a configuration variable by looking into
922 /// multiple sections in order. The latter sections overwrite
923 /// any values previously found.
924 /// </summary>
925 /// <typeparam name="T">Type of the variable</typeparam>
926 /// <param name="config">The configuration object</param>
927 /// <param name="varname">The configuration variable</param>
928 /// <param name="sections">Ordered sequence of sections to look at</param>
929 /// <returns></returns>
930 public static T GetConfigVarFromSections<T>(IConfigSource config, string varname, string[] sections)
931 {
932 return GetConfigVarFromSections<T>(config, varname, sections, default(T));
933 }
934
935 /// <summary>
936 /// Gets the value of a configuration variable by looking into
937 /// multiple sections in order. The latter sections overwrite
938 /// any values previously found.
939 /// </summary>
940 /// <remarks>
941 /// If no value is found then the given default value is returned
942 /// </remarks>
943 /// <typeparam name="T">Type of the variable</typeparam>
944 /// <param name="config">The configuration object</param>
945 /// <param name="varname">The configuration variable</param>
946 /// <param name="sections">Ordered sequence of sections to look at</param>
947 /// <param name="val">Default value</param>
948 /// <returns></returns>
949 public static T GetConfigVarFromSections<T>(IConfigSource config, string varname, string[] sections, object val)
950 {
951 foreach (string section in sections)
952 {
953 IConfig cnf = config.Configs[section];
954 if (cnf == null)
955 continue;
956
957 if (typeof(T) == typeof(String))
958 val = cnf.GetString(varname, (string)val);
959 else if (typeof(T) == typeof(Boolean))
960 val = cnf.GetBoolean(varname, (bool)val);
961 else if (typeof(T) == typeof(Int32))
962 val = cnf.GetInt(varname, (int)val);
963 else if (typeof(T) == typeof(float))
964 val = cnf.GetFloat(varname, (int)val);
965 else
966 m_log.ErrorFormat("[UTIL]: Unhandled type {0}", typeof(T));
967 }
968
969 return (T)val;
970 }
971
972 #endregion
973
868 public static float Clip(float x, float min, float max) 974 public static float Clip(float x, float min, float max)
869 { 975 {
870 return Math.Min(Math.Max(x, min), max); 976 return Math.Min(Math.Max(x, min), max);
@@ -1651,7 +1757,13 @@ namespace OpenSim.Framework
1651 if (m_ThreadPool != null) 1757 if (m_ThreadPool != null)
1652 throw new InvalidOperationException("SmartThreadPool is already initialized"); 1758 throw new InvalidOperationException("SmartThreadPool is already initialized");
1653 1759
1654 m_ThreadPool = new SmartThreadPool(2000, maxThreads, 2); 1760 STPStartInfo startInfo = new STPStartInfo();
1761 startInfo.ThreadPoolName = "Util";
1762 startInfo.IdleTimeout = 2000;
1763 startInfo.MaxWorkerThreads = maxThreads;
1764 startInfo.MinWorkerThreads = 2;
1765
1766 m_ThreadPool = new SmartThreadPool(startInfo);
1655 } 1767 }
1656 1768
1657 public static int FireAndForgetCount() 1769 public static int FireAndForgetCount()
@@ -1724,7 +1836,7 @@ namespace OpenSim.Framework
1724 break; 1836 break;
1725 case FireAndForgetMethod.SmartThreadPool: 1837 case FireAndForgetMethod.SmartThreadPool:
1726 if (m_ThreadPool == null) 1838 if (m_ThreadPool == null)
1727 m_ThreadPool = new SmartThreadPool(2000, 15, 2); 1839 InitThreadPool(15);
1728 m_ThreadPool.QueueWorkItem(SmartThreadPoolCallback, new object[] { realCallback, obj }); 1840 m_ThreadPool.QueueWorkItem(SmartThreadPoolCallback, new object[] { realCallback, obj });
1729 break; 1841 break;
1730 case FireAndForgetMethod.Thread: 1842 case FireAndForgetMethod.Thread:
@@ -1753,12 +1865,16 @@ namespace OpenSim.Framework
1753 StringBuilder sb = new StringBuilder(); 1865 StringBuilder sb = new StringBuilder();
1754 if (FireAndForgetMethod == FireAndForgetMethod.SmartThreadPool) 1866 if (FireAndForgetMethod == FireAndForgetMethod.SmartThreadPool)
1755 { 1867 {
1756 threadPoolUsed = "SmartThreadPool"; 1868 // ROBUST currently leaves this the FireAndForgetMethod but never actually initializes the threadpool.
1757 maxThreads = m_ThreadPool.MaxThreads; 1869 if (m_ThreadPool != null)
1758 minThreads = m_ThreadPool.MinThreads; 1870 {
1759 inUseThreads = m_ThreadPool.InUseThreads; 1871 threadPoolUsed = "SmartThreadPool";
1760 allocatedThreads = m_ThreadPool.ActiveThreads; 1872 maxThreads = m_ThreadPool.MaxThreads;
1761 waitingCallbacks = m_ThreadPool.WaitingCallbacks; 1873 minThreads = m_ThreadPool.MinThreads;
1874 inUseThreads = m_ThreadPool.InUseThreads;
1875 allocatedThreads = m_ThreadPool.ActiveThreads;
1876 waitingCallbacks = m_ThreadPool.WaitingCallbacks;
1877 }
1762 } 1878 }
1763 else if ( 1879 else if (
1764 FireAndForgetMethod == FireAndForgetMethod.UnsafeQueueUserWorkItem 1880 FireAndForgetMethod == FireAndForgetMethod.UnsafeQueueUserWorkItem
@@ -1863,6 +1979,12 @@ namespace OpenSim.Framework
1863 /// </summary> 1979 /// </summary>
1864 public static void PrintCallStack() 1980 public static void PrintCallStack()
1865 { 1981 {
1982 PrintCallStack(m_log.DebugFormat);
1983 }
1984
1985 public delegate void DebugPrinter(string msg, params Object[] parm);
1986 public static void PrintCallStack(DebugPrinter printer)
1987 {
1866 StackTrace stackTrace = new StackTrace(true); // get call stack 1988 StackTrace stackTrace = new StackTrace(true); // get call stack
1867 StackFrame[] stackFrames = stackTrace.GetFrames(); // get method calls (frames) 1989 StackFrame[] stackFrames = stackTrace.GetFrames(); // get method calls (frames)
1868 1990
@@ -1870,7 +1992,7 @@ namespace OpenSim.Framework
1870 foreach (StackFrame stackFrame in stackFrames) 1992 foreach (StackFrame stackFrame in stackFrames)
1871 { 1993 {
1872 MethodBase mb = stackFrame.GetMethod(); 1994 MethodBase mb = stackFrame.GetMethod();
1873 m_log.DebugFormat("{0}.{1}:{2}", mb.DeclaringType, mb.Name, stackFrame.GetFileLineNumber()); // write method name 1995 printer("{0}.{1}:{2}", mb.DeclaringType, mb.Name, stackFrame.GetFileLineNumber()); // write method name
1874 } 1996 }
1875 } 1997 }
1876 1998
@@ -2096,6 +2218,17 @@ namespace OpenSim.Framework
2096 return firstName + "." + lastName + " " + "@" + uri.Authority; 2218 return firstName + "." + lastName + " " + "@" + uri.Authority;
2097 } 2219 }
2098 #endregion 2220 #endregion
2221
2222 /// <summary>
2223 /// Escapes the special characters used in "LIKE".
2224 /// </summary>
2225 /// <remarks>
2226 /// For example: EscapeForLike("foo_bar%baz") = "foo\_bar\%baz"
2227 /// </remarks>
2228 public static string EscapeForLike(string str)
2229 {
2230 return str.Replace("_", "\\_").Replace("%", "\\%");
2231 }
2099 } 2232 }
2100 2233
2101 public class DoubleQueue<T> where T:class 2234 public class DoubleQueue<T> where T:class