Advertisement
Guest User

Untitled

a guest
Sep 16th, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.08 KB | None | 0 0
  1. trait SimpleRoutingApp extends HttpService {
  2.  
  3.   @volatile private[this] var _refFactory: Option[ActorRefFactory] = None
  4.  
  5.   implicit def actorRefFactory = _refFactory getOrElse sys.error(
  6.     "Route creation is not fully supported before `startServer` has been called, " +
  7.       "maybe you can turn your route definition into a `def` ?")
  8.  
  9.   /**
  10.    * Starts a new spray-can HTTP server with a default HttpService for the given route and binds the server to the
  11.    * given interface and port.
  12.    * The method returns a Future on the Bound event returned by the HttpListener as a reply to the Bind command.
  13.    * You can use the Future to determine when the server is actually up (or you can simply drop it if you are not
  14.    * interested in it).
  15.    */
  16.   def startServer(interface: String,
  17.                   port: Int,
  18.                   serviceActorName: String = "simple-service-actor",
  19.                   backlog: Int = 100,
  20.                   options: immutable.Traversable[Inet.SocketOption] = Nil,
  21.                   settings: Option[ServerSettings] = None)(route: ⇒ Route)(implicit system: ActorSystem, sslEngineProvider: ServerSSLEngineProvider,
  22.                                                                            bindingTimeout: Timeout = 1.second): Future[Http.Bound] = {
  23.     val serviceActor = system.actorOf(
  24.       props = Props {
  25.         new Actor {
  26.           _refFactory = Some(context)
  27.           def receive = {
  28.             val system = 0 // shadow implicit system
  29.             runRoute(route)
  30.           }
  31.         }
  32.       },
  33.       name = serviceActorName)
  34.     IO(Http).ask(Http.Bind(serviceActor, interface, port, backlog, options, settings)).flatMap {
  35.       case b: Http.Bound ⇒ Future.successful(b)
  36.       case Tcp.CommandFailed(b: Http.Bind)
  37.         // TODO: replace by actual exception when Akka #3861 is fixed.
  38.         //       see https://www.assembla.com/spaces/akka/tickets/3861
  39.         Future.failed(new RuntimeException(
  40.           "Binding failed. Switch on DEBUG-level logging for `akka.io.TcpListener` to log the cause."))
  41.     }(system.dispatcher)
  42.   }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement