|
| 1 | +package actors |
| 2 | + |
| 3 | +import org.apache.pekko.actor.{Actor, ActorRef, Props} |
| 4 | +import org.apache.pekko.pattern.{ask, pipe} |
| 5 | +import org.apache.pekko.util.Timeout |
| 6 | + |
| 7 | +import scala.concurrent.ExecutionContext |
| 8 | +import scala.concurrent.duration._ |
| 9 | + |
| 10 | +/** |
| 11 | + * Actor that manages a WebSocket connection for a user in a specific project. |
| 12 | + */ |
| 13 | +object ProjectClientActor { |
| 14 | + def props(out: ActorRef, |
| 15 | + userId: Int, |
| 16 | + projectId: Int, |
| 17 | + registry: ActorRef): Props = |
| 18 | + Props(new ProjectClientActor(out, userId, projectId, registry)) |
| 19 | + |
| 20 | + /** |
| 21 | + * Message indicating that the ProjectActor has been found in the registry. |
| 22 | + * @param projectRef the ActorRef of the ProjectActor |
| 23 | + */ |
| 24 | + private case class RegistryFound(projectRef: ActorRef) |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Actor that manages a WebSocket connection for a user in a specific project. |
| 29 | + * It registers the user with the ProjectActor upon creation and deregisters |
| 30 | + * upon termination. |
| 31 | + * |
| 32 | + * @param out the ActorRef to send messages to the WebSocket |
| 33 | + * @param userId the ID of the user |
| 34 | + * @param projectId the ID of the project |
| 35 | + * @param registry the ActorRef of the ProjectActorRegistry |
| 36 | + */ |
| 37 | +class ProjectClientActor(out: ActorRef, |
| 38 | + userId: Int, |
| 39 | + projectId: Int, |
| 40 | + registry: ActorRef) |
| 41 | + extends Actor { |
| 42 | + import ProjectActor._ |
| 43 | + import ProjectActorRegistry._ |
| 44 | + import ProjectClientActor._ |
| 45 | + |
| 46 | + // Execution context and timeout for ask pattern |
| 47 | + implicit val ec: ExecutionContext = context.dispatcher |
| 48 | + implicit val timeout: Timeout = Timeout(3.seconds) |
| 49 | + |
| 50 | + // Reference to the ProjectActor, once found |
| 51 | + private var projectRefOpt: Option[ActorRef] = None |
| 52 | + |
| 53 | + // On start, ask the registry for the ProjectActor |
| 54 | + override def preStart(): Unit = { |
| 55 | + (registry ? GetProjectActor(projectId)) |
| 56 | + .mapTo[ActorRef] |
| 57 | + .map(RegistryFound) pipeTo self |
| 58 | + } |
| 59 | + |
| 60 | + // On stop, inform the ProjectActor that the user is leaving |
| 61 | + override def postStop(): Unit = { |
| 62 | + projectRefOpt.foreach(_ ! Leave(userId)) |
| 63 | + super.postStop() |
| 64 | + } |
| 65 | + |
| 66 | + // Handle incoming messages |
| 67 | + def receive: Receive = { |
| 68 | + case RegistryFound(projectRef) => |
| 69 | + projectRefOpt = Some(projectRef) |
| 70 | + projectRef ! Join(userId, out) |
| 71 | + } |
| 72 | +} |
0 commit comments