new grpc/hello_world example

This commit is contained in:
therecipe 2016-12-13 15:49:02 +01:00
parent d10ed7da32
commit 06658a29c5
8 changed files with 753 additions and 0 deletions

View file

@ -0,0 +1,61 @@
//hello.proto and this code is taken from: https://github.com/kenshaw/go-jakarta/tree/master/02-gomobile-and-grpc
package main
import (
"errors"
"time"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
// A wrapper type to expose via gomobile.
type HelloClient struct {
conn *grpc.ClientConn
client HelloServiceClient
}
// New creates a new HelloClient with the endpoint addr.
func New(addr string) (*HelloClient, error) {
var err error
// create connection
conn, err := grpc.Dial(addr, grpc.WithInsecure(), grpc.WithTimeout(10*time.Second))
if err != nil {
return nil, err
}
// return our "object"
return &HelloClient{
conn: conn,
client: NewHelloServiceClient(conn),
}, nil
}
// SayHello calls HelloClient.
func (hc *HelloClient) SayHello(s string) (string, error) {
// create request
req := &HelloRequest{Greeting: s}
// some safety checking
if hc.conn == nil || hc.client == nil {
return "", errors.New("unable to SayHello")
}
// call method
res, err := hc.client.SayHello(context.Background(), req)
if err != nil {
return "", err
}
return res.Reply, nil
}
// Shutdown closes connections.
func (hc *HelloClient) Shutdown() error {
if hc.conn != nil {
return hc.conn.Close()
}
return nil
}

View file

@ -0,0 +1,164 @@
// Code generated by protoc-gen-go.
// source: hello.proto
// DO NOT EDIT!
/*
Package helloworld is a generated protocol buffer package.
It is generated from these files:
hello.proto
profile.proto
It has these top-level messages:
HelloRequest
HelloResponse
EmptyMessage
IDMessage
ProfileMessage
*/
package main
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
import (
context "golang.org/x/net/context"
grpc "google.golang.org/grpc"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
type HelloRequest struct {
Greeting string `protobuf:"bytes,1,opt,name=greeting" json:"greeting,omitempty"`
}
func (m *HelloRequest) Reset() { *m = HelloRequest{} }
func (m *HelloRequest) String() string { return proto.CompactTextString(m) }
func (*HelloRequest) ProtoMessage() {}
func (*HelloRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
func (m *HelloRequest) GetGreeting() string {
if m != nil {
return m.Greeting
}
return ""
}
type HelloResponse struct {
Reply string `protobuf:"bytes,1,opt,name=reply" json:"reply,omitempty"`
}
func (m *HelloResponse) Reset() { *m = HelloResponse{} }
func (m *HelloResponse) String() string { return proto.CompactTextString(m) }
func (*HelloResponse) ProtoMessage() {}
func (*HelloResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
func (m *HelloResponse) GetReply() string {
if m != nil {
return m.Reply
}
return ""
}
func init() {
proto.RegisterType((*HelloRequest)(nil), "helloworld.HelloRequest")
proto.RegisterType((*HelloResponse)(nil), "helloworld.HelloResponse")
}
// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ grpc.ClientConn
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
const _ = grpc.SupportPackageIsVersion4
// Client API for HelloService service
type HelloServiceClient interface {
SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloResponse, error)
}
type helloServiceClient struct {
cc *grpc.ClientConn
}
func NewHelloServiceClient(cc *grpc.ClientConn) HelloServiceClient {
return &helloServiceClient{cc}
}
func (c *helloServiceClient) SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloResponse, error) {
out := new(HelloResponse)
err := grpc.Invoke(ctx, "/helloworld.HelloService/SayHello", in, out, c.cc, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for HelloService service
type HelloServiceServer interface {
SayHello(context.Context, *HelloRequest) (*HelloResponse, error)
}
func RegisterHelloServiceServer(s *grpc.Server, srv HelloServiceServer) {
s.RegisterService(&_HelloService_serviceDesc, srv)
}
func _HelloService_SayHello_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(HelloRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(HelloServiceServer).SayHello(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/helloworld.HelloService/SayHello",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(HelloServiceServer).SayHello(ctx, req.(*HelloRequest))
}
return interceptor(ctx, in, info, handler)
}
var _HelloService_serviceDesc = grpc.ServiceDesc{
ServiceName: "helloworld.HelloService",
HandlerType: (*HelloServiceServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "SayHello",
Handler: _HelloService_SayHello_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "hello.proto",
}
func init() { proto.RegisterFile("hello.proto", fileDescriptor0) }
var fileDescriptor0 = []byte{
// 145 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xe2, 0xce, 0x48, 0xcd, 0xc9,
0xc9, 0xd7, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x02, 0x73, 0xca, 0xf3, 0x8b, 0x72, 0x52,
0x94, 0xb4, 0xb8, 0x78, 0x3c, 0x40, 0xbc, 0xa0, 0xd4, 0xc2, 0xd2, 0xd4, 0xe2, 0x12, 0x21, 0x29,
0x2e, 0x8e, 0xf4, 0xa2, 0xd4, 0xd4, 0x92, 0xcc, 0xbc, 0x74, 0x09, 0x46, 0x05, 0x46, 0x0d, 0xce,
0x20, 0x38, 0x5f, 0x49, 0x95, 0x8b, 0x17, 0xaa, 0xb6, 0xb8, 0x20, 0x3f, 0xaf, 0x38, 0x55, 0x48,
0x84, 0x8b, 0xb5, 0x28, 0xb5, 0x20, 0xa7, 0x12, 0xaa, 0x12, 0xc2, 0x31, 0xf2, 0x87, 0x1a, 0x19,
0x9c, 0x5a, 0x54, 0x96, 0x99, 0x9c, 0x2a, 0x64, 0xcf, 0xc5, 0x11, 0x9c, 0x58, 0x09, 0x16, 0x12,
0x92, 0xd0, 0x43, 0xd8, 0xad, 0x87, 0x6c, 0xb1, 0x94, 0x24, 0x16, 0x19, 0x88, 0x35, 0x49, 0x6c,
0x60, 0x67, 0x1b, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0x40, 0xf2, 0xf1, 0x9a, 0xc5, 0x00, 0x00,
0x00,
}

View file

@ -0,0 +1,15 @@
syntax = "proto3";
package main;
message HelloRequest {
string greeting = 1;
}
message HelloResponse {
string reply = 1;
}
service HelloService {
rpc SayHello(HelloRequest) returns (HelloResponse);
}

View file

@ -0,0 +1,73 @@
package main
import (
"encoding/json"
"os"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/gui"
"github.com/therecipe/qt/quick"
)
//go:generate qtmoc
type HelloClientRPC struct {
core.QObject
_ func(string) string `slot:"sayHello"` //TODO: should be func(string) []*core.QVariant, once generic containers/lists are supported
_ func() string `slot:"shutdown"` //TODO: should be func() error, once "error" is mapped to QString
}
type HelloClientFactory struct {
HelloClientRPC //TODO: only needed until a bug is fixed (should be core.QObject)
_ func(addr string) *HelloClientRPC `slot:"newClient"`
}
func main() {
go serve()
gui.NewQGuiApplication(len(os.Args), os.Args)
var view = quick.NewQQuickView(nil)
view.SetResizeMode(quick.QQuickView__SizeRootObjectToView)
var helloClientFactory = NewHelloClientFactory(nil)
HelloClientRPC_QRegisterMetaType() //needed to register HelloClientRPC* as a return struct for QML
helloClientFactory.ConnectNewClient(func(addr string) *HelloClientRPC {
var (
client, err = New(addr)
ret = NewHelloClientRPC(nil)
)
if err != nil {
return nil
}
ret.ConnectSayHello(func(s string) string {
var out, err = client.SayHello(s)
if err != nil {
var rs, _ = json.Marshal(struct{ Error string }{err.Error()})
return string(rs)
}
var rs, _ = json.Marshal(struct{ Data string }{out})
return string(rs)
})
ret.ConnectShutdown(func() string {
var err = client.Shutdown()
if err != nil {
return err.Error()
}
return ""
})
return ret
})
view.RootContext().SetContextProperty("HelloClientFactory", helloClientFactory)
view.SetSource(core.NewQUrl3("qrc:///qml/hello_world.qml", 0))
view.Show()
gui.QGuiApplication_Exec()
}

View file

@ -0,0 +1,304 @@
// Code generated by protoc-gen-go.
// source: profile.proto
// DO NOT EDIT!
package main
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
import _ "github.com/gengo/grpc-gateway/third_party/googleapis/google/api"
import (
context "golang.org/x/net/context"
grpc "google.golang.org/grpc"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
type ProfileMessage_ProfileType int32
const (
ProfileMessage_USER ProfileMessage_ProfileType = 0
ProfileMessage_ADMIN ProfileMessage_ProfileType = 1
)
var ProfileMessage_ProfileType_name = map[int32]string{
0: "USER",
1: "ADMIN",
}
var ProfileMessage_ProfileType_value = map[string]int32{
"USER": 0,
"ADMIN": 1,
}
func (x ProfileMessage_ProfileType) String() string {
return proto.EnumName(ProfileMessage_ProfileType_name, int32(x))
}
func (ProfileMessage_ProfileType) EnumDescriptor() ([]byte, []int) {
return fileDescriptor1, []int{2, 0}
}
type EmptyMessage struct {
}
func (m *EmptyMessage) Reset() { *m = EmptyMessage{} }
func (m *EmptyMessage) String() string { return proto.CompactTextString(m) }
func (*EmptyMessage) ProtoMessage() {}
func (*EmptyMessage) Descriptor() ([]byte, []int) { return fileDescriptor1, []int{0} }
type IDMessage struct {
Id string `protobuf:"bytes,1,opt,name=id" json:"id,omitempty"`
}
func (m *IDMessage) Reset() { *m = IDMessage{} }
func (m *IDMessage) String() string { return proto.CompactTextString(m) }
func (*IDMessage) ProtoMessage() {}
func (*IDMessage) Descriptor() ([]byte, []int) { return fileDescriptor1, []int{1} }
func (m *IDMessage) GetId() string {
if m != nil {
return m.Id
}
return ""
}
type ProfileMessage struct {
Id string `protobuf:"bytes,1,opt,name=id" json:"id,omitempty"`
ProfileType ProfileMessage_ProfileType `protobuf:"varint,2,opt,name=profile_type,json=profileType,enum=helloworld.ProfileMessage_ProfileType" json:"profile_type,omitempty"`
Name string `protobuf:"bytes,3,opt,name=name" json:"name,omitempty"`
}
func (m *ProfileMessage) Reset() { *m = ProfileMessage{} }
func (m *ProfileMessage) String() string { return proto.CompactTextString(m) }
func (*ProfileMessage) ProtoMessage() {}
func (*ProfileMessage) Descriptor() ([]byte, []int) { return fileDescriptor1, []int{2} }
func (m *ProfileMessage) GetId() string {
if m != nil {
return m.Id
}
return ""
}
func (m *ProfileMessage) GetProfileType() ProfileMessage_ProfileType {
if m != nil {
return m.ProfileType
}
return ProfileMessage_USER
}
func (m *ProfileMessage) GetName() string {
if m != nil {
return m.Name
}
return ""
}
func init() {
proto.RegisterType((*EmptyMessage)(nil), "helloworld.EmptyMessage")
proto.RegisterType((*IDMessage)(nil), "helloworld.IDMessage")
proto.RegisterType((*ProfileMessage)(nil), "helloworld.ProfileMessage")
proto.RegisterEnum("helloworld.ProfileMessage_ProfileType", ProfileMessage_ProfileType_name, ProfileMessage_ProfileType_value)
}
// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ grpc.ClientConn
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
const _ = grpc.SupportPackageIsVersion4
// Client API for ProfileService service
type ProfileServiceClient interface {
// List returns all available profiles associated with the account.
List(ctx context.Context, in *EmptyMessage, opts ...grpc.CallOption) (ProfileService_ListClient, error)
// Get
Get(ctx context.Context, in *IDMessage, opts ...grpc.CallOption) (*ProfileMessage, error)
// Update saves profile.
Update(ctx context.Context, in *ProfileMessage, opts ...grpc.CallOption) (*IDMessage, error)
}
type profileServiceClient struct {
cc *grpc.ClientConn
}
func NewProfileServiceClient(cc *grpc.ClientConn) ProfileServiceClient {
return &profileServiceClient{cc}
}
func (c *profileServiceClient) List(ctx context.Context, in *EmptyMessage, opts ...grpc.CallOption) (ProfileService_ListClient, error) {
stream, err := grpc.NewClientStream(ctx, &_ProfileService_serviceDesc.Streams[0], c.cc, "/helloworld.ProfileService/List", opts...)
if err != nil {
return nil, err
}
x := &profileServiceListClient{stream}
if err := x.ClientStream.SendMsg(in); err != nil {
return nil, err
}
if err := x.ClientStream.CloseSend(); err != nil {
return nil, err
}
return x, nil
}
type ProfileService_ListClient interface {
Recv() (*ProfileMessage, error)
grpc.ClientStream
}
type profileServiceListClient struct {
grpc.ClientStream
}
func (x *profileServiceListClient) Recv() (*ProfileMessage, error) {
m := new(ProfileMessage)
if err := x.ClientStream.RecvMsg(m); err != nil {
return nil, err
}
return m, nil
}
func (c *profileServiceClient) Get(ctx context.Context, in *IDMessage, opts ...grpc.CallOption) (*ProfileMessage, error) {
out := new(ProfileMessage)
err := grpc.Invoke(ctx, "/helloworld.ProfileService/Get", in, out, c.cc, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *profileServiceClient) Update(ctx context.Context, in *ProfileMessage, opts ...grpc.CallOption) (*IDMessage, error) {
out := new(IDMessage)
err := grpc.Invoke(ctx, "/helloworld.ProfileService/Update", in, out, c.cc, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for ProfileService service
type ProfileServiceServer interface {
// List returns all available profiles associated with the account.
List(*EmptyMessage, ProfileService_ListServer) error
// Get
Get(context.Context, *IDMessage) (*ProfileMessage, error)
// Update saves profile.
Update(context.Context, *ProfileMessage) (*IDMessage, error)
}
func RegisterProfileServiceServer(s *grpc.Server, srv ProfileServiceServer) {
s.RegisterService(&_ProfileService_serviceDesc, srv)
}
func _ProfileService_List_Handler(srv interface{}, stream grpc.ServerStream) error {
m := new(EmptyMessage)
if err := stream.RecvMsg(m); err != nil {
return err
}
return srv.(ProfileServiceServer).List(m, &profileServiceListServer{stream})
}
type ProfileService_ListServer interface {
Send(*ProfileMessage) error
grpc.ServerStream
}
type profileServiceListServer struct {
grpc.ServerStream
}
func (x *profileServiceListServer) Send(m *ProfileMessage) error {
return x.ServerStream.SendMsg(m)
}
func _ProfileService_Get_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(IDMessage)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(ProfileServiceServer).Get(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/helloworld.ProfileService/Get",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(ProfileServiceServer).Get(ctx, req.(*IDMessage))
}
return interceptor(ctx, in, info, handler)
}
func _ProfileService_Update_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(ProfileMessage)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(ProfileServiceServer).Update(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/helloworld.ProfileService/Update",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(ProfileServiceServer).Update(ctx, req.(*ProfileMessage))
}
return interceptor(ctx, in, info, handler)
}
var _ProfileService_serviceDesc = grpc.ServiceDesc{
ServiceName: "helloworld.ProfileService",
HandlerType: (*ProfileServiceServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "Get",
Handler: _ProfileService_Get_Handler,
},
{
MethodName: "Update",
Handler: _ProfileService_Update_Handler,
},
},
Streams: []grpc.StreamDesc{
{
StreamName: "List",
Handler: _ProfileService_List_Handler,
ServerStreams: true,
},
},
Metadata: "profile.proto",
}
func init() { proto.RegisterFile("profile.proto", fileDescriptor1) }
var fileDescriptor1 = []byte{
// 314 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x7c, 0x91, 0xdf, 0x4a, 0x3a, 0x41,
0x14, 0xc7, 0x7f, 0xb3, 0xfe, 0xf9, 0xe5, 0x51, 0x37, 0x39, 0x20, 0x2c, 0x5b, 0x17, 0x32, 0x17,
0x21, 0x5d, 0xb8, 0x62, 0x77, 0xdd, 0x05, 0x4a, 0x08, 0x1a, 0xb1, 0xe6, 0x75, 0x4c, 0xed, 0xc9,
0x06, 0xd6, 0x9d, 0x61, 0x77, 0x30, 0x24, 0xba, 0xa8, 0x57, 0xe8, 0x11, 0x7a, 0xa4, 0x5e, 0xa1,
0x07, 0x09, 0x46, 0xd3, 0x0d, 0xd2, 0xbb, 0xf9, 0xc2, 0xf7, 0x7c, 0xce, 0x67, 0x66, 0xa0, 0xae,
0x53, 0xf5, 0x20, 0x63, 0xea, 0xe8, 0x54, 0x19, 0x85, 0xf0, 0x48, 0x71, 0xac, 0x9e, 0x54, 0x1a,
0x47, 0xfe, 0xf1, 0x4c, 0xa9, 0x59, 0x4c, 0x81, 0xd0, 0x32, 0x10, 0x49, 0xa2, 0x8c, 0x30, 0x52,
0x25, 0xd9, 0xaa, 0xc9, 0x5d, 0xa8, 0x0d, 0xe6, 0xda, 0x2c, 0xc7, 0x94, 0x65, 0x62, 0x46, 0xfc,
0x08, 0x2a, 0xc3, 0xfe, 0x3a, 0xa0, 0x0b, 0x8e, 0x8c, 0x3c, 0xd6, 0x62, 0xed, 0x4a, 0xe8, 0xc8,
0x88, 0x7f, 0x30, 0x70, 0xaf, 0x57, 0x8b, 0x76, 0x54, 0x70, 0x08, 0xb5, 0xb5, 0xca, 0xad, 0x59,
0x6a, 0xf2, 0x9c, 0x16, 0x6b, 0xbb, 0xbd, 0x93, 0xce, 0x56, 0xa8, 0xf3, 0x9b, 0xf0, 0x13, 0x6f,
0x96, 0x9a, 0xc2, 0xaa, 0xde, 0x06, 0x44, 0x28, 0x26, 0x62, 0x4e, 0x5e, 0xc1, 0xc2, 0xed, 0x99,
0x73, 0xa8, 0xe6, 0xfa, 0x78, 0x00, 0xc5, 0xe9, 0x64, 0x10, 0x36, 0xfe, 0x61, 0x05, 0x4a, 0x17,
0xfd, 0xf1, 0xf0, 0xaa, 0xc1, 0x7a, 0xaf, 0xce, 0xc6, 0x72, 0x42, 0xe9, 0x42, 0xde, 0x13, 0x8e,
0xa1, 0x38, 0x92, 0x99, 0x41, 0x2f, 0xef, 0x91, 0xbf, 0xb7, 0xef, 0xef, 0x36, 0xe4, 0xf5, 0xb7,
0xcf, 0xaf, 0x77, 0xe7, 0x3f, 0x96, 0x82, 0x45, 0x37, 0xd0, 0x5d, 0x86, 0x23, 0x28, 0x5c, 0x92,
0xc1, 0x66, 0x7e, 0x66, 0xf3, 0x6a, 0x7b, 0x51, 0x68, 0x51, 0x35, 0x04, 0x8b, 0x0a, 0x9e, 0x65,
0xf4, 0x82, 0x21, 0x94, 0xa7, 0x3a, 0x12, 0x86, 0x70, 0xcf, 0xa4, 0xff, 0xf7, 0x32, 0xde, 0xb4,
0xc0, 0x43, 0x3f, 0x07, 0x3c, 0x67, 0xa7, 0x77, 0x65, 0xfb, 0xbb, 0x67, 0xdf, 0x01, 0x00, 0x00,
0xff, 0xff, 0x61, 0xac, 0x88, 0x52, 0x18, 0x02, 0x00, 0x00,
}

View file

@ -0,0 +1,44 @@
syntax = "proto3";
package main;
import "google/api/annotations.proto";
message EmptyMessage {}
message IDMessage {
string id = 1;
}
message ProfileMessage {
string id = 1;
enum ProfileType {
USER = 0;
ADMIN = 1;
}
ProfileType profile_type = 2;
string name = 3;
}
service ProfileService {
// List returns all available profiles associated with the account.
rpc List(EmptyMessage) returns (stream ProfileMessage) {
option (google.api.http) = {
get: "/v0/p"
};
}
// Get
rpc Get(IDMessage) returns (ProfileMessage) {
option (google.api.http) = {
get: "/v0/p/{id}"
};
}
// Update saves profile.
rpc Update(ProfileMessage) returns (IDMessage) {
option (google.api.http) = {
put: "/v0/p/{id}"
body: "*"
};
}
}

View file

@ -0,0 +1,48 @@
import QtQuick 2.0
Rectangle
{
width: 320
height: 240
Text
{
id: textView
anchors.centerIn: parent
text: "click here!"
}
MouseArea
{
anchors.fill: parent
onClicked:
{
var addr = "localhost:8833"
textView.text = "Trying " + addr + "\n"
var hc = HelloClientFactory.newClient(addr)
if (hc == null)
{
textView.text = "Failed to create client for " + addr + "\n"
return
}
var result = JSON.parse(hc.sayHello("world"))
if (result.Error != undefined)
{
textView.text = "Failed to receive response for " + addr + "\nWith error message: " + result.Error + "\n"
return
}
textView.text = "Received: \"" + result.Data + "\"\n"
var exit = hc.shutdown()
if (exit.length > 0)
{
textView.text = "Failed to shutdown client for " + addr + "\nWith error message: " + exit + "\n"
}
}
}
}

View file

@ -0,0 +1,44 @@
//profile.proto and this code is taken from: https://github.com/kenshaw/go-jakarta/tree/master/02-gomobile-and-grpc
package main
import (
"fmt"
"log"
"net"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
type HelloServer struct{}
// SayHello says 'hi' to the user.
func (hs *HelloServer) SayHello(ctx context.Context, req *HelloRequest) (*HelloResponse, error) {
// create response
res := &HelloResponse{
Reply: fmt.Sprintf("hello %s from go", req.Greeting),
}
return res, nil
}
func serve() {
var err error
// create socket listener
l, err := net.Listen("tcp", ":8833")
if err != nil {
log.Fatalf("error: %v\n", err)
}
// create server
helloServer := &HelloServer{}
// register server with grpc
s := grpc.NewServer()
RegisterHelloServiceServer(s, helloServer)
// run
s.Serve(l)
}