Added LwesBinaryInstaller0.1.486.19070.msi
[lwes-dotnet/github-mirror.git] / Org.Lwes / Emitter / EventEmitter.cs
blobbc508c6d6ff2703748ce6f1cc1f058dfaaba5ba4
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.Emitter
22 using System;
23 using System.Net;
25 using Org.Lwes.Config;
26 using Org.Lwes.DB;
27 using System.Diagnostics;
28 using Org.Lwes.Trace;
30 /// <summary>
31 /// Utility class for accessing the default IEventEmitter implementation.
32 /// </summary>
33 /// <remarks>
34 /// <para>If there is an ambient ServiceLocator present then this utility class will
35 /// delegate to the ServiceLocator. The ServiceLocator should declare an instance
36 /// of IEventEmitter with the name "eventEmitter".</para>
37 /// <para>If an IoC container is not present, or if a service instance is not defined
38 /// with the name "eventEmitter" then this utility will create a MulticastEventEmitter.</para>
39 /// </remarks>
40 public static class EventEmitter
42 /// <summary>
43 /// Accesses the default instance of the IEventEmitter. Delegates to
44 /// an IoC container if present.
45 /// </summary>
46 public static IEventEmitter CreateDefault()
48 Traceable.TraceData(typeof(EventEmitter), TraceEventType.Verbose, "EventEmitter - creating default emitter");
50 IEventEmitter result;
51 if (!IoCAdapter.TryCreateFromIoC<IEventEmitter>(Constants.DefaultEventEmitterContainerKey, out result))
52 { // Either there isn't a default event template defined in the IoC container
53 // or there isn't an IoC container in use... fall back to configuration section.
54 var def = LwesConfigurationSection.Current.DefaultEmitterName;
55 result = CreateFromConfig(def);
57 if (result == null)
58 { // Not in IoC and not configured; fallback to programmatic default.
59 result = CreateFallbackEmitter();
61 return result;
64 /// <summary>
65 /// Creates an emitter from the configuration.
66 /// </summary>
67 /// <param name="name">name of the instance to create</param>
68 /// <returns>the named instance if it exists within the configuration;
69 /// otherwise null</returns>
70 /// <remarks>Note that two subsequent calls to this method will return
71 /// two separate instances of the configured instance.</remarks>
72 public static IEventEmitter CreateFromConfig(string name)
74 LwesConfigurationSection config = LwesConfigurationSection.Current;
75 if (config.Emitters == null) return null;
77 EmitterConfigurationSection namedConfig = config.Emitters[name];
78 if (namedConfig == null)
80 Traceable.TraceData(typeof(EventEmitter), TraceEventType.Verbose, () =>
82 return new object[] { String.Concat("EventEmitter - no configuration found for emitter: ", name) };
83 });
84 return null;
87 Traceable.TraceData(typeof(EventEmitter), TraceEventType.Verbose, () =>
89 return new object[] { String.Concat("EventEmitter - configuration found for emitter: ", name) };
90 });
91 if (namedConfig.UseMulticast)
93 MulticastEventEmitter mee = new MulticastEventEmitter();
94 mee.InitializeAll(namedConfig.Encoding, false, EventTemplateDB.CreateDefault(),
95 IPAddress.Parse(namedConfig.AddressString), namedConfig.Port,
96 namedConfig.MulticastTimeToLive, namedConfig.UseParallelEmit);
97 return mee;
99 else
101 throw new NotImplementedException("TODO: Support UnicastEventEmitter");
102 //UnicastEventEmitter mee = new UnicastEventEmitter();
103 //mee.Initialize(namedEmitterConfig.Encoding, false, EventTemplateDB.Default,
104 // IPAddress.Parse(namedEmitterConfig.AddressString), namedEmitterConfig.Port,
105 // namedEmitterConfig.MulticastTimeToLive, namedEmitterConfig.UseParallelEmit);
106 //return mee;
110 /// <summary>
111 /// Creates the named instance. If an IoC container is in use the IoC
112 /// container is consulted for the named instance first.
113 /// </summary>
114 /// <param name="name">name of the instance to create</param>
115 /// <returns>the named instance if it exists within the IoC container
116 /// or the configuration; otherwise null</returns>
117 public static IEventEmitter CreateNamedEmitter(string name)
119 IEventEmitter result;
120 if (!IoCAdapter.TryCreateFromIoC<IEventEmitter>(name, out result))
122 Traceable.TraceData(typeof(EventEmitter), TraceEventType.Verbose, () =>
124 return new object[] { String.Concat("EventEmitter - IoC cannot resolve emitter: ", name) };
126 result = CreateFromConfig(name);
128 return result;
131 private static IEventEmitter CreateFallbackEmitter()
133 Traceable.TraceData(typeof(EventEmitter), TraceEventType.Verbose, "EventEmitter - using fallback emitter");
135 MulticastEventEmitter emitter = new MulticastEventEmitter();
136 emitter.InitializeAll(SupportedEncoding.Default
137 , Constants.DefaultPerformValidation
138 , EventTemplateDB.CreateDefault()
139 , Constants.DefaultMulticastAddress
140 , Constants.CDefaultMulticastPort
141 , Constants.CDefaultMulticastTtl
142 , true);
143 return emitter;