Skip to content

Commit

Permalink
Replace asyncore with socketserver in network_time_sync.py
Browse files Browse the repository at this point in the history
  • Loading branch information
romanroibu committed May 4, 2021
1 parent 0d1bee0 commit 0f2fd7a
Showing 1 changed file with 27 additions and 37 deletions.
64 changes: 27 additions & 37 deletions pupil_src/shared_modules/network_time_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
---------------------------------------------------------------------------~(*)
"""

import functools
from time import sleep
from uvc import get_time_monotonic
import socket
import socketserver
import threading
import asyncore
import struct
from random import random

Expand All @@ -36,49 +37,42 @@
"""


class Time_Echo(asyncore.dispatcher_with_send):
class Time_Echo(socketserver.BaseRequestHandler):
"""
Subclass do not use directly!
reply to request with timestamp
"""

def __init__(self, sock, time_fn):
def __init__(self, *args, time_fn, **kwargs):
self.time_fn = time_fn
asyncore.dispatcher_with_send.__init__(self, sock)
super().__init__(*args, **kwargs)

def handle_read(self):
def handle(self):
# expecting `sync` message
data = self.recv(4)
if data:
self.send(struct.pack("<d", self.time_fn()))
data = self.request.recv(4)
if data.decode("utf-8") == "sync":
self.request.send(struct.pack("<d", self.time_fn()))

def __del__(self):
pass
# print 'goodbye'


class Time_Echo_Server(asyncore.dispatcher):
class Time_Echo_Server(socketserver.ThreadingTCPServer):
"""
Subclass do not use directly!
bind at next open port and listen for time sync requests.
"""

def __init__(self, time_fn, socket_map, host=""):
asyncore.dispatcher.__init__(self, socket_map)
self.time_fn = time_fn
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.set_reuse_addr()
self.bind((host, 0))
self.port = self.socket.getsockname()[1]
self.listen(5)
def __init__(self, *, time_fn, host="", **kwargs):
handler_class = functools.partial(Time_Echo, time_fn=time_fn)
super().__init__((host, 0), handler_class, **kwargs)
self.allow_reuse_address = True
logger.debug("Timer Server ready on port: {}".format(self.port))

def handle_accept(self):
pair = self.accept()
if pair is not None:
sock, addr = pair
logger.debug("syching with %s" % str(addr))
Time_Echo(sock, self.time_fn)
@property
def host(self) -> str:
return self.server_address[0]

@property
def port(self) -> int:
return self.server_address[1]

def __del__(self):
logger.debug("Server closed")
Expand All @@ -92,20 +86,15 @@ class Clock_Sync_Master(threading.Thread):

def __init__(self, time_fn):
threading.Thread.__init__(self)
self.socket_map = {}
self.server = Time_Echo_Server(time_fn, self.socket_map)
self.server = Time_Echo_Server(time_fn=time_fn)
self.start()

def run(self):
asyncore.loop(use_poll=True, timeout=1)
self.server.serve_forever()

def stop(self):
# we dont use server.close() as this raises a bad file decritoor exception in loop
self.server.connected = False
self.server.accepting = False
self.server.del_channel()
self.server.shutdown()
self.join()
self.server.socket.close()
logger.debug("Server Thread closed")

def terminate(self):
Expand Down Expand Up @@ -221,8 +210,9 @@ def _get_offset(self):
server_socket.send(b"sync")
message = server_socket.recv(8)
t2 = self.get_time()
t1 = struct.unpack("<d", message)[0]
times.append((t0, t1, t2))
if message:
t1 = struct.unpack("<d", message)[0]
times.append((t0, t1, t2))

server_socket.close()

Expand Down

0 comments on commit 0f2fd7a

Please sign in to comment.