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


Scala Buf类代码示例

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


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

示例1: ResourceCache

//设置package包名称以及导入依赖的类
package se.gigurra.dcs.remote

import java.time.Instant

import com.google.common.cache.CacheBuilder
import com.google.common.cache.Cache
import com.twitter.io.Buf

import scala.collection.concurrent
import scala.collection.JavaConversions._

case class ResourceCache(maxItemsPerCategory: Int) {

  private val categories = new concurrent.TrieMap[String, Cache[String, CacheItem]]

  def put(category: String, id: String, _data: Buf): Unit = {
    val data = Buf.ByteArray.coerce(_data)
    categories.getOrElseUpdate(category, newCategory()).put(id, CacheItem(id, data, time))
  }

  def get(category: String, id: String, maxAgeSeconds: Double): Option[CacheItem] = {
    categories.get(category).flatMap(c => Option(c.getIfPresent(id))).filter(_.age <= maxAgeSeconds)
  }

  def delete(category: String, id: String): Unit = {
    categories.get(category).foreach(_.invalidate(id))
  }

  def getCategory(category: String, maxAgeSeconds: Double): Seq[CacheItem] = {
    categories.get(category).map { c =>
      c.asMap().values().filter(_.age <= maxAgeSeconds).toSeq
    }.getOrElse(Nil)
  }

  def categoryNames: Seq[String] = {
    categories.keys.toSeq
  }

  private def time: Double = Instant.now.toEpochMilli.toDouble / 1000.0
  private def newCategory() = CacheBuilder.newBuilder().maximumSize(maxItemsPerCategory).build[String, CacheItem]()
}

case class CacheItem(id: String, data: Buf, timestamp: Double) {
  def age: Double = time - timestamp
  private def time: Double = Instant.now.toEpochMilli.toDouble / 1000.0
} 
开发者ID:GiGurra,项目名称:dcs-remote2,代码行数:47,代码来源:ResourceCache.scala

示例2: RefusedByRateLimiterError

//设置package包名称以及导入依赖的类
package com.lookout.ratelimitingfilter

import com.twitter.finagle.http.{Response, Status}
import com.twitter.finagle.RefusedByRateLimiter
import com.twitter.io.Buf
import com.twitter.logging.Logger
import io.circe.{Decoder, Encoder}
import io.circe.syntax._

final case class RefusedByRateLimiterError(
  message: String
) extends Exception(message) {
  def toResponse: Response = RefusedByRateLimiterError.toResponse(this)
}

object RefusedByRateLimiterError {
  val LOG = Logger.get(getClass)

  implicit val errorEncoder: Encoder[RefusedByRateLimiterError] =
    Encoder.forProduct1("message") { err => err.message }

  implicit val errorDecoder: Decoder[RefusedByRateLimiterError] =
    Decoder.forProduct1[String, RefusedByRateLimiterError]("message") {
      case (message: String) => RefusedByRateLimiterError(message)
    }

  def toResponse(error: RefusedByRateLimiterError): Response = {
    val response = Response(Status.TooManyRequests)
    val content = error.asJson.noSpaces
    LOG.info(content)
    response.content = Buf.Utf8(content)
    response.contentType = "application/json"
    response
  }
} 
开发者ID:lookout,项目名称:rate-limiting-strategy,代码行数:36,代码来源:RefusedByRateLimiterError.scala

示例3: UserContext

//设置package包名称以及导入依赖的类
package net.gutefrage.context

import com.twitter.finagle.context.Contexts
import com.twitter.finagle.util.ByteArrays
import com.twitter.io.Buf
import com.twitter.util.{Return, Throw, Try}

case class UserContext(userId: Long)


  def current: Option[UserContext] = Contexts.broadcast.get(UserContext)

  override def marshal(userContext: UserContext): Buf = {
    val bytes = new Array[Byte](bodyLengthBytes)
    ByteArrays.put64be(bytes, 0, userContext.userId)
    Buf.ByteArray.Owned(bytes)
  }

  override def tryUnmarshal(body: Buf): Try[UserContext] = {
    if (body.length != bodyLengthBytes) {
      return Throw(new IllegalArgumentException(s"Invalid body. Length ${body.length} but required 16"))
    }

    val bytes = Buf.ByteArray.Owned.extract(body)
    val userId = ByteArrays.get64be(bytes, 0)

    Return(UserContext(userId))
  }
} 
开发者ID:gutefrage,项目名称:the-finagle-docs,代码行数:30,代码来源:UserContext.scala

示例4: info

//设置package包名称以及导入依赖的类
package uk.co.appministry.scathon.testServer.plugins

import uk.co.appministry.scathon.models.v2.Plugin
import com.twitter.finagle.http.{Request, Status, Response}
import com.twitter.io.{Buf, Reader}
import play.api.libs.json.{JsString, Json}

trait TPlugin {

  def info: Plugin
  def get(request: Request, path: String): Response = Response(Status.NoContent)
  def put(request: Request, path: String): Response = Response(Status.NoContent)
  def post(request: Request, path: String): Response = Response(Status.NoContent)
  def delete(request: Request, path: String): Response = Response(Status.NoContent)

}

class TestPlugin extends TPlugin {

  def info: Plugin = {
    Plugin(
      id = "test-plugin",
      plugin = this.getClass.getName,
      implementation = this.getClass.getName,
      tags = Some(List("test", "plugin")),
      info = Some(Map("test" -> JsString("value")))
    )
  }

  override def get(request: Request, path: String): Response = {
    Response(request.version, Status.Ok, Reader.fromBuf(Buf.Utf8("GET")))
  }

  override def put(request: Request, path: String): Response = {
    Response(request.version, Status.Ok, Reader.fromBuf(Buf.Utf8("PUT")))
  }

  override def post(request: Request, path: String): Response = {
    Response(request.version, Status.Ok, Reader.fromBuf(Buf.Utf8("POST")))
  }

  override def delete(request: Request, path: String): Response = {
    Response(request.version, Status.Ok, Reader.fromBuf(Buf.Utf8("DELETE")))
  }

} 
开发者ID:AppMinistry,项目名称:scathon,代码行数:47,代码来源:Plugin.scala

示例5: Test

//设置package包名称以及导入依赖的类
package com.github.reikje

import java.net.URL

import com.twitter.conversions.time._
import com.twitter.finagle.client.Transporter
import com.twitter.finagle.factory.TimeoutFactory
import com.twitter.finagle.http.Request
import com.twitter.finagle.service.FailFastFactory.FailFast
import com.twitter.finagle.{Http, param}
import com.twitter.io.Buf
import com.twitter.util.Await

object Test {
  def main(args: Array[String]): Unit = {
    val url = new URL("https://github.com")

    println(s"Creating client using inet!${url.getHost}:443")

    val client = Http.client
      .configured(param.Label("finagle_pools"))
      .configured(TimeoutFactory.Param(10.seconds))
      .configured(Transporter.ConnectTimeout(10.seconds))
      .configured(FailFast(enabled = false))
      .withTransport.tls(hostname = url.getHost)
      .withTransport.verbose
      .withSessionPool.minSize(1)
      .withSessionPool.maxSize(2)
      .withSessionPool.maxWaiters(250)
      .newClient(s"inet!${url.getHost}:443")
      .toService

    val request = Request("https://github.com/twitter/finagle")
    val response = Await.result(client(request))
    println(new String(Buf.ByteArray.Owned.extract(response.content), "UTF-8"))
  }
} 
开发者ID:reikje,项目名称:finagle_pools,代码行数:38,代码来源:Test.scala

示例6: RedisService

//设置package包名称以及导入依赖的类
package com.twitter.finagle.redis.server

import com.twitter.finagle.Service
import com.twitter.finagle.redis.server.runner.{CommandRunner, Store}
import com.twitter.io.Buf
import com.twitter.util.Future
import com.twitter.concurrent.AsyncMutex
import com.twitter.finagle.redis.server.protocol.{Command, CommandParser}
import com.twitter.io.Buf.ByteArray

class RedisService extends Service[List[Buf], Buf] {
  private var store: Store = Store()
  private val mutex = new AsyncMutex()
  override def apply(request: List[Buf]): Future[Buf] = {
    val parsedCmd: Command = CommandParser(request)
    val sync = mutex.acquireAndRunSync {
      val (reply, newStore) = CommandRunner.run(parsedCmd, store)
      store = newStore
      reply
    }
    sync.map(reply => ByteArray.Owned(reply.decode))
  }
} 
开发者ID:gustavoamigo,项目名称:finagle-redis-server,代码行数:24,代码来源:RedisService.scala

示例7: RedisCodec

//设置package包名称以及导入依赖的类
package com.twitter.finagle.redis.server.protocol

import com.twitter.finagle.Failure
import com.twitter.finagle.netty3.BufChannelBuffer
import com.twitter.io.Buf
import com.twitter.io.Buf.ByteArray
import org.jboss.netty.channel.{ChannelHandlerContext, Channels, MessageEvent, SimpleChannelHandler}

import scala.collection.JavaConversions._

class RedisCodec extends SimpleChannelHandler {
  override def writeRequested(ctx: ChannelHandlerContext, e: MessageEvent): Unit =
    e.getMessage match {
      case b: Buf => Channels.write(ctx, e.getFuture, BufChannelBuffer(b))
      case typ => e.getFuture.setFailure(Failure(
        s"unexpected type ${typ.getClass.getSimpleName} when encoding to ChannelBuffer"))
    }

  override def messageReceived(ctx: ChannelHandlerContext, e: MessageEvent): Unit =
    e.getMessage match {
      case frame: RedisFrame =>
        val message: List[Buf] = frame.getParts.toList.map(ByteArray.Owned.apply)
        Channels.fireMessageReceived(ctx, message)
      case typ => Channels.fireExceptionCaught(ctx, Failure(
        s"unexpected type ${typ.getClass.getSimpleName} when encoding to Buf"))
    }
} 
开发者ID:gustavoamigo,项目名称:finagle-redis-server,代码行数:28,代码来源:RedisCodec.scala

示例8: RequestContextBuilder

//设置package包名称以及导入依赖的类
package com.redbubble.hawk

import java.net.URI

import com.redbubble.hawk.HawkAuthenticate.parseRawRequestAuthHeader
import com.redbubble.hawk.params._
import com.redbubble.hawk.validate.RequestAuthorisationHeader
import com.twitter.finagle.http.Request
import com.twitter.io.Buf
import com.twitter.util.Try
import mouse.all._

object RequestContextBuilder {
  def buildContext(request: Request): Option[ValidatableRequestContext] =
    for {
      header <- parseAuthHeader(request)
      method <- HttpMethod.httpMethod(request.method.toString())
      requestUri <- Try(new URI(request.uri)).toOption
    } yield {
      val host = request.host.map(Host(_)).getOrElse(Host.UnknownHost)
      val port = Port(requestUri.getPort)
      val path = UriPath(requestUri.getRawPath)
      val pc = methodDependantPayloadContext(method, request.contentType, request.content)
      ValidatableRequestContext(RequestContext(method, host, port, path, pc), header)
    }

  private def parseAuthHeader(request: Request): Option[RequestAuthorisationHeader] =
    request.headerMap.get(AuthorisationHttpHeader).flatMap(s => parseRawRequestAuthHeader(RawAuthenticationHeader(s)))

  private def methodDependantPayloadContext(method: HttpMethod, contentType: Option[String], content: Buf): Option[PayloadContext] =
    List(Put, Post, Patch).contains(method).option(payloadContext(contentType, content))

  private def payloadContext(contentType: Option[String], content: Buf): PayloadContext = {
    val ct = contentType.map(ContentType(_)).getOrElse(ContentType.UnknownContentType)
    PayloadContext(ct, bufToBytes(content))
  }

  private def bufToBytes(b: Buf): Array[Byte] = {
    val output = new Array[Byte](b.length)
    b.write(output, 0)
    output
  }
} 
开发者ID:redbubble,项目名称:finagle-hawk,代码行数:44,代码来源:RequestContextBuilder.scala

示例9: Packet

//设置package包名称以及导入依赖的类
package com.twitter.finagle.mysql.transport

import com.twitter.io.Buf

object Packet {
  val HeaderSize = 0x04
  val OkByte     = 0x00.toByte
  val ErrorByte  = 0xFF.toByte
  val EofByte    = 0xFE.toByte

  val MaxBodySize = 0xffffff

  def fromBuf(buf: Buf): Packet = {
    val br = MysqlBuf.reader(buf)

    val size = br.readUnsignedMediumLE()
    val seq = br.readUnsignedByte()
    val body = br.readAll()
    if (size != body.length) {
      throw new IllegalStateException(
        s"Bad Packet size. Expected: $size, actual ${body.length}")
    }
    Packet(seq, body)
  }
}


  def size: Int = body.length

  def toBuf: Buf = {
    val bw = MysqlBuf.writer(new Array[Byte](Packet.HeaderSize))
    bw.writeMediumLE(size)
    bw.writeByte(seq)
    bw.owned().concat(body)
  }
} 
开发者ID:wenkeyang,项目名称:finagle,代码行数:37,代码来源:Packet.scala

示例10: PacketFrameDecoderTest

//设置package包名称以及导入依赖的类
package com.twitter.finagle.mysql.transport

import com.twitter.io.Buf
import org.jboss.netty.buffer.ChannelBuffers
import org.jboss.netty.channel.{Channel, ChannelHandlerContext}
import org.junit.runner.RunWith
import org.scalatest.FunSuite
import org.scalatest.junit.JUnitRunner
import org.scalatest.mock.MockitoSugar

@RunWith(classOf[JUnitRunner])
class PacketFrameDecoderTest extends FunSuite with MockitoSugar {
  val ctx = mock[ChannelHandlerContext]
  val c = mock[Channel]
  val frameDecoder = new PacketFrameDecoder

  test("ignore incomplete packets") {
    val partial = Array[Byte](0x05, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03)
    val result = frameDecoder.decode(ctx, c, ChannelBuffers.wrappedBuffer(partial))
    assert(result === null)
  }

  test("decode complete packets") {
    val complete = Array[Byte](0x02, 0x00, 0x00, 0x01, 0x01, 0x02)
    val result = frameDecoder.decode(ctx, c, ChannelBuffers.wrappedBuffer(complete))
    assert(result != null)
    assert(result.size == 2)
    assert(result.seq == 1)
    assert(Buf.ByteArray.Owned.extract(result.body) === Array[Byte](0x01, 0x02))
  }

  test("16Mbyte packets") {
    val ff = (-1).toByte
    val frame: Array[Byte] = Array[Byte](ff, ff, ff, 0x01) ++ Array.fill[Byte](0xffffff)(0x00)
    val result = frameDecoder.decode(ctx, c, ChannelBuffers.wrappedBuffer(frame))
    assert(result != null)
    assert(result.size == 16777215)
    assert(result.seq == 1)
  }
} 
开发者ID:wenkeyang,项目名称:finagle,代码行数:41,代码来源:PacketFrameDecoderTest.scala

示例11: PacketTest

//设置package包名称以及导入依赖的类
package com.twitter.finagle.mysql.transport

import com.twitter.io.Buf
import org.junit.runner.RunWith
import org.scalatest.FunSuite
import org.scalatest.junit.JUnitRunner

@RunWith(classOf[JUnitRunner])
class PacketTest extends FunSuite {
 val seq = 2.toShort
 val bytes = Array[Byte](0x01, 0x02, 0x03, 0x04)
 val body = Buf.ByteArray.Owned(bytes)
 val packet = Packet(seq, body)

 test("Encode a Packet") {
   val br = MysqlBuf.reader(packet.toBuf)
   assert(bytes.size == br.readMediumLE())
   assert(seq == br.readByte())
   assert(bytes === br.take(br.remaining))
 }
} 
开发者ID:wenkeyang,项目名称:finagle,代码行数:22,代码来源:PacketTest.scala

示例12: ClientId

//设置package包名称以及导入依赖的类
package com.twitter.finagle.thrift

import com.twitter.finagle.context.Contexts
import com.twitter.util.{Return, Throw}
import com.twitter.io.Buf

case class ClientId(name: String) {
  
object ClientId {
  // As a matter of legacy, we need to support the notion of
  // an empty client id. Old version of contexts could serialize
  // the absence of a client id with an empty buffer.
  private[finagle] val clientIdCtx = new Contexts.broadcast.Key[Option[ClientId]]("com.twitter.finagle.thrift.ClientIdContext") {
    def marshal(clientId: Option[ClientId]): Buf = clientId match {
      case None => Buf.Empty
      case Some(ClientId(name)) => Buf.Utf8(name)
    }

    def tryUnmarshal(buf: Buf) = buf match {
      case buf if buf.isEmpty => Return.None
      case Buf.Utf8(name) => Return(Some(ClientId(name)))
      case invalid => Throw(new IllegalArgumentException("client id not a utf8 string"))
    }
  }

  private[this] val NoClientFn: () => Option[ClientId] = () => None

  def current: Option[ClientId] =
    Contexts.broadcast.getOrElse(clientIdCtx, NoClientFn)

  private[finagle] def let[R](clientId: ClientId)(f: => R): R =
    Contexts.broadcast.let(clientIdCtx, Some(clientId))(f)

  private[finagle] def let[R](clientId: Option[ClientId])(f: => R): R = {
    clientId match {
      case Some(id) => Contexts.broadcast.let(clientIdCtx, Some(id))(f)
      case None => Contexts.broadcast.letClear(clientIdCtx)(f)
    }
  }
} 
开发者ID:wenkeyang,项目名称:finagle,代码行数:41,代码来源:ClientId.scala

示例13: Retries

//设置package包名称以及导入依赖的类
package com.twitter.finagle.context

import com.twitter.finagle.util.{BufReader, BufWriter}
import com.twitter.io.Buf
import com.twitter.util.{Return, Throw, Try}


private[finagle] case class Retries(val retries: Int)

private[finagle] object Retries
  extends Contexts.broadcast.Key[Retries]("com.twitter.finagle.Retries")
{

  def current: Option[Retries] =
    Contexts.broadcast.get(Retries)

  override def marshal(retries: Retries): Buf = {
    val bw: BufWriter = BufWriter.fixed(4)
    bw.writeIntBE(retries.retries)
    bw.owned()
  }

  override def tryUnmarshal(buf: Buf): Try[Retries] = {
    if (buf.length != 4) {
      Throw(new IllegalArgumentException(
        s"Could not extract Retries from Buf. Length ${buf.length} but required 4"))
    } else {
      val retries: Int = BufReader(buf).readIntBE()
      Return(Retries(retries))
    }
  }
} 
开发者ID:wenkeyang,项目名称:finagle,代码行数:33,代码来源:Retries.scala

示例14: FrameDecoderHandler

//设置package包名称以及导入依赖的类
package com.twitter.finagle.netty3.codec

import com.twitter.finagle.Failure
import com.twitter.finagle.framer.Framer
import com.twitter.io.Buf
import org.jboss.netty.channel._


private[finagle] class FrameDecoderHandler(
  framer: Framer) extends SimpleChannelUpstreamHandler {

  override def messageReceived(ctx: ChannelHandlerContext, e: MessageEvent): Unit =
    e.getMessage match {
      case buf: Buf =>
        val frames: IndexedSeq[Buf] = framer(buf)
        var i = 0
        while (i < frames.length) {
          Channels.fireMessageReceived(ctx, frames(i))
          i += 1
        }
      case msg => Channels.fireExceptionCaught(ctx, Failure(
        "unexpected type when framing Buf." +
          s"Expected Buf, got ${msg.getClass.getSimpleName}."))
    }
} 
开发者ID:wenkeyang,项目名称:finagle,代码行数:26,代码来源:FrameDecoderHandler.scala

示例15: BufCodec

//设置package包名称以及导入依赖的类
package com.twitter.finagle.netty3.codec

import com.twitter.finagle.Failure
import com.twitter.finagle.netty3.{BufChannelBuffer, ChannelBufferBuf}
import com.twitter.io.Buf
import org.jboss.netty.buffer.ChannelBuffer
import org.jboss.netty.channel._


private[finagle] class BufCodec extends SimpleChannelHandler {
  override def writeRequested(ctx: ChannelHandlerContext, e: MessageEvent): Unit =
    e.getMessage match {
      case b: Buf => Channels.write(ctx, e.getFuture, BufChannelBuffer(b))
      case typ => e.getFuture.setFailure(Failure(
        s"unexpected type ${typ.getClass.getSimpleName} when encoding to ChannelBuffer"))
    }

  override def messageReceived(ctx: ChannelHandlerContext, e: MessageEvent): Unit =
    e.getMessage match {
      case cb: ChannelBuffer => Channels.fireMessageReceived(ctx, ChannelBufferBuf.Owned(cb))
      case typ => Channels.fireExceptionCaught(ctx, Failure(
        s"unexpected type ${typ.getClass.getSimpleName} when encoding to Buf"))
    }
} 
开发者ID:wenkeyang,项目名称:finagle,代码行数:25,代码来源:BufCodec.scala


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