2 // This file is part of the LWES .NET Binding (LWES.net)
4 // COPYRIGHT (C) 2009, Phillip Clark (cerebralkungfu[at*g mail[dot*com)
5 // original .NET implementation
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
23 using System
.Configuration
;
26 using Microsoft
.Practices
.ServiceLocation
;
28 using Org
.Lwes
.Config
;
32 /// Utility class for accessing the default IEventEmitter implementation.
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>
41 public static class EventEmitter
46 /// Accesses the default instance of the IEventEmitter. Delegates to
47 /// an IoC container if present.
49 public static IEventEmitter
CreateDefault()
51 IEventEmitter result
= IoCAdapter
.CreateFromIoC
<IEventEmitter
>(Constants
.DefaultEventEmitterContainerKey
);
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
);
59 { // Not in IoC and not configured; fallback to programmatic default.
60 result
= CreateFallbackEmitter();
66 /// Creates an emitter from the configuration.
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
;
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
);
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);
103 /// Creates the named instance. If an IoC container is in use the IoC
104 /// container is consulted for the named instance first.
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
);
114 result
= CreateFromConfig(name
);
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