Week 4 - Remote Procedure Call
Distributed Systems and Network Programming - Spring 2023
Overview
Your task for this lab is to use gRPC to remotely call functions defined on a server.
- The client calls stub functions to do some CRUD operations against a SQLite database.
- The server executes the corresponding implementations to modify the database content.
- Communication happens over the network, with Protocol Buffers as the data serialization format.
Remote Functions
The server should expose the following functions for RPC, the logic for each function is explained below:
Task
-
Create a Python virtual environment and install the required external dependencies
python3 -m venv venv
source venv/bin/activate
pip3 install grpcio grpcio-tools
-
Create schema.proto
which defines the following:
- The database
service
with remote functions that can be called through rpc
.
- Request/response
message
format for the client/server communication.
-
Compile the schema file to generate the stub and service source files (schema_pb2_grpc.py
and schema_pb2.py
) using the following command:
python3 -m grpc_tools.protoc --proto_path=. --python_out=. --grpc_python_out=. schema.proto
-
Write the gRPC server code to do the following:
- Create or overwrite a local database file
db.sqlite
in the current directory.
- Initialize the database with an empty table
Users(id INTEGER, name STRING)
- Create a
grpc.server
that listens forever for client RPC requests and executes them.
- The server should terminate gracefully whenever a
KeyboardInterrupt
is received.
- Upon receiving a request from the client, the server should print the name of the function to be executed along with the supplied arguments.
- Implement the functions for
PutUser
, DeleteUser
, and GetUsers
as explained above.
-
Run your gRPC server, then run the given client (you are not supposed to modify the client).
-
Verify that the database was populated by inspecting the file db.sqlite
using your favorite SQLite viewer tool/extension.
Example Run
$ python3 server.py
gRPC server is listening on 0.0.0.0:1234
PutUser(1, 'User1')
PutUser(2, 'User2')
PutUser(3, 'User3')
PutUser(4, 'User4')
PutUser(2, 'User2_updated')
DeleteUser(3)
GetUsers()
$ python3 client.py
PutUser(1, 'User1') = True
PutUser(2, 'User2') = True
PutUser(3, 'User3') = True
PutUser(4, 'User4') = True
PutUser(2, 'User2_updated') = True
DeleteUser(3) = True
GetUsers() = {1: 'User1', 2: 'User2_updated', 4: 'User4'}
Checklist
Week 4 - Remote Procedure Call
Overview
Your task for this lab is to use gRPC to remotely call functions defined on a server.
Remote Functions
The server should expose the following functions for RPC, the logic for each function is explained below:
PutUser(user_id: int, user_name: str)
user_id
to have the specifieduser_name
True
on success andFalse
on failureDeleteUser(user_id: int)
user_id
True
on success andFalse
on failureGetUsers()
user_id
anduser_name
Task
Create a Python virtual environment and install the required external dependencies
python3 -m venv venv source venv/bin/activate pip3 install grpcio grpcio-tools
Create
schema.proto
which defines the following:service
with remote functions that can be called throughrpc
.message
format for the client/server communication.Compile the schema file to generate the stub and service source files (
schema_pb2_grpc.py
andschema_pb2.py
) using the following command:Write the gRPC server code to do the following:
db.sqlite
in the current directory.Users(id INTEGER, name STRING)
grpc.server
that listens forever for client RPC requests and executes them.KeyboardInterrupt
is received.PutUser
,DeleteUser
, andGetUsers
as explained above.Run your gRPC server, then run the given client (you are not supposed to modify the client).
Verify that the database was populated by inspecting the file
db.sqlite
using your favorite SQLite viewer tool/extension.Example Run
$ python3 server.py gRPC server is listening on 0.0.0.0:1234 PutUser(1, 'User1') PutUser(2, 'User2') PutUser(3, 'User3') PutUser(4, 'User4') PutUser(2, 'User2_updated') DeleteUser(3) GetUsers()
$ python3 client.py PutUser(1, 'User1') = True PutUser(2, 'User2') = True PutUser(3, 'User3') = True PutUser(4, 'User4') = True PutUser(2, 'User2_updated') = True DeleteUser(3) = True GetUsers() = {1: 'User1', 2: 'User2_updated', 4: 'User4'}
Checklist
NameSurname.zip
) containing onlyserver.py
andschema.proto
grpc
)schema.proto
file correctly compiles without errors or warnings.db.sqlite
contains the expected data after applying all queries.