11using Akka . Actor ;
2+ using Akka . Configuration ;
3+
24using McpServer . Actor ;
35using McpServer . Actor . Model ;
46
@@ -14,23 +16,57 @@ public class ActorService
1416
1517 public IActorRef HistoryActor { get ; set ; }
1618
17- public ActorService ( )
19+ public ActorService ( bool serverMode )
1820 {
19- actorSystem = ActorSystem . Create ( "MyActorSystem" ) ;
20-
21- SearchActor = actorSystem . ActorOf < SearchActor > ( "search-actor" ) ;
22- RecordActor = actorSystem . ActorOf < RecordActor > ( "record-actor" ) ;
23- HistoryActor = actorSystem . ActorOf < HistoryActor > ( "history-actor" ) ;
24-
25- RecordActor . Tell ( new SetHistoryActorCommand ( )
21+ Console . WriteLine ( $ "ActorService initialized in { ( serverMode ? "Server" : "Client" ) } mode.") ;
22+
23+ // HOCON 설정
24+ var config = ConfigurationFactory . ParseString ( $@ "
25+ akka {{
26+ actor {{
27+ provider = ""Akka.Remote.RemoteActorRefProvider, Akka.Remote""
28+ }}
29+ remote {{
30+ dot-netty.tcp {{
31+ hostname = ""127.0.0.1""
32+ port = { ( serverMode ? 5500 : 0 ) } // 서버 모드일 때만 포트 5500 사용
33+ }}
34+ }}
35+ }}
36+ " ) ;
37+
38+ actorSystem = ActorSystem . Create ( "MyActorSystem" , config ) ;
39+
40+ if ( serverMode )
2641 {
27- HistoryActor = HistoryActor
28- } ) ;
29-
30- SearchActor . Tell ( new SetHistoryActorCommand ( )
42+ // 서버 모드일 때만 특정 액터를 초기화
43+ SearchActor = actorSystem . ActorOf < SearchActor > ( "search-actor" ) ;
44+ RecordActor = actorSystem . ActorOf < RecordActor > ( "record-actor" ) ;
45+ HistoryActor = actorSystem . ActorOf < HistoryActor > ( "history-actor" ) ;
46+
47+ RecordActor . Tell ( new SetHistoryActorCommand ( )
48+ {
49+ HistoryActor = HistoryActor
50+ } ) ;
51+
52+ SearchActor . Tell ( new SetHistoryActorCommand ( )
53+ {
54+ HistoryActor = HistoryActor
55+ } ) ;
56+ }
57+ else
3158 {
32- HistoryActor = HistoryActor
33- } ) ;
59+ // 클라이언트 모드일 때 원격 액터 참조
60+ var remoteAddress = "akka.tcp://MyActorSystem@127.0.0.1:5500" ;
61+ SearchActor = actorSystem . ActorSelection ( $ "{ remoteAddress } /user/search-actor")
62+ . ResolveOne ( TimeSpan . FromSeconds ( 3 ) ) . Result ;
63+
64+ RecordActor = actorSystem . ActorSelection ( $ "{ remoteAddress } /user/record-actor")
65+ . ResolveOne ( TimeSpan . FromSeconds ( 3 ) ) . Result ;
66+
67+ HistoryActor = actorSystem . ActorSelection ( $ "{ remoteAddress } /user/history-actor")
68+ . ResolveOne ( TimeSpan . FromSeconds ( 3 ) ) . Result ;
69+ }
3470 }
3571
3672}
0 commit comments