forked from sbozzie/test-intel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntelChannelContainer.cs
More file actions
730 lines (676 loc) · 28.8 KB
/
Copy pathIntelChannelContainer.cs
File metadata and controls
730 lines (676 loc) · 28.8 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
using PleaseIgnore.IntelMap.Properties;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
namespace PleaseIgnore.IntelMap {
/// <summary>
/// Manages the list of <see cref="IntelChannel"/> watched by an instance
/// of <see cref="IntelReporter"/>.
/// </summary>
/// <threadsafety static="true" instance="true"/>
[DefaultEvent("IntelReported")]
public class IntelChannelContainer : Component, IContainer, INestedContainer,
INotifyPropertyChanged {
// Default value of the ChannelUpdateInterval property
internal const string defaultUpdateInterval = "24:00:00";
// Default value of the RetryInterval property
internal const string defaultRetryInterval = "00:15:00";
// Thread Synchronization object
private readonly object syncRoot = new object();
// List of active IntelChannel objects
private readonly List<IntelChannel> channels
= new List<IntelChannel>();
// Timer object used to fetch updated channel lists
private readonly Timer updateTimer;
// The current channel processing state
[ContractPublicPropertyName("Status")]
private volatile IntelStatus status;
// The update period for the channel list
[ContractPublicPropertyName("ChannelUpdateInterval")]
private TimeSpan? updateInterval = TimeSpan.Parse(
defaultUpdateInterval,
CultureInfo.InvariantCulture);
// The network retry period for the channel list
[ContractPublicPropertyName("RetryInterval")]
private TimeSpan retryInterval = TimeSpan.Parse(
defaultRetryInterval,
CultureInfo.InvariantCulture);
// The intel upload count
[ContractPublicPropertyName("IntelCount")]
private int uploadCount;
// URI to use when fetching the channel list
[ContractPublicPropertyName("ChannelListUri")]
private Uri channelListUri = IntelExtensions.ChannelsUrl;
// Directory to use when overriding the IntelChannel's Path
[ContractPublicPropertyName("Path")]
public string logDirectory;
// The contents of the channel list the last time we fetched it
private string[] channelList;
/// <summary>
/// Initializes a new instance of the <see cref="IntelChannelContainer"/>
/// class.
/// </summary>
public IntelChannelContainer() : this(null) {
Contract.Ensures(Status == IntelStatus.Stopped);
Contract.Ensures(Container == null);
}
/// <summary>
/// Initializes a new instance of the <see cref="IntelChannelContainer"/>
/// class and adds it to the specified container.
/// </summary>
public IntelChannelContainer(IContainer container) {
Contract.Ensures(Status == IntelStatus.Stopped);
Contract.Ensures(Container == container);
this.updateTimer = new Timer(this.timer_Callback);
if (container != null) {
container.Add(this);
}
}
/// <summary>
/// Occurs when a new log entry has been read from the chat logs.
/// </summary>
public event EventHandler<IntelEventArgs> IntelReported;
/// <summary>
/// Occurs when a property value changes.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Gets the current operational status of the
/// <see cref="IntelChannelContainer"/> object and its children.
/// </summary>
public IntelStatus Status {
get { return this.status; }
private set {
Contract.Ensures(Status == value);
if (this.status != value) {
this.status = value;
this.OnPropertyChanged(new PropertyChangedEventArgs("Status"));
}
}
}
/// <summary>
/// Gets a value indicating whether the <see cref="IntelChannelContainer"/>
/// is currently running and watching for log entries.
/// </summary>
public bool IsRunning { get { return this.status.IsRunning(); } }
/// <summary>
/// Gets an instance of <see cref="IntelChannelCollection"/>
/// </summary>
/// <remarks>
/// Calling <see cref="IntelChannel.Dispose"/> on an
/// <see cref="IntelChannel"/> will remove it from
/// <see cref="Channels"/>. It may be readded when the
/// channel list is redownloaded.
/// </remarks>
public IntelChannelCollection Channels {
get {
lock (this.syncRoot) {
return new IntelChannelCollection(this.channels.ToArray());
}
}
}
/// <summary>
/// Gets or sets the time between downloads of the intel
/// channel list.
/// </summary>
/// <value>
/// The time between downloads of the intel channel list or
/// <see langword="null"/> to disable periodic downloads of
/// the channel list.
/// </value>
[DefaultValue(typeof(TimeSpan), defaultUpdateInterval)]
public TimeSpan? ChannelUpdateInterval {
get {
Contract.Ensures(!Contract.Result<TimeSpan?>().HasValue
|| (Contract.Result<TimeSpan?>() > TimeSpan.Zero));
return this.updateInterval;
}
set {
Contract.Requires<InvalidOperationException>(!IsRunning);
Contract.Requires<ArgumentOutOfRangeException>(
!value.HasValue || (value > TimeSpan.Zero),
"value");
Contract.Ensures(ChannelUpdateInterval == value);
if (this.updateInterval != value) {
this.updateInterval = value;
this.OnPropertyChanged(new PropertyChangedEventArgs("ChannelUpdatePeriod"));
}
}
}
/// <summary>
/// Gets or sets the time to wait after a failed attempt to
/// download the channel list before trying again.
/// </summary>
[DefaultValue(typeof(TimeSpan), defaultRetryInterval)]
public TimeSpan RetryInterval {
get {
Contract.Ensures(Contract.Result<TimeSpan>() > TimeSpan.Zero);
return this.retryInterval;
}
set {
Contract.Requires<InvalidOperationException>(!IsRunning);
Contract.Requires<ArgumentOutOfRangeException>(
value > TimeSpan.Zero,
"value");
Contract.Ensures(RetryInterval == value);
if (this.retryInterval != value) {
this.retryInterval = value;
this.OnPropertyChanged(new PropertyChangedEventArgs("RetryInterval"));
}
}
}
/// <summary>
/// Gets or sets the <see cref="Uri"/> to use when downloading
/// the channel list.
/// </summary>
[AmbientValue((string)null)]
public string ChannelListUri {
get {
Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<string>()));
return (this.channelListUri ?? IntelExtensions.ChannelsUrl).OriginalString;
}
set {
Contract.Requires<InvalidOperationException>(!IsRunning);
var uri = (value != null) ? new Uri(value) : IntelExtensions.ChannelsUrl;
if (!uri.IsAbsoluteUri) {
// TODO: Proper exception
throw new ArgumentException();
}
if (this.channelListUri != uri) {
this.channelListUri = uri;
this.OnPropertyChanged(new PropertyChangedEventArgs("ChannelListUri"));
}
}
}
/// <summary>
/// Gets or sets the directory to search for log files.
/// </summary>
[DefaultValue((string)null)]
public string Path {
get { return this.logDirectory; }
set {
lock (this.syncRoot) {
if (value != this.logDirectory) {
this.logDirectory = value;
this.channels.ForEach(x => x.Path = (value ?? IntelChannel.DefaultPath));
this.OnPropertyChanged(new PropertyChangedEventArgs("Path"));
}
}
}
}
/// <summary>
/// Gets an object that can be used for synchronization of
/// the component state.
/// </summary>
/// <value>
/// An <see cref="Object"/> that can be used by classes
/// derived from <see cref="IntelChannelContainer"/> to
/// synchronize their own internal state.
/// </value>
protected object SyncRoot {
get {
Contract.Ensures(Contract.Result<object>() != null);
return this.syncRoot;
}
}
/// <summary>
/// Gets the number of reports that have been made by this
/// <see cref="IntelChannel"/>.
/// </summary>
public int IntelCount { get { return this.uploadCount; } }
/// <summary>
/// Downloads the channel list and begins the acquisition of log
/// entries from the EVE chat logs. This method enables
/// <see cref="IntelReported"/> events.
/// </summary>
public void Start() {
Contract.Requires<ObjectDisposedException>(
Status != IntelStatus.Disposed,
null);
Contract.Requires<InvalidOperationException>(
Status != IntelStatus.FatalError);
Contract.Ensures(IsRunning);
lock (this.syncRoot) {
if (this.status == IntelStatus.Stopped) {
try {
this.Status = IntelStatus.Starting;
this.OnStart();
this.Status = IntelStatus.Waiting;
this.OnPropertyChanged(new PropertyChangedEventArgs("IsRunning"));
} catch {
this.Status = IntelStatus.FatalError;
throw;
}
}
}
}
/// <summary>
/// Stops the <see cref="IntelChannelChannel"/> from providing
/// location data and events. <see cref="IntelReported"/>
/// events will no longer be raised.
/// </summary>
public void Stop() {
Contract.Ensures(!IsRunning);
lock (this.syncRoot) {
if (this.IsRunning) {
try {
this.Status = IntelStatus.Stopping;
this.OnStop();
this.Status = IntelStatus.Stopped;
} catch {
this.Status = IntelStatus.FatalError;
throw;
} finally {
this.OnPropertyChanged(new PropertyChangedEventArgs("IsRunning"));
}
}
}
}
/// <inheritdoc/>
protected override void Dispose(bool disposing) {
Contract.Ensures(Status == IntelStatus.Disposed);
Contract.Ensures(!IsRunning);
if (disposing) {
lock (this.syncRoot) {
if (this.status != IntelStatus.Disposed) {
try {
this.Status = IntelStatus.Disposing;
this.updateTimer.Dispose();
channels.ForEach(x => x.Dispose());
} catch {
// Ignore any exceptions during disposal
} finally {
channels.Clear();
}
}
}
this.IntelReported = null;
this.PropertyChanged = null;
}
this.status = IntelStatus.Disposed;
base.Dispose(disposing);
}
/// <summary>
/// Creates an instance of <see cref="IntelChannel"/> to manage
/// the monitoring of an intel channel log file.
/// </summary>
/// <param name="channelName">
/// The base file name of the intel channel to monitor.
/// </param>
/// <returns>
/// An instance of <see cref="IntelChannel"/> to use when
/// monitoring the log file.
/// </returns>
/// <remarks>
/// Classes derived from <see cref="IntelChannelContainer"/> are
/// free to override <see cref="CreateChannel"/> and completely
/// replace the logic without calling the base implementation.
/// In this case, the derivative class must register handlers to
/// call <see cref="OnUpdateStatus"/> and <see cref="OnIntelReported"/>
/// under the appropriate circumstances. The
/// <see cref="IntelChannel.Site"/> must be initialzied to a proper
/// linking instance of <see cref="ISite"/>.
/// </remarks>
protected virtual IntelChannel CreateChannel(string channelName) {
Contract.Requires<ArgumentException>(!String.IsNullOrEmpty(channelName));
Contract.Ensures(Contract.Result<IntelChannel>() != null);
Contract.Ensures(Contract.Result<IntelChannel>().Site != null);
Contract.Ensures(Contract.Result<IntelChannel>().Site.Container == this);
Contract.Ensures(Contract.Result<IntelChannel>().Name == channelName);
var channel = new IntelChannel();
channel.Site = new ChannelSite(this, channel, channelName);
if (this.logDirectory != null) {
channel.Path = this.logDirectory;
}
channel.IntelReported += channel_IntelReported;
channel.PropertyChanged += channel_PropertyChanged;
return channel;
}
/// <summary>
/// Raises the <see cref="IntelReported"/> event.
/// </summary>
/// <param name="e">
/// Arguments of the event being raised.
/// </param>
/// <remarks>
/// <see cref="OnIntelReported"/> makes no changes to the internal
/// object state and can be safely called at any time. If the
/// logic within <see cref="CreateChannel"/> is replaced, the
/// <see cref="IntelChannel.IntelReported"/> event needs to be
/// forwarded to <see cref="OnIntelReported"/>.
/// </remarks>
protected virtual void OnIntelReported(IntelEventArgs e) {
Contract.Requires<ArgumentNullException>(e != null, "e");
Interlocked.Increment(ref this.uploadCount);
this.OnPropertyChanged(new PropertyChangedEventArgs("IntelCount"));
var handler = this.IntelReported;
if (handler != null) {
handler(this, e);
}
}
/// <summary>
/// Raises the <see cref="PropertyChanged"/> event.
/// </summary>
/// <param name="e">
/// Arguments of the event being raised.
/// </param>
/// <remarks>
/// <see cref="OnIntelReported"/> makes no changes to the internal
/// object state and can be safely called at any time. The
/// <see cref="PropertyChanged"/> event is scheduled for asynchronous
/// handling by the <see cref="ThreadPool"/>.
/// </remarks>
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) {
Contract.Requires<ArgumentNullException>(e != null, "e");
Debug.Assert(String.IsNullOrEmpty(e.PropertyName)
|| (this.GetType().GetProperty(e.PropertyName) != null));
var handler = this.PropertyChanged;
if (handler != null) {
ThreadPool.QueueUserWorkItem(delegate(object state) {
handler(this, e);
});
}
}
/// <summary>
/// Called after <see cref="Start()"/> has been called.
/// </summary>
/// <remarks>
/// <see cref="OnFileCreated"/> will be called with synchronized
/// access to the object state.
/// </remarks>
protected virtual void OnStart() {
this.channels.ForEach(x => x.Start());
this.updateTimer.Change(0, Timeout.Infinite);
}
/// <summary>
/// Called periodically to download the intel channel list and
/// update the list of <see cref="IntelChannel"/> components.
/// </summary>
/// <remarks>
/// As <see cref="OnUpdateList"/> downloads data from a remote
/// server, locks should not be maintained on object state during
/// the call to <see cref="OnUpdateList"/> as this may lead to
/// significantly impairments of the UI.
/// </remarks>
protected virtual void OnUpdateList() {
// A lot may have happened...
if (!this.IsRunning) {
return;
}
string[] list;
try {
// Download the new list outside the lock
list = GetChannelList(this.channelListUri);
} catch (WebException) {
// Retry in a few minutes
lock (this.syncRoot) {
if (this.IsRunning) {
this.updateTimer.Change(this.retryInterval, TimeSpan.Zero);
}
}
return;
}
// Alter program state within the lock
lock (this.syncRoot) {
if (!this.IsRunning) {
return;
}
if (this.channelList == null) {
// Initializing the channel list
this.channels.AddRange(list.Select(x => this.CreateChannel(x)));
this.channelList = list;
this.channels.ForEach(x => x.Start());
this.OnPropertyChanged(new PropertyChangedEventArgs("Channels"));
} else {
// Patching the existing channel list
var toAdd = list
.Except(this.channelList, StringComparer.OrdinalIgnoreCase)
.Select(x => this.CreateChannel(x))
.ToList();
var toRemove = this.channels
.Where(x => !list.Contains(x.Name, StringComparer.OrdinalIgnoreCase))
.ToList();
this.channels.AddRange(toAdd);
this.channelList = list;
toRemove.ForEach(x => x.Dispose());
toAdd.ForEach(x => x.Start());
if ((toAdd.Count > 0) || (toRemove.Count > 0)) {
this.OnPropertyChanged(new PropertyChangedEventArgs("Channels"));
}
}
// Schedule the next update
this.OnUpdateStatus();
if (this.updateInterval.HasValue) {
this.updateTimer.Change(this.updateInterval.Value, TimeSpan.Zero);
}
}
}
/// <summary>
/// Updates the <see cref="Status"/> property to reflect the aggregate
/// state of those components in <see cref="Channels"/>.
/// </summary>
/// <remarks>
/// If a derived class replaces <see cref="CreateChannel"/>, it
/// must ensure that <see cref="OnUpdateStatus"/> is called
/// when there is a change to the <see cref="IntelChannel.Status"/>
/// property by monitoring the <see cref="IntelChannel.PropertyChanged"/>
/// event.
/// </remarks>
protected virtual void OnUpdateStatus() {
lock (this.syncRoot) {
if (this.IsRunning) {
this.Status = this.channels.Aggregate(
IntelStatus.Waiting,
(sum, x) => IntelExtensions.Combine(sum, x.Status));
}
}
}
/// <summary>
/// Called after <see cref="Stop()"/> has been called.
/// </summary>
/// <remarks>
/// <see cref="OnStop()"/> will be called with synchronized
/// access to the object state.
/// </remarks>
protected virtual void OnStop() {
this.channels.ForEach(x => x.Stop());
this.updateTimer.Change(Timeout.Infinite, Timeout.Infinite);
}
/// <summary>
/// Calls <see cref="OnIntelReported"/>.
/// </summary>
private void channel_IntelReported(object sender, IntelEventArgs e) {
Contract.Requires(e != null);
this.OnIntelReported(e);
}
/// <summary>
/// Calls <see cref="OnUpdateStatus"/>, trapping any exceptions.
/// </summary>
private void channel_PropertyChanged(object sender, PropertyChangedEventArgs e) {
Contract.Requires(e != null);
if (String.IsNullOrEmpty(e.PropertyName) || (e.PropertyName == "Status")) {
this.OnUpdateStatus();
}
}
/// <summary>
/// Calls <see cref="OnTimer"/>, trapping any exceptions.
/// </summary>
private void timer_Callback(object state) {
try {
this.OnUpdateList();
} catch {
// Fail on error
lock (this.syncRoot) {
if (this.status != IntelStatus.Disposed) {
this.updateTimer.Dispose();
this.Status = IntelStatus.FatalError;
}
}
throw;
}
}
/// <summary>
/// Code Contracts class invariants.
/// </summary>
[ContractInvariantMethod]
private void ObjectInvariant() {
Contract.Invariant(!this.updateInterval.HasValue
|| (this.updateInterval > TimeSpan.Zero));
Contract.Invariant(this.retryInterval > TimeSpan.Zero);
Contract.Invariant(this.channelListUri != null);
Contract.Invariant(this.uploadCount >= 0);
Contract.Invariant(Contract.ForAll(this.channels, x => x != null));
}
/// <inheritdoc/>
ComponentCollection IContainer.Components {
get {
lock (this.syncRoot) {
return new ComponentCollection(channels.ToArray());
}
}
}
/// <inheritdoc/>
void IContainer.Add(IComponent component, string name) {
throw new NotSupportedException(Resources.IntelChannelCollection_ReadOnly);
}
/// <inheritdoc/>
void IContainer.Add(IComponent component) {
throw new NotSupportedException(Resources.IntelChannelCollection_ReadOnly);
}
/// <inheritdoc/>
void IContainer.Remove(IComponent component) {
// Called when the channel is being disposed
if (this.status != IntelStatus.Disposing) {
lock (this.syncRoot) {
var count = this.channels.RemoveAll(x => x == component);
if (count > 0) {
this.OnPropertyChanged(new PropertyChangedEventArgs("Channels"));
}
}
}
}
/// <inheritdoc/>
IComponent INestedContainer.Owner { get { return this; } }
/// <summary>
/// Downloads the list of channels to monitor from the Test
/// Alliance Intel Map server.
/// </summary>
/// <returns>
/// A <see cref="String"/> <see cref="Array"/> of channel
/// filenames.
/// </returns>
/// <exception cref="WebException">
/// There was a problem contacting the server or the server
/// response was invalid.
/// </exception>
public static string[] GetChannelList() {
Contract.Ensures(Contract.Result<string[]>() != null);
Contract.Ensures(Contract.Result<string[]>().Length > 0);
return GetChannelList(IntelExtensions.ChannelsUrl);
}
/// <summary>
/// Downloads the list of channels to monitor from a specific
/// reporting server.
/// </summary>
/// <param name="serviceUri">
/// The server URI to download maps from.
/// </param>
/// <returns>
/// A <see cref="String"/> <see cref="Array"/> of channel
/// filenames.
/// </returns>
/// <exception cref="WebException">
/// There was a problem contacting the server or the server
/// response was invalid.
/// </exception>
public static string[] GetChannelList(Uri serviceUri) {
Contract.Requires<ArgumentNullException>(serviceUri != null, "serviceUri");
Contract.Requires<ArgumentException>(serviceUri.IsAbsoluteUri);
Contract.Ensures(Contract.Result<string[]>() != null);
Contract.Ensures(Contract.Result<string[]>().Length > 0);
// TODO: More thorough sanity check of the server response
var channels = WebRequest
.Create(serviceUri)
.GetResponse()
.ReadContent()
.Split('\n', '\r')
.Select(x => x.Trim())
.Where(x => x.Length > 0)
.Select(x => x.Split(',')[0])
.ToArray();
if (channels.Length == 0) {
throw new WebException(Resources.IntelException);
} else {
return channels;
}
}
/// <summary>
/// Implementation of <see cref="ISite"/> for linking an instance of
/// <see cref="IntelChannel"/> to its parent
/// <see cref="IntelChannelContainer"/>.
/// </summary>
private class ChannelSite : INestedSite {
[ContractPublicPropertyName("Container")]
private readonly IntelChannelContainer container;
[ContractPublicPropertyName("Component")]
private readonly IntelChannel component;
[ContractPublicPropertyName("Name")]
private readonly string name;
internal ChannelSite(IntelChannelContainer container,
IntelChannel component, string name) {
Contract.Requires(container != null);
Contract.Requires(component != null);
Contract.Requires(!String.IsNullOrEmpty(name));
this.container = container;
this.component = component;
this.name = name;
}
public IComponent Component {
get { return this.component; }
}
public IContainer Container {
get { return this.container; }
}
public bool DesignMode {
get { return this.container.DesignMode; }
}
public string Name {
get { return this.name; }
set { throw new NotSupportedException(); }
}
public string FullName {
get {
var site = this.container.Site;
var siteName = (site != null) ? site.Name : null;
if (!String.IsNullOrEmpty(siteName)) {
return siteName + '.' + this.name;
} else {
return this.name;
}
}
}
public object GetService(Type serviceType) {
if (serviceType == typeof(ISite)) {
return this;
} else if(serviceType == typeof(INestedSite)) {
return this;
} else if (serviceType == typeof(IContainer)) {
return this.container;
} else if (serviceType == typeof(INestedContainer)) {
return this.container;
} else {
return this.container.GetService(serviceType);
}
}
}
}
}