-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverman.py
130 lines (109 loc) · 4.98 KB
/
serverman.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import paho.mqtt.client as mqtt
import time
#Create an array that will act as a database of functions
#Functions are in the form [client_id, action, parameters]
function_db = []
id_list = []
def getID(function_db, requester_id):
print(requester_id + " is requesting an ID")
id_in_use = True
i = 2
client_name = requester_id
#Create an array that lists all IDs currently in DB
while(id_in_use):
if requester_id not in id_list:
id_in_use = False
else:
requester_id = client_name + str(i)
i+=1
assigned_id = requester_id
id_list.append(assigned_id)
print(assigned_id + " is available.")
serverman_client.publish("lobby", assigned_id, qos=2, retain=True)
print("ID published to the lobby")
def declareAction(function_db, actor_id, action, parameters):
if actor_id in id_list:
function_db.append([actor_id, action, parameters])
print(actor_id + " has declared the action " + action)
def getActions(function_db,requester_id, actor_prefix):
print("Replying to ID:" + requester_id)
print("Requesting functions from actors related to IDs with prefix:" + actor_prefix)
for function in range(len(function_db)):
reply_message = ""
prefix_match = False
for element in range(len(function_db[function])):
if actor_prefix in function_db[function][0]:
reply_message += str(function_db[function][element]) + ","
prefix_match = True
if prefix_match == True:
print(str(function) + ", " + reply_message)
serverman_client.publish(requester_id, reply_message, qos=2, retain=True)
def removeActions(function_db, actor_id):
for function in range(len(function_db)):
if function_db[function][0] == actor_id:
function_db.remove(function)
"""
def updateActions(function_db, requester_id, client_function_length):
db_length = len(function_db)
if client_function_length != db_length:
for function in range(client_function_length, db_length):
reply_message = ""
for element in function_db[function]:
reply_message += str(function_db[function][element]) + ","
serverman_client.publish(requester_id, reply_message, qos=2, retain=True)
"""
#This is used to callback
def on_message(client, userdata, message):
request_message = str(message.payload.decode("utf-8"))
topic = message.topic
quality_of_service = message.qos
retain_message = message.retain
# Display incoming message
#print("message received ", request_message)
#print("message topic=", topic)
#print("message qos=", quality_of_service)
#print("message retain flag=", retain_message)
if topic == "serverman":
#############################################################
###### Examples of an incoming messages for serverman: ######
###### declareAction_clientID_action_param1_param2 ######
###### getActions_clientID_door1 ######
###### getID_clientName ######
###### removeActions_clientID ######
#############################################################
# Split incoming payload into function elements
function_elements = request_message.split("_")
request_type = function_elements[0]
actor_client_id = function_elements[1]
if len(function_elements) >= 3:
# for a declare request_type
request_action = function_elements[2]
# for a get request_type
requested_client_id = function_elements[2]
# request_params can be empty for an action declaration so let's initialize
request_parameters = []
if len(function_elements) >= 4:
request_parameters = function_elements[3:]
if "declareAction" in request_type:
declareAction(function_db, actor_client_id, request_action, request_parameters)
elif "getActions" in request_type:
getActions(function_db, actor_client_id, requested_client_id)
elif "getID" in request_type:
getID(function_db, actor_client_id)
elif "removeActions" in request_type:
removeActions(function_db, actor_client_id)
#Broker Address
broker_address = "ec2-34-207-65-122.compute-1.amazonaws.com"
#Instantiate Server Manifest Client
serverman_client = mqtt.Client(client_id="serverman", clean_session=True, userdata=None, protocol= mqtt.MQTTv311, transport="tcp")
serverman_client.on_message=on_message # attach function to callback
#Connect to the MOSQUITTO on EC2 instance
serverman_client.username_pw_set("ubuntu", password=None)
serverman_client.connect(broker_address, 1883, 60)
#Start loop to stay connected
serverman_client.loop_start()
#serverman_client.loop_forever()
#Subscribe to 'test' topic with QOS of 2
serverman_client.subscribe("serverman", qos=2)
time.sleep(5000) # wait
serverman_client.loop_stop() #stop the loop