/grpcCppPythonBoilerplate

An example boilerplate for getting started with protobuf, grpc, c++, and python clients and servers

Primary LanguagePythonMIT No AttributionMIT-0

grpcCppPythonBoilerplate

purpose

What is my purpose? You don't want to know, but the purpose of this repo is to illustrate one way of using gRPC and protobuf to build a client and server in c++. It also has a python client that can hit that server just like the c++ client does. A few weeks ago I was like, "I really need to learn me some modern c++" and when I got started I tried to think of something to build. I couldn't think of anything good so I just started raw dogging some sockets. That got old real quick so I did some crap with asio which was better but not awesome. While digging for answers I kept coming across people praising protobuf and gRPC so I figued I should give them a shot.

While doing all this, I found myself fighting a lot with cmake because I really have no idea what it is. In fact, only yesterday I learned that cmake just makes the makefiles and you still have to use make (I was using an IDE which did all the making out on my behalf. (Not going to lie, it is way better doing it manually yourself. Give it a shot, you can thank me later.)) The point here is that, hopefully, this project represents one way you can use cmake to generate your protobuf source files (or whatever they're called) and the gRPC service and also build some projects that use those. Of course, I learned how to do this yesterday so, you know, it probably ain't right.

To wrap this mess up, I think this is a decent boilerplate project that you could copy, add your own probobuf and gRPC specs, use them in a client/server demo, and build without too much trouble. Hopefully I will think of something useful to build using this and, you know, build it with some of that post modern c++ style.

building and getting started

I have no idea if you will have any luck bulding and running this project. I can say that I'm doing this on a clapped out thinkpad running ubuntu with who knows what installed on it. I also know that I had to install a few extra things to get this project off the ground. I also know that over the past few weeks I have installed a ton of crap. So, I'm not 100% sure what you'll need.

if I am missing something, please update this readme and do a PR. someone somewhere would really appreciate it.

This project uses cmake to build the protobuf definitions for c++ and python and also to build the c++ client and server. I think I got that working by doing the things here (https://cmake.org/install/)

Oh, and hey, listen:

Part of the cmake file in the protobuf directory (At this time) has a hard-coded path to the grpc_python_plugin executable on my machine. You, of course, are going to have to change that to get it to work on your machine. Unless your name is jordan and you installed grpc in ~/.local. If you did... sup twinsie!??!

one note on that, i just read on some blog post One small wrinkle I ran into is that you also have to make install the protobuf library (pulled in as a Git submodule by the gRPC checkout process). Even though gRPC's Makefile compiles it, it doesn't install it. so maybe if I installed the protobuf lib that came with gRCP things would be easier? you should try it and do a PR.

install gRPC locally, i have it at ~/.local/ because I followed these instructions exactly (https://grpc.io/docs/languages/cpp/quickstart/)

I already had python3 on my machine so I did this

pip3 install --upgrade protobuf

pip3 install grpcio-tools

pip3 install googleapis-common-protos

And THEN, I decided I wanted to be able to pass arguments to the apps and have config files. I decided to use boost::program_options. To get that to work (and make it so you can compile this mess) you have to install boost. On my machine, i downloaded the tar file from boost, unziped (i know) it to ~/.local and had all kinds of trouble getting cmake to see it. WELP... you have to build it. so, I did that by doing bootstrap.sh and then ./b2 - after that was done it just worked.

And THEN, I decided I wanted logging. So, I did this in my ~/.local directory: $ git clone https://github.com/gabime/spdlog.git $ cd spdlog && mkdir build && cd build $ cmake .. && make -j and then updated the cmake file to use it. but that didn't work because when you make spdlog it doesn't install it. Since I didn't specify a build prefix when I did the initial cmake, it wanted to install it to /user/local... which, i was like... this is fine so I ran sudo make install and that installed it to /usr/local/include and /usr/local/lib

Hopefully you can just make the project by doing this:

to make the project cmake -DCMAKE_FIND_PACKAGE_PREFER_CONFIG=TRUE

make

Did it build? Of course! let's run these things. You'll need a few terminals. Not to humblebrag, but I use a few tmux panes.

cd cppserver

./server

new terminal: cd cppclient

./client

new terminal: cd pythonclient

python3 main.py

The output for those programs is nothing exciting, but it is proof that you can get processes to talk to each other by sending protobuf message to each other via gRCP. yay.

notes about the python client

The python client is a crappy example, I think. I'm not 100% happy about the protobuf import situation. Seems real janky.

Thanks

Inspired by https://github.com/faaxm/exmpl-cmake-grpc