Skip to content

Instantly share code, notes, and snippets.

@elyphas
Created November 5, 2022 22:21
Show Gist options
  • Save elyphas/8eac2b7ce75c6b9656d84169d5793691 to your computer and use it in GitHub Desktop.
Save elyphas/8eac2b7ce75c6b9656d84169d5793691 to your computer and use it in GitHub Desktop.
package myceliumHandler
import scarango_catalogs.{CInputs, CItemInputs, CItemRecipe, CQueryArticulo, CRecipe, CTableScarango, database}
import scarango_catalogs.{CArticulo, CItemsMyCelium}
import scarango_catalogs.ActionStock._
import create_pdf_files.CreatePdf
import types_scarango.TypeDocument
import types_scarango.{Articulo, ArticuloStock, Inputs, ItemInput, ItemRecipe, ItemRequisition, Recipe, Requisition, User}
import types_scarango.ItemTypeTC
import scarango_catalogs.{CArticuloStock, CItemRecipeMyCelium, CStock2, CTypeDocument, CUsers}
import types_scarango.RecordScarango.articuloScarango
import type_event.EventsDB._
/************ ***********/
import mycelium.core.client._
import mycelium.akka.client._
import mycelium.akka.server._
import mycelium.akka.core._
import mycelium.core.message._
import mycelium.akka.core.AkkaMessageBuilder.AkkaMessageBuilderByteBuffer
import boopickle.Default._
import java.nio.ByteBuffer
import chameleon._
import chameleon.ext.boopickle._
import akka.stream.OverflowStrategy
//import akka.actor.ActorSystem
//import akka.stream.{ActorMaterializer, OverflowStrategy}
//import akka.stream.ActorMaterializer
/*import akka.stream.scaladsl._
import akka.http.scaladsl.server.RouteResult._
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.Http*/
import scala.language.postfixOps
import scala.collection.mutable
import scala.concurrent._
object FullRequestHandler {
//implicit val system = ActorSystem()
//implicit val materializer = ActorMaterializer()
implicit val executionContext = scala.concurrent.ExecutionContext.Implicits.global
type Payload = ByteBuffer
type Event = String
type Failure = String
type State = String
/**
val builder = implicitly[AkkaMessageBuilder[ByteBuffer]]
val serializer = implicitly[Serializer[ClientMessage[Payload], ByteBuffer]]
val deserializer = implicitly[Deserializer[ServerMessage[Payload, Event, Failure], ByteBuffer]]
*/
val config = WebsocketServerConfig(bufferSize = 5, overflowStrategy = OverflowStrategy.fail)
//[PayLoad, Event, Failure, State]
val handler = new FullRequestHandler[ByteBuffer, String, String, Option[String]] {
val clients = mutable.HashSet.empty[NotifiableClient[String, Option[String]]]
val events = mutable.ArrayBuffer.empty[String]
override val initialState = Future.successful(None)
override def onRequest(
client: NotifiableClient[String, Option[String]],
state: Future[Option[String]],
path: List[String],
args: ByteBuffer) = {
//def deserialize[S: Pickler](ts: ByteBuffer) = Unpickle[S].fromBytes(ts)
def serialize[S: Pickler](ts: S) = Right(Pickle.intoBytes[S](ts))
def value[S: Pickler](ts: S, events: List[String] = Nil) = Future.successful(ReturnValue(serialize(ts), events))
def valueFut[S: Pickler](ts: Future[S], events: List[String] = Nil) = ts.map(ts => ReturnValue(serialize(ts), events))
//def error(ts: String, events: List[String] = Nil) = Future.successful(ReturnValue(Left(ts), events))
path match {
case "getAllUsers" :: Nil =>
/*println("1.- Changos ahora si ta funcionando !!!!!!!!!!!")
val event: EventsDBWillDo = deserialize[EventsDBWillDo](args)
println("2.- Changos ahora si ta funcionando !!!!!!!!!!!")
val catalogs = new CUsers
val result = event match {
case _ => catalogs.getAll()
}
result.foreach(println)*/
val result = Future.successful(Left("nada"))
Response(state, value(""))
//Response(state, valueFut[Either[String, (List[User], EventsDBResult)]]( result))
}
}
override def onEvent(client: NotifiableClient[String, Option[String]], state: Future[Option[String]], newEvents: List[String]) = {
events ++= newEvents
val downstreamEvents = newEvents.map(event => s"${event}-ok")
Reaction(state, Future.successful(downstreamEvents))
}
override def onClientConnect(client: NotifiableClient[String, Option[String]], state: Future[Option[String]]): Unit = {
client.notify(_ => Future.successful("started" :: Nil))
clients += client
()
}
override def onClientDisconnect(client: NotifiableClient[String, Option[String]], state: Future[Option[String]], reason: DisconnectReason): Unit = {
clients -= client
()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment