Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
uncle blog
Nov 18, 2012

I have an assignment in an operating systems class I'm taking. I'm supposed to create a client with a connection to a server, which calls methods which are defined in a .proto file, I think? I'm supposed to call methods from the client file anyway, which I need to implement in a separate server file.

Problem is, I have no idea what I'm doing. I'm fairly adequate at coding, but I can't wrap my head around how gRPC actually work and the syntaxes of request handlers and functions. I'm terrible at learning and understanding concepts from reading documentations, I haven't found any relevant tutorials online, and my teacher has been sick for two weeks with pneumonia, so he hasn't been any help either.



I have been given a .proto file with a service and several request and response definitions, an empty client.go file and a service.go file where I need to implement the methods from the .proto file. I've managed to create a client file, I think. Now I need it to communicate with the server file and make the right method calls.
If somebody could explain the relationship between these, how they operate, and maybe a basic guide to how to do function calls from the client, that would be very helpful.


Here is the kv.proto file
code:
syntax = "proto3";

package proto;

service KeyValueService {
	rpc Insert(InsertRequest) returns (InsertResponse) {}
	rpc Lookup(LookupRequest) returns (LookupResponse) {}
	rpc Keys(KeysRequest) returns (KeysResponse) {}
}

message InsertRequest {
	string key = 1;
	string value = 2;
}

message InsertResponse {
	bool success = 1;
}

message LookupRequest {
	string key = 1;
}

message LookupResponse {
	string value = 1;
}

message KeysRequest {
}

message KeysResponse {
	repeated string keys = 1;
}
Here is the client file (so far):
code:
// +build !solution

// Leave an empty line above this comment.
package main

import (
	"context"
	"flag"
	"fmt"

	"google.golang.org/grpc"
	"google.golang.org/grpc/grpclog"

	pb "github.com/uis-dat320-fall16/labs/lab3/grpc/proto"
)


func main() {
	flag.Parse()
	var opts []grpc.DialOption
	opts = append(opts, grpc.WithInsecure())
	conn, err := grpc.Dial("localhost:12111", opts...)
	if err != nil {
		grpclog.Fatalf("fail to dial: %v", err)
	}
	defer conn.Close()
	server := pb.NewKeyValueServiceClient(conn)


	// Here I need function calls

}
Here is one of the methods I need to implement and call in the client (snippet from the server file):
code:
//**************************************************************************************************************
// The Keys() gRPC returns a slice listing all the keys.
// Input:  ctx     The context of the client's request.
//         req     The request from the client.
// Output: (1)     A response to the client containing a slice of the keys.
//         (2)     An error (if any).
//**************************************************************************************************************
func (s *keyValueServicesServer) Keys(ctx context.Context, req *pb.KeysRequest) (*pb.KeysResponse, error) {
	// TODO (student): Implement function Keys

	return &pb.KeysResponse{Keys: []string{"Initial", "value"}}, nil
}

Adbot
ADBOT LOVES YOU

Coffee Mugshot
Jun 26, 2010

by Lowtax
First, I'd recommend going through the examples on http://www.grpc.io/docs/quickstart/go.html to get a basic understanding of how this work, but you say you're terrible at this, so I'll try to help. Secondly, this probably isn't a big enough question for its own thread, but the Go thread is inactive so w/e.

In a nutshell, all you're going to do is define a struct that implements Insert, Lookup, and Keys in your server file, register that struct as the server for those methods, and start up a server listening on some port. In your client, you simply connect to that port and use the client interface to make calls to that server. It's almost exactly what is shown in the quickstart example I linked above.
Your server is probably going to look something like: https://play.golang.org/p/4SB6fFzyYm. Everything just return nil, that's where your logic goes, I guess.
Your client is probably going to do something like: https://play.golang.org/p/mtxyScfsja. Yet again, the values that you're inserting or looking up or keying for are all missing, but that's supposed to be your homework.

You run the server forever and then run the client. That's the main structure, hope that is helpful. Don't think I could help anymore without directly doing your homework for you.

  • Locked thread