Commit 6cf812a0 authored by Georg von Zengen's avatar Georg von Zengen

added Locks

parent 09b76980
......@@ -19,8 +19,9 @@ def client(config, channels):
alfred.sendData(channels,sock,address)
batadv.sendData(config['interface'], sock, address)
def alfred_cleaner(config, alfred_channels,batadv):
def alfred_cleaner(config, alfred_channels,batadv,alfredLock,batadvLock):
while True:
alfredLock.acquire(True)
for channel in alfred_channels.keys():
to_del = []
for node in alfred_channels[channel].keys():
......@@ -38,7 +39,9 @@ def alfred_cleaner(config, alfred_channels,batadv):
f = open(config['alfred_out'],'w')
json.dump(alfred_channels,f)
f.close()
alfredLock.release()
batadvLock.acquire(True)
new = list(filter(lambda entry: time.time() - entry['time'] > config['update_interval'] * 2, batadv))
for entry in new:
batadv.remove(entry)
......@@ -49,10 +52,11 @@ def alfred_cleaner(config, alfred_channels,batadv):
output += entry['data']+'\n'
f.write(output)
f.close()
batadvLock.release()
time.sleep(10)
def alfred_server(config, alfred_channels,batadv_data):
def alfred_server(config, alfred_channels,batadv_data,alfredLock,batadvLock):
UDP_IP = config['server_bind_to']
UDP_PORT = config['server_port']
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
......@@ -62,6 +66,7 @@ def alfred_server(config, alfred_channels,batadv_data):
data = gzip.zlib.decompress(data)
rdata = json.loads(data.decode('utf-8'))
if rdata[0] == "alfred":
alfredLock.acquire(True)
channel = rdata[1]
message = rdata[2]
node = json.loads(message)
......@@ -78,12 +83,15 @@ def alfred_server(config, alfred_channels,batadv_data):
else:
alfred_channels[str(channel)][mac] = node
alfred_channels[str(channel)][mac]['time'] = int(time.time())
alfredLock.release()
elif rdata[0] == "batadv-vis":
batadvLock.acquire(True)
line = rdata[1]
entry = {'time': time.time()}
entry['data'] = line
batadv_data.append(entry)
batadvLock.release()
else:
print('got unknown data')
......@@ -96,6 +104,8 @@ def main():
parser.add_argument('-c', default="config.json", action='store', dest='configFile', help='config file')
parser.add_argument('-r', default=[], action='append', dest='channels', help='alfred channels to transmit')
args = parser.parse_args()
alfredLock = threading.Lock()
batadvLock = threading.Lock()
try:
f = open(args.configFile)
......@@ -119,8 +129,8 @@ def main():
print("server mode")
global_alfred_channels = {}
batadv_data=[]
threading.Thread(target=alfred_server, args=(config,global_alfred_channels,batadv_data)).start()
threading.Thread(target=alfred_cleaner, args=(config,global_alfred_channels,batadv_data)).start()
threading.Thread(target=alfred_server, args=(config,global_alfred_channels,batadv_data,alfredLock,batadvLock)).start()
threading.Thread(target=alfred_cleaner, args=(config,global_alfred_channels,batadv_data,alfredLock,batadvLock)).start()
exit(0)
try:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment