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