-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ActorPath.cs
838 lines (736 loc) · 28.8 KB
/
ActorPath.cs
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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
//-----------------------------------------------------------------------
// <copyright file="ActorPath.cs" company="Akka.NET Project">
// Copyright (C) 2009-2021 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2021 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Akka.Util;
using Newtonsoft.Json;
using static System.String;
namespace Akka.Actor
{
/// <summary>
/// Actor path is a unique path to an actor that shows the creation path
/// up through the actor tree to the root actor.
/// ActorPath defines a natural ordering (so that ActorRefs can be put into
/// collections with this requirement); this ordering is intended to be as fast
/// as possible, which owing to the bottom-up recursive nature of ActorPath
/// is sorted by path elements FROM RIGHT TO LEFT, where RootActorPath >
/// ChildActorPath in case the number of elements is different.
/// Two actor paths are compared equal when they have the same name and parent
/// elements, including the root address information. That does not necessarily
/// mean that they point to the same incarnation of the actor if the actor is
/// re-created with the same path. In other words, in contrast to how actor
/// references are compared the unique id of the actor is not taken into account
/// when comparing actor paths.
/// </summary>
public abstract class ActorPath : IEquatable<ActorPath>, IComparable<ActorPath>, ISurrogated
{
/// <summary>
/// This class represents a surrogate of an <see cref="ActorPath"/>.
/// Its main use is to help during the serialization process.
/// </summary>
public class Surrogate : ISurrogate, IEquatable<Surrogate>, IEquatable<ActorPath>
{
/// <summary>
/// Initializes a new instance of the <see cref="Surrogate"/> class.
/// </summary>
/// <param name="path">The string representation of the actor path.</param>
public Surrogate(string path)
{
Path = path;
}
/// <summary>
/// The string representation of the actor path
/// </summary>
public string Path { get; }
/// <summary>
/// Creates an <see cref="ActorPath"/> encapsulated by this surrogate.
/// </summary>
/// <param name="system">The actor system that contains this actor path.</param>
/// <returns>The <see cref="ActorPath"/> encapsulated by this surrogate.</returns>
public ISurrogated FromSurrogate(ActorSystem system)
{
if (TryParse(Path, out var path))
{
return path;
}
return null;
}
#region Equality
/// <inheritdoc/>
public bool Equals(Surrogate other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return string.Equals(Path, other.Path);
}
/// <inheritdoc/>
public bool Equals(ActorPath other)
{
if (other == null) return false;
return Equals(other.ToSurrogate(null)); //TODO: not so sure if this is OK
}
/// <inheritdoc/>
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
var actorPath = obj as ActorPath;
if (actorPath != null) return Equals(actorPath);
return Equals(obj as Surrogate);
}
/// <inheritdoc/>
public override int GetHashCode()
{
return Path.GetHashCode();
}
#endregion
}
/// <summary>
/// INTERNAL API
/// </summary>
internal static readonly char[] ValidSymbols = @"""-_.*$+:@&=,!~';""()".ToCharArray();
/// <summary>
/// Method that checks if actor name conforms to RFC 2396, http://www.ietf.org/rfc/rfc2396.txt
/// Note that AKKA JVM does not allow parenthesis ( ) but, according to RFC 2396 those are allowed, and
/// since we use URL Encode to create valid actor names, we must allow them.
/// </summary>
/// <param name="s">TBD</param>
/// <returns>TBD</returns>
public static bool IsValidPathElement(string s)
{
if (IsNullOrEmpty(s))
{
return false;
}
return !s.StartsWith("$") && Validate(s);
}
private static bool IsValidChar(char c) => (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') || ValidSymbols.Contains(c);
private static bool IsHexChar(char c) => (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') ||
(c >= '0' && c <= '9');
private static bool Validate(string chars)
{
int len = chars.Length;
var pos = 0;
while (pos < len)
{
if (IsValidChar(chars[pos]))
{
pos = pos + 1;
}
else if (chars[pos] == '%' && pos + 2 < len && IsHexChar(chars[pos + 1]) && IsHexChar(chars[pos + 2]))
{
pos = pos + 3;
}
else
{
return false;
}
}
return true;
}
/// <summary>
/// Initializes a new instance of the <see cref="ActorPath" /> class.
/// </summary>
/// <param name="address"> The address. </param>
/// <param name="name"> The name. </param>
protected ActorPath(Address address, string name)
{
Name = name;
Address = address;
}
/// <summary>
/// Initializes a new instance of the <see cref="ActorPath" /> class.
/// </summary>
/// <param name="parentPath"> The parent path. </param>
/// <param name="name"> The name. </param>
/// <param name="uid"> The uid. </param>
protected ActorPath(ActorPath parentPath, string name, long uid)
{
Address = parentPath.Address;
Uid = uid;
Name = name;
}
/// <summary>
/// Gets the uid.
/// </summary>
/// <value> The uid. </value>
public long Uid { get; }
internal static readonly string[] EmptyElements = { };
/// <summary>
/// Gets the elements.
/// </summary>
/// <value> The elements. </value>
public abstract IReadOnlyList<string> Elements { get; }
/// <summary>
/// INTERNAL API.
///
/// Used in Akka.Remote - when resolving deserialized local actor references
/// we need to be able to include the UID at the tail end of the elements.
///
/// It's implemented in this class because we don't have an ActorPathExtractor equivalent.
/// </summary>
internal IReadOnlyList<string> ElementsWithUid
{
get
{
if (this is RootActorPath) return EmptyElements;
var elements = (List<string>)Elements;
elements[elements.Count - 1] = AppendUidFragment(Name);
return elements;
}
}
/// <summary>
/// Gets the name.
/// </summary>
/// <value> The name. </value>
public string Name { get; }
/// <summary>
/// The Address under which this path can be reached; walks up the tree to
/// the RootActorPath.
/// </summary>
/// <value> The address. </value>
public Address Address { get; }
/// <summary>
/// The root actor path.
/// </summary>
public abstract ActorPath Root { get; }
/// <summary>
/// The path of the parent to this actor.
/// </summary>
public abstract ActorPath Parent { get; }
/// <inheritdoc/>
public bool Equals(ActorPath other)
{
if (other == null)
return false;
if (!Address.Equals(other.Address))
return false;
ActorPath a = this;
ActorPath b = other;
for (; ; )
{
if (ReferenceEquals(a, b))
return true;
else if (a == null || b == null)
return false;
else if (a.Name != b.Name)
return false;
a = a.Parent;
b = b.Parent;
}
}
/// <inheritdoc/>
public abstract int CompareTo(ActorPath other);
/// <summary>
/// Withes the uid.
/// </summary>
/// <param name="uid"> The uid. </param>
/// <returns> ActorPath. </returns>
public abstract ActorPath WithUid(long uid);
/// <summary>
/// Creates a new <see cref="ChildActorPath"/> with the specified parent <paramref name="path"/>
/// and the specified <paramref name="name"/>.
/// </summary>
/// <param name="path">The parent path of the newly created actor path</param>
/// <param name="name">The name of child actor path</param>
/// <returns>A newly created <see cref="ChildActorPath"/></returns>
public static ActorPath operator /(ActorPath path, string name)
{
var (s, uid) = ActorCell.GetNameAndUid(name);
return new ChildActorPath(path, s, uid);
}
/// <summary>
/// Creates a new <see cref="ActorPath"/> by appending all the names in <paramref name="name"/>
/// to the specified <paramref name="path"/>.
/// </summary>
/// <param name="path">The base path of the newly created actor path.</param>
/// <param name="name">The names being appended to the specified <paramref name="path"/>.</param>
/// <returns>A newly created <see cref="ActorPath"/></returns>
public static ActorPath operator /(ActorPath path, IEnumerable<string> name)
{
var a = path;
foreach (string element in name)
{
if (!string.IsNullOrEmpty(element))
a = a / element;
}
return a;
}
/// <summary>
/// Creates an <see cref="ActorPath"/> from the specified <paramref name="path"/>.
/// </summary>
/// <param name="path">The string representing a possible <see cref="ActorPath"/></param>
/// <exception cref="UriFormatException">
/// This exception is thrown if the given <paramref name="path"/> cannot be parsed into an <see cref="ActorPath"/>.
/// </exception>
/// <returns>A newly created <see cref="ActorPath"/></returns>
public static ActorPath Parse(string path)
{
ActorPath actorPath;
if (TryParse(path, out actorPath))
{
return actorPath;
}
throw new UriFormatException($"Can not parse an ActorPath: {path}");
}
/// <summary>
/// Tries to parse the uri, which should be a full uri, i.e containing protocol.
/// For example "akka://System/user/my-actor"
/// </summary>
/// <param name="path">TBD</param>
/// <param name="actorPath">TBD</param>
/// <returns>TBD</returns>
public static bool TryParse(string path, out ActorPath actorPath)
{
actorPath = null;
if (!TryParseAddress(path, out var address, out var absoluteUri)) return false;
var spanified = absoluteUri;
// check for Uri fragment here
var fragment = ReadOnlySpan<char>.Empty;
var fragLoc = spanified.IndexOf('#');
if (fragLoc > -1)
{
fragment = spanified.Slice(fragLoc + 1);
spanified = spanified.Slice(0, fragLoc);
}
var nextSlash = 0;
actorPath = new RootActorPath(address);
do
{
nextSlash = spanified.IndexOf('/');
if (nextSlash > 0)
{
actorPath /= spanified.Slice(0, nextSlash).ToString();
}
else if (nextSlash < 0 && spanified.Length > 0)
{
actorPath /= spanified.ToString();
}
spanified = spanified.Slice(nextSlash + 1);
} while (nextSlash >= 0);
if (!fragment.IsEmpty)
{
var uid = SpanHacks.Parse(fragment);
actorPath = actorPath.WithUid(uid);
}
return true;
}
/// <summary>
/// Attempts to parse an <see cref="Address"/> from a stringified <see cref="ActorPath"/>.
/// </summary>
/// <param name="path">The string representation of the <see cref="ActorPath"/>.</param>
/// <param name="address">If <c>true</c>, the parsed <see cref="Address"/>. Otherwise <c>null</c>.</param>
/// <returns><c>true</c> if the <see cref="Address"/> could be parsed, <c>false</c> otherwise.</returns>
public static bool TryParseAddress(string path, out Address address)
{
return TryParseAddress(path, out address, out var _);
}
/// <summary>
/// Attempts to parse an <see cref="Address"/> from a stringified <see cref="ActorPath"/>.
/// </summary>
/// <param name="path">The string representation of the <see cref="ActorPath"/>.</param>
/// <param name="address">If <c>true</c>, the parsed <see cref="Address"/>. Otherwise <c>null</c>.</param>
/// <param name="absoluteUri">A <see cref="ReadOnlySpan{T}"/> containing the path following the address.</param>
/// <returns><c>true</c> if the <see cref="Address"/> could be parsed, <c>false</c> otherwise.</returns>
private static bool TryParseAddress(string path, out Address address, out ReadOnlySpan<char> absoluteUri)
{
address = null;
var spanified = path.AsSpan();
absoluteUri = spanified;
var firstColonPos = spanified.IndexOf(':');
if (firstColonPos == -1) // not an absolute Uri
return false;
var fullScheme = SpanHacks.ToLowerInvariant(spanified.Slice(0, firstColonPos));
if (!fullScheme.StartsWith("akka"))
return false;
spanified = spanified.Slice(firstColonPos + 1);
if (spanified.Length < 2 || !(spanified[0] == '/' && spanified[1] == '/'))
return false;
spanified = spanified.Slice(2); // move past the double //
var firstAtPos = spanified.IndexOf('@');
var sysName = string.Empty;
if (firstAtPos == -1)
{ // dealing with an absolute local Uri
var nextSlash = spanified.IndexOf('/');
if (nextSlash == -1)
{
sysName = spanified.ToString();
absoluteUri = "/".AsSpan(); // RELY ON THE JIT
}
else
{
sysName = spanified.Slice(0, nextSlash).ToString();
absoluteUri = spanified.Slice(nextSlash);
}
address = new Address(fullScheme, sysName);
return true;
}
// dealing with a remote Uri
sysName = spanified.Slice(0, firstAtPos).ToString();
spanified = spanified.Slice(firstAtPos + 1);
/*
* Need to check for:
* - IPV4 / hostnames
* - IPV6 (must be surrounded by '[]') according to spec.
*/
var host = string.Empty;
// check for IPV6 first
var openBracket = spanified.IndexOf('[');
var closeBracket = spanified.IndexOf(']');
if (openBracket > -1 && closeBracket > openBracket)
{
// found an IPV6 address
host = spanified.Slice(openBracket, closeBracket - openBracket + 1).ToString();
spanified = spanified.Slice(closeBracket + 1); // advance past the address
// need to check for trailing colon
var secondColonPos = spanified.IndexOf(':');
if (secondColonPos == -1)
return false;
spanified = spanified.Slice(secondColonPos + 1);
}
else
{
var secondColonPos = spanified.IndexOf(':');
if (secondColonPos == -1)
return false;
host = spanified.Slice(0, secondColonPos).ToString();
// move past the host
spanified = spanified.Slice(secondColonPos + 1);
}
var actorPathSlash = spanified.IndexOf('/');
ReadOnlySpan<char> strPort;
if (actorPathSlash == -1)
{
strPort = spanified;
}
else
{
strPort = spanified.Slice(0, actorPathSlash);
}
if (SpanHacks.TryParse(strPort, out var port))
{
address = new Address(fullScheme, sysName, host, port);
// need to compute the absolute path after the Address
if (actorPathSlash == -1)
{
absoluteUri = "/".AsSpan();
}
else
{
absoluteUri = spanified.Slice(actorPathSlash);
}
return true;
}
return false;
}
/// <summary>
/// Joins this instance.
/// </summary>
/// <returns> System.String. </returns>
private string Join()
{
if (this is RootActorPath)
return "/";
// Resolve length of final string
int totalLength = 0;
ActorPath p = this;
while (!(p is RootActorPath))
{
totalLength += p.Name.Length + 1;
p = p.Parent;
}
// Concatenate segments (in reverse order) into buffer with '/' prefixes
char[] buffer = new char[totalLength];
int offset = buffer.Length;
p = this;
while (!(p is RootActorPath))
{
offset -= p.Name.Length + 1;
buffer[offset] = '/';
p.Name.CopyTo(0, buffer, offset + 1, p.Name.Length);
p = p.Parent;
}
return new string(buffer);
}
/// <summary>
/// String representation of the path elements, excluding the address
/// information. The elements are separated with "/" and starts with "/",
/// e.g. "/user/a/b".
/// </summary>
/// <returns> System.String. </returns>
public string ToStringWithoutAddress()
{
return Join();
}
/// <inheritdoc/>
public override string ToString()
{
return $"{Address}{Join()}";
}
/// <summary>
/// Returns a string representation of this instance including uid.
/// </summary>
/// <returns>TBD</returns>
public string ToStringWithUid()
{
var uid = Uid;
if (uid == ActorCell.UndefinedUid)
return ToStringWithAddress();
return ToStringWithAddress() + "#" + uid;
}
/// <summary>
/// Creates a child with the specified name
/// </summary>
/// <param name="childName"> Name of the child. </param>
/// <returns> ActorPath. </returns>
public ActorPath Child(string childName)
{
return this / childName;
}
/// <inheritdoc/>
public override int GetHashCode()
{
unchecked
{
var hash = 17;
hash = (hash * 23) ^ Address.GetHashCode();
foreach (var e in Elements)
hash = (hash * 23) ^ e.GetHashCode();
return hash;
}
}
/// <inheritdoc/>
public override bool Equals(object obj)
{
var other = obj as ActorPath;
return Equals(other);
}
/// <summary>
/// Compares two specified actor paths for equality.
/// </summary>
/// <param name="left">The first actor path used for comparison</param>
/// <param name="right">The second actor path used for comparison</param>
/// <returns><c>true</c> if both actor paths are equal; otherwise <c>false</c></returns>
public static bool operator ==(ActorPath left, ActorPath right)
{
return Equals(left, right);
}
/// <summary>
/// Compares two specified actor paths for inequality.
/// </summary>
/// <param name="left">The first actor path used for comparison</param>
/// <param name="right">The second actor path used for comparison</param>
/// <returns><c>true</c> if both actor paths are not equal; otherwise <c>false</c></returns>
public static bool operator !=(ActorPath left, ActorPath right)
{
return !Equals(left, right);
}
/// <summary>
/// Generate String representation, with the address in the RootActorPath.
/// </summary>
/// <returns> System.String. </returns>
public string ToStringWithAddress()
{
return ToStringWithAddress(Address);
}
/// <summary>
/// TBD
/// </summary>
/// <returns>TBD</returns>
public string ToSerializationFormat()
{
return AppendUidFragment(ToStringWithAddress());
}
/// <summary>
/// TBD
/// </summary>
/// <param name="address">TBD</param>
/// <returns>TBD</returns>
public string ToSerializationFormatWithAddress(Address address)
{
if (IgnoreActorRef.IsIgnoreRefPath(this))
{
// we never change address for IgnoreActorRef
return ToString();
}
var withAddress = ToStringWithAddress(address);
var result = AppendUidFragment(withAddress);
return result;
}
private string AppendUidFragment(string withAddress)
{
if (Uid == ActorCell.UndefinedUid)
return withAddress;
return String.Concat(withAddress, "#", Uid.ToString());
}
/// <summary>
/// Generate String representation, replacing the Address in the RootActorPath
/// with the given one unless this path’s address includes host and port
/// information.
/// </summary>
/// <param name="address"> The address. </param>
/// <returns> System.String. </returns>
public string ToStringWithAddress(Address address)
{
if (IgnoreActorRef.IsIgnoreRefPath(this))
{
// we never change address for IgnoreActorRef
return ToString();
}
if (Address.Host != null && Address.Port.HasValue)
return $"{Address}{Join()}";
return $"{address}{Join()}";
}
/// <summary>
/// TBD
/// </summary>
/// <param name="pathElements">TBD</param>
/// <returns>TBD</returns>
public static string FormatPathElements(IEnumerable<string> pathElements)
{
return String.Join("/", pathElements);
}
/// <summary>
/// Creates a surrogate representation of the current <see cref="ActorPath"/>.
/// </summary>
/// <param name="system">The actor system that references this actor path.</param>
/// <returns>The surrogate representation of the current <see cref="ActorPath"/>.</returns>
public ISurrogate ToSurrogate(ActorSystem system)
{
return new Surrogate(ToSerializationFormat());
}
}
/// <summary>
/// Actor paths for root guardians, such as "/user" and "/system"
/// </summary>
public class RootActorPath : ActorPath
{
/// <summary>
/// Initializes a new instance of the <see cref="RootActorPath" /> class.
/// </summary>
/// <param name="address"> The address. </param>
/// <param name="name"> The name. </param>
public RootActorPath(Address address, string name = "")
: base(address, name)
{
}
/// <inheritdoc/>
public override ActorPath Parent => null;
public override IReadOnlyList<string> Elements => EmptyElements;
/// <inheritdoc/>
[JsonIgnore]
public override ActorPath Root => this;
/// <inheritdoc/>
public override ActorPath WithUid(long uid)
{
if (uid == 0)
return this;
throw new NotSupportedException("RootActorPath must have undefined Uid");
}
/// <inheritdoc/>
public override int CompareTo(ActorPath other)
{
if (other is ChildActorPath) return 1;
return Compare(ToString(), other.ToString(), StringComparison.Ordinal);
}
}
/// <summary>
/// Actor paths for child actors, which is to say any non-guardian actor.
/// </summary>
public class ChildActorPath : ActorPath
{
private readonly string _name;
private readonly ActorPath _parent;
/// <summary>
/// Initializes a new instance of the <see cref="ChildActorPath" /> class.
/// </summary>
/// <param name="parentPath"> The parent path. </param>
/// <param name="name"> The name. </param>
/// <param name="uid"> The uid. </param>
public ChildActorPath(ActorPath parentPath, string name, long uid)
: base(parentPath, name, uid)
{
_name = name;
_parent = parentPath;
}
/// <inheritdoc/>
public override ActorPath Parent => _parent;
public override IReadOnlyList<string> Elements
{
get
{
ActorPath p = this;
var acc = new Stack<string>();
while (true)
{
if (p is RootActorPath)
return acc.ToList();
acc.Push(p.Name);
p = p.Parent;
}
}
}
/// <inheritdoc/>
public override ActorPath Root
{
get
{
var current = _parent;
while (current is ChildActorPath child)
{
current = child._parent;
}
return current.Root;
}
}
/// <summary>
/// Creates a copy of the given ActorPath and applies a new Uid
/// </summary>
/// <param name="uid"> The uid. </param>
/// <returns> ActorPath. </returns>
public override ActorPath WithUid(long uid)
{
if (uid == Uid)
return this;
return new ChildActorPath(_parent, _name, uid);
}
/// <inheritdoc/>
public override int GetHashCode()
{
unchecked
{
var hash = 17;
hash = (hash * 23) ^ Address.GetHashCode();
for (ActorPath p = this; p != null; p = p.Parent)
hash = (hash * 23) ^ p.Name.GetHashCode();
return hash;
}
}
/// <inheritdoc/>
public override int CompareTo(ActorPath other)
{
return InternalCompareTo(this, other);
}
private int InternalCompareTo(ActorPath left, ActorPath right)
{
if (ReferenceEquals(left, right)) return 0;
var leftRoot = left as RootActorPath;
if (leftRoot != null)
return leftRoot.CompareTo(right);
var rightRoot = right as RootActorPath;
if (rightRoot != null)
return -rightRoot.CompareTo(left);
var nameCompareResult = Compare(left.Name, right.Name, StringComparison.Ordinal);
if (nameCompareResult != 0)
return nameCompareResult;
return InternalCompareTo(left.Parent, right.Parent);
}
}
}