2 // This file is part of the LWES .NET Binding (LWES.net)
4 // COPYRIGHT© 2009, Phillip Clark (phillip[at*flitbit[dot*org)
5 // original .NET implementation
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/>.
27 using Org
.Lwes
.Properties
;
30 /// Utility class containing constants used by LWES
32 public static class Constants
37 /// Default encoding for character data.
39 public static readonly Encoding DefaultEncoding
= Encoding
.UTF8
;
42 /// Name identifying the default event emitter in the configuration file.
44 public static readonly string DefaultEventEmitterConfigName
= "default";
47 /// IoC container key for the default IEventEmitter instance.
49 public static readonly string DefaultEventEmitterContainerKey
= "eventEmitter";
52 /// Name identifying the default event listener in the configuration file.
54 public static readonly string DefaultEventListenerConfigName
= "default";
57 /// IoC container key for the default IEventListener instance.
59 public static readonly string DefaultEventListenerContainerKey
= "eventListener";
62 /// Name identifying the default event template DB in the configuration file.
64 public static readonly string DefaultEventTemplateDBConfigName
= "default";
67 /// IoC container key for the default IEventTemplateDB instance.
69 public static readonly string DefaultEventTemplateDBContainerKey
= "templateDB";
72 /// Default address used for multicast listening.
74 public static readonly IPAddress DefaultMulticastAddress
= IPAddress
.Parse(CDefaultMulticastAddressString
);
77 /// Encoding for ISO-8859-1
79 public static readonly Encoding ISO8859_1Encoding
= Encoding
.GetEncoding("ISO-8859-1");
82 /// Represents the difference between the epoc used by LWES and the .NET DateTime ticks.
84 public static readonly long LwesEpocOffsetTicks
= new DateTime(1970, 1, 1).Ticks
;
87 /// Name of the event template inherited by other events.
89 public static readonly EventTemplate MetaEventInfo
;
92 /// Default length of buffers.
94 public const int CAllocationBufferLength
= 65535;
97 /// Default multicast address (as a string)
99 public const string CDefaultMulticastAddressString
= "224.0.0.69";
102 /// Default port used for multicast listening.
104 public const int CDefaultMulticastPort
= 9191;
107 /// Default time-to-live for multicast emitting.
109 public const int CDefaultMulticastTtl
= 31;
112 /// Identifies the default encoding used if encoding is not specified.
114 public const SupportedEncoding CDefaultSupportedEncoding
= SupportedEncoding
.UTF_8
;
117 /// Maximum memory used for buffering incoming events.
119 public const int CMaximumBufferMemory
= 0x20000000; // .5GB
126 /// Default value indicating whether validation is performed
128 public static readonly bool DefaultPerformValidation
= true;
133 /// Default value indicating whether validation is performed
135 public static readonly bool DefaultPerformValidation
= false;
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
156 /// Converts a DateTime to LWES ticks.
158 /// <param name="time"></param>
159 /// <returns></returns>
160 public static Int64
DateTimeToLwesTimeTicks(DateTime time
)
162 return (time
.Ticks
- LwesEpocOffsetTicks
);
166 /// Identifies the encoding's value on the wire.
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
));
179 /// Converts LWES ticks to DateTime.
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
)
197 if (enc
== (short)SupportedEncoding
.ISO_8859_1
) return ISO8859_1Encoding
;
198 else return DefaultEncoding
;
206 /// Contains names of event attributes that may be inherited by any event.
208 public static class MetaEventInfoAttributes
213 /// Name of the attribute that identifies the encoding used for an event. (set by emitters)
215 public static readonly AttributeTemplate Encoding
= new AttributeTemplate(TypeToken
.INT16
, "enc", 0);
218 /// Name of the attribute that reflects the time an event was received,
219 /// in milliseconds since epoch. (set by journallers and listeners)
221 public static readonly AttributeTemplate ReceiptTime
= new AttributeTemplate(TypeToken
.UINT64
, "ReceiptTime", 1);
224 /// Name of the attribute that reflects the sender's IP address.
225 /// (set by journallers and listeners)
227 public static readonly AttributeTemplate SenderIP
= new AttributeTemplate(TypeToken
.IP_ADDR
, "SenderIP", 2);
230 /// Name of the attribute that reflects the sender's port.
231 /// (set by journallers and listeners)
233 public static readonly AttributeTemplate SenderPort
= new AttributeTemplate(TypeToken
.INT32
, "SenderPort", 3);
236 /// Name of the attribute that reflects event's source site ID.
237 /// (set by emitters)
239 public static readonly AttributeTemplate SiteID
= new AttributeTemplate(TypeToken
.UINT32
,"SiteID", 4);
244 #endregion Nested Types