Added LGPL license information to code files.
[lwes-dotnet/github-mirror.git] / Org.Lwes / Emitter / EventEmitter.cs
blob661de7cd3eace78bce5d1c71740c3eed22650b65
1 //
2 // This file is part of the LWES .NET Binding (LWES.net)
3 //
4 // COPYRIGHT (C) 2009, Phillip Clark (cerebralkungfu[at*g mail[dot*com)
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 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 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with LWES.net. If not, see <http://www.gnu.org/licenses/>.
20 namespace Org.Lwes.Emitter
22 using System;
23 using System.Configuration;
24 using System.Net;
26 using Microsoft.Practices.ServiceLocation;
28 using Org.Lwes.Config;
29 using Org.Lwes.DB;
31 /// <summary>
32 /// Utility class for accessing the default IEventEmitter implementation.
33 /// </summary>
34 /// <remarks>
35 /// <para>If there is an ambient ServiceLocator present then this utility class will
36 /// delegate to the ServiceLocator. The ServiceLocator should declare an instance
37 /// of IEventEmitter with the name "eventEmitter".</para>
38 /// <para>If an IoC container is not present, or if a service instance is not defined
39 /// with the name "eventEmitter" then this utility will create a MulticastEventEmitter.</para>
40 /// </remarks>
41 public static class EventEmitter
43 #region Methods
45 /// <summary>
46 /// Accesses the default instance of the IEventEmitter. Delegates to
47 /// an IoC container if present.
48 /// </summary>
49 public static IEventEmitter CreateDefault()
51 IEventEmitter result = IoCAdapter.CreateFromIoC<IEventEmitter>(Constants.DefaultEventEmitterContainerKey);
53 if (result == null)
54 { // Either there isn't a default event template defined in the IoC container
55 // or there isn't an IoC container in use... fall back to configuration section.
56 result = CreateFromConfig(Constants.DefaultEventEmitterConfigName);
58 if (result == null)
59 { // Not in IoC and not configured; fallback to programmatic default.
60 result = CreateFallbackEmitter();
62 return result;
65 /// <summary>
66 /// Creates an emitter from the configuration.
67 /// </summary>
68 /// <param name="name">name of the instance to create</param>
69 /// <returns>the named instance if it exists within the configuration;
70 /// otherwise null</returns>
71 /// <remarks>Note that two subsequent calls to this method will return
72 /// two separate instances of the configured instance.</remarks>
73 public static IEventEmitter CreateFromConfig(string name)
75 EmitterConfigurationSection namedEmitterConfig = null;
76 LwesConfigurationSection config = ConfigurationManager.GetSection(LwesConfigurationSection.SectionName) as LwesConfigurationSection;
77 if (config != null)
79 namedEmitterConfig = config.Emitters[name];
81 if (namedEmitterConfig == null) return null;
83 if (namedEmitterConfig.UseMulticast)
85 MulticastEventEmitter mee = new MulticastEventEmitter();
86 mee.Initialize(namedEmitterConfig.Encoding, false, EventTemplateDB.CreateDefault(),
87 IPAddress.Parse(namedEmitterConfig.AddressString), namedEmitterConfig.Port,
88 namedEmitterConfig.MulticastTimeToLive, namedEmitterConfig.UseParallelEmit);
89 return mee;
91 else
93 throw new NotImplementedException("TODO: Support UnicastEventEmitter");
94 //UnicastEventEmitter mee = new UnicastEventEmitter();
95 //mee.Initialize(namedEmitterConfig.Encoding, false, EventTemplateDB.Default,
96 // IPAddress.Parse(namedEmitterConfig.AddressString), namedEmitterConfig.Port,
97 // namedEmitterConfig.MulticastTimeToLive, namedEmitterConfig.UseParallelEmit);
98 //return mee;
102 /// <summary>
103 /// Creates the named instance. If an IoC container is in use the IoC
104 /// container is consulted for the named instance first.
105 /// </summary>
106 /// <param name="name">name of the instance to create</param>
107 /// <returns>the named instance if it exists within the IoC container
108 /// or the configuration; otherwise null</returns>
109 public static IEventEmitter CreateNamedEmitter(string name)
111 IEventEmitter result = IoCAdapter.CreateFromIoC<IEventEmitter>(name);
112 if (result == null)
114 result = CreateFromConfig(name);
116 return result;
119 private static IEventEmitter CreateFallbackEmitter()
121 MulticastEventEmitter emitter = new MulticastEventEmitter();
122 emitter.Initialize(SupportedEncoding.Default
123 , Constants.DefaultPerformValidation
124 , EventTemplateDB.CreateDefault()
125 , Constants.DefaultMulticastAddress
126 , Constants.CDefaultMulticastPort
127 , Constants.CDefaultMulticastTtl
128 , true);
129 return emitter;
132 #endregion Methods