51 lines
1.4 KiB
Scala
51 lines
1.4 KiB
Scala
package cm.gintou
|
|
|
|
import akka.actor.typed.ActorSystem
|
|
import com.typesafe.config.ConfigFactory
|
|
import scala.concurrent.Await
|
|
import scala.concurrent.duration.Duration
|
|
import scala.util.Try
|
|
|
|
object WordCountApp {
|
|
|
|
def main(args: Array[String]): Unit = {
|
|
/*
|
|
Paramètres attendus :
|
|
1) role : "master" ou "worker"
|
|
2) port : le port sur lequel ce nœud va écouter
|
|
3) filePath : si role = "master", chemin vers le fichier
|
|
|
|
Exemples de lancement en local :
|
|
sbt "runMain cm.gintou.WordCountApp master 2551 /chemin/vers/monFichier.txt"
|
|
sbt "runMain cm.gintou.WordCountApp worker 2552"
|
|
*/
|
|
|
|
if (args.length < 2) {
|
|
println("Usage:\n WordCountApp master <port> <filePath>\n WordCountApp worker <port>")
|
|
System.exit(1)
|
|
}
|
|
|
|
val role = args(0)
|
|
val port = args(1).toInt
|
|
val filePathOpt = if (role == "master" && args.length >= 3) Some(args(2)) else None
|
|
|
|
// On construit une configuration Akka en surchargeant le port et en attribuant un rôle
|
|
val config = ConfigFactory.parseString(
|
|
s"""
|
|
|akka.remote.artery.canonical.port = $port
|
|
|akka.cluster.roles = [ "$role" ]
|
|
|""".stripMargin
|
|
).withFallback(ConfigFactory.load())
|
|
|
|
// On crée l'ActorSystem avec l'acteur guardian
|
|
val system = ActorSystem(
|
|
GuardianActor(role, filePathOpt),
|
|
"WordCountCluster",
|
|
config
|
|
)
|
|
|
|
Await.result(system.whenTerminated, Duration.Inf)
|
|
}
|
|
|
|
}
|