-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSampleNetworkServer.py
207 lines (170 loc) · 7.52 KB
/
SampleNetworkServer.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import threading
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# from Group8Incubator import infinc
import infinc
import time
import math
import socket
import fcntl
import os
import errno
import random
import string
# Lab 6, Task 1
from patch_01 import pw # Lab 6, Task 1: Remove Hardcoded Password
class SmartNetworkThermometer (threading.Thread) :
open_cmds = ["AUTH", "LOGOUT"]
prot_cmds = ["SET_DEGF", "SET_DEGC", "SET_DEGK", "GET_TEMP", "UPDATE_TEMP"]
def __init__ (self, source, updatePeriod, port) :
threading.Thread.__init__(self, daemon = True)
#set daemon to be true, so it doesn't block program from exiting
self.source = source
self.updatePeriod = updatePeriod
self.curTemperature = 0
self.updateTemperature()
self.tokens = []
self.serverSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
self.serverSocket.bind(("127.0.0.1", port))
fcntl.fcntl(self.serverSocket, fcntl.F_SETFL, os.O_NONBLOCK)
self.deg = "K"
def setSource(self, source) :
self.source = source
def setUpdatePeriod(self, updatePeriod) :
self.updatePeriod = updatePeriod
def setDegreeUnit(self, s) :
self.deg = s
if self.deg not in ["F", "K", "C"] :
self.deg = "K"
def updateTemperature(self) :
self.curTemperature = self.source.getTemperature()
def getTemperature(self) :
if self.deg == "C" :
return self.curTemperature - 273
if self.deg == "F" :
return (self.curTemperature - 273) * 9 / 5 + 32
return self.curTemperature
def processCommands(self, msg, addr) :
cmds = msg.split(';')
for c in cmds :
cs = c.split(' ')
if len(cs) == 2 : #should be either AUTH or LOGOUT
if cs[0] == "AUTH":
if cs[1] == pw : # Lab 6, Task 1: Remove Hardcoded Password. Used to say: if cs[1] == "!Q#E%T&U8i6y4r2w"
self.tokens.append(''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(16)))
# print(self.tokens)
self.serverSocket.sendto(self.tokens[-1].encode("utf-8"), addr)
#print (self.tokens[-1])
else:
self.serverSocket.sendto(b"Invalid Command\n", addr)
elif cs[0] == "LOGOUT":
if cs[1] in self.tokens :
self.tokens.remove(cs[1])
else : #unknown command
self.serverSocket.sendto(b"Invalid Command\n", addr)
elif c == "SET_DEGF" :
self.deg = "F"
elif c == "SET_DEGC" :
self.deg = "C"
elif c == "SET_DEGK" :
self.deg = "K"
elif c == "GET_TEMP" :
self.serverSocket.sendto(b"%f\n" % self.getTemperature(), addr)
elif c == "UPDATE_TEMP" :
self.updateTemperature()
elif c :
self.serverSocket.sendto(b"Invalid Command\n", addr)
def run(self) : #the running function
while True :
try :
msg, addr = self.serverSocket.recvfrom(1024)
msg = msg.decode("utf-8").strip()
cmds = msg.split(' ')
if len(cmds) == 1 : # protected commands case
semi = msg.find(';')
if semi != -1 : #if we found the semicolon
#print (msg)
if msg[:semi] in self.tokens : #if its a valid token
self.processCommands(msg[semi+1:], addr)
else :
self.serverSocket.sendto(b"Bad Token\n", addr)
else :
self.serverSocket.sendto(b"Bad Command\n", addr)
elif len(cmds) == 2 :
if cmds[0] in self.open_cmds : #if its AUTH or LOGOUT
self.processCommands(msg, addr)
else :
self.serverSocket.sendto(b"Authenticate First\n", addr)
else :
# otherwise bad command
self.serverSocket.sendto(b"Bad Command\n", addr)
except IOError as e :
if e.errno == errno.EWOULDBLOCK :
#do nothing
pass
else :
#do nothing for now
pass
msg = ""
self.updateTemperature()
time.sleep(self.updatePeriod)
class SimpleClient :
def __init__(self, therm1, therm2) :
self.fig, self.ax = plt.subplots()
now = time.time()
self.lastTime = now
self.times = [time.strftime("%H:%M:%S", time.localtime(now-i)) for i in range(30, 0, -1)]
self.infTemps = [0]*30
self.incTemps = [0]*30
self.infLn, = plt.plot(range(30), self.infTemps, label="Infant Temperature")
self.incLn, = plt.plot(range(30), self.incTemps, label="Incubator Temperature")
plt.xticks(range(30), self.times, rotation=45)
plt.ylim((20,50))
plt.legend(handles=[self.infLn, self.incLn])
self.infTherm = therm1
self.incTherm = therm2
self.ani = animation.FuncAnimation(self.fig, self.updateInfTemp, interval=500)
self.ani2 = animation.FuncAnimation(self.fig, self.updateIncTemp, interval=500)
def updateTime(self) :
now = time.time()
if math.floor(now) > math.floor(self.lastTime) :
t = time.strftime("%H:%M:%S", time.localtime(now))
self.times.append(t)
#last 30 seconds of of data
self.times = self.times[-30:]
self.lastTime = now
plt.xticks(range(30), self.times,rotation = 45)
plt.title(time.strftime("%A, %Y-%m-%d", time.localtime(now)))
def updateInfTemp(self, frame) :
self.updateTime()
self.infTemps.append(self.infTherm.getTemperature()-273)
#self.infTemps.append(self.infTemps[-1] + 1)
self.infTemps = self.infTemps[-30:]
self.infLn.set_data(range(30), self.infTemps)
return self.infLn,
def updateIncTemp(self, frame) :
self.updateTime()
self.incTemps.append(self.incTherm.getTemperature()-273)
#self.incTemps.append(self.incTemps[-1] + 1)
self.incTemps = self.incTemps[-30:]
self.incLn.set_data(range(30), self.incTemps)
return self.incLn,
UPDATE_PERIOD = .05 #in seconds
SIMULATION_STEP = .1 #in seconds
#create a new instance of IncubatorSimulator
bob = infinc.Human(mass = 8, length = 1.68, temperature = 36 + 273)
#bobThermo = infinc.SmartThermometer(bob, UPDATE_PERIOD)
bobThermo = SmartNetworkThermometer(bob, UPDATE_PERIOD, 23456)
bobThermo.start() #start the thread
inc = infinc.Incubator(width = 1, depth=1, height = 1, temperature = 37 + 273, roomTemperature = 20 + 273)
#incThermo = infinc.SmartNetworkThermometer(inc, UPDATE_PERIOD)
incThermo = SmartNetworkThermometer(inc, UPDATE_PERIOD, 23457)
incThermo.start() #start the thread
incHeater = infinc.SmartHeater(powerOutput = 1500, setTemperature = 45 + 273, thermometer = incThermo, updatePeriod = UPDATE_PERIOD)
inc.setHeater(incHeater)
incHeater.start() #start the thread
sim = infinc.Simulator(infant = bob, incubator = inc, roomTemp = 20 + 273, timeStep = SIMULATION_STEP, sleepTime = SIMULATION_STEP / 10)
sim.start()
sc = SimpleClient(bobThermo, incThermo)
plt.grid()
plt.show()