-
Couldn't load subscription status.
- Fork 79
Another mini sample to show starting two processes and discovery #1139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift Distributed Actors open source project | ||
| // | ||
| // Copyright (c) 2018-2022 Apple Inc. and the Swift Distributed Actors project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // See CONTRIBUTORS.md for the list of Swift Distributed Actors project authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import Distributed | ||
| import DistributedCluster | ||
| import Logging | ||
|
|
||
| distributed actor Person: CustomStringConvertible { | ||
| private let name: String | ||
|
|
||
| @ActorID.Metadata(\.receptionID) | ||
| var receptionID: String | ||
|
|
||
|
|
||
| init(name: String, actorSystem: ActorSystem) async { | ||
| self.actorSystem = actorSystem | ||
| self.name = name | ||
|
|
||
| self.receptionID = "*" | ||
|
|
||
| await actorSystem.receptionist.checkIn(self) | ||
|
|
||
| Task { | ||
| for try await person in await actorSystem.receptionist.listing(of: Person.self) where person != self { | ||
| print("[\(name) on \(actorSystem.cluster.node)] Found: \(person)") | ||
| try await person.hello(message: "Hello from \(self) on \(actorSystem.cluster.node)!") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| distributed func hello(message: String) { | ||
| print("[\(name) on \(actorSystem.cluster.node)] Received message: \(message)") | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift Distributed Actors open source project | ||
| // | ||
| // Copyright (c) 2018-2022 Apple Inc. and the Swift Distributed Actors project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // See CONTRIBUTORS.md for the list of Swift Distributed Actors project authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import Distributed | ||
| import DistributedCluster | ||
| import Logging | ||
| import NIO | ||
|
|
||
| typealias DefaultDistributedActorSystem = ClusterSystem | ||
|
|
||
| @main enum Main { | ||
| static func main() async { | ||
| print("===-----------------------------------------------------===") | ||
| print("| Sample App Showing Two Actors Talking |") | ||
| print("| |") | ||
| print("| USAGE: swift run SamplePair first|second |") | ||
| print("===-----------------------------------------------------===") | ||
|
|
||
| let firstPort = 1111 | ||
| let secondPort = 2222 | ||
|
|
||
| switch CommandLine.arguments.dropFirst().first { | ||
| case "first": | ||
| let system = await ClusterSystem("system") { settings in | ||
| settings.bindPort = firstPort | ||
| settings.logging.logLevel = .error | ||
| } | ||
| let p = await Person(name: "first-actor", actorSystem: system) | ||
|
|
||
| case "second": | ||
| let system = await ClusterSystem("system") { settings in | ||
| settings.logging.logLevel = .error | ||
| settings.bindPort = secondPort | ||
| } | ||
| system.cluster.join(host: "127.0.0.1", port: firstPort) | ||
|
|
||
| try! await system.cluster.joined(within: .seconds(10)) | ||
| let p = await Person(name: "first-actor", actorSystem: system) | ||
|
|
||
| default: | ||
| print("Please select 'first' or 'second'") | ||
| return | ||
| } | ||
|
|
||
| try? await Task.sleep(for: .seconds(60)) | ||
| _ = p | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess
second-actor.