Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

making demo project semi useful.

  • Loading branch information...
commit ccc831f768f53d8007a5086295934961a41a15cf 1 parent 3243e1d
@chunyan chunyan authored
View
30 scrooge-maven-plugin/demo/DemoServer.scala
@@ -0,0 +1,30 @@
+import java.net.InetSocketAddress
+import com.twitter.util.Future
+import java.util.concurrent.atomic.AtomicInteger
+import com.twitter.mydemo.renamed.{User, UserService}
+import com.twitter.finagle.thrift.{ThriftServerFramedCodec, ThriftClientFramedCodec, ThriftClientRequest}
+import com.twitter.finagle.builder.{ServerBuilder, ClientBuilder}
+import org.apache.thrift.protocol.TBinaryProtocol
+
+
+object DemoServer {
+ private val userIdCounter: AtomicInteger = new AtomicInteger(0)
+
+ // need to provide an implementation for the finagle service
+ class MyUserImpl extends UserService.FutureIface {
+ def createUser(name: String): Future[User] = {
+ val id = userIdCounter.incrementAndGet()
+ Future.value(User(id, name))
+ }
+ }
+
+ def buildServer() = {
+ val protocol = new TBinaryProtocol.Factory()
+ val serverService = new UserService.FinagledService(new MyUserImpl, protocol)
+ ServerBuilder()
+ .codec(ThriftServerFramedCodec())
+ .name("binary_service")
+ .bindTo(new InetSocketAddress(0))
+ .build(serverService)
+ }
+}
View
8 scrooge-maven-plugin/demo/pom.xml
@@ -65,7 +65,7 @@
<dependency>
<groupId>com.twitter</groupId>
<artifactId>scrooge-runtime</artifactId>
- <version>3.1.0-SNAPSHOT</version>
+ <version>3.0.9</version>
</dependency>
<dependency>
<groupId>junit</groupId>
@@ -144,12 +144,12 @@
<plugin>
<groupId>com.twitter</groupId>
<artifactId>scrooge-maven-plugin</artifactId>
- <version>3.1.0-SNAPSHOT</version>
+ <version>3.0.9</version>
<configuration>
<thriftNamespaceMappings>
<thriftNamespaceMapping>
- <from>mythrift.bird</from>
- <to>mythrift.bird_renamed</to>
+ <from>com.twitter.demo</from>
+ <to>com.twitter.mydemo.renamed</to>
</thriftNamespaceMapping>
</thriftNamespaceMappings>
<thriftOpts>
View
41 scrooge-maven-plugin/demo/src/main/scala/com/twitter/example/Demo.scala
@@ -0,0 +1,41 @@
+package com.twitter.example
+import com.twitter.mydemo.renamed.{User, UserService}
+import com.twitter.finagle.thrift.{ThriftServerFramedCodec, ThriftClientFramedCodec, ThriftClientRequest}
+import com.twitter.finagle.builder.{ServerBuilder, ClientBuilder}
+import org.apache.thrift.protocol.TBinaryProtocol
+import java.net.{SocketAddress, InetSocketAddress}
+import com.twitter.util.Future
+import java.util.concurrent.atomic.AtomicInteger
+
+object DemoClient {
+ def buildClient(address: SocketAddress) = {
+ val clientService = ClientBuilder()
+ .hosts(Seq(address))
+ .codec(ThriftClientFramedCodec())
+ .hostConnectionLimit(1)
+ .build()
+ new UserService.FinagledClient(clientService)
+ }
+}
+
+object DemoServer {
+ private val userIdCounter: AtomicInteger = new AtomicInteger(0)
+
+ // need to provide an implementation for the finagle service
+ class MyUserImpl extends UserService.FutureIface {
+ def createUser(name: String): Future[User] = {
+ val id = userIdCounter.incrementAndGet()
+ Future.value(User(id, name))
+ }
+ }
+
+ def buildServer() = {
+ val protocol = new TBinaryProtocol.Factory()
+ val serverService = new UserService.FinagledService(new MyUserImpl, protocol)
+ ServerBuilder()
+ .codec(ThriftServerFramedCodec())
+ .name("binary_service")
+ .bindTo(new InetSocketAddress(0))
+ .build(serverService)
+ }
+}
View
10 scrooge-maven-plugin/demo/src/main/scala/com/twitter/example/Foo.scala
@@ -1,10 +0,0 @@
-package com.twitter.example
-import mythrift.week._
-import mythrift.bird_renamed._
-import com.twitter.finagle.thrift.ThriftClientRequest
-
-class Foo {
-
- // Put your own code here
-
-}
View
19 scrooge-maven-plugin/demo/src/main/thrift/bird.thrift
@@ -1,19 +0,0 @@
-namespace java mythrift.bird
-
-enum Location {
- Africa,
- America,
- Antarctica,
- Asia,
- Australia,
- Europe
-}
-
-struct Bird {
- 1: bool canFly
- 2: string habitat
-}
-
-service BirdService {
- list<Bird> getNonFlyingBirds(1: Location habitat)
-}
View
11 scrooge-maven-plugin/demo/src/main/thrift/user.thrift
@@ -0,0 +1,11 @@
+namespace java com.twitter.demo
+
+struct User {
+ 1: i64 id
+ 2: string name
+}
+
+service UserService {
+ User createUser(1: string name)
+}
+
View
16 scrooge-maven-plugin/demo/src/main/thrift/week.thrift
@@ -1,16 +0,0 @@
-namespace java mythrift.week
-
-enum weekDay {
- mon = 1,
- TUE,
- Wed = 3,
- thu,
- Fri,
- Sat,
- sUN
-}
-
-const weekDay myWfhDay = weekDay.thu;
-const list<weekDay> myDaysOut = [myWfhDay, weekDay.Sat, weekDay.sUN]
-
-
View
18 scrooge-maven-plugin/demo/src/test/scala/com/twitter/example/DemoSpec.scala
@@ -0,0 +1,18 @@
+package com.twitter.example
+
+import org.specs.SpecificationWithJUnit
+import com.twitter.mydemo.renamed.User
+
+class DemoSpec extends SpecificationWithJUnit {
+ def printUser(user: User) {println("User %s, id %d".format(user.name, user.id))}
+
+ "generated finagle service" should {
+ "server and client" in {
+ val server = DemoServer.buildServer()
+ val client = DemoClient.buildClient(server.localAddress)
+ client.createUser("Tyrion")().name mustEqual("Tyrion")
+ client.createUser("Jon")().name mustEqual("Jon")
+ }
+ }
+}
+
View
21 scrooge-maven-plugin/demo/src/test/scala/com/twitter/example/FooSpec.scala
@@ -1,21 +0,0 @@
-package com.twitter.example
-
-import org.specs.SpecificationWithJUnit
-import org.apache.thrift.protocol.TBinaryProtocol
-import org.apache.thrift.transport.TMemoryBuffer
-
-class FooSpec extends SpecificationWithJUnit {
- val protocol = new TBinaryProtocol(new TMemoryBuffer(10000))
- "test code" should {
- import mythrift.week._
- import mythrift.bird_renamed._
- "work with scala code generated by maven-scrooge plugin" in {
- Constants.myWfhDay mustEqual(WeekDay.Thu)
-
- val originalPenguin = Bird(canFly = false, habitat = "Antarctica")
- Bird.encode(originalPenguin, protocol)
- Bird.decode(protocol) mustEqual(originalPenguin)
- }
- }
-}
-
Please sign in to comment.
Something went wrong with that request. Please try again.