Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Basic EchoServer example

  • Loading branch information...
commit 52d00c8d27e079e3b482a688b1ae663ad6c6671b 1 parent 81fdcd4
Nick Kallen authored
View
2  finagle-core/src/main/scala/com/twitter/finagle/Service.scala
@@ -12,7 +12,7 @@ import com.twitter.finagle.service.RefcountedService
* compatibility, as it has implementation as well as interface.
*/
abstract class Service[-Req, +Rep] extends (Req => Future[Rep]) {
- def map[Req1](f: (Req1) => (Req)) = new Service[Req1, Rep] {
+ def map[Req1](f: Req1 => Req) = new Service[Req1, Rep] {
def apply(req1: Req1) = Service.this.apply(f(req1))
override def release() = Service.this.release()
}
View
2  finagle-core/src/main/scala/com/twitter/finagle/builder/ClientBuilder.scala
@@ -120,6 +120,8 @@ case class ClientBuilder[Req, Rep](
cluster(_cluster)
}
+ def hosts(address: SocketAddress): ClientBuilder[Req, Rep] = hosts(Seq(address))
+
def cluster(cluster: Cluster): ClientBuilder[Req, Rep] = {
copy(_cluster = Some(cluster))
}
View
24 finagle-example/src/main/scala/com/twitter/finagle/example/echo/EchoClient.scala
@@ -0,0 +1,24 @@
+package com.twitter.finagle.example.echo
+
+import com.twitter.finagle.builder.ClientBuilder
+import java.net.InetSocketAddress
+
+object EchoClient {
+ def main(args: Array[String]) {
+ val client = ClientBuilder()
+ .codec(StringCodec)
+ .hosts(new InetSocketAddress(8080))
+ .build()
+
+ // Issue request:
+ client("hi mom\n") onSuccess { result =>
+ println("Received result: " + result)
+ } onFailure { error =>
+ error.printStackTrace()
+ } ensure {
+ // All done! Close TCP connection:
+ println("releasin")
+ client.release()
+ }
+ }
+}
View
19 finagle-example/src/main/scala/com/twitter/finagle/example/echo/EchoServer.scala
@@ -0,0 +1,19 @@
+package com.twitter.finagle.example.echo
+
+import com.twitter.finagle.Service
+import com.twitter.util.Future
+import com.twitter.finagle.builder.ServerBuilder
+import java.net.InetSocketAddress
+
+object EchoServer {
+ def main(args: Array[String]) {
+ val service = new Service[String, String] {
+ def apply(request: String) = Future.value(request)
+ }
+
+ val server = ServerBuilder()
+ .codec(StringCodec)
+ .bindTo(new InetSocketAddress(8080))
+ .build(service)
+ }
+}
View
31 finagle-example/src/main/scala/com/twitter/finagle/example/echo/StringCodec.scala
@@ -0,0 +1,31 @@
+package com.twitter.finagle.example.echo
+
+import com.twitter.finagle.Codec
+import org.jboss.netty.handler.codec.string.{StringEncoder, StringDecoder}
+import org.jboss.netty.channel.{Channels, ChannelPipelineFactory}
+import org.jboss.netty.handler.codec.frame.{Delimiters, DelimiterBasedFrameDecoder}
+import org.jboss.netty.util.CharsetUtil
+
+object StringCodec extends StringCodec
+
+class StringCodec extends Codec[String, String] {
+ val serverPipelineFactory = new ChannelPipelineFactory {
+ def getPipeline = {
+ val pipeline = Channels.pipeline()
+ pipeline.addLast("line",
+ new DelimiterBasedFrameDecoder(100, Delimiters.lineDelimiter: _*))
+ pipeline.addLast("stringDecoder", new StringDecoder(CharsetUtil.UTF_8))
+ pipeline.addLast("stringEncoder", new StringEncoder(CharsetUtil.UTF_8))
+ pipeline
+ }
+ }
+
+ val clientPipelineFactory = new ChannelPipelineFactory {
+ def getPipeline = {
+ val pipeline = Channels.pipeline()
+ pipeline.addLast("stringEncode", new StringEncoder(CharsetUtil.UTF_8))
+ pipeline.addLast("stringDecode", new StringDecoder(CharsetUtil.UTF_8))
+ pipeline
+ }
+ }
+}
View
11 project/build/Project.scala
@@ -72,6 +72,14 @@ class Project(info: ProjectInfo) extends StandardParentProject(info)
new StreamProject(_), coreProject, kestrelProject)
/**
+ * finagle-stream contains a streaming http codec identical to
+ * Twitter's "firehose".
+ */
+ val exampleProject = project(
+ "finagle-example", "finagle-example",
+ new ExampleProject(_), coreProject, streamProject, thriftProject, memcachedProject, kestrelProject)
+
+ /**
* finagle-stress has stress/integration test suites & tools for
* development.
*/
@@ -121,6 +129,9 @@ class Project(info: ProjectInfo) extends StandardParentProject(info)
override def compileOrder = CompileOrder.ScalaThenJava
}
+ class ExampleProject(info: ProjectInfo) extends StandardProject(info)
+ with AdhocInlines
+
class OstrichProject(info: ProjectInfo) extends StandardProject(info)
with SubversionPublisher with AdhocInlines
{
Please sign in to comment.
Something went wrong with that request. Please try again.