Error while trying to pass Byte through TCP connection - scala

I need to pass byte like "0x02" through tcp connection and process it on a server. Code of client is here:
package protocol
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Source, Tcp}
import akka.util.ByteString
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Source, Tcp}
import akka.util.ByteString
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
class UpperServiceClient(ip: String, port: Int) {
def run = {
implicit val system = ActorSystem("ClientSys")
implicit val materializer = ActorMaterializer()
val a1 = Array("0x02", "0x02").toSeq
val testInput = a1.map(ByteString(_))
val result: Future[ByteString] = Source(testInput).via(Tcp().outgoingConnection(ip, port)).
runFold(ByteString.empty) { (acc, in) => acc ++ in }
val res: ByteString = Await.result(result, 10.seconds)
}
}
But IDEA shows me error:
Type mismatch, expected: Iterable[NotInferedT], actual: Seq[ByteString]
What should I do to pass "0x02" as whole byte?

The Source factory method that you're using expects an immutable Iterable. By default, calling .toSeq on an array returns a mutable Seq. One way to address this is to call .toList instead:
val a1 = Array("0x02", "0x02").toList

Related

How do I execute a list of effects?

I have a value of this type:
val effects : List[EitherT[Future, Error, Foo]] = ...
How do I run through the list and execute those effects? I did
effects.traverse
But that method is not defined for that type.
As mentioned in comments, it's better to get rid of Future when you are declaring some list of effects and want to execute them and gather effects in one list after. It's better to use IO or another lazy evaluated effect.
So, if you just need to traverse on Future[List[A]]:
import cats.data.EitherT
import scala.concurrent.{ExecutionContext, Future}
import cats.syntax.traverse._
import cats.instances.list._
import cats.instances.future._
implicit val ec: ExecutionContext = scala.concurrent.ExecutionContext.global
case class Foo()
val effects : List[EitherT[Future, Error, Foo]] = ???
def f(e: EitherT[Future, Error, Foo]): Future[Either[Error, Foo]] = ???
val result: Future[List[Either[Error, Foo]]] = effects.traverse(f)
this code compiles and all you need is implement f. You need to add import cats.instances.future._ to have traverse function in Furure.
but better way to construct you process on something like IO:
import cats.data.EitherT
import cats.syntax.traverse._
import cats.instances.list._
import cats.effect.IO
val effects : List[EitherT[IO, Error, Foo]] = ???
def f(e: EitherT[IO, Error, Foo]): IO[Either[Error, Foo]] = ???
val result: IO[List[Either[Error, Foo]]] = effects.traverse(f)
and call it using unsafeRunAsync.

How do I supply an implicit value for an akka.stream.Materializer when sending a FakeRequest?

I'm trying to make sense of the error(s) I'm seeing below, and to learn how to fix it.
could not find implicit value for parameter materializer: akka.Stream.Materializer
val fut: Future[Result] = action.apply(fakeRequest).run
^
not enough arguments for method run (implicit materializer: akka.stream.Materializer)scala.concurrent.Future[play.api.mvc.Result].
Unspecified value parameter materializer.
val fut: Future[Result] = action.apply(fakeRequest).run
^
Here is the test code that produced the error(s):
package com.foo.test
import com.foo.{Api, BoundingBox}
import org.scalatest.{FlatSpec, Matchers}
import play.api.libs.json._
import play.api.mvc._
import play.api.test.{FakeHeaders, FakeRequest}
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
class TestJmlPlay extends FlatSpec with Matchers {
val bbox = new BoundingBox(-76.778154438007732F, 39.239828198015971F, -76.501003519894326F, 39.354663763993926F)
"latitudes" should "be between swLat and neLat" in {
val action: Action[AnyContent] = (new Api).getForPlay(bbox)
val jsonStr = getStringFromAction(action)
areLatitudesOk(jsonStr, bbox) shouldBe true
}
private def getStringFromAction(action:Action[AnyContent]):String = {
val fakeRequest: Request[String] = new FakeRequest("fakeMethod", "fakeUrl", new FakeHeaders, "fakeBody")
val fut: Future[Result] = action.apply(fakeRequest).run // <== ERROR!
val result = Await.result(fut, 5000 milliseconds)
result.body.toString
}
private def areLatitudesOk(jsonStr: String, bbox: BoundingBox): Boolean = ...
}
You can create an implicit ActorMaterializer within your test class which will use testkit's ActorSystem:
import akka.testkit.TestKit
import akka.actor.ActorSystem
class TestJmlPlay(_system : ActorSystem) extends TestKit(_system) ... {
implicit val materializer: ActorMaterializer = ActorMaterializer()
val bbox = ...
You don't need Materializer.
I believe you are calling not the right action.apply method.
You want def apply(request: Request[A]): Future[Result]
To call the right, you need FakeRequest[AnyContent], same parametrized type as action:Action[AnyContent].This type is forced by PlayBodyParser I believe you set for your action.
After that you don't need .run call

Not enough arguments for method unmarshal: (implicit evidence$1: spray.httpx.unmarshalling.FromResponseUnmarshaller

I am passing from SprayJsonSupport to argonaut based on this example.
After some code modification :
object ElevationJsonProtocol extends DefaultJsonProtocol {
implicit val locationCodec: CodecJson[Elevation] = casecodec2(Elevation, Elevation.unapply)("location", "elevation")
implicit val elevationCodec: CodecJson[Location] = casecodec2(Location, Location.unapply)("lat", "lng")
implicit def googleApiResultCodec: CodecJson[GoogleApiResult] = casecodec2(GoogleApiResult, GoogleApiResult.unapply)("status", "results")
}
I got this error
Error:(41, 42) not enough arguments for method unmarshal: (implicit evidence$1: spray.httpx.unmarshalling.FromResponseUnmarshaller[GoogleApiResult])spray.http.HttpResponse => GoogleApiResult.
Unspecified value parameter evidence$1.
val pipeline = sendReceive ~> unmarshal[GoogleApiResult]
^
I take a look at the unmarshall method:
def unmarshal[T](implicit evidence$1 : spray.httpx.unmarshalling.FromResponseUnmarshaller[T]) : scala.Function1[spray.http.HttpResponse, T]
How can I add the implicit parameter? and why I did not got such error whith the sprayJsonSupport ?
The hole code :
import spray.httpx.unmarshalling.FromResponseUnmarshaller
import scala.util.{Success, Failure}
import scala.concurrent.duration._
import akka.actor.ActorSystem
import akka.pattern.ask
import akka.event.Logging
import akka.io.IO
import spray.json.{JsonFormat, DefaultJsonProtocol}
import spray.can.Http
import spray.httpx.SprayJsonSupport
import spray.client.pipelining._
import spray.util._
import argonaut._, Argonaut._
case class Elevation(location: Location, elevation: Double)
case class Location(lat: Double, lng: Double)
case class GoogleApiResult(status: String, results: List[Elevation])
object ElevationJsonProtocol extends DefaultJsonProtocol {
implicit val locationCodec: CodecJson[Elevation] = casecodec2(Elevation, Elevation.unapply)("location", "elevation")
implicit val elevationCodec: CodecJson[Location] = casecodec2(Location, Location.unapply)("lat", "lng")
implicit def googleApiResultCodec: CodecJson[GoogleApiResult] = casecodec2(GoogleApiResult, GoogleApiResult.unapply)("status", "results")
}
object Main extends App {
// we need an ActorSystem to host our application in
implicit val system = ActorSystem("simple-spray-client")
import system.dispatcher // execution context for futures below
val log = Logging(system, getClass)
log.info("Requesting the elevation of Mt. Everest from Googles Elevation API...")
import ElevationJsonProtocol._
val pipeline = sendReceive ~> unmarshal[GoogleApiResult]
val responseFuture = pipeline (
Get("http://maps.googleapis.com/maps/api/elevation/json?locations=27.988056,86.925278&sensor=false")
)
responseFuture onComplete {
case Success(GoogleApiResult(_, Elevation(_, elevation) :: _)) =>
log.info("The elevation of Mt. Everest is: {} m", elevation)
shutdown()
case Success(somethingUnexpected) =>
log.warning("The Google API call was successful but returned something unexpected: '{}'.", somethingUnexpected)
shutdown()
case Failure(error) =>
log.error(error, "Couldn't get elevation")
shutdown()
}
def shutdown(): Unit = {
IO(Http).ask(Http.CloseAll)(1.second).await
system.shutdown()
}
}
I don't really use argonaut, I use play json with spray. But at a glance it seems like there needs to be an argonaut support trait/import pulled in for your implicit codecs to convert to spray's unmarshaller (similar thing is required for play json).
https://github.com/dwhjames/argonaut-spray
this library seems to be what you want. Your implicits and imports look fine, pulling in the library should solve your problem.

How to complete a request in another actor when using akka-http

I am using akka-http 1.0 and I would like to use a route defined as
def route: Route = path("") {
// start actor with requestContext
// call requestContext.complete(...) in actor with the result
}
How do I accomplish this?
Elaborating on #jrudolph's comment, the below code satisfies your requirements of dispatching RequestContext values to an Actor. Your question indicated that you wanted a new Actor for each request; however, the below code uses the same Actor for all requests which I think is a more efficient/likely use case. The Actor creation can always be moved inside handleRequest if needed.
First we need an Actor for processing a request to a response:
import akka.actor.Actor
import akka.http.scaladsl.server.{RequestContext, RouteResult}
import akka.http.scaladsl.model.HttpResponse
class RequestActor extends Actor {
//business logic - returns empty HttpResponse
def handleRequestMessage(requestContext : RequestContext) =
RouteResult.Complete(new HttpResponse())
override def receive = {
case reqContext : RequestContext =>
sender ! handleRequestMessage(reqContext)
}
}//end class RequestActor
Now create a utility function for querying the Actor:
import akka.actor.ActorRef
import scala.concurrent.Future
import akka.pattern.ask
object RequestActor {
val handleRequest : ActorRef => RequestContext => Future[RouteResult] =
(actorRef) =>
(requestContext) =>
ask(actorRef,reqContext).mapTo[RouteResult]
}
And all that is left to do is wire everything together into a service:
import akka.actor.{ActorSystem, Props}
import akka.stream.ActorMaterializer
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Directives.{get,path}
import akka.util.Timeout
object RouteActorTest extends App {
implicit val as = ActorSystem("RouteActorTest")
implicit val timeout = new Timeout(1000)
val sendRequestToActor : RequestContext => Future[RouteResult] =
RequestActor handleRequest (as actorOf Props[RequestActor])
val route = path("")(get(sendRequestToActor))
//rest of application...
}//end object RouteActorTest
you may try even better like:
package controllers
import akka.actor.{Actor, ActorSystem, Props}
import akka.stream.ActorMaterializer
import scala.concurrent.{Await, Future}
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
import akka.util.Timeout
import akka.pattern.ask
import scala.concurrent.Await
import scala.concurrent.duration._
import scala.io.StdIn
import akka.actor._
import akka.util.Timeout
case object Message
class TestActor(name:String) extends Actor {
def receive = {
case Message =>
sender ! "Testing Ask pattern Approach"
println(s"hello from $name")
case _ =>
println("that was unexpected")
}
}
object AskTest extends App {
implicit val system= ActorSystem("myactor")
implicit val material=ActorMaterializer()
// implicit val props=Props.empty
implicit val timeout = Timeout(5 seconds)
implicit val result =system.actorOf(Props(new TestActor("TestingName")),name = "Scala")
val future3:Future[String]= ask(result ,Message).mapTo[String]
val results = Await.result(future3, 2 seconds)
println(results)
}

No implicit format for List for BSONObjectID

one of my models includes a list of BSONObjectIDs:
case class User(
_id: BSONObjectID = BSONObjectID.generate,
email: String,
favorite_ids: List[BSONObjectID] = List(),
home_folder_id: Option[BSONObjectID] = None
)
unfortunately, the compiler complains with the following message:
No implicit format for List[reactivemongo.bson.BSONObjectID]
available.
it complains in the last line of the following snippet.
import play.api.libs.json._
import reactivemongo.bson._
import play.modules.reactivemongo.json.BSONFormats._
import play.modules.reactivemongo.json._, ImplicitBSONHandlers._
import play.modules.reactivemongo.json.collection._
implicit val userFormat = Json.format[User]
Funny observation: the Option[BSONObjectID] is working when i comment the List[] line out.
Anyone know how to include a format for lists? I figured that should be available implicitly.
thanks
You can try with snapshot "org.reactivemongo" %% "play2-reactivemongo" % "0.11.2.play24-SNAPSHOT".
scala> import play.modules.reactivemongo.json._
import play.modules.reactivemongo.json._
scala> import reactivemongo.bson._
import reactivemongo.bson._
scala> import play.api.libs.json._
import play.api.libs.json._
scala> implicitly[Reads[BSONObjectID]]
res0: play.api.libs.json.Reads[reactivemongo.bson.BSONObjectID] = play.modules.reactivemongo.json.BSONFormats$BSONObjectIDFormat$#4d27019c
scala> implicitly[Writes[BSONObjectID]]
res1: play.api.libs.json.Writes[reactivemongo.bson.BSONObjectID] = play.modules.reactivemongo.json.BSONFormats$BSONObjectIDFormat$#4d27019c
scala> implicitly[Format[BSONObjectID]]
res2: play.api.libs.json.Format[reactivemongo.bson.BSONObjectID] = play.modules.reactivemongo.json.BSONFormats$BSONObjectIDFormat$#4d27019c
scala> implicitly[Format[List[BSONObjectID]]]
res3: play.api.libs.json.Format[List[reactivemongo.bson.BSONObjectID]] = play.api.libs.json.DefaultFormat$$anon$4#43b5fbbd
scala> implicitly[Reads[JsObject]]
res4: play.api.libs.json.Reads[play.api.libs.json.JsObject] = play.api.libs.json.DefaultReads$JsObjectReads$#78a1f869
scala> implicitly[OWrites[BSONDocument]]
res5: play.api.libs.json.OWrites[reactivemongo.bson.BSONDocument] = play.modules.reactivemongo.json.ImplicitBSONHandlers$BSONDocumentWrites$#1763c4c3
The implicits are all provided by the unified import play.modules.reactivemongo.json._