0% found this document useful (0 votes)
20 views

Distributed Computing Streaming Lab

The document describes an experiment to implement a program demonstrating message-oriented communication using unary RPC in gRPC. It involves creating a client and server that can perform unary RPC calls where the client sends a single request and receives a single response. The client program is able to make different types of calls - unary, server streaming, client streaming, and bidirectional streaming. The server program implements the methods to handle these calls. The conclusion is that a unary RPC was successfully demonstrated between the client and server using gRPC.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Distributed Computing Streaming Lab

The document describes an experiment to implement a program demonstrating message-oriented communication using unary RPC in gRPC. It involves creating a client and server that can perform unary RPC calls where the client sends a single request and receives a single response. The client program is able to make different types of calls - unary, server streaming, client streaming, and bidirectional streaming. The server program implements the methods to handle these calls. The conclusion is that a unary RPC was successfully demonstrated between the client and server using gRPC.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

PRIOTOSH MONDAL D17A 43

DC LAB 1

Aim: To implement a program to demonstrate message-oriented communication using


grpc. (Unary rpc where client sends a single request to the server and gets a single
response back)

Theory:

Working of RPC

A remote procedure call is an interprocess communication technique that is used for


client-server-based applications. It is also known as a subroutine call or a function call.
A client has a request message that the RPC translates and sends to the server. This request
may be a procedure or a function call to a remote server. When the server receives the
request, it sends the required response back to the client. The client is blocked while the
server is processing the call and only resumes execution after the server is finished.

The sequence of events in a remote procedure call is given as follows −

● 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

● Remote procedure calls support process-oriented and thread-oriented models.


● The internal message-passing mechanism of RPC is hidden from the user.
● The effort to re-write and re-develop the code is minimal in remote procedure calls.
● Remote procedure calls can be used in a distributed environment as well as the local
environment.
● Many of the protocol layers are omitted by RPC to improve performance.
● RPC supports modular programming by allowing developers to divide a complex
system into smaller, manageable components that can communicate with each other
through remote calls.

Disadvantages 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)

for hello_reply in hello_replies:


print("ParrotSaysHello Response Received:")
print(hello_reply)
elif rpc_call == "3":
delayed_reply =
stub.ChattyClientSaysHello(get_client_stream_requests())

print("ChattyClientSaysHello Response Received:")


print(delayed_reply)
elif rpc_call == "4":
responses = stub.InteractingHello(get_client_stream_requests())

for response in responses:


print("InteractingHello Response Received: ")
print(response)

if __name__ == "__main__":
run()

Greet_server.py

from concurrent import futures


import time

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

def ParrotSaysHello(self, request, context):


print("ParrotSaysHello Request Made:")
print(request)

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)

def ChattyClientSaysHello(self, request_iterator, context):


delayed_reply = greet_pb2.DelayedReply()
for request in request_iterator:
print("ChattyClientSaysHello Request Made:")
print(request)
delayed_reply.request.append(request)

delayed_reply.message = f"You have sent {len(delayed_reply.request)}


messages. Please expect a delayed response."
return delayed_reply

def InteractingHello(self, request_iterator, context):


for request in request_iterator:
print("InteractingHello Request Made:")
print(request)

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.

You might also like