-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestaurant_server.py
executable file
·100 lines (87 loc) · 4.2 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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 FoodOrder(self, request, context):
item_messages = [restaurant_pb2.items(itemName=item) for item in request.items]
if all(item in RESTAURANT_ITEMS_FOOD for item in request.items):
return restaurant_pb2.RestaurantResponse(
orderID=request.orderID,
status=restaurant_pb2.RestaurantResponse.Status.ACCEPTED,
itemMessage=item_messages,
)
else:
return restaurant_pb2.RestaurantResponse(
orderID=request.orderID,
status=restaurant_pb2.RestaurantResponse.Status.REJECTED,
itemMessage=item_messages,
)
def DrinkOrder(self, request, context):
item_messages = [restaurant_pb2.items(itemName=item) for item in request.items]
if all(item in RESTAURANT_ITEMS_DRINK for item in request.items):
return restaurant_pb2.RestaurantResponse(
orderID=request.orderID,
status=restaurant_pb2.RestaurantResponse.Status.ACCEPTED,
itemMessage=item_messages,
)
else:
return restaurant_pb2.RestaurantResponse(
orderID=request.orderID,
status=restaurant_pb2.RestaurantResponse.Status.REJECTED,
itemMessage=item_messages,
)
def DessertOrder(self, request, context):
item_messages = [restaurant_pb2.items(itemName=item) for item in request.items]
if all(item in RESTAURANT_ITEMS_DESSERT for item in request.items):
return restaurant_pb2.RestaurantResponse(
orderID=request.orderID,
status=restaurant_pb2.RestaurantResponse.Status.ACCEPTED,
itemMessage=item_messages,
)
else:
return restaurant_pb2.RestaurantResponse(
orderID=request.orderID,
status=restaurant_pb2.RestaurantResponse.Status.REJECTED,
itemMessage=item_messages,
)
def MealOrder(self, request, context):
# only receive orders that contains only 3 items according to the following order:
# 1 food, then 1 drink, and finally 1 dessert
item_messages = [restaurant_pb2.items(itemName=item) for item in request.items]
if len(request.items) != 3:
return restaurant_pb2.RestaurantResponse(
orderID=request.orderID,
status=restaurant_pb2.RestaurantResponse.Status.REJECTED,
itemMessage=item_messages,
)
else:
if (request.items[0] in RESTAURANT_ITEMS_FOOD and request.items[1] in RESTAURANT_ITEMS_DRINK and request.items[2] in RESTAURANT_ITEMS_DESSERT):
return restaurant_pb2.RestaurantResponse(
orderID=request.orderID,
status=restaurant_pb2.RestaurantResponse.Status.ACCEPTED,
itemMessage=item_messages,
)
else:
return restaurant_pb2.RestaurantResponse(
orderID=request.orderID,
status=restaurant_pb2.RestaurantResponse.Status.REJECTED,
itemMessage=item_messages,
)
def serve():
# Logic goes here
# Remember to start the server on localhost and a port defined by the first command line argument
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
restaurant_pb2_grpc.add_RestaurantServicer_to_server(Restaurant(), server)
server.add_insecure_port('[::]:' + sys.argv[1])
server.start()
server.wait_for_termination()
if __name__ == '__main__':
serve()