akka.io.IO Scala Examples

The following examples show how to use akka.io.IO. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: EchoServer.scala    From Neutrino   with Apache License 2.0 5 votes vote down vote up
package com.ebay.neutrino

import java.util.concurrent.TimeUnit

import akka.actor._
import akka.io.IO
import com.ebay.neutrino.util.Random
import com.typesafe.config.{Config, ConfigFactory}
import spray.can.Http
import spray.http._

import scala.concurrent.duration._


object EchoServer extends App {

  // Extract port from args, if provided
  val port = if (args.size > 0) args(0).toInt else 8081

  // Load our configuration from file and merge in the port parameter
  val config = ConfigFactory.parseString(s"echo-server.port = $port") withFallback ConfigFactory.load("echo.conf")
  val system = ActorSystem("echo-server", config)
  system.actorOf(Props[EchoServer], "echo-server")
}


class EchoServer extends Actor with ActorLogging {
  import scala.language.implicitConversions

  implicit val system = context.system
  val startup  = System.currentTimeMillis
  val settings = EchoServerSettings(system)

  //Use the system's dispatcher as ExecutionContext
  import system.dispatcher

  // Register connection service
  IO(Http) ! Http.Bind(self, interface = settings.host, port = settings.port)

  
case class EchoServerSettings(host: String, port: Int, random: Boolean, duration: FiniteDuration)
  extends Extension
{
  def latency = random match {
    case false => duration
    case true  => Random.nextMillis(duration)
  }
}

object EchoServerSettings {

  def apply(c: Config): EchoServerSettings = EchoServerSettings(
    c getString "host",
    c getInt "port",
    c getBoolean "random",
    c getDuration("duration", TimeUnit.MILLISECONDS) milliseconds
  )

  def apply(system: ActorSystem): EchoServerSettings =
    EchoServerSettings(system.settings.config getConfig "echo-server")

} 
Example 2
Source File: Main.scala    From quiz-management-service   with Apache License 2.0 5 votes vote down vote up
package com.danielasfregola.quiz.management

import akka.actor._
import akka.io.IO
import akka.pattern.ask
import akka.util.Timeout
import com.typesafe.config.ConfigFactory
import spray.can.Http

import scala.concurrent.duration._

object Main extends App {
  val config = ConfigFactory.load()
  val host = config.getString("http.host")
  val port = config.getInt("http.port")

  implicit val system = ActorSystem("quiz-management-service")

  val api = system.actorOf(Props(new RestInterface()), "httpInterface")

  implicit val executionContext = system.dispatcher
  implicit val timeout = Timeout(10 seconds)

  IO(Http).ask(Http.Bind(listener = api, interface = host, port = port))
    .mapTo[Http.Event]
    .map {
      case Http.Bound(address) =>
        println(s"REST interface bound to $address")
      case Http.CommandFailed(cmd) =>
        println("REST interface could not bind to " +
          s"$host:$port, ${cmd.failureMessage}")
        system.shutdown()
    }
} 
Example 3
Source File: HighchartsTestApp.scala    From scalajs-highcharts   with MIT License 5 votes vote down vote up
package com.karasiq.highcharts.test.backend

import java.util.concurrent.TimeUnit

import akka.actor._
import akka.io.IO
import akka.pattern.ask
import akka.util.Timeout
import spray.can.Http
import spray.http.MediaTypes
import spray.routing.HttpService

import scala.concurrent.Await
import scala.concurrent.duration._
import scala.language.postfixOps

object HighchartsTestApp extends App {
  final class AppHandler extends Actor with HttpService {
    override def receive: Actor.Receive = runRoute {
      get {
        compressResponse() {
          // Index page
          (pathSingleSlash & respondWithMediaType(MediaTypes.`text/html`)) {
            getFromResource("webapp/index.html")
          } ~
          // Other resources
          getFromResourceDirectory("webapp")
        }
      }
    }

    override def actorRefFactory: ActorRefFactory = context
  }

  def startup(): Unit = {
    implicit val timeout = Timeout(20 seconds)

    implicit val actorSystem = ActorSystem("highcharts-test")

    val service = actorSystem.actorOf(Props[AppHandler], "webService")

    IO(Http) ? Http.Bind(service, interface = "localhost", port = 9000)

    Runtime.getRuntime.addShutdownHook(new Thread(new Runnable {
      override def run(): Unit = {
        Await.result(actorSystem.terminate(), FiniteDuration(5, TimeUnit.MINUTES))
      }
    }))
  }

  startup()
} 
Example 4
Source File: RestClient.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.commons.rest.client

import java.net.URL
import java.util.UUID

import scala.concurrent.{ExecutionContext, Future}

import akka.io.IO
import akka.pattern.ask
import spray.can.Http
import spray.can.Http.HostConnectorInfo
import spray.client.pipelining._
import spray.http.{HttpCredentials, HttpRequest, HttpResponse}
import spray.httpx.unmarshalling.FromResponseUnmarshaller

trait RestClient extends RestClientImplicits {
  import RestClient._

  def apiUrl: URL
  def userId: Option[UUID]
  def userName: Option[String]
  def credentials: Option[HttpCredentials]
  implicit override val ctx: ExecutionContext = as.dispatcher

  private def hostConnectorFut(): Future[HostConnectorInfo] = {
    (IO(Http) ? Http.HostConnectorSetup(apiUrl.getHost, port = apiUrl.getPort)).mapTo[HostConnectorInfo]
  }

  private def sendReceivePipeline() : Future[HttpRequest => Future[HttpResponse]] = {
    for {
      HostConnectorInfo(hostConnector, _) <- hostConnectorFut()
    } yield {
      credentials.map(addCredentials).getOrElse[RequestTransformer](identity) ~>
        userId.map(id => addHeader(UserIdHeader, id.toString)).getOrElse[RequestTransformer](identity) ~>
        userName.map(addHeader(UserNameHeader, _)).getOrElse[RequestTransformer](identity) ~>
        sendReceive(hostConnector)
    }
  }

  private def unmarshalPipeline[U: FromResponseUnmarshaller](): Future[HttpRequest => Future[U]] = {
    for {
      sr <- sendReceivePipeline()
    } yield {
      sr ~> unmarshal[U]
    }
  }

  def fetchResponse[U : FromResponseUnmarshaller](
      req: HttpRequest
  ): Future[U] = {
    unmarshalPipeline().flatMap(_(req))
  }

  def fetchHttpResponse(req: HttpRequest) : Future[HttpResponse] = {
    sendReceivePipeline().flatMap(_(req))
  }

  def endpointPath(endpoint: String): String = {
    new URL(apiUrl, endpoint).getFile
  }
}


object RestClient {
  val UserIdHeader = "X-Seahorse-UserId"
  val UserNameHeader = "X-Seahorse-UserName"
} 
Example 5
Source File: WorkflowDownloadClient.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.workflowexecutor

import scala.concurrent.Future
import scala.concurrent.duration._
import scala.util.Try

import akka.actor.ActorSystem
import akka.io.IO
import akka.pattern.ask
import spray.can.Http
import spray.client.pipelining._
import spray.http._
import spray.util._

import ai.deepsense.commons.utils.Logging
import ai.deepsense.sparkutils
import ai.deepsense.workflowexecutor.exception.UnexpectedHttpResponseException

class WorkflowDownloadClient(
    val address: String,
    val path: String,
    val timeout: Int)
  extends Logging {

  val downloadUrl = (workflowId: String) =>
    s"$address/$path/$workflowId/download"

  def downloadWorkflow(workflowId: String): Future[String] = {

    logger.info(s"Downloading workflow $workflowId...")

    implicit val system = ActorSystem()
    import system.dispatcher
    implicit val timeoutSeconds = timeout.seconds

    val pipeline: HttpRequest => Future[HttpResponse] = sendReceive
    val futureResponse = pipeline(Get(downloadUrl(workflowId)))

    futureResponse.onComplete { _ =>
      Try(IO(Http).ask(Http.CloseAll)(1.second).await)
      sparkutils.AkkaUtils.terminate(system)
    }
    futureResponse.map(handleResponse)
  }

  private def handleResponse(response: HttpResponse): String = {
    response.status match {
      case StatusCodes.OK =>
        response.entity.data.asString
      case _ => throw UnexpectedHttpResponseException(
        "Workflow download failed", response.status, response.entity.data.asString)
    }
  }
} 
Example 6
Source File: ShieldActor.scala    From shield   with MIT License 5 votes vote down vote up
package shield.actors

import akka.actor._
import akka.io.{IO, Tcp}
import shield.config.{DomainSettings, Settings}
import shield.metrics.Instrumented
import shield.routing._
import spray.can.Http
import spray.http._

// todo: verify how Spray handles HEAD requests
// todo: 412 Precondition Failed support
// todo: 417 Expectation Failed support
// todo: does spray automatically handle `505 HTTP Version Not Supported`?
// todo: periodically inspect a response to validate it's compliance with the documented swagger schema
object ShieldActor {
  def props() : Props = Props(new ShieldActor())
}

object ShieldActorMsgs {
  case class DomainsUpdated(domains: Map[String, DomainSettings])
  case class RouterUpdated(domain: String, router: RequestRouter)
  case class ListenersUpdated(domain: String, listeners: collection.Map[String, ActorRef])
}
class ShieldActor() extends Actor with ActorLogging with RestartLogging with Instrumented {
  // todo: integrate spray's service statistics into this
  import context.system
  val settings = Settings(context.system)

  val domainWatcher = context.actorOf(Props(settings.DomainWatcher), "domain-watcher")
  var domains = Map[String, DomainSettings]()

  var defaultRouter = buildDefaultRouter()
  var routers = Map[String, RequestRouter]()
  var listeners = Map[String,  collection.Map[String, ActorRef]]()

  // start a new HTTP server on port 8080 with our service actor as the handler
  IO(Http) ! Http.Bind(self, interface = "0.0.0.0", port = settings.Port)

  val connectionCounter = metrics.counter("open-connections")
  val requestMeter = metrics.meter("requests")
  val connectionOpenMeter = metrics.meter("connection-open")
  val connectionCloseMeter = metrics.meter("connection-close")


  def receive: Receive = {
    // todo: graceful shutdown (save bound's sender for later)
    case Http.Bound(addr) =>
      log.info("Server successfully bound to {}", addr)

    case Http.CommandFailed(cmd) =>
      log.error("Failed to bind the server: cmd={}", cmd)
      system.shutdown()

    case _ : Tcp.Connected =>
      connectionCounter += 1
      connectionOpenMeter.mark()
      sender ! Tcp.Register(self)
    case _ : Tcp.ConnectionClosed =>
      connectionCounter -= 1
      connectionCloseMeter.mark()

    case request: HttpRequest =>
      requestMeter.mark()
      val domain = request.header[HttpHeaders.Host].map(_.host).getOrElse(request.uri.authority.host.address)
      val destination = for {
        location <- Some(settings.DefaultServiceLocation)
        domainSettings <- domains.get(domain)
        router <- routers.get(domain)
        listeners <- listeners.get(domain)
      } yield (location, domainSettings.IgnoreExtensions, router, listeners)
      val (location, ignoredExtensions, router, listenerList) = destination.getOrElse(settings.DefaultServiceLocation, Set[String](), defaultRouter, collection.Map[String, ActorRef]())

      // todo: profiling optimization: destroying these actors is expensive.  Make a resizable pool
      context.actorOf(RequestProcessor.props(router, listenerList, sender(), location, ignoredExtensions)) ! request

    case du: ShieldActorMsgs.DomainsUpdated =>
      val newDomains = du.domains.keySet.diff(domains.keySet)
      for (d <- newDomains) {
        routers += d -> BootstrapRouter.router(settings.DefaultServiceLocation, settings.LocalServiceName, settings.HealthcheckTemplate)
        listeners += d -> collection.Map[String, ActorRef]()
      }
      val removedDomains = domains.keySet.diff(du.domains.keySet)
      for (d <- removedDomains) {
        routers -= d
        listeners -= d
      }
      domains = du.domains
      defaultRouter = buildDefaultRouter()

    case ru: ShieldActorMsgs.RouterUpdated => routers += ru.domain -> ru.router
    case lu: ShieldActorMsgs.ListenersUpdated => listeners += lu.domain -> lu.listeners
  }

  private def buildDefaultRouter() : RequestRouter = {
    BootstrapRouter.defaultRouter(settings.DefaultServiceLocation, settings.LocalServiceName, settings.HealthcheckTemplate, domains.keySet)
  }
} 
Example 7
Source File: HttpTransport.scala    From shield   with MIT License 5 votes vote down vote up
package shield.transports

import akka.actor.{ActorRef, ActorRefFactory}
import akka.pattern.ask
import akka.io.IO
import akka.util.Timeout
import spray.can.Http
import spray.can.Http.HostConnectorSetup
import spray.can.client.HostConnectorSettings
import spray.http.Uri.Authority
import spray.http.{Uri, HttpResponsePart, HttpResponse, HttpRequest}
import spray.httpx.{ResponseTransformation, RequestBuilding}
import spray.util._
import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.duration._

object HttpTransport extends RequestBuilding with ResponseTransformation {
  // Imitating client\pipelining.scala from spray, but taking advantage of the fact that
  // Spray's "Request-level API" lets you send a tuple of (Request, ConnectorSetup) to specify destination
  // without an absolute uri or a Host header
  type SendReceive = spray.client.pipelining.SendReceive

  def httpTransport(host: Uri, futureTimeout: FiniteDuration)(implicit refFactory: ActorRefFactory, executionContext: ExecutionContext) : SendReceive =
    httpTransport(IO(Http)(actorSystem), HostConnectorSetup(host.authority.host.address, host.effectivePort, sslEncryption = host.scheme == "https"))(executionContext, Timeout(futureTimeout))

  def httpTransport(host: Uri, settings: HostConnectorSettings)(implicit refFactory: ActorRefFactory, executionContext: ExecutionContext,
                                                                futureTimeout: Timeout) : SendReceive =
    httpTransport(IO(Http)(actorSystem), HostConnectorSetup(host.authority.host.address, host.effectivePort, sslEncryption = host.scheme == "https", settings=Some(settings)))


  def httpTransport(transport: ActorRef, config: HostConnectorSetup)(implicit ec: ExecutionContext, futureTimeout: Timeout): SendReceive =
    request => transport ? (request, config) map {
      case x: HttpResponse          => x
      case x: HttpResponsePart      => sys.error("sendReceive doesn't support chunked responses, try sendTo instead")
      case x: Http.ConnectionClosed => sys.error("Connection closed before reception of response: " + x)
      case x                        => sys.error("Unexpected response from HTTP transport: " + x)
    }
} 
Example 8
Source File: TelnetClientActor.scala    From asura   with MIT License 5 votes vote down vote up
package asura.dubbo.actor

import java.net.InetSocketAddress

import akka.actor.{ActorRef, Props, Status}
import akka.io.{IO, Tcp}
import akka.util.ByteString
import asura.common.actor.BaseActor
import asura.common.util.LogUtils
import asura.dubbo.DubboConfig

class TelnetClientActor(remote: InetSocketAddress, listener: ActorRef) extends BaseActor {

  import Tcp._
  import context.system

  IO(Tcp) ! Connect(remote)

  override def receive: Receive = {
    case CommandFailed(_: Connect) =>
      listener ! ByteString(s"${TelnetClientActor.MSG_CONNECT_TO} ${remote.getAddress.getHostAddress}:${remote.getPort} ${TelnetClientActor.MSG_FAIL}\r\n")
      context stop self
    case Connected(remote, local) =>
      log.debug(s"local address: ${local}, remote address: ${remote}")
      listener ! ByteString(s"${TelnetClientActor.MSG_CONNECT_TO} ${remote.getAddress.getHostAddress}:${remote.getPort} ${TelnetClientActor.MSG_SUCCESS}\r\n")
      val remoteConnection = sender()
      remoteConnection ! Register(self)
      context.become {
        case data: ByteString =>
          remoteConnection ! Write(data)
        case CommandFailed(_: Write) =>
          listener ! ByteString("write failed\r\n")
        case Received(data) =>
          listener ! data
        case TelnetClientActor.CMD_CLOSE =>
          remoteConnection ! Close
        case _: ConnectionClosed =>
          listener ! ByteString(s"connection to ${remote.getAddress.getHostAddress}:${remote.getPort} closed\r\n")
          context stop self
      }
    case Status.Failure(t) =>
      val stackTrace = LogUtils.stackTraceToString(t)
      log.warning(stackTrace)
      listener ! t.getMessage
      context stop self
  }

  override def postStop(): Unit = log.debug(s"${self.path} stopped")
}


object TelnetClientActor {

  val CMD_CLOSE = "close"
  val MSG_CONNECT_TO = "connect to"
  val MSG_SUCCESS = "success"
  val MSG_FAIL = "fail"

  def props(remote: InetSocketAddress, replies: ActorRef) = {
    Props(new TelnetClientActor(remote, replies))
  }

  def props(address: String, port: Int, replies: ActorRef) = {
    Props(
      new TelnetClientActor(
        new InetSocketAddress(address, if (port > 0) port else DubboConfig.DEFAULT_PORT),
        replies
      )
    )
  }
} 
Example 9
Source File: KeepAliveTest.scala    From Neutrino   with Apache License 2.0 5 votes vote down vote up
package com.ebay.neutrino.channel

import akka.actor.ActorSystem
import akka.io.IO
import akka.pattern.ask
import akka.util.Timeout
import com.ebay.neutrino.{EchoIntegrationTest, ProxyIntegrationTest}
import org.scalatest._
import spray.can.Http
import spray.http._

import scala.concurrent.Await
import scala.concurrent.duration._


class KeepAliveTest extends FlatSpec with ProxyIntegrationTest with EchoIntegrationTest with Matchers {
  behavior of "KeepAlive integration support"

  // Default requests
  override val configurationFile = "proxy-echo-test.conf"

  implicit val system = ActorSystem()
  implicit val timeout: Timeout = 15 seconds
  val Get = HttpRequest(HttpMethods.GET, Uri("http://localhost:8080/someurl"))


  def send(request: HttpRequest) = (IO(Http) ? request).mapTo[HttpResponse]


  it should "successfully send a single HTTP 1.1 GET" in {
    val request  = Get
    val response = Await.result(send(request), 2 seconds)

    //response.status should be (StatusCodes.OK)
    //response.protocol should be (HttpProtocols.`HTTP/1.1`)
    //response.header[HttpHeaders.Connection] shouldBe a [Some[HttpHeader]]
    //response.headers find (_ == HttpHeaders.Connection) .hasKeepAlive should be (false)
  }

  it should "successfully send a single HTTP 1.1 GET with connection-close" in {
    val start    = System.currentTimeMillis
    val request  = Get.copy(headers = List(HttpHeaders.Connection("close")))
    val response = Await.result(send(request), 2 seconds)

    response.status should be (StatusCodes.OK)
    response.protocol should be (HttpProtocols.`HTTP/1.1`)
    response.header[HttpHeaders.Connection] should be (Some(HttpHeaders.Connection("close")))
  }

  it should "successfully send a single HTTP 1.0 GET" in {
    val request  = Get.copy(protocol=HttpProtocols.`HTTP/1.0`)
    val response = Await.result(send(request), 2 seconds)

    // Regardless of Client:1.0, server should support 1.1
    response.status should be (StatusCodes.OK)
    response.protocol should be (HttpProtocols.`HTTP/1.1`)
    // response.headers should not contain keep-alive
  }

  it should "successfully send a single HTTP 1.0 GET w. keepalive" in {
    val request  = Get.copy(protocol=HttpProtocols.`HTTP/1.0`, headers = List(HttpHeaders.Connection("Keep-Alive")))
    val response = Await.result(send(request), 2 seconds)

    // Regardless of Client:1.0, server should support 1.1
    response.status should be (StatusCodes.OK)
    response.protocol should be (HttpProtocols.`HTTP/1.1`)
    response.header[HttpHeaders.Connection] shouldBe a [Some[_]]
    response.header[HttpHeaders.Connection].get.hasKeepAlive should be (true)
  }
} 
Example 10
Source File: Main.scala    From quiz-management-service   with Apache License 2.0 5 votes vote down vote up
package com.danielasfregola.quiz.management

import akka.actor._
import akka.io.IO
import akka.pattern.ask
import akka.util.Timeout
import com.typesafe.config.ConfigFactory
import spray.can.Http

import scala.concurrent.duration._

object Main extends App {
  val config = ConfigFactory.load()
  val host = config.getString("http.host")
  val port = config.getInt("http.port")

  implicit val system = ActorSystem("quiz-management-service")
  implicit val executionContext = system.dispatcher
  implicit val timeout = Timeout(10 seconds)

  val api = system.actorOf(Props(new RestInterface))

  IO(Http).ask(Http.Bind(listener = api, interface = host, port = port))
    .mapTo[Http.Event]
    .map {
      case Http.Bound(address) =>
        println(s"REST interface bound to $address")
      case Http.CommandFailed(cmd) =>
        println("REST interface could not bind to " +
          s"$host:$port, ${cmd.failureMessage}")
        system.shutdown()
    }
} 
Example 11
Source File: StatsdExporter.scala    From akka-mon   with MIT License 5 votes vote down vote up
package org.akkamon.core.exporters

import java.net.InetSocketAddress

import akka.actor.{ActorRef, Actor, Props}
import akka.io.{Udp, IO}
import akka.util.ByteString
import org.akkamon.core.{Config, InstrumentExporter}


object StatsdExporter extends InstrumentExporter {

  def formatEvent(event: TimerEvent): String = s"${event.timer}:${event.value}|ms"
  def formatEvent(event: CounterEvent): String = s"${event.key}:${event.value}|g"  // we sent this as a gauge so statsd keeps track of the values
  def formatEvent(event: SampledEvent): String = s"${event.key}:${event.value}|c"

  val instrumentActor = system.actorOf(Props(classOf[SimpleSender], new InetSocketAddress(Config.StatsdHost, Config.StatsdPort)))

  class SimpleSender(remote: InetSocketAddress) extends Actor {

    IO(Udp) ! Udp.SimpleSender

    def receive = {
      case Udp.SimpleSenderReady =>
        context.become(ready(sender()))
    }

    def ready(send: ActorRef): Receive = {
      case msg: MessageEvent => // do nothing, a message can't be sent to statsd
      case timer: TimerEvent => send ! Udp.Send(ByteString(formatEvent(timer)), remote)
      case counter: CounterEvent => send ! Udp.Send(ByteString(formatEvent(counter)), remote)
      case counter: SampledEvent => send ! Udp.Send(ByteString(formatEvent(counter)), remote)
      case counterMap: CounterEventMap =>
        counterMap.counts.foreach{ case (key, value) => send ! Udp.Send(ByteString(formatEvent(CounterEvent(key, value))), remote)}
    }
  }
} 
Example 12
Source File: Boot.scala    From unicorn   with Apache License 2.0 5 votes vote down vote up
package unicorn.rhino

import scala.concurrent.duration._
import akka.actor.{ActorSystem, Props}
import akka.routing.FromConfig
import akka.io.IO
import spray.can.Http
import akka.pattern.ask
import akka.util.Timeout
import com.typesafe.config.ConfigFactory


object Boot extends App {

  // we need an ActorSystem to host our application in
  implicit val actorSystem = ActorSystem("unicorn-rhino")

  // create a pool of RhinoActors
  val service = actorSystem.actorOf(FromConfig.props(Props[RhinoActor]), "rhino-router")

  val conf = ConfigFactory.load()
  val serverPort = conf.getInt("spray.can.server.port")

  val ip = if (System.getProperty("loopback.only") != null) "127.0.0.1" else "0.0.0.0"
  IO(Http) ! Http.Bind(service, interface = ip, port = serverPort)
} 
Example 13
Source File: BootstrapTestApp.scala    From scalajs-bootstrap   with MIT License 5 votes vote down vote up
package com.karasiq.bootstrap.test.backend

import java.util.concurrent.TimeUnit

import scala.concurrent.Await
import scala.concurrent.duration._
import scala.language.postfixOps

import akka.actor._
import akka.io.IO
import akka.pattern.ask
import akka.util.Timeout
import spray.can.Http
import spray.http._
import spray.routing.HttpService

import com.karasiq.bootstrap.test.frontend.TestHtmlPage

object BootstrapTestApp extends App {
  final class AppHandler extends Actor with HttpService {
    override def receive: Actor.Receive = runRoute {
      get {
         {
          // Server-rendered page
          path("serverside.html") {
            complete(HttpResponse(entity = HttpEntity(ContentType(MediaTypes.`text/html`), TestHtmlPage())))
          } ~
          // Index page
          (pathSingleSlash & respondWithMediaType(MediaTypes.`text/html`)) {
            getFromResource("webapp/index.html")
          } ~
          // Other resources
          getFromResourceDirectory("webapp")
        }
      }
    }

    override def actorRefFactory: ActorRefFactory = context
  }

  def startup(): Unit = {
    implicit val timeout = Timeout(20 seconds)

    implicit val actorSystem = ActorSystem("bootstrap-test")

    Runtime.getRuntime.addShutdownHook(new Thread(new Runnable {
      override def run(): Unit = {
        Await.result(actorSystem.terminate(), FiniteDuration(5, TimeUnit.MINUTES))
      }
    }))

    val service = actorSystem.actorOf(Props[AppHandler], "webService")
    IO(Http) ? Http.Bind(service, interface = "localhost", port = 9000)
  }

  startup()
} 
Example 14
Source File: Main.scala    From geotrellis-osm-elevation   with Apache License 2.0 5 votes vote down vote up
package geotrellis.osme.server

import geotrellis.raster._
import geotrellis.raster.io.geotiff._
import geotrellis.raster.render._
import geotrellis.raster.resample._

import geotrellis.spark._
import geotrellis.spark.io._
import geotrellis.spark.io.file._
import geotrellis.spark.io.avro._
import geotrellis.spark.io.avro.codecs._

import geotrellis.spark.io.index._

import org.apache.spark._
import org.apache.avro.Schema

import com.github.nscala_time.time.Imports._
import akka.actor._
import akka.io.IO
import spray.can.Http
import spray.routing.{HttpService, RequestContext}
import spray.routing.directives.CachingDirectives
import spray.http.MediaTypes
import spray.json._
import spray.json.DefaultJsonProtocol._

import com.typesafe.config.ConfigFactory

import scala.concurrent._
import scala.collection.JavaConverters._
import scala.reflect.ClassTag

object Main {
  def main(args: Array[String]): Unit = {
    val conf =
      new SparkConf()
        .setIfMissing("spark.master", "local[*]")
        .setAppName("Osme Server")
        .set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
        .set("spark.kryo.registrator", "geotrellis.spark.io.hadoop.KryoRegistrator")

    implicit val sc = new SparkContext(conf)

    implicit val system = akka.actor.ActorSystem("demo-system")

    // create and start our service actor
    val service =
      system.actorOf(Props(classOf[OsmeServiceActor], sc), "osme")

    // start a new HTTP server on port 8088 with our service actor as the handler
    IO(Http) ! Http.Bind(service, "0.0.0.0", 8088)
  }
} 
Example 15
Source File: RestClient.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.commons.rest.client

import java.net.URL
import java.util.UUID

import scala.concurrent.{ExecutionContext, Future}

import akka.io.IO
import akka.pattern.ask
import spray.can.Http
import spray.can.Http.HostConnectorInfo
import spray.client.pipelining._
import spray.http.{HttpCredentials, HttpRequest, HttpResponse}
import spray.httpx.unmarshalling.FromResponseUnmarshaller

trait RestClient extends RestClientImplicits {
  import RestClient._

  def apiUrl: URL
  def userId: Option[UUID]
  def userName: Option[String]
  def credentials: Option[HttpCredentials]
  implicit override val ctx: ExecutionContext = as.dispatcher

  private def hostConnectorFut(): Future[HostConnectorInfo] = {
    (IO(Http) ? Http.HostConnectorSetup(apiUrl.getHost, port = apiUrl.getPort)).mapTo[HostConnectorInfo]
  }

  private def sendReceivePipeline() : Future[HttpRequest => Future[HttpResponse]] = {
    for {
      HostConnectorInfo(hostConnector, _) <- hostConnectorFut()
    } yield {
      credentials.map(addCredentials).getOrElse[RequestTransformer](identity) ~>
        userId.map(id => addHeader(UserIdHeader, id.toString)).getOrElse[RequestTransformer](identity) ~>
        userName.map(addHeader(UserNameHeader, _)).getOrElse[RequestTransformer](identity) ~>
        sendReceive(hostConnector)
    }
  }

  private def unmarshalPipeline[U: FromResponseUnmarshaller](): Future[HttpRequest => Future[U]] = {
    for {
      sr <- sendReceivePipeline()
    } yield {
      sr ~> unmarshal[U]
    }
  }

  def fetchResponse[U : FromResponseUnmarshaller](
      req: HttpRequest
  ): Future[U] = {
    unmarshalPipeline().flatMap(_(req))
  }

  def fetchHttpResponse(req: HttpRequest) : Future[HttpResponse] = {
    sendReceivePipeline().flatMap(_(req))
  }

  def endpointPath(endpoint: String): String = {
    new URL(apiUrl, endpoint).getFile
  }
}


object RestClient {
  val UserIdHeader = "X-Seahorse-UserId"
  val UserNameHeader = "X-Seahorse-UserName"
} 
Example 16
Source File: WorkflowDownloadClient.scala    From seahorse-workflow-executor   with Apache License 2.0 5 votes vote down vote up
package io.deepsense.workflowexecutor

import scala.concurrent.Future
import scala.concurrent.duration._
import scala.util.Try

import akka.actor.ActorSystem
import akka.io.IO
import akka.pattern.ask
import spray.can.Http
import spray.client.pipelining._
import spray.http._
import spray.util._

import io.deepsense.commons.utils.Logging
import io.deepsense.sparkutils
import io.deepsense.workflowexecutor.exception.UnexpectedHttpResponseException

class WorkflowDownloadClient(
    val address: String,
    val path: String,
    val timeout: Int)
  extends Logging {

  val downloadUrl = (workflowId: String) =>
    s"$address/$path/$workflowId/download"

  def downloadWorkflow(workflowId: String): Future[String] = {

    logger.info(s"Downloading workflow $workflowId...")

    implicit val system = ActorSystem()
    import system.dispatcher
    implicit val timeoutSeconds = timeout.seconds

    val pipeline: HttpRequest => Future[HttpResponse] = sendReceive
    val futureResponse = pipeline(Get(downloadUrl(workflowId)))

    futureResponse.onComplete { _ =>
      Try(IO(Http).ask(Http.CloseAll)(1.second).await)
      sparkutils.AkkaUtils.terminate(system)
    }
    futureResponse.map(handleResponse)
  }

  private def handleResponse(response: HttpResponse): String = {
    response.status match {
      case StatusCodes.OK =>
        response.entity.data.asString
      case _ => throw UnexpectedHttpResponseException(
        "Workflow download failed", response.status, response.entity.data.asString)
    }
  }
} 
Example 17
Source File: RestInterface.scala    From akka-sharding-example   with MIT License 5 votes vote down vote up
package com.michalplachta.shoesorter.api

import akka.actor.{Actor, ActorLogging, ActorRef}
import akka.io.IO
import akka.pattern.ask
import com.michalplachta.shoesorter.Domain.{Container, Junction}
import com.michalplachta.shoesorter.Messages._
import spray.can.Http
import spray.httpx.SprayJsonSupport._
import spray.routing._

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._

class RestInterface(decider: ActorRef, exposedPort: Int) extends Actor with HttpServiceBase with ActorLogging {
  val route: Route = {
    path("junctions" / IntNumber / "decisionForContainer" / IntNumber) { (junctionId, containerId) =>
      get {
        complete {
          log.info(s"Request for junction $junctionId and container $containerId")
          val junction = Junction(junctionId)
          val container = Container(containerId)
          decider.ask(WhereShouldIGo(junction, container))(5 seconds).mapTo[Go]
        }
      }
    }
  }

  def receive = runRoute(route)

  implicit val system = context.system
  IO(Http) ! Http.Bind(self, interface = "0.0.0.0", port = exposedPort)
} 
Example 18
Source File: ScoringTask.scala    From gearpump-examples   with Apache License 2.0 5 votes vote down vote up
package io.gearpump.examples.kafka_hdfs_pipeline

import akka.io.IO
import akka.pattern.ask
import io.gearpump.Message
import io.gearpump.cluster.UserConfig
import io.gearpump.streaming.task.{Task, TaskContext}
import io.gearpump.util.Constants
import spray.can.Http
import spray.http.HttpMethods._
import spray.http.{HttpRequest, HttpResponse, Uri}
import upickle.default._

import scala.concurrent.duration._
import scala.concurrent.{Await, ExecutionContext, Future}
import scala.util.{Failure, Success, Try}


class ScoringTask(taskContext : TaskContext, config: UserConfig) extends Task(taskContext, config) {
  implicit val timeout = Constants.FUTURE_TIMEOUT
  implicit val ec: ExecutionContext = system.dispatcher

  import taskContext.output

  override def onNext(msg: Message): Unit = {
    Try( {
      val jsonData = msg.msg.asInstanceOf[Array[Byte]]
      val jsonValue = new String(jsonData)
      val spaceShuttleMessage = read[SpaceShuttleMessage](jsonValue)
      val vector = read[Array[Float]](spaceShuttleMessage.body)
      val featureVector = vector.drop(1)
      val featureVectorString = featureVector.mkString(",")
      val url = s"http://atk-scoringengine.demo-gotapaas.com/v1/models/DemoModel/score?data=$featureVectorString"

      val result = Await.result((IO(Http) ? HttpRequest(GET, Uri(url))).asInstanceOf[Future[HttpResponse]], 5 seconds)
      val entity = result.entity.data.asString.toFloat
      entity match {
        case 1.0F =>
        case anomaly:Float =>
          output(Message(SpaceShuttleRecord(System.currentTimeMillis, anomaly), System.currentTimeMillis))
      }
    }) match {
      case Success(ok) =>
      case Failure(throwable) =>
        LOG.error(s"failed ${throwable.getMessage}")
    }

  }
} 
Example 19
Source File: ServerActor.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.network

import java.net.InetSocketAddress

import akka.actor.{Actor, ActorLogging, ActorRef, Props}
import akka.agent.Agent
import akka.io.Tcp.{Bind, Bound, CommandFailed, Connected}
import akka.io.{IO, Tcp}
import io.iohk.ethereum.utils.{NodeStatus, ServerStatus}
import org.spongycastle.util.encoders.Hex

class ServerActor(nodeStatusHolder: Agent[NodeStatus], peerManager: ActorRef) extends Actor with ActorLogging {

  import ServerActor._
  import context.system

  override def receive: Receive = {
    case StartServer(address) =>
      IO(Tcp) ! Bind(self, address)
      context become waitingForBindingResult
  }

  def waitingForBindingResult: Receive = {
    case Bound(localAddress) =>
      val nodeStatus = nodeStatusHolder()
      log.info("Listening on {}", localAddress)
      log.info("Node address: enode://{}@{}:{}",
        Hex.toHexString(nodeStatus.nodeId),
        getHostName(localAddress.getAddress),
        localAddress.getPort)
      nodeStatusHolder.send(_.copy(serverStatus = ServerStatus.Listening(localAddress)))
      context become listening

    case CommandFailed(b: Bind) =>
      log.warning("Binding to {} failed", b.localAddress)
      context stop self
  }

  def listening: Receive = {
    case Connected(remoteAddress, _) =>
      val connection = sender()
      peerManager ! PeerManagerActor.HandlePeerConnection(connection, remoteAddress)
  }
}

object ServerActor {
  def props(nodeStatusHolder: Agent[NodeStatus], peerManager: ActorRef): Props =
    Props(new ServerActor(nodeStatusHolder, peerManager))

  case class StartServer(address: InetSocketAddress)
} 
Example 20
Source File: TotalTweetsScheduler.scala    From redrock   with Apache License 2.0 5 votes vote down vote up
package com.restapi

import java.io.{File, FileInputStream}

import akka.actor.{ActorRef, Actor, ActorSystem, Props}
import akka.io.IO
import org.slf4j.LoggerFactory
import play.api.libs.json.Json
import spray.can.Http
import akka.pattern.ask
import spray.http.DateTime
import scala.concurrent.duration._
import akka.util.Timeout
import scala.concurrent.ExecutionContext.Implicits.global
import org.apache.commons.codec.digest.DigestUtils
import scala.io.Source

case object GetTotalTweetsScheduler

object CurrentTotalTweets {
  @volatile
  var totalTweets: Long = 0
}

class ExecuterTotalTweetsES(delay: FiniteDuration, interval: FiniteDuration) extends Actor {
  context.system.scheduler.schedule(delay, interval) {
    getTotalTweetsES
  }

  val logger = LoggerFactory.getLogger(this.getClass)

  override def receive: Actor.Receive = {
    case GetTotalTweetsScheduler => {
      logger.info(s"Getting Total of Tweets. Begin: ${CurrentTotalTweets.totalTweets}")
    }
    case _ => // just ignore any messages
  }

  def getTotalTweetsES: Unit = {
    val elasticsearchRequests = new GetElasticsearchResponse(0, Array[String](), Array[String](),
      LoadConf.restConf.getString("searchParam.defaulStartDatetime"),
      LoadConf.restConf.getString("searchParam.defaultEndDatetime"),
      LoadConf.esConf.getString("decahoseIndexName"))
    val totalTweetsResponse = Json.parse(elasticsearchRequests.getTotalTweetsESResponse())
    logger.info(s"Getting Total of Tweets. Current: ${CurrentTotalTweets.totalTweets}")
    CurrentTotalTweets.totalTweets = (totalTweetsResponse \ "hits" \ "total").as[Long]
    logger.info(s"Total users updated. New: ${CurrentTotalTweets.totalTweets}")
  }
} 
Example 21
Source File: Application.scala    From redrock   with Apache License 2.0 5 votes vote down vote up
package com.restapi

import akka.actor.{ActorSystem, Props}
import akka.io.IO
import spray.can.Http
import akka.pattern.ask
import scala.concurrent.duration._
import akka.util.Timeout
import org.slf4j.LoggerFactory;


object Application extends App {
  val logger = LoggerFactory.getLogger(this.getClass)
  
  // we need an ActorSystem to host our application in
  implicit val system = ActorSystem(LoadConf.restConf.getString("actor"))
  // create and start our service actor
  val service = system.actorOf(Props[MyServiceActor], LoadConf.restConf.getString("name"))
  val sessionTimeout = system.actorOf(Props[SessionTimeoutActor])

  val sessionTable = system.actorOf(Props(classOf[SimpleSession], sessionTimeout,
    LoadConf.accessConf.getInt("delay") seconds,
    LoadConf.accessConf.getInt("timeout-interval") seconds))
  sessionTable ! InitSessionTable

  val sessionLoader = system.actorOf(Props(classOf[LoadSessionActor], sessionTable,
    LoadConf.accessConf.getInt("delay") seconds,
    LoadConf.accessConf.getInt("check-interval") seconds))
  sessionLoader ! InitFileMd5Sum

  val schedTotalTweets = system.actorOf(Props(classOf[ExecuterTotalTweetsES],
    LoadConf.restConf.getInt("totalTweetsScheduler.delay") seconds,
    LoadConf.restConf.getInt("totalTweetsScheduler.reapeatEvery") seconds))
  schedTotalTweets ! GetTotalTweetsScheduler

  implicit val timeout = Timeout(800.seconds)
  IO(Http) ? Http.Bind(service, interface = "0.0.0.0", port = LoadConf.restConf.getInt("port"))

  logger.info( s"""Application: ${LoadConf.globalConf.getString("appName")} running version: ${LoadConf.globalConf.getString("appVersion")}""".stripMargin)
} 
Example 22
Source File: DatadogRegistrySpec.scala    From akka-http-metrics   with Apache License 2.0 5 votes vote down vote up
package fr.davit.akka.http.metrics.datadog

import java.net.InetSocketAddress

import akka.actor.ActorSystem
import akka.http.scaladsl.model.StatusCodes
import akka.io.{IO, Udp}
import akka.testkit.{TestKit, TestProbe}
import com.timgroup.statsd.NonBlockingStatsDClient
import fr.davit.akka.http.metrics.core.HttpMetricsRegistry.{PathDimension, StatusGroupDimension}
import org.scalatest.BeforeAndAfterAll
import org.scalatest.flatspec.AnyFlatSpecLike
import org.scalatest.matchers.should.Matchers

import scala.concurrent.duration._

class DatadogRegistrySpec extends TestKit(ActorSystem("DatadogRegistrySpec")) with AnyFlatSpecLike with Matchers with BeforeAndAfterAll {

  val dimensions = Seq(StatusGroupDimension(StatusCodes.OK), PathDimension("/api"))

  def withFixture(test: (TestProbe, DatadogRegistry) => Any) = {
    val statsd = TestProbe()
    statsd.send(IO(Udp), Udp.Bind(statsd.ref, new InetSocketAddress(0)))
    val port = statsd.expectMsgType[Udp.Bound].localAddress.getPort
    val socket = statsd.sender()
    val client = new NonBlockingStatsDClient("", "localhost", port)
    val registry = DatadogRegistry(client)
    try {
      test(statsd, registry)
    } finally {
      client.close()
      socket ! Udp.Unbind
    }
  }

  override def afterAll(): Unit = {
    shutdown()
    super.afterAll()
  }

  "DatadogRegistry" should "send active datagrams to the statsd server" in withFixture { (statsd, registry) =>
    registry.active.inc()
    statsd.expectMsgType[Udp.Received].data.utf8String shouldBe "akka.http.requests_active:1|c"
  }

  it should "send requests datagrams to the statsd server" in withFixture { (statsd, registry) =>
    registry.requests.inc()
    statsd.expectMsgType[Udp.Received].data.utf8String shouldBe "akka.http.requests_count:1|c"
  }

  it should "send receivedBytes datagrams to the statsd server" in withFixture { (statsd, registry) =>
    registry.receivedBytes.update(3)
    statsd.expectMsgType[Udp.Received].data.utf8String shouldBe "akka.http.requests_bytes:3|d"

    registry.receivedBytes.update(3, dimensions)
    statsd.expectMsgType[Udp.Received].data.utf8String shouldBe "akka.http.requests_bytes:3|d|#path:/api,status:2xx"
  }

  it should "send responses datagrams to the statsd server" in withFixture { (statsd, registry) =>
    registry.responses.inc()
    statsd.expectMsgType[Udp.Received].data.utf8String shouldBe "akka.http.responses_count:1|c"

    registry.responses.inc(dimensions)
    statsd.expectMsgType[Udp.Received].data.utf8String shouldBe "akka.http.responses_count:1|c|#path:/api,status:2xx"
  }

  it should "send errors datagrams to the statsd server" in withFixture { (statsd, registry) =>
    registry.errors.inc()
    statsd.expectMsgType[Udp.Received].data.utf8String shouldBe "akka.http.responses_errors_count:1|c"

    registry.errors.inc(dimensions)
    statsd.expectMsgType[Udp.Received].data.utf8String shouldBe "akka.http.responses_errors_count:1|c|#path:/api,status:2xx"
  }

  it should "send duration datagrams to the statsd server" in withFixture { (statsd, registry) =>
    registry.duration.observe(3.seconds)
    statsd.expectMsgType[Udp.Received].data.utf8String shouldBe "akka.http.responses_duration:3000|d"

    registry.duration.observe(3.seconds, dimensions)
    statsd.expectMsgType[Udp.Received].data.utf8String shouldBe "akka.http.responses_duration:3000|d|#path:/api,status:2xx"
  }

  it should "send sentBytes datagrams to the statsd server" in withFixture { (statsd, registry) =>
    registry.sentBytes.update(3)
    statsd.expectMsgType[Udp.Received].data.utf8String shouldBe "akka.http.responses_bytes:3|d"

    registry.sentBytes.update(3, dimensions)
    statsd.expectMsgType[Udp.Received].data.utf8String shouldBe "akka.http.responses_bytes:3|d|#path:/api,status:2xx"
  }

  it should "send connected datagrams to the statsd server" in withFixture { (statsd, registry) =>
    registry.connected.inc()
    statsd.expectMsgType[Udp.Received].data.utf8String shouldBe "akka.http.connections_active:1|c"
  }
  it should "send connections datagrams to the statsd server" in withFixture { (statsd, registry) =>
    registry.connections.inc()
    statsd.expectMsgType[Udp.Received].data.utf8String shouldBe "akka.http.connections_count:1|c"
  }
} 
Example 23
Source File: Boot.scala    From reactive-consul   with MIT License 5 votes vote down vote up
package stormlantern.consul.example

import java.net.URL

import akka.actor.ActorSystem
import akka.io.IO
import akka.pattern._
import akka.util.Timeout
import spray.can.Http
import spray.json.{ JsString, JsObject }
import stormlantern.consul.client.discovery.{ ConnectionStrategy, ServiceDefinition, ConnectionProvider }
import stormlantern.consul.client.loadbalancers.RoundRobinLoadBalancer
import stormlantern.consul.client.ServiceBroker
import stormlantern.consul.client.DNS

import scala.concurrent.Future
import scala.concurrent.duration._

object Boot extends App {
  implicit val system = ActorSystem("reactive-consul")
  implicit val executionContext = system.dispatcher

  val service = system.actorOf(ReactiveConsulHttpServiceActor.props(), "webservice")

  implicit val timeout = Timeout(5.seconds)

  IO(Http) ? Http.Bind(service, interface = "0.0.0.0", port = 8080)

  def connectionProviderFactory = (host: String, port: Int) ⇒ new ConnectionProvider {
    val client = new SprayExampleServiceClient(new URL(s"http://$host:$port"))
    override def getConnection: Future[Any] = Future.successful(client)
  }
  val connectionStrategy1 = ConnectionStrategy("example-service-1", connectionProviderFactory)
  val connectionStrategy2 = ConnectionStrategy("example-service-2", connectionProviderFactory)

  val services = Set(connectionStrategy1, connectionStrategy2)
  val serviceBroker = ServiceBroker(DNS.lookup("consul-8500.service.consul"), services)

  system.scheduler.schedule(5.seconds, 5.seconds) {
    serviceBroker.withService("example-service-1") { client: SprayExampleServiceClient ⇒
      client.identify
    }.foreach(println)
    serviceBroker.withService("example-service-2") { client: SprayExampleServiceClient ⇒
      client.identify
    }.foreach(println)
  }
} 
Example 24
Source File: VisualMailboxMetricClient.scala    From akka-visualmailbox   with Apache License 2.0 5 votes vote down vote up
package de.aktey.akka.visualmailbox

import java.net.InetSocketAddress

import akka.actor.{Actor, ActorRef, ExtendedActorSystem, Extension, ExtensionId, ExtensionIdProvider, Props}
import akka.io.{IO, Udp}
import akka.util.ByteString
import de.aktey.akka.visualmailbox.packing.Packing


object VisualMailboxMetricClient extends ExtensionId[VisualMailboxMetricClient] with ExtensionIdProvider {
  override def createExtension(system: ExtendedActorSystem): VisualMailboxMetricClient = {
    new VisualMailboxMetricClient(
      system,
      VisualMailboxMetricClientConfig.fromConfig(system.settings.config)
    )
  }

  override def lookup(): ExtensionId[_ <: Extension] = VisualMailboxMetricClient
}

class VisualMailboxMetricClient(system: ExtendedActorSystem, config: VisualMailboxMetricClientConfig) extends Extension {
  private val udpSender = system.systemActorOf(
    Props(new UdpSender(config.serverAddress)).withDispatcher("de.aktey.akka.visualmailbox.client.dispatcher"),
    "de-aktey-akka-visualmailbox-sender"
  )
  system.systemActorOf(
    Props(new VisualMailboxMetricListener(udpSender)).withDispatcher("de.aktey.akka.visualmailbox.client.dispatcher"),
    "de-aktey-akka-visualmailbox-receiver"
  )
}

class VisualMailboxMetricListener(udpSender: ActorRef) extends Actor {

  import context._

  import concurrent.duration._

  var buffer: List[VisualMailboxMetric] = Nil

  system.eventStream.subscribe(self, classOf[VisualMailboxMetric])
  system.scheduler.schedule(1.second, 1.second, self, "flush")

  @scala.throws[Exception](classOf[Exception])
  override def postStop(): Unit = {
    system.eventStream.unsubscribe(self)
  }

  def receive: Receive = {
    case v: VisualMailboxMetric =>
      buffer ::= v
      if (buffer.size > 40) self ! "flush"

    case "flush" if buffer.nonEmpty =>
      udpSender ! Packing.pack(MetricEnvelope(1, Packing.pack(buffer)))
      buffer = Nil
  }
}

class UdpSender(remote: InetSocketAddress) extends Actor {

  import context._

  IO(Udp) ! Udp.SimpleSender

  def receive = {
    case Udp.SimpleSenderReady =>
      context.become(ready(sender()))
  }

  def ready(send: ActorRef): Receive = {
    case msg: Array[Byte] =>
      send ! Udp.Send(ByteString(msg), remote)
  }
} 
Example 25
Source File: VisualMailboxMetricServer.scala    From akka-visualmailbox   with Apache License 2.0 5 votes vote down vote up
package de.aktey.akka.visualmailbox

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.Http.ServerBinding
import akka.io.Udp.{Bind, Bound, CommandFailed}
import akka.io.{IO, Udp}
import akka.pattern._
import akka.stream.ActorMaterializer
import akka.util.Timeout
import com.typesafe.config.ConfigFactory
import de.aktey.akka.visualmailbox.data.DataSourceEndpoint
import de.aktey.akka.visualmailbox.web.{Routing, WebConfig}

import scala.concurrent.duration._


object VisualMailboxMetricServer extends App {

  val allConfig = ConfigFactory.load()
  val config = VisualMailboxMetricClientConfig.fromConfig(allConfig)

  implicit val system = ActorSystem("visualmailbox-visualizer")
  implicit val meterializer = ActorMaterializer()
  implicit val bindTimeout = Timeout(2.seconds)

  import system._

  val router = system.actorOf(MetricsRouter.props(), "router")

  val dataHandler = system.actorOf(DataSourceEndpoint.props(router), "data-sink")

  (IO(Udp) ? Bind(dataHandler, config.serverAddress)).map {
    case CommandFailed(cmd) =>
      system.terminate()
    case Bound(address) =>
      log.info(s"""{"type":"udp-bound","address":"$address"}""")
  }

  val webConfig = WebConfig.fromConfig(allConfig)

  Http()
    .bindAndHandle(Routing.root(MetricFlow.metricSource(router)), webConfig.host, webConfig.port)
    .foreach { case ServerBinding(address) =>
      log.info(s"""{"type":"http-bound","address":"$address"}""")
    }
} 
Example 26
Source File: SBTBotTestRunner.scala    From perf_tester   with Apache License 2.0 5 votes vote down vote up
package org.perftester.sbtbot

import akka.actor.{Actor, ActorRef, ActorSystem, Props}
import akka.io.{IO, Tcp}
import akka.testkit.TestProbe
import ammonite.ops.Path
import org.perftester.sbtbot.SBTBot.{ExecuteTask, SBTBotReady, TaskResult}

object SBTBotTestRunner {

  
  def run(testDir: Path,
          programArgs: List[String],
          jvmArgs: List[String],
          repeats: Int,
          commands: List[String],
          debugging: Boolean): Unit = {
    implicit val actorSystem: ActorSystem = ActorSystem("test")

    val manager = IO(Tcp)

    val proxy = TestProbe()
    val parent = actorSystem.actorOf(Props(new Actor {
      val child: ActorRef =
        context.actorOf(SBTBot.props(testDir, programArgs, jvmArgs), "sbtbot")

      def receive: Receive = {
        case x if sender == child => proxy.ref forward x
        case x                    => child forward x
      }
    }))

    import scala.concurrent.duration._

    try {
      proxy.expectMsg(600.seconds, SBTBotReady)
      println("SBT Bot ready - starting run")
      val timeout = if (debugging) 20 minutes else 40 minutes

      for (i <- 1 to repeats) {
        implicit val sender: ActorRef = proxy.ref
        commands.zipWithIndex foreach {
          case (cmd, idx) =>
            println(
              s"--------------- $cmd - iteration  $i/$repeats -------------------------------")
            parent ! ExecuteTask(s"$idx", cmd)
            proxy.expectMsgClass(timeout, classOf[TaskResult])
//            Thread.sleep(5000)
        }
      }

      println("---------------Finished --------------------------------")
    } finally {
      actorSystem.terminate()
    }
  }
} 
Example 27
Source File: ChaosInterface.scala    From eventuate-chaos   with Apache License 2.0 5 votes vote down vote up
package com.rbmhtechnology.eventuate.chaos

import java.net.InetSocketAddress

import akka.actor.Actor
import akka.actor.ActorLogging
import akka.actor.ActorRef
import akka.io.IO
import akka.io.Tcp
import akka.util.ByteString

abstract class ChaosInterface extends Actor with ActorLogging {
  val port = 8080
  val endpoint = new InetSocketAddress(port)
  val command = """(?s)(\w+)\s+(\d+).*""".r

  implicit val ec = context.dispatcher

  IO(Tcp)(context.system) ! Tcp.Bind(self, endpoint)

  println(s"Now listening on port $port")

  def handleCommand: PartialFunction[(String, Option[Int], ActorRef), Unit]

  protected def reply(message: String, receiver: ActorRef) = {
    receiver ! Tcp.Write(ByteString(message))
    receiver ! Tcp.Close
  }

  protected def closeOnError(receiver: ActorRef): PartialFunction[Throwable, Unit] = {
    case err: Throwable =>
      receiver ! Tcp.Close
  }

  def receive: Receive = {
    case Tcp.Connected(remote, _) =>
      sender ! Tcp.Register(self)

    case Tcp.Received(bs) =>
      val content = bs.utf8String

      content match {
        case command(c, value) if handleCommand.isDefinedAt(c, Some(value.toInt), sender) =>
          handleCommand(c, Some(value.toInt), sender)
        case c if c.startsWith("quit") =>
          context.system.terminate()
        case c if handleCommand.isDefinedAt(c, None, sender) =>
          handleCommand(c, None, sender)
        case _ =>
          sender ! Tcp.Close
      }

    case Tcp.Closed =>
    case Tcp.PeerClosed =>
  }
} 
Example 28
Source File: SpartaHelper.scala    From sparta   with Apache License 2.0 5 votes vote down vote up
package com.stratio.sparta.serving.api.helpers

import akka.actor.{ActorSystem, Props}
import akka.event.slf4j.SLF4JLogging
import akka.io.IO
import com.stratio.sparkta.serving.api.ssl.SSLSupport
import com.stratio.sparta.driver.service.StreamingContextService
import com.stratio.sparta.serving.api.actor._
import com.stratio.sparta.serving.core.actor.StatusActor.AddClusterListeners
import com.stratio.sparta.serving.core.actor.{FragmentActor, RequestActor, StatusActor}
import com.stratio.sparta.serving.core.config.SpartaConfig
import com.stratio.sparta.serving.core.constants.AkkaConstant._
import com.stratio.sparta.serving.core.curator.CuratorFactoryHolder
import spray.can.Http


  def initSpartaAPI(appName: String): Unit = {
    if (SpartaConfig.mainConfig.isDefined && SpartaConfig.apiConfig.isDefined) {
      val curatorFramework = CuratorFactoryHolder.getInstance()

      log.info("Initializing Sparta Actors System ...")
      implicit val system = ActorSystem(appName, SpartaConfig.mainConfig)

      val statusActor = system.actorOf(Props(new StatusActor(curatorFramework)), StatusActorName)
      val fragmentActor = system.actorOf(Props(new FragmentActor(curatorFramework)), FragmentActorName)
      val policyActor = system.actorOf(Props(new PolicyActor(curatorFramework, statusActor)), PolicyActorName)
      val executionActor = system.actorOf(Props(new RequestActor(curatorFramework)), ExecutionActorName)
      val scService = StreamingContextService(curatorFramework, SpartaConfig.mainConfig)
      val launcherActor = system.actorOf(Props(new LauncherActor(scService, curatorFramework)), LauncherActorName)
      val pluginActor = system.actorOf(Props(new PluginActor()), PluginActorName)
      val driverActor = system.actorOf(Props(new DriverActor()), DriverActorName)
      val configActor = system.actorOf(Props(new ConfigActor()), ConfigActorName)
      val metadataActor = system.actorOf(Props(new MetadataActor()), MetadataActorName)
      val actors = Map(
        StatusActorName -> statusActor,
        FragmentActorName -> fragmentActor,
        PolicyActorName -> policyActor,
        LauncherActorName -> launcherActor,
        PluginActorName -> pluginActor,
        DriverActorName -> driverActor,
        ExecutionActorName -> executionActor,
        ConfigActorName -> configActor,
        MetadataActorName -> metadataActor
      )
      val controllerActor = system.actorOf(Props(new ControllerActor(actors, curatorFramework)), ControllerActorName)

      IO(Http) ! Http.Bind(controllerActor,
        interface = SpartaConfig.apiConfig.get.getString("host"),
        port = SpartaConfig.apiConfig.get.getInt("port")
      )
      log.info("Sparta Actors System initiated correctly")

      statusActor ! AddClusterListeners
    } else log.info("Sparta Configuration is not defined")
  }

} 
Example 29
Source File: Server.scala    From mqttd   with MIT License 5 votes vote down vote up
package plantae.citrus.mqtt.actors.connection

import java.net.InetSocketAddress

import akka.actor.{Actor, ActorLogging, Props}
import akka.io.{IO, Tcp}
import plantae.citrus.mqtt.actors.SystemRoot

class Server extends Actor with ActorLogging {

  import Tcp._
  import context.system

  IO(Tcp) ! Bind(self, new InetSocketAddress(
    SystemRoot.config.getString("mqtt.broker.hostname"),
    SystemRoot.config.getInt("mqtt.broker.port"))
    , backlog = 1023)

  def receive = {
    case Bound(localAddress) =>

    case CommandFailed(_: Bind) =>
      log.error("bind failure")
      context stop self

    case Connected(remote, local) =>
      log.info("new connection" + remote)
      sender ! Register(context.actorOf(Props(classOf[PacketBridge], sender)))
  }
} 
Example 30
Source File: Coordinator.scala    From chordial   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package com.tristanpenman.chordial.dht

import java.net.InetSocketAddress

import akka.actor.{Actor, ActorLogging, ActorRef, Props}
import akka.io.{IO, Udp}
import akka.util.Timeout
import com.tristanpenman.chordial.core.Node

import scala.concurrent.duration._
import scala.util.Random

class Coordinator(keyspaceBits: Int, nodeAddress: String, nodePort: Int, seedNode: Option[SeedNode])
    extends Actor
    with ActorLogging {
  import context.system

  require(keyspaceBits > 0, "keyspaceBits must be a positive Int value")

  private val idModulus = 1 << keyspaceBits

  implicit val ec = context.system.dispatcher

  IO(Udp) ! Udp.Bind(self, new InetSocketAddress(nodeAddress, nodePort))

  // How long Node should wait until an algorithm is considered to have timed out. This should be significantly
  // longer than the external request timeout, as some algorithms will make multiple external requests before
  // running to completion
  private val algorithmTimeout = Timeout(5000.milliseconds)

  // How long to wait when making requests that may be routed to other nodes
  private val externalRequestTimeout = Timeout(500.milliseconds)

  // TODO: Research how to handle collisions...
  val firstNodeId = Random.nextLong(idModulus)
  val firstNode = system.actorOf(
    Node.props(firstNodeId, keyspaceBits, algorithmTimeout, externalRequestTimeout, system.eventStream),
    s"node:${firstNodeId}"
  )

  seedNode match {
    case Some(value) =>
      log.info(s"seed node: ${value}")
    case _ =>
      log.info("not using a seed node")
  }

  def receive = {
    case Udp.Bound(local) =>
      context.become(ready(sender()))
  }

  def ready(socket: ActorRef): Receive = {
    case Udp.Received(data, remote) =>
    case Udp.Unbind                 => socket ! Udp.Unbind
    case Udp.Unbound                => context.stop(self)
  }
}

object Coordinator {
  def props(keyspaceBits: Int, nodeAddress: String, nodePort: Int, seedNode: Option[SeedNode]): Props =
    Props(new Coordinator(keyspaceBits, nodeAddress, nodePort, seedNode))
} 
Example 31
Source File: Controller.scala    From eclair   with Apache License 2.0 5 votes vote down vote up
package fr.acinq.eclair.tor

import java.net.InetSocketAddress

import akka.actor.{Actor, ActorLogging, OneForOneStrategy, Props, SupervisorStrategy, Terminated}
import akka.io.{IO, Tcp}
import akka.util.ByteString

import scala.concurrent.ExecutionContext


class Controller(address: InetSocketAddress, protocolHandlerProps: Props)
                (implicit ec: ExecutionContext = ExecutionContext.global) extends Actor with ActorLogging {

  import Controller._
  import Tcp._
  import context.system

  IO(Tcp) ! Connect(address)

  def receive = {
    case e@CommandFailed(_: Connect) =>
      e.cause match {
        case Some(ex) => log.error(ex, "Cannot connect")
        case _ => log.error("Cannot connect")
      }
      context stop self
    case c: Connected =>
      val protocolHandler = context actorOf protocolHandlerProps
      protocolHandler ! c
      val connection = sender()
      connection ! Register(self)
      context watch connection
      context become {
        case data: ByteString =>
          connection ! Write(data)
        case CommandFailed(w: Write) =>
          // O/S buffer was full
          protocolHandler ! SendFailed
          log.error("Tor command failed")
        case Received(data) =>
          protocolHandler ! data
        case _: ConnectionClosed =>
          context stop self
        case Terminated(actor) if actor == connection =>
          context stop self
      }
  }

  // we should not restart a failing tor session
  override val supervisorStrategy = OneForOneStrategy(loggingEnabled = true) { case _ => SupervisorStrategy.Escalate }

}

object Controller {
  def props(address: InetSocketAddress, protocolHandlerProps: Props)(implicit ec: ExecutionContext = ExecutionContext.global) =
    Props(new Controller(address, protocolHandlerProps))

  case object SendFailed

} 
Example 32
Source File: Server.scala    From eclair   with Apache License 2.0 5 votes vote down vote up
package fr.acinq.eclair.io

import java.net.InetSocketAddress

import akka.Done
import akka.actor.{Actor, ActorRef, DiagnosticActorLogging, Props}
import akka.event.Logging.MDC
import akka.io.Tcp.SO.KeepAlive
import akka.io.{IO, Tcp}
import fr.acinq.eclair.Logs.LogCategory
import fr.acinq.eclair.{Logs, NodeParams}

import scala.concurrent.Promise


class Server(nodeParams: NodeParams, switchboard: ActorRef, router: ActorRef, address: InetSocketAddress, bound: Option[Promise[Done]] = None) extends Actor with DiagnosticActorLogging {

  import Tcp._
  import context.system

  IO(Tcp) ! Bind(self, address, options = KeepAlive(true) :: Nil, pullMode = true)

  def receive() = {
    case Bound(localAddress) =>
      bound.map(_.success(Done))
      log.info(s"bound on $localAddress")
      // Accept connections one by one
      sender() ! ResumeAccepting(batchSize = 1)
      context.become(listening(sender()))

    case CommandFailed(_: Bind) =>
      bound.map(_.failure(new RuntimeException("TCP bind failed")))
      context stop self
  }

  def listening(listener: ActorRef): Receive = {
    case Connected(remote, _) =>
      log.info(s"connected to $remote")
      val connection = sender
      val peerConnection = context.actorOf(PeerConnection.props(
        nodeParams = nodeParams,
        switchboard = switchboard,
        router = router
      ))
      peerConnection ! PeerConnection.PendingAuth(connection, remoteNodeId_opt = None, address = remote, origin_opt = None)
      listener ! ResumeAccepting(batchSize = 1)
  }

  override def mdc(currentMessage: Any): MDC = Logs.mdc(Some(LogCategory.CONNECTION))
}

object Server {

  def props(nodeParams: NodeParams, switchboard: ActorRef, router: ActorRef, address: InetSocketAddress, bound: Option[Promise[Done]] = None): Props = Props(new Server(nodeParams, switchboard, router: ActorRef, address, bound))

} 
Example 33
Source File: Main.scala    From quiz-management-service   with Apache License 2.0 5 votes vote down vote up
package com.danielasfregola.quiz.management

import akka.actor._
import akka.io.IO
import akka.pattern.ask
import akka.util.Timeout
import com.typesafe.config.ConfigFactory
import spray.can.Http

import scala.concurrent.duration._

object Main extends App {
  val config = ConfigFactory.load()
  val host = config.getString("http.host")
  val port = config.getInt("http.port")

  implicit val system = ActorSystem("quiz-management-service")

  val api = system.actorOf(Props(new RestInterface()), "httpInterface")

  implicit val executionContext = system.dispatcher
  implicit val timeout = Timeout(10 seconds)

  IO(Http).ask(Http.Bind(listener = api, interface = host, port = port))
    .mapTo[Http.Event]
    .map {
      case Http.Bound(address) =>
        println(s"REST interface bound to $address")
      case Http.CommandFailed(cmd) =>
        println("REST interface could not bind to " +
          s"$host:$port, ${cmd.failureMessage}")
        system.shutdown()
    }
} 
Example 34
Source File: Main.scala    From quiz-management-service   with Apache License 2.0 5 votes vote down vote up
package com.danielasfregola.quiz.management

import akka.actor._
import akka.io.IO
import akka.pattern.ask
import akka.util.Timeout
import com.typesafe.config.ConfigFactory
import spray.can.Http

import scala.concurrent.duration._

object Main extends App {
  val config = ConfigFactory.load()
  val host = config.getString("http.host")
  val port = config.getInt("http.port")

  implicit val system = ActorSystem("quiz-management-service")

  val api = system.actorOf(Props(new RestInterface()), "httpInterface")

  implicit val executionContext = system.dispatcher
  implicit val timeout = Timeout(10 seconds)

  IO(Http).ask(Http.Bind(listener = api, interface = host, port = port))
    .mapTo[Http.Event]
    .map {
      case Http.Bound(address) =>
        println(s"REST interface bound to $address")
      case Http.CommandFailed(cmd) =>
        println("REST interface could not bind to " +
          s"$host:$port, ${cmd.failureMessage}")
        system.shutdown()
    }
}