Added trace output to many classes.
[lwes-dotnet/github-mirror.git] / Org.Lwes / Constants.cs
bloba048e5a099c33bcfbabf0cd38c2dd1dcf3570d27
1 //
2 // This file is part of the LWES .NET Binding (LWES.net)
3 //
4 // COPYRIGHT© 2009, Phillip Clark (phillip[at*flitbit[dot*org)
5 // original .NET implementation
6 //
7 // LWES.net is free software: you can redistribute it and/or modify
8 // it under the terms of the Lesser GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
12 // LWES.net is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // Lesser GNU General Public License for more details.
17 // You should have received a copy of the Lesser GNU General Public License
18 // along with LWES.net. If not, see <http://www.gnu.org/licenses/>.
20 namespace Org.Lwes
22 using System;
23 using System.Net;
24 using System.Text;
26 using Org.Lwes.ESF;
27 using Org.Lwes.Properties;
29 /// <summary>
30 /// Utility class containing constants used by LWES
31 /// </summary>
32 public static class Constants
34 #region Fields
36 /// <summary>
37 /// Default encoding for character data.
38 /// </summary>
39 public static readonly Encoding DefaultEncoding = Encoding.UTF8;
41 /// <summary>
42 /// Name identifying the default event emitter in the configuration file.
43 /// </summary>
44 public static readonly string DefaultEventEmitterConfigName = "default";
46 /// <summary>
47 /// IoC container key for the default IEventEmitter instance.
48 /// </summary>
49 public static readonly string DefaultEventEmitterContainerKey = "eventEmitter";
51 /// <summary>
52 /// Name identifying the default event listener in the configuration file.
53 /// </summary>
54 public static readonly string DefaultEventListenerConfigName = "default";
56 /// <summary>
57 /// IoC container key for the default IEventListener instance.
58 /// </summary>
59 public static readonly string DefaultEventListenerContainerKey = "eventListener";
61 /// <summary>
62 /// Name identifying the default event template DB in the configuration file.
63 /// </summary>
64 public static readonly string DefaultEventTemplateDBConfigName = "default";
66 /// <summary>
67 /// IoC container key for the default IEventTemplateDB instance.
68 /// </summary>
69 public static readonly string DefaultEventTemplateDBContainerKey = "templateDB";
71 /// <summary>
72 /// Default address used for multicast listening.
73 /// </summary>
74 public static readonly IPAddress DefaultMulticastAddress = IPAddress.Parse(CDefaultMulticastAddressString);
76 /// <summary>
77 /// Encoding for ISO-8859-1
78 /// </summary>
79 public static readonly Encoding ISO8859_1Encoding = Encoding.GetEncoding("ISO-8859-1");
81 /// <summary>
82 /// Represents the difference between the epoc used by LWES and the .NET DateTime ticks.
83 /// </summary>
84 public static readonly long LwesEpocOffsetTicks = new DateTime(1970, 1, 1).Ticks;
86 /// <summary>
87 /// Name of the event template inherited by other events.
88 /// </summary>
89 public static readonly EventTemplate MetaEventInfo;
91 /// <summary>
92 /// Default length of buffers.
93 /// </summary>
94 public const int CAllocationBufferLength = 65535;
96 /// <summary>
97 /// Default multicast address (as a string)
98 /// </summary>
99 public const string CDefaultMulticastAddressString = "224.0.0.69";
101 /// <summary>
102 /// Default port used for multicast listening.
103 /// </summary>
104 public const int CDefaultMulticastPort = 9191;
106 /// <summary>
107 /// Default time-to-live for multicast emitting.
108 /// </summary>
109 public const int CDefaultMulticastTtl = 31;
111 /// <summary>
112 /// Identifies the default encoding used if encoding is not specified.
113 /// </summary>
114 public const SupportedEncoding CDefaultSupportedEncoding = SupportedEncoding.UTF_8;
116 /// <summary>
117 /// Maximum memory used for buffering incoming events.
118 /// </summary>
119 public const int CMaximumBufferMemory = 0x20000000; // .5GB
121 #endregion Fields
123 #if DEBUG
125 /// <summary>
126 /// Default value indicating whether validation is performed
127 /// </summary>
128 public static readonly bool DefaultPerformValidation = true;
130 #else
132 /// <summary>
133 /// Default value indicating whether validation is performed
134 /// </summary>
135 public static readonly bool DefaultPerformValidation = false;
137 #endif
139 #region Constructors
141 static Constants()
143 MetaEventInfo = new EventTemplate(true, "MetaInfoEvent")
144 .AppendAttributes(MetaEventInfoAttributes.Encoding)
145 .AppendAttributes(MetaEventInfoAttributes.SenderIP)
146 .AppendAttributes(MetaEventInfoAttributes.SenderPort)
147 .AppendAttributes(MetaEventInfoAttributes.ReceiptTime)
148 .AppendAttributes(MetaEventInfoAttributes.SiteID);
151 #endregion Constructors
153 #region Methods
155 /// <summary>
156 /// Converts a DateTime to LWES ticks.
157 /// </summary>
158 /// <param name="time"></param>
159 /// <returns></returns>
160 public static Int64 DateTimeToLwesTimeTicks(DateTime time)
162 return (time.Ticks - LwesEpocOffsetTicks);
165 /// <summary>
166 /// Identifies the encoding's value on the wire.
167 /// </summary>
168 /// <param name="enc">An Encoding instance to identify</param>
169 /// <returns>Either ISO_8859_1 (0) or UTF_8 (1)</returns>
170 /// <exception cref="ArgumentOutOfRangeException">thrown if the encoding is not recognized</exception>
171 public static SupportedEncoding IdentifyEncoding(Encoding enc)
173 if (enc == ISO8859_1Encoding) return SupportedEncoding.ISO_8859_1;
174 if (enc == DefaultEncoding) return SupportedEncoding.UTF_8;
175 throw new ArgumentOutOfRangeException(String.Format(Resources.Error_UnrecognizedEncoding, enc.EncodingName));
178 /// <summary>
179 /// Converts LWES ticks to DateTime.
180 /// </summary>
181 /// <param name="lwesTimeTicks"></param>
182 /// <returns></returns>
183 public static DateTime LwesTimeTicksToDateTime(long lwesTimeTicks)
185 return new DateTime(lwesTimeTicks + LwesEpocOffsetTicks);
188 internal static void CheckEncoding(short enc)
190 if (!Enum.IsDefined(typeof(SupportedEncoding), enc))
191 throw new InvalidOperationException("Encoding not supported");
194 internal static Encoding GetEncoding(short enc)
196 CheckEncoding(enc);
197 if (enc == (short)SupportedEncoding.ISO_8859_1) return ISO8859_1Encoding;
198 else return DefaultEncoding;
201 #endregion Methods
203 #region Nested Types
205 /// <summary>
206 /// Contains names of event attributes that may be inherited by any event.
207 /// </summary>
208 public static class MetaEventInfoAttributes
210 #region Fields
212 /// <summary>
213 /// Name of the attribute that identifies the encoding used for an event. (set by emitters)
214 /// </summary>
215 public static readonly AttributeTemplate Encoding = new AttributeTemplate(TypeToken.INT16, "enc", 0);
217 /// <summary>
218 /// Name of the attribute that reflects the time an event was received,
219 /// in milliseconds since epoch. (set by journallers and listeners)
220 /// </summary>
221 public static readonly AttributeTemplate ReceiptTime = new AttributeTemplate(TypeToken.UINT64, "ReceiptTime", 1);
223 /// <summary>
224 /// Name of the attribute that reflects the sender's IP address.
225 /// (set by journallers and listeners)
226 /// </summary>
227 public static readonly AttributeTemplate SenderIP = new AttributeTemplate(TypeToken.IP_ADDR, "SenderIP", 2);
229 /// <summary>
230 /// Name of the attribute that reflects the sender's port.
231 /// (set by journallers and listeners)
232 /// </summary>
233 public static readonly AttributeTemplate SenderPort = new AttributeTemplate(TypeToken.INT32, "SenderPort", 3);
235 /// <summary>
236 /// Name of the attribute that reflects event's source site ID.
237 /// (set by emitters)
238 /// </summary>
239 public static readonly AttributeTemplate SiteID = new AttributeTemplate(TypeToken.UINT32,"SiteID", 4);
241 #endregion Fields
244 #endregion Nested Types