Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Samples/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
45 changes: 45 additions & 0 deletions Samples/Sources/SamplePair/Person.swift
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)")
}
}
59 changes: 59 additions & 0 deletions Samples/Sources/SamplePair/boot.swift
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess second-actor.


default:
print("Please select 'first' or 'second'")
return
}

try? await Task.sleep(for: .seconds(60))
_ = p
Copy link

@lyzkov lyzkov Aug 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_ = p changes nothing and means nothing, breaking Occam's Razor principle. The _ = p definition exists only to suppress compiler warnings. Are there any other ways of retaining Person instance without declaring an unused constant? autoreleasepool?

}
}