-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver2.py
More file actions
49 lines (44 loc) · 1.58 KB
/
server2.py
File metadata and controls
49 lines (44 loc) · 1.58 KB
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
# AUM SHREEGANESHAAYA NAMAH||
from numpy.random import poisson
from copy import deepcopy
from threading import Lock
poi_avg_server = 25000 # Average computing times (Poisson distribution) in microseconds (us)
class Server2:
def __init__(self, carNum = 0):
self.carNum = carNum
self.infoBucket = {}
self.lock = Lock()
self.maxLocalTs = -1
self.mode = "bc" # "bc" (broadcast) / "rx" (receive)
self.rxRemain = 0 # remaining cars to receive infoBucket
def broadcast(self, info):
# assume info is dictionary with keys "ID", "timestamp" at the least
while self.mode != "bc" : pass
infoCopy = deepcopy(info)
self.lock.acquire()
localTs = infoCopy["timestamp"] # this is the broadasting start timestamp, not the fpStartTs
if localTs > self.maxLocalTs: self.maxLocalTs = localTs
self.infoBucket[infoCopy["ID"]] = infoCopy
if len(self.infoBucket) == self.carNum: # prepare for next receive
self.rxRemain = self.carNum
self.mode = "rx"
self.lock.release()
def receive(self):
while self.mode != "rx" : pass
offset = poisson(poi_avg_server)
retVal = (deepcopy(self.infoBucket), self.maxLocalTs + offset)
self.lock.acquire()
self.rxRemain -= 1
if self.rxRemain == 0: # prepare for next broadcast
self.infoBucket = {}
self.maxLocalTs = -1
self.mode = "bc"
self.lock.release()
return retVal
def dontCare(self): # ID not even required
self.lock.acquire()
self.carNum -= 1
if len(self.infoBucket) == self.carNum:
self.rxRemain = self.carNum
self.mode = "rx"
self.lock.release()