Distributed Computing Streaming Lab
Distributed Computing Streaming Lab
DC LAB 1
Theory:
Working of RPC
● The client procedure calls the client stub in the normal way.
● The client stub builds a message and calls the local operating system.
● The client's as sends the message to the remote as.
● The remote as gives the message to the server stub.
● The server stub unpacks the parameters and calls the server.
● The server does the work and returns the result to the stub.
● The server stub packs it in a message and calls its local as.
● The server's as sends the message to the client's as.
● The client's as gives the message to the client stub.
● The stub unpacks the result and returns to the client.
Advantages of Remote Procedure Call
● The remote procedure call is a concept that can be implemented in different ways. It is
not a standard.
● There is no flexibility in RPC for hardware architecture. It is only interaction-based.
● There is an increase in costs because of remote procedure calls.
● Debugging and testing RPC systems can be more challenging than working with local
procedures. Identifying and resolving issues related to remote calls can be
time-consuming.
gRPC
gRPC (gRPC Remote Procedure Call) is an open-source framework developed by Google for
building efficient and scalable distributed systems. It is based on the HTTP/2 protocol and
uses Protocol Buffers (protobuf) as the interface description language. gRPC facilitates
communication between clients and servers in a language-agnostic manner, allowing
developers to define services and message types using protobuf, and then automatically
generate client and server code in various programming languages. The framework supports
multiple programming languages, including but not limited to, Java, C++, Python, Go, and
more, making it highly versatile for building distributed systems with polyglot environments.
One of the key advantages of gRPC is its support for bidirectional streaming, allowing both
clients and servers to send a stream of messages to each other concurrently. This enables the
creation of real-time communication systems, such as chat applications or live updates.
Additionally, gRPC provides features like built-in authentication, load balancing, and
automatic generation of client libraries, simplifying the development of robust and efficient
distributed applications.
OUTPUT :
Greet_client.py:
import greet_pb2_grpc
import greet_pb2
import time
import grpc
def get_client_stream_requests():
while True:
name = input("Please enter a name (or nothing to stop chatting): ")
if name == "":
break
hello_request = greet_pb2.HelloRequest(greeting = "Bonjour ma belle
mademoiselle", name = name)
yield hello_request
time.sleep(1)
def run():
with grpc.insecure_channel('localhost:50051') as channel:
stub = greet_pb2_grpc.GreeterStub(channel)
print("1. SayHello - Unary")
print("2. ParrotSaysHello - Server Side Streaming")
print("3. ChattyClientSaysHello - Client Side Streaming")
print("4. InteractingHello - Both Streaming")
rpc_call = input("Which rpc would you like to make: ")
if rpc_call == "1":
hello_request = greet_pb2.HelloRequest(greeting = "Bonjour", name
= "YouTube")
hello_reply = stub.SayHello(hello_request)
print("SayHello Response Received:")
print(hello_reply)
elif rpc_call == "2":
hello_request = greet_pb2.HelloRequest(greeting = "Bonjour", name
= "YouTube")
hello_replies = stub.ParrotSaysHello(hello_request)
if __name__ == "__main__":
run()
Greet_server.py
import grpc
import greet_pb2
import greet_pb2_grpc
class GreeterServicer(greet_pb2_grpc.GreeterServicer):
def SayHello(self, request, context):
print("SayHello Request Made:")
print(request)
hello_reply = greet_pb2.HelloReply()
hello_reply.message = f"{request.greeting} {request.name}"
return hello_reply
for i in range(3):
hello_reply = greet_pb2.HelloReply()
hello_reply.message = f"{request.greeting} {request.name} {i + 1}"
yield hello_reply
time.sleep(3)
hello_reply = greet_pb2.HelloReply()
hello_reply.message = f"{request.greeting} {request.name}"
yield hello_reply
def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
greet_pb2_grpc.add_GreeterServicer_to_server(GreeterServicer(), server)
server.add_insecure_port("localhost:50051")
server.start()
server.wait_for_termination()
if __name__ == "__main__":
serve()
OUTPUT:
1.
clientside:
serverside:
conclusion:
Unary RPC refers to the simplest form of remote procedure call, where a single request is
sent from the client to the server, and a single response is returned. It represents a basic
one-to-one interaction between the client and server. gRPC (gRPC Remote Procedure Call) is
an open-source framework developed by Google that facilitates efficient communication
between distributed systems. In this experiment, I have successfully implemented a program
to demonstrate message-oriented communication using unary RPC in gRPC.