Skip to content

Commit

Permalink
Allowing creation of subscriber before publishes in python interface too
Browse files Browse the repository at this point in the history
  • Loading branch information
Bardo91 committed Feb 4, 2019
1 parent 0b4e6b3 commit 7032c70
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
8 changes: 7 additions & 1 deletion python/fastcom/ImagePublisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,13 @@ def __callbackListen(self):
print("Waiting for connection")
data, address = self.sock.recvfrom(1)
print("Received new connection from: ", address)
self.sock.sendto(b'1', address)
self.guard_list.acquire()
self.client_list.append(address)
addCon = True
for addr in self.client_list:
if addr == address:
addCon = False
if addCon:
self.client_list.append(address)
self.guard_list.release()

16 changes: 14 additions & 2 deletions python/fastcom/ImageSubscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,20 @@ def __init__(self, _host, _port):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.server_address = (_host, _port)

# Send one byte to notify publisher
self.sock.sendto(b'1', self.server_address)
# Ensure connection
self.sock.setblocking(False)
self.sock.settimeout(0.2)
receivedConnection = False
while not receivedConnection:
try:
self.sock.sendto(b'1', self.server_address)
data, address = self.sock.recvfrom(1)
if(len(data) == 1):
receivedConnection = True
except socket.timeout:
pass

self.sock.setblocking(True)

# Start listening for publisher to call callbacks
self.listen_thread = threading.Thread(target=self.__callbackListen)
Expand Down
8 changes: 7 additions & 1 deletion python/fastcom/Publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ def __callbackListen(self):
print("Waiting for connection")
data, address = self.sock.recvfrom(1)
print("Received new connection from: ", address)
self.sock.sendto(b'1', address)
self.guard_list.acquire()
self.client_list.append(address)
addCon = True
for addr in self.client_list:
if addr == address:
addCon = False
if addCon:
self.client_list.append(address)
self.guard_list.release()

18 changes: 15 additions & 3 deletions python/fastcom/Subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,21 @@ def __init__(self, _host, _port):
# Create socket
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.server_address = (_host, _port)

# Send one byte to notify publisher
self.sock.sendto(b'1', self.server_address)

# Ensure connection
self.sock.setblocking(False)
self.sock.settimeout(0.2)
receivedConnection = False
while not receivedConnection:
try:
self.sock.sendto(b'1', self.server_address)
data, address = self.sock.recvfrom(1)
if(len(data) == 1):
receivedConnection = True
except socket.timeout:
pass

self.sock.setblocking(True)

# Start listening for publisher to call callbacks
self.listen_thread = threading.Thread(target=self.__callbackListen)
Expand Down

0 comments on commit 7032c70

Please sign in to comment.