-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMessagingFacade.cs
More file actions
124 lines (113 loc) · 6.14 KB
/
MessagingFacade.cs
File metadata and controls
124 lines (113 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================
using RapidField.SolidInstruments.Core;
using RapidField.SolidInstruments.Core.ArgumentValidation;
using RapidField.SolidInstruments.Core.Concurrency;
using RapidField.SolidInstruments.Core.Extensions;
using RapidField.SolidInstruments.TextEncoding;
using System;
using System.Diagnostics;
using System.Threading;
namespace RapidField.SolidInstruments.Messaging
{
/// <summary>
/// Facilitates implementation-specific operations for a message bus.
/// </summary>
/// <typeparam name="TSender">
/// The type of the implementation-specific send client.
/// </typeparam>
/// <typeparam name="TReceiver">
/// The type of the implementation-specific receive client.
/// </typeparam>
/// <typeparam name="TAdaptedMessage">
/// The type of implementation-specific adapted messages.
/// </typeparam>
/// <remarks>
/// <see cref="MessagingFacade{TSender, TReceiver, TAdaptedMessage}" /> is the default implementation of
/// <see cref="IMessagingFacade{TSender, TReceiver, TAdaptedMessage}" />.
/// </remarks>
public abstract class MessagingFacade<TSender, TReceiver, TAdaptedMessage> : Instrument, IMessagingFacade<TSender, TReceiver, TAdaptedMessage>
where TAdaptedMessage : class
{
/// <summary>
/// Initializes a new instance of the <see cref="MessagingFacade{TSender, TReceiver, TAdaptedMessage}" /> class.
/// </summary>
/// <param name="clientFactory">
/// An appliance that creates manages implementation-specific messaging clients.
/// </param>
/// <param name="messageAdapter">
/// An appliance that facilitates implementation-specific message conversion.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="clientFactory" /> is <see langword="null" /> -or- <paramref name="messageAdapter" /> is
/// <see langword="null" />.
/// </exception>
protected MessagingFacade(IMessagingClientFactory<TSender, TReceiver, TAdaptedMessage> clientFactory, IMessageAdapter<TAdaptedMessage> messageAdapter)
: base(ConcurrencyControlMode.SingleThreadLock, Timeout.InfiniteTimeSpan)
{
ClientFactory = clientFactory.RejectIf().IsNull(nameof(clientFactory)).TargetArgument;
LazyApplicationIdentity = new Lazy<String>(InitializeApplicationIdentity, LazyThreadSafetyMode.ExecutionAndPublication);
LazyIdentifier = new Lazy<String>(InitializeIdentifier, LazyThreadSafetyMode.ExecutionAndPublication);
MessageAdapter = messageAdapter.RejectIf().IsNull(nameof(messageAdapter)).TargetArgument;
}
/// <summary>
/// Releases all resources consumed by the current <see cref="MessagingFacade{TSender, TReceiver, TAdaptedMessage}" />.
/// </summary>
/// <param name="disposing">
/// A value indicating whether or not managed resources should be released.
/// </param>
protected override void Dispose(Boolean disposing) => base.Dispose(disposing);
/// <summary>
/// Initializes the name or value that uniquely identifies the application in which the current
/// <see cref="MessagingFacade{TSender, TReceiver, TAdaptedMessage}" /> is running.
/// </summary>
/// <returns>
/// A unique textual identifier.
/// </returns>
protected virtual String InitializeApplicationIdentity()
{
var process = Process.GetCurrentProcess();
return $"{process.MachineName}_{process.ProcessName}_{process.Id}";
}
/// <summary>
/// Initializes the unique textual identifier for the current
/// <see cref="MessagingFacade{TSender, TReceiver, TAdaptedMessage}" />.
/// </summary>
/// <returns>
/// A unique textual identifier.
/// </returns>
protected virtual String InitializeIdentifier() => new ZBase32Encoding().GetString(Guid.NewGuid().ToByteArray().ComputeThirtyTwoBitHash().ToByteArray());
/// <summary>
/// Gets the name or value that uniquely identifies the application in which the current
/// <see cref="MessagingFacade{TSender, TReceiver, TAdaptedMessage}" /> is running.
/// </summary>
public String ApplicationIdentity => LazyApplicationIdentity.Value;
/// <summary>
/// Gets the unique identifier for the current <see cref="MessagingFacade{TSender, TReceiver, TAdaptedMessage}" />.
/// </summary>
public String Identifier => LazyIdentifier.Value;
/// <summary>
/// Represents an appliance that creates and manages implementation-specific messaging clients.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
internal readonly IMessagingClientFactory<TSender, TReceiver, TAdaptedMessage> ClientFactory;
/// <summary>
/// Represents an appliance that facilitates implementation-specific message conversion.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
internal readonly IMessageAdapter<TAdaptedMessage> MessageAdapter;
/// <summary>
/// Represents the lazily-initialized name or value that uniquely identifies the application in which the current
/// <see cref="MessagingFacade{TSender, TReceiver, TAdaptedMessage}" /> is running.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly Lazy<String> LazyApplicationIdentity;
/// <summary>
/// Represents the lazily-initialized unique identifier for the current
/// <see cref="MessagingFacade{TSender, TReceiver, TAdaptedMessage}" />.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly Lazy<String> LazyIdentifier;
}
}