// Copyright (C) 2016 Sebastian 'tokkee' Harl // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions // are met: // 1. Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright // notice, this list of conditions and the following disclaimer in the // documentation and/or other materials provided with the distribution. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // server is the backend server implementation. package main import ( "flag" "log" "net" "strconv" "strings" "golang.org/x/net/context" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/peer" pb "tokkee.org/go-talk/grpc/proto/backend" ) var ( listen = flag.String("listen", ":50051", "listening address") ) // A server implements the Backend service. type server struct{} // Query runs the user-provided query and returns the result. func (*server) Query(ctx context.Context, in *pb.QueryRequest) (*pb.QueryReply, error) { if peer, ok := peer.FromContext(ctx); ok { log.Printf("Query() called from %v", peer) } else { log.Print("No peer information available") } t, n, err := runQuery(in.Query) if err != nil { return nil, err } return &pb.QueryReply{ Type: t, N: n, }, nil } // runQuery executes a query. It implements a highly sophisticated query // engine. func runQuery(q string) (string, int64, error) { fields := strings.SplitN(q, " ", 2) if len(fields) != 2 { return "", 0, grpc.Errorf(codes.InvalidArgument, "invalid query %q: want ", q) } var n int64 cmd, arg := strings.ToUpper(fields[0]), fields[1] switch cmd { case "COUNT": n = int64(len(arg)) case "RANDOM": i, err := strconv.Atoi(arg) if err != nil { return "", 0, grpc.Errorf(codes.InvalidArgument, "RANDOM: %v", err) } // Chosen by fair dice roll. n = 4 if i <= 4 { n = 0 } default: return "", 0, grpc.Errorf(codes.InvalidArgument, "unknown query command %q", cmd) } return cmd, n, nil } func main() { flag.Parse() l, err := net.Listen("tcp", *listen) if err != nil { log.Fatalf("Failed to listen on %s: %v", *listen, err) } log.Printf("Listening on %s ...", *listen) s := grpc.NewServer() pb.RegisterBackendServer(s, &server{}) s.Serve(l) }