-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestaurant_server.py
66 lines (54 loc) · 2.91 KB
/
restaurant_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from concurrent import futures
import grpc
import sys
from proto import restaurant_pb2
from proto import restaurant_pb2_grpc
RESTAURANT_ITEMS_FOOD = ["chips", "fish", "burger", "pizza", "pasta", "salad"]
RESTAURANT_ITEMS_DRINK = ["water", "fizzy drink",
"juice", "smoothie", "coffee", "beer"]
RESTAURANT_ITEMS_DESSERT = ["ice cream", "chocolate cake",
"cheese cake", "brownie", "pancakes", "waffles"]
class Restaurant(restaurant_pb2_grpc.RestaurantServicer):
# Logic goes here
def _create_response(self, orderID, items, status):
"""Helper to create a RestaurantResponse"""
item_messages = [restaurant_pb2.items(itemName=item) for item in items]
return restaurant_pb2.RestaurantResponse(
orderID=orderID,
status=restaurant_pb2.RestaurantResponse.ACCEPTED if status else restaurant_pb2.RestaurantResponse.REJECTED,
itemMessage=item_messages,
)
def FoodOrder(self, request, context):
# Check if all items are valid food items
if all(item in RESTAURANT_ITEMS_FOOD for item in request.items):
return self._create_response(request.orderID, request.items, True)
return self._create_response(request.orderID, request.items, False)
def DrinkOrder(self, request, context):
# Check if all items are valid drink items
if all(item in RESTAURANT_ITEMS_DRINK for item in request.items):
return self._create_response(request.orderID, request.items, True)
return self._create_response(request.orderID, request.items, False)
def DessertOrder(self, request, context):
# Check if all items are valid dessert items
if all(item in RESTAURANT_ITEMS_DESSERT for item in request.items):
return self._create_response(request.orderID, request.items, True)
return self._create_response(request.orderID, request.items, False)
def MealOrder(self, request, context):
# Check if there are exactly three items: one each from food, drink, dessert in the correct order
if len(request.items) == 3 and \
request.items[0] in RESTAURANT_ITEMS_FOOD and \
request.items[1] in RESTAURANT_ITEMS_DRINK and \
request.items[2] in RESTAURANT_ITEMS_DESSERT:
return self._create_response(request.orderID, request.items, True)
return self._create_response(request.orderID, request.items, False)
def serve():
# start the server on localhost and a port defined by the first command line argument
port = sys.argv[1] if len(sys.argv) > 1 else "50051"
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
restaurant_pb2_grpc.add_RestaurantServicer_to_server(Restaurant(), server)
server.add_insecure_port(f"[::]:{port}")
server.start()
print(f"Server started on port {port}")
server.wait_for_termination()
if __name__ == '__main__':
serve()