##// END OF EJS Templates
sync, minor changes
cin -
r284:bcb6c16f6fed v3
parent child
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -1,152 +1,155
1 1 using System;
2 2 using System.Collections.Generic;
3 3 using System.Diagnostics;
4 using System.Dynamic;
4 5 using System.Linq;
5 6 using Implab.Components;
6 7 using Implab.Diagnostics;
7 8 using Implab.ServiceHost.Unity;
8 9 using Implab.Xml;
9 10 using Unity;
10 11 using Unity.Injection;
11 12 using Unity.Registration;
12 13
13 14 namespace Implab.Playground {
14 15
15 16 public class Foo {
16 17
17 18 public class Bar {
18 19
19 20 }
20 21
21 22 public string Name { get; set; }
22 23
23 24 public int IntValue { get; set; }
24 25
25 26 public string StringValue { get; set; }
26 27
27 28 public void AddRange(Foo[] items) {
28 29 Console.WriteLine($"AddRange: Foo[]");
29 30 }
30 31
31 32 }
32 33
33 34 public class FooFactory : IFactory<Foo>, IFactory<Foo.Bar> {
34 35
35 36 public bool UseSsl { get; set; }
36 37
37 38 public string Connection { get; set; }
38 39
39 40 public Foo Create() {
40 41 return new Foo() {
41 42 Name = "AutoFac"
42 43 };
43 44 }
44 45
45 46 Foo.Bar IFactory<Foo.Bar>.Create() {
46 47 return new Foo.Bar();
47 48 }
48 49 }
49 50
50 51 public interface IContainer<T> {
51 52 T Instance { get; set; }
52 53 }
53 54
54 55 public class Container<T> : IContainer<T> {
55 56 public class Bar {
56 57
57 58 }
58 59
59 60 public class Bar<T2> {
60 61 public class Baz {
61 62
62 63 }
63 64
64 65 }
65 66
66 67 public Container() {
67 68
68 69 }
69 70
70 71 public Container(T instance) {
71 72 Instance = instance;
72 73 }
73 74
74 75 public T Instance { get; set; }
75 76
76 77 public void SetInstance(T value) {
77 78 Instance = value;
78 79 }
79 80
80 81 public void AddRange(List<T> items) {
81 82 Console.WriteLine($"AddRange: {typeof(List<T>)}");
82 83 }
83 84
84 85 public void AddRange(T[] items) {
85 86 Console.WriteLine($"AddRange: T[] ofType {typeof(T[])}");
86 87 }
87 88 }
88 89
89 90 public class Program {
90 91
91 92 static void Main(string[] args) {
92 93 var u1 = new Uri("/some/one");
93 94
94 Console.WriteLine($"{u1.IsAbsoluteUri}: {u1}");
95 dynamic obj = new ExpandoObject();
96
97 obj.Name = "Dynamo";
95 98
96 var u2 = new Uri(u1, "../../two");
99 obj.Hello = new Func<string>(() => { return "Hello"; });
97 100
98 Console.WriteLine($"{u2.IsAbsoluteUri}: {u2}");
101 Console.WriteLine($"{obj.Hello()}");
99 102
100 103 }
101 104
102 105 static void Main2(string[] args) {
103 106 var listener = new SimpleTraceListener(Console.Out);
104 107 var source = Trace<TypeResolver>.TraceSource;
105 108 source.Switch.Level = SourceLevels.All;
106 109 source.Listeners.Add(listener);
107 110
108 111 var stopwatch = new Stopwatch();
109 112 stopwatch.Start();
110 113
111 114 var container = new UnityContainer();
112 115
113 116 Console.WriteLine($"Created: {stopwatch.ElapsedMilliseconds}");
114 117 stopwatch.Restart();
115 118
116 119 container.LoadXmlConfiguration("data/sample.xml");
117 120
118 121 Console.WriteLine($"Loaded: {stopwatch.ElapsedMilliseconds}");
119 122
120 123 stopwatch.Restart();
121 124 var instace1 = container.Resolve<IContainer<string>>();
122 125 Console.WriteLine($"Resolved1: {stopwatch.ElapsedMilliseconds}");
123 126
124 127 stopwatch.Restart();
125 128 var instace2 = container.Resolve<IContainer<Foo>>();
126 129 Console.WriteLine($"Resolved2: {stopwatch.ElapsedMilliseconds}");
127 130
128 131 DisplayContainerRegistrations(container);
129 132 }
130 133
131 134 static void DisplayContainerRegistrations(IUnityContainer theContainer) {
132 135 string regName, regType, mapTo, lifetime;
133 136 Console.WriteLine("Container has {0} Registrations:",
134 137 theContainer.Registrations.Count());
135 138 foreach (ContainerRegistration item in theContainer.Registrations) {
136 139 regType = item.RegisteredType.FullName;
137 140 mapTo = item.MappedToType.FullName;
138 141 regName = item.Name ?? "[default]";
139 142 lifetime = item.LifetimeManager.LifetimeType.Name;
140 143 if (mapTo != regType) {
141 144 mapTo = " -> " + mapTo;
142 145 } else {
143 146 mapTo = string.Empty;
144 147 }
145 148 lifetime = lifetime.Substring(0, lifetime.Length - "LifetimeManager".Length);
146 149 Console.WriteLine("+ {0}{1} '{2}' {3}", regType, mapTo, regName, lifetime);
147 150 }
148 151 }
149 152
150 153
151 154 }
152 155 }
1 NO CONTENT: file renamed from Implab.ServiceHost/docs/XmlConfiguration.md to Implab.ServiceHost/docs/XmlConfiguration.ru.md
@@ -1,94 +1,107
1 1 using System;
2 2 using System.Threading;
3 3 using System.Threading.Tasks;
4 4
5 5 namespace Implab.Components {
6 6 public abstract class PollingComponent : RunnableComponent {
7 7
8 8 readonly Timer m_timer;
9 9
10 10 readonly CancellationTokenSource m_cancellation = new CancellationTokenSource();
11 11
12 12 Task m_pending;
13 13 Task m_poll;
14 14
15 15 /// <summary>
16 16 /// Poll interval in milliseconds.
17 17 /// </summary>
18 18 /// <returns></returns>
19 19 public int Interval { get; set; }
20 20
21 21 /// <summary>
22 22 /// Delay to the first poll after start in milliseconds
23 23 /// </summary>
24 24 /// <returns></returns>
25 25 public int Delay { get; set; }
26 26
27 27 /// <summary>
28 28 /// Indicates how to handle unhandled exceptions in <see cref="Poll()"/> method.
29 29 /// </summary>
30 30 /// <returns></returns>
31 31 public bool FailOnError { get; set; }
32 32
33 33 /// <summary>
34 34 /// Event for the unhandled exceptions in <see cref="Poll()"/> method.
35 35 /// </summary>
36 36 public event EventHandler<UnhandledExceptionEventArgs> UnhandledException;
37 37
38 38 protected PollingComponent(bool initialized) : base(initialized) {
39 39 m_timer = new Timer(OnTimer);
40 40 }
41 41
42 42 protected override void RunInternal() {
43 43 ScheduleNextPoll(Delay);
44 44 }
45 45
46 46
47 47 protected override async Task StopInternalAsync(CancellationToken ct) {
48 48 // component in Stopping state, no new polls will be scheduled
49
50 // we do not need additional synchronization logic here
51 // since RunnableComponent already done this
52
49 53 m_cancellation.Cancel();
50 54 try {
51 55 // await for pending poll
52 56 if (m_poll != null)
53 57 await m_poll;
54 58 } catch (OperationCanceledException) {
55 59 // OK
56 60 }
57 61 }
58 62
59 63 protected abstract Task Poll(CancellationToken ct);
60 64
61 65 void ScheduleNextPoll(int timeout) {
66 // access and modification of the component state
67 // in custom methods requires a synchronization
62 68 lock (SynchronizationObject) {
69
63 70 if (State == ExecutionState.Running) {
64 71 m_pending = Safe.CreateTask(m_cancellation.Token);
65 72 m_poll = m_pending.Then(() => Poll(m_cancellation.Token));
66 73 m_timer.Change(timeout, Timeout.Infinite);
67 74 }
68 75 }
69 76 }
70 77
71 78 async void OnTimer(object state) {
72 79 try {
80 // changes to m_pending and m_poll are done
81 // only in ScheduleNextPoll method, hence we
82 // can safely use them here
73 83 m_pending.Start();
74 84 await m_poll;
85
86 // schedule next poll
75 87 ScheduleNextPoll(Interval);
76 88 } catch (Exception e) {
89 // hanle error
77 90 UnhandledException.DispatchEvent(this, new UnhandledExceptionEventArgs(e, false));
78 91
79 92 if (FailOnError)
80 93 Fail(e);
81 94 else
82 95 ScheduleNextPoll(Interval);
83 96 }
84 97
85 98 }
86 99
87 100 protected override void Dispose(bool disposing) {
88 101 if (disposing)
89 102 Safe.Dispose(m_timer, m_cancellation);
90 103 base.Dispose(disposing);
91 104 }
92 105
93 106 }
94 107 } No newline at end of file
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
General Comments 3
Under Review
author

Auto status change to "Under Review"

Approved
author

ok, latest stable version should be in default

You need to be logged in to leave comments. Login now