当前位置: 首页>>代码示例>>Scala>>正文


Scala Tcp类代码示例

本文整理汇总了Scala中akka.stream.scaladsl.Tcp的典型用法代码示例。如果您正苦于以下问题:Scala Tcp类的具体用法?Scala Tcp怎么用?Scala Tcp使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Tcp类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: Repl_

//设置package包名称以及导入依赖的类
package akka_in_action.streams.StreamingIO

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Framing, Source, Flow, Tcp}
import akka.util.ByteString

object Repl_ extends App {
  implicit val system = ActorSystem()
  implicit val ec = system.dispatcher
  implicit val materializer = ActorMaterializer()

  val connection = Tcp().outgoingConnection("127.0.0.1", 8888)

  val replParser =
    Flow[String].takeWhile(_ != "q")
      .concat(Source.single("BYE"))
      .map(elem => ByteString(s"$elem\n"))

  val repl = Flow[ByteString]
    .via(Framing.delimiter(
      ByteString("\n"),
      maximumFrameLength = 256,
      allowTruncation = true
    ))
  .map(_.utf8String)
  .map(text => println("Server: "+text))
  .map(_ => readLine("> "))
  .via(replParser)

  connection.join(repl).run()

} 
开发者ID:rockdragon,项目名称:fourthgala,代码行数:34,代码来源:Repl_.scala

示例2: BaseStationSource

//设置package包名称以及导入依赖的类
package com.darienmt.airplaneadventures.basestation.collector.streams

import akka.NotUsed
import akka.actor.ActorSystem
import akka.stream.scaladsl.{ Framing, Source, Tcp }
import akka.util.ByteString
import com.darienmt.airplaneadventures.basestation.collector.parsing.MessageParser
import com.darienmt.airplaneadventures.basestation.data.BaseStation.Message

import scala.collection.immutable.IndexedSeq

object BaseStationSource {

  def apply(address: String, port: Int)(implicit actorSystem: ActorSystem): Source[Message, NotUsed] =
    Source(IndexedSeq(ByteString.empty))
      .via(
        Tcp().outgoingConnection(address, port)
          .via(Framing.delimiter(ByteString("\n"), 256, allowTruncation = true))
          .map(_.utf8String)
      )
      .map(MessageParser(_))

} 
开发者ID:darienmt,项目名称:airplane-adventures,代码行数:24,代码来源:BaseStationSource.scala

示例3: Main

//设置package包名称以及导入依赖的类
package com.darienmt.airplaneadventures.basestation.repeater

import akka.stream.scaladsl.Tcp
import com.darienmt.keepers.MainCommons

object Main extends App with MainCommons {

  val bsAddress = config.getString("station.address")
  val bsPort = config.getInt("station.port")

  val upstreamFlow = Tcp().outgoingConnection(bsAddress, bsPort)

  Tcp()
    .bind("0.0.0.0", bsPort)
    .runForeach {
      _
        .flow
        .join(upstreamFlow)
        .run()
    }
} 
开发者ID:darienmt,项目名称:airplane-adventures,代码行数:22,代码来源:Main.scala

示例4: TCPTransport

//设置package包名称以及导入依赖的类
package rere.driver.connection

import java.net.InetSocketAddress

import akka.NotUsed
import akka.actor.ActorSystem
import akka.io.Tcp.SO.KeepAlive
import akka.stream.TLSProtocol._
import akka.stream.scaladsl.Tcp.OutgoingConnection
import akka.stream.scaladsl.{BidiFlow, Flow, Keep, TLS, TLSPlacebo, Tcp}
import akka.stream.{Client, EagerClose}
import akka.util.ByteString
import rere.driver.ConnectionSettings

import scala.concurrent.Future

object TCPTransport {

  private val wrapTls = Flow[ByteString].map(SendBytes)
  private val unwrapTls = Flow[SslTlsInbound].collect { case SessionBytes(_, bytes) => bytes }
  private val tlsBidi = BidiFlow.fromFlows(wrapTls, unwrapTls)

  private def getTLSStage(
    settings: ConnectionSettings
  ): BidiFlow[SslTlsOutbound, ByteString, ByteString, SslTlsInbound, NotUsed] = {
    settings.sslContextProvider match {
      case None => TLSPlacebo()
      case Some(provider) =>
        val sslContext = provider.getSSLContext
        TLS(sslContext, NegotiateNewSession, Client, EagerClose, Some((settings.host, settings.port)))
    }
  }

  def makeConnection(
    settings: ConnectionSettings)(
    implicit actorSystem: ActorSystem
  ): Flow[ByteString, ByteString, Future[OutgoingConnection]] = {
    val transportFlow = Tcp().outgoingConnection(
      InetSocketAddress.createUnresolved(settings.host, settings.port),
      options = KeepAlive(true) :: Nil,
      halfClose = false,
      connectTimeout = settings.connectTimeout)
    val tlsStage = getTLSStage(settings)

    val tlsFlow = tlsStage.joinMat(transportFlow)(Keep.right)
    tlsBidi.joinMat(tlsFlow)(Keep.right)
  }
} 
开发者ID:pbaun,项目名称:rere,代码行数:49,代码来源:TCPTransport.scala

示例5: StreamTcpClient

//设置package包名称以及导入依赖的类
package socket.client

import akka.actor.ActorSystem
import akka.stream._
import akka.stream.scaladsl.{Flow, Framing, Source, Tcp}
import akka.util.ByteString

object StreamTcpClient extends App {
  implicit val system = ActorSystem("system")
  implicit val materializer = ActorMaterializer()

  val connection = Tcp().outgoingConnection("127.0.0.1", 8080)

  val parse = Flow[String].takeWhile(_ != "q")
    .concat(Source.single("BYE"))
    .map(elem => ByteString(s"$elem\n"))

  val flow = Flow[ByteString]
    .via(Framing.delimiter(ByteString("\n"), maximumFrameLength = 256, allowTruncation = true))
    .map(_.utf8String)
    .map(text => println("Server: " + text))
    .map(_ => "ping")
    .via(parse)

  connection.join(flow).run()

} 
开发者ID:focusj,项目名称:address-service,代码行数:28,代码来源:StreamTcpClient.scala

示例6: TransactionProcessor

//设置package包名称以及导入依赖的类
package frdomain.ch7
package streams

import akka.actor.{ActorSystem, Props}
import akka.stream.ActorMaterializer
import akka.stream.actor.ActorSubscriber
import akka.stream.scaladsl.{Tcp, Source, Sink, Framing}
import akka.util.ByteString

import scala.concurrent.duration._


class TransactionProcessor(host: String, port: Int)(implicit val system: ActorSystem) extends Logging {

  def run(): Unit = {
    implicit val mat = ActorMaterializer()

    val summarizer = system.actorOf(Props[Summarizer])

    logger.info(s"Receiver: binding to $host:$port")

    Tcp().bind(host, port).runForeach { conn =>
      val receiveSink = 
        conn.flow
            .via(Framing.delimiter(ByteString(System.lineSeparator), maximumFrameLength = 4000, allowTruncation = true))
            .map(_.utf8String)
            .map(_.split(","))
            .mapConcat(Transaction(_).toList)
            .to(Sink.fromSubscriber(ActorSubscriber[Transaction](summarizer)))

      receiveSink.runWith(Source.maybe)
    }

    import system.dispatcher
    system.scheduler.schedule(0.seconds, 5.seconds, summarizer, LogSummaryBalance)
  }
}

object TransactionProcessor extends App {
  implicit val system = ActorSystem("processor")
  new TransactionProcessor("127.0.0.1", 9982).run()
} 
开发者ID:ricardo8504,项目名称:frdomain-master,代码行数:43,代码来源:TransactionProcessor.scala

示例7: PlainTcpClient

//设置package包名称以及导入依赖的类
package com.scalanerds.wireserver.example.tcpClient

import akka.stream.OverflowStrategy
import akka.actor.{ActorRef, Kill, PoisonPill}
import akka.stream.scaladsl.{Flow, Sink, Source, Tcp}
import akka.util.ByteString
import com.scalanerds.wireserver.tcpserver.TcpFraming

class PlainTcpClient(listener: ActorRef, address: String, port: Int)
  extends TcpClient(listener, address, port) with TcpFraming {

  private val sink = Flow[ByteString].to(Sink.actorRef(self, PoisonPill))

  private val tcpFlow = Flow[ByteString].via(Tcp().outgoingConnection(address, port)).alsoTo(Sink.onComplete(_ => {
    println("Bob died")
    //TODO update parent supervisor strategy to restart the actor
    self ! Kill
  }))
  val connection: ActorRef = Source.actorRef(1, OverflowStrategy.fail).via(killSwitch.flow).via(tcpFlow).via(framing).to(sink).run()

} 
开发者ID:scalanerds,项目名称:wireserver,代码行数:22,代码来源:PlainTcpClient.scala

示例8: GraphMaker

//设置package包名称以及导入依赖的类
package gatling.protocol.protoclient

import akka.NotUsed
import akka.actor.{ActorRef, ActorSystem}
import akka.stream.scaladsl.{Flow, Framing, GraphDSL, RunnableGraph, Sink, Source, Tcp}
import akka.stream.{ClosedShape, FlowShape}
import akka.util.ByteString


object GraphMaker {

	import GraphDSL.Implicits._

	implicit val sys = ActorSystem("mySystem")

	def escape(raw: String): String = {
		import scala.reflect.runtime.universe._
		Literal(Constant(raw)).toString
	}

	def bsframer: Flow[ByteString, ByteString, NotUsed] = Flow[ByteString].via(Framing.delimiter(ByteString("!"), maximumFrameLength = 1000, allowTruncation = true))

	def printer: Flow[ByteString, ByteString, NotUsed] = Flow[ByteString].map(x => {
		println(escape(x.utf8String));
		x
	})

	def tcpFlow(ip: String, port: Int, router: ActorRef) = RunnableGraph.fromGraph(GraphDSL.create() { implicit b =>
		val source = b.add(Source.actorPublisher[String](MessageSender.props(router)))
		val tcp = b.add(Tcp().outgoingConnection(ip, port))
		val sink = b.add(Sink.actorSubscriber(MessageReceiver.props(router)))
		val mapToByteStr = b.add(Flow[String].map(x => ByteString(x + "!")))
		//val framer: FlowShape[ByteString, ByteString] = b.add(bsframer)
		//val separator = b.add(Flow[String].map(x => ByteString(x + "?")))

		source ~> mapToByteStr ~> tcp ~> sink
		ClosedShape
	}
	)
} 
开发者ID:tg44,项目名称:akka-streams-simple-chat,代码行数:41,代码来源:GraphMaker.scala


注:本文中的akka.stream.scaladsl.Tcp类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。