Skip to content

Commit

Permalink
Try working around sbt/sbt-ghpages#46
Browse files Browse the repository at this point in the history
  • Loading branch information
thesamet committed Sep 30, 2018
1 parent be13d82 commit a11e6b9
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 58 deletions.
4 changes: 3 additions & 1 deletion .travis/push_website.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ rvm use 2.2.8 --install --fuzzy
gem update --system
gem install sass
gem install jekyll -v 3.2.1
sbt docs/publishMicrosite

# Workaround for https://github.com/sbt/sbt-ghpages/issues/46
sbt docs/makeMicrosite docs/ghpagesSynchLocal docs/publishMicrosite
125 changes: 68 additions & 57 deletions docs/src/main/tut/grpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,93 +23,104 @@ implement the server side.
For example, if your proto file in `src/main/protobuf/hello.proto` looks like
this:

syntax = "proto3";
```protobuf
syntax = "proto3";
package com.example.protos;
package com.example.protos;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}

// The response message containing the greetings
message HelloReply {
string message = 1;
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
```

The generated code will look like this (simplified and commented):

object GreeterGrpc {
```scala
object GreeterGrpc {

// Base trait for an asynchronous client and server.
trait Greeter extends AbstractService {
def serviceCompanion = Greeter
def sayHello(request: HelloRequest): Future[HelloReply]
}
// Base trait for an asynchronous client and server.
trait Greeter extends AbstractService {
def serviceCompanion = Greeter
def sayHello(request: HelloRequest): Future[HelloReply]
}

object Greeter extend ServiceCompanion[Greeter] {
def descriptor = ...
}
object Greeter extend ServiceCompanion[Greeter] {
def descriptor = ...
}

// Abstract trait for a blocking client:
trait GreeterBlockingClient {
def serviceCompanion = Greeter
def sayHello(request: HelloRequest): HelloReply
}
// Abstract trait for a blocking client:
trait GreeterBlockingClient {
def serviceCompanion = Greeter
def sayHello(request: HelloRequest): HelloReply
}

// Concrete blocking client:
class GreeterBlockingStub(channel, options) extends [...] with GreeterBlockingClient {
// omitted
}
// Concrete blocking client:
class GreeterBlockingStub(channel, options) extends [...] with GreeterBlockingClient {
// omitted
}

// Concrete asynchronous client:
class GreeterStub(channel, options) extends io.grpc.stub.AbstractStub[GreeterStub](channel, options) with Greeter {
// omitted
}
// Concrete asynchronous client:
class GreeterStub(channel, options) extends io.grpc.stub.AbstractStub[GreeterStub](channel, options) with Greeter {
// omitted
}

// Creates a new blocking client.
def blockingStub(channel: io.grpc.Channel): GreeterBlockingStub = new GreeterBlockingStub(channel)
// Creates a new blocking client.
def blockingStub(channel: io.grpc.Channel): GreeterBlockingStub = new GreeterBlockingStub(channel)

// Creates a new asynchronous client.
def stub(channel: io.grpc.Channel): GreeterStub = new GreeterStub(channel)
}
// Creates a new asynchronous client.
def stub(channel: io.grpc.Channel): GreeterStub = new GreeterStub(channel)
}
```

## Using the client

Creating a channel:

val channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext(true).build
val request = HelloRequest(name = "World")
```scala
val channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext(true).build
val request = HelloRequest(name = "World")
```

Blocking call:

val blockingStub = GreeterGrpc.blockingStub(channel)
val reply: HelloReply = blockingStub.sayHello(request)
println(reply)
```scala
val blockingStub = GreeterGrpc.blockingStub(channel)
val reply: HelloReply = blockingStub.sayHello(request)
println(reply)
```

Async call:

val stub = GreeterGrpc.stub(channel)
val f: Future[HelloReply] = stub.sayHello(request)
f onComplete println
```
val stub = GreeterGrpc.stub(channel)
val f: Future[HelloReply] = stub.sayHello(request)
f onComplete println
```

## Writing a server

Implement the service:

private class GreeterImpl extends GreeterGrpc.Greeter {
override def sayHello(req: HelloRequest) = {
val reply = HelloReply(message = "Hello " + req.name)
Future.successful(reply)
}
}
```scala
private class GreeterImpl extends GreeterGrpc.Greeter {
override def sayHello(req: HelloRequest) = {
val reply = HelloReply(message = "Hello " + req.name)
Future.successful(reply)
}
}
```

See
[complete example server](https://github.com/xuwei-k/grpc-scala-sample/blob/master/grpc-scala/src/main/scala/io/grpc/examples/helloworld/HelloWorldServer.scala) here.
Expand Down

0 comments on commit a11e6b9

Please sign in to comment.