Skip to content

Commit

Permalink
making demo project semi useful.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chunyan Song committed Apr 5, 2013
1 parent 3243e1d commit ccc831f
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 70 deletions.
30 changes: 30 additions & 0 deletions 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)
}
}
8 changes: 4 additions & 4 deletions scrooge-maven-plugin/demo/pom.xml
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
@@ -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)
}
}

This file was deleted.

19 changes: 0 additions & 19 deletions scrooge-maven-plugin/demo/src/main/thrift/bird.thrift

This file was deleted.

11 changes: 11 additions & 0 deletions 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)
}

16 changes: 0 additions & 16 deletions scrooge-maven-plugin/demo/src/main/thrift/week.thrift

This file was deleted.

@@ -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")
}
}
}

This file was deleted.

0 comments on commit ccc831f

Please sign in to comment.