diff --git a/Samples/Package.swift b/Samples/Package.swift index 3b8decbc1..e03ef0312 100644 --- a/Samples/Package.swift +++ b/Samples/Package.swift @@ -29,6 +29,14 @@ var targets: [PackageDescription.Target] = [ ] ), + .executableTarget( + name: "SamplePair", + dependencies: [ + .product(name: "DistributedCluster", package: "swift-distributed-actors"), + ], + path: "Sources/SamplePair" + ), + /* --- tests --- */ // no-tests placeholder project to not have `swift test` fail on Samples/ diff --git a/Samples/Sources/SamplePair/Person.swift b/Samples/Sources/SamplePair/Person.swift new file mode 100644 index 000000000..83adf5839 --- /dev/null +++ b/Samples/Sources/SamplePair/Person.swift @@ -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)") + } +} diff --git a/Samples/Sources/SamplePair/boot.swift b/Samples/Sources/SamplePair/boot.swift new file mode 100644 index 000000000..a81e60134 --- /dev/null +++ b/Samples/Sources/SamplePair/boot.swift @@ -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 + } +}