summaryrefslogtreecommitdiff
path: root/csharp/src/Ice/PropertiesAdmin.cs
blob: a8b5181de4b637eb27bcb5f0f999961a2d9c54a9 (plain)
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
// Copyright (c) ZeroC, Inc. All rights reserved.

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace ZeroC.Ice
{
    // This partial interface extends the IAsyncPropertieAdmin interface generated from the Slice interface
    // PropertiesAdmin.
    public partial interface IAsyncPropertiesAdmin
    {
        /// <summary>The Updated event is triggered when the communicator's properties are updated through a remote call
        /// to the setProperties operation implemented by this Properties admin facet. The event's args include only the
        /// communicator properties that were updated by this remote call.</summary>
        public abstract event EventHandler<IReadOnlyDictionary<string, string>>? Updated;
    }

    // Default implementation of the Properties Admin facet.
    internal sealed class PropertiesAdmin : IAsyncPropertiesAdmin
    {
        public event EventHandler<IReadOnlyDictionary<string, string>>? Updated;

        private const string TraceCategory = "Ice.Admin.Properties";
        private readonly Communicator _communicator;
        private readonly ILogger _logger;

        public ValueTask<string> GetPropertyAsync(string key, Current current, CancellationToken cancel) =>
            new(_communicator.GetProperty(key) ?? "");

        public ValueTask<IReadOnlyDictionary<string, string>> GetPropertiesForPrefixAsync(
            string prefix,
            Current current,
            CancellationToken cancel) =>
            new(_communicator.GetProperties(forPrefix: prefix));

        public ValueTask SetPropertiesAsync(
            Dictionary<string, string> newProperties,
            Current current,
            CancellationToken cancel)
        {
            int? traceLevel = _communicator.GetPropertyAsInt("Ice.Trace.Admin.Properties");

            // Update the communicator's properties and remove the properties that did not change from props.
            _communicator.SetProperties(newProperties);

            if (traceLevel > 0 && newProperties.Count > 0)
            {
                var message = new System.Text.StringBuilder("Summary of property changes");

                message.Append("\nNew or updated properties:");
                foreach (KeyValuePair<string, string> e in newProperties)
                {
                    if (e.Value.Length > 0)
                    {
                        message.Append("\n  ");
                        message.Append(e.Key);
                        if (traceLevel > 1)
                        {
                            message.Append(" = ");
                            message.Append(e.Value);
                        }
                    }
                }

                message.Append("\nRemoved properties:");
                foreach (KeyValuePair<string, string> e in newProperties)
                {
                    if (e.Value.Length == 0)
                    {
                        message.Append("\n  ");
                        message.Append(e.Key);
                    }
                }

                _logger.Trace(TraceCategory, message.ToString());
            }

            Updated?.Invoke(_communicator, newProperties);
            return default;
        }

        internal PropertiesAdmin(Communicator communicator)
        {
            _communicator = communicator;
            _logger = communicator.Logger;
        }
    }
}