PageRenderTime 50ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/src/scala/app/services/ServiceRegister.scala

https://gitlab.com/SharkyLV/Mirio
Scala | 40 lines | 30 code | 10 blank | 0 comment | 1 complexity | 95ac2c2b0c46d6b11c85afbc6bc09477 MD5 | raw file
  1. package app.services
  2. import akka.actor.ActorRef
  3. import app.utils.MongoTrait
  4. import app.{RootActor, Event, Config, TextEvent}
  5. import app.features.ServiceType.ServiceType
  6. import app.features.{ServiceType, Service}
  7. import app.features.ServiceType.ServiceType
  8. import app.models.Address
  9. import com.mongodb.casbah
  10. import com.mongodb.casbah.Imports._
  11. import com.mongodb.casbah.TypeImports
  12. import com.mongodb.casbah.commons.Imports
  13. class ServiceRegister extends Service with MongoTrait {
  14. override def SERVICE_TYPE: ServiceType = ServiceType.SERVICE_REGISTRY
  15. override def SERVICE_ADDRESS: Address = Address("127.0.0.1", 8080)
  16. lazy val serviceCol = MongoClient(mongoIP, mongoPort)(appName)(SERVICE_TYPE.toString)
  17. override def receive = {
  18. case RegisterServiceEvent(service, id) =>
  19. serviceCol.insert(MongoDBObject("service"->id,"path"->service.path.toString))
  20. println("Registered "+service.toString())
  21. case ListServicesEvent() =>
  22. val toList = serviceCol.find("path" $exists true)
  23. val strings: Iterator[String] = for (x <- toList) yield x.getAsOrElse[String]("path", "").toString
  24. sender ! ListServicesResponse(strings)
  25. case HeartBeatPing(key) => sender ! HeartBeatPong(key)
  26. }
  27. }
  28. case class RegisterServiceEvent(service:ActorRef, id:String) extends Event
  29. case class ListServicesEvent() extends Event
  30. case class ListServicesResponse(services:Iterator[String]) extends Event