diff --git a/client_config.yml b/client_config.yml index 611621d..ec74ec9 100644 --- a/client_config.yml +++ b/client_config.yml @@ -1,6 +1,6 @@ events: cmd_address: "tcp://127.0.0.1:5555" - publish_address: "tcp://0.0.0.0:5556" + publish_address: "tcp://127.0.0.1:5556" voice: input_rate: 44100 target_rate: 16000 diff --git a/hugvey/client.py b/hugvey/client.py index b740f11..a31a005 100644 --- a/hugvey/client.py +++ b/hugvey/client.py @@ -113,6 +113,7 @@ class CommandHandler(object): self.hugvey_id = hugvey_id self.cmd_address = cmd_address self.publish_address = publish_address + # self.showMyself() # queue message for connection request def handle(self, cmd): # self.sendMessage({'reply':'test'}) @@ -121,9 +122,12 @@ class CommandHandler(object): return logger.info("Received {}".format(cmd)) + if cmd['action'] == 'show_yourself': + self.showMyself() if cmd['action'] == 'play': self.cmdPlay(cmd['id'], cmd['msg']) + def cmdPlay(self, msgId, msgText): # espeak(msgText) # TODO kill if playing & play wave file @@ -135,6 +139,23 @@ class CommandHandler(object): 'msgId': msgId }) + def showMyself(self): + """Publish about this hugvey to central command + """ + self.sendMessage({ + 'event': 'connection', + 'id': self.hugvey_id, + 'host': socket.gethostname(), + 'ip': self.getIp(), + }) + + @staticmethod + def getIp(): + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + s.connect(("185.66.250.60", 80)) + return s.getsockname()[0] + + def sendMessage(self, msg): self.eventQueue.append(msg) @@ -153,12 +174,14 @@ class CommandHandler(object): async def event_sender(self): s = self.ctx.socket(zmq.PUB) - # TODO: see if we can connect() here. So we can PUSH(??) the ip - s.bind(self.publish_address) + s.connect(self.publish_address) logger.info("Publish on: {}".format(self.publish_address)) + # For some reason, sending only one message is lost, perhaps due + # to connect() rather than bind() ?? + self.showMyself() while True: for i in range(len(self.eventQueue)): - await zmqSend(s, self.hugvey_id, self.eventQueue.pop(0)) + zmqSend(s, self.hugvey_id, self.eventQueue.pop(0)) if len(self.eventQueue) == 0: await asyncio.sleep(0.05) @@ -203,6 +226,6 @@ class Hugvey(object): asyncio.ensure_future(self.voice_server.asyncStart(loop)) asyncio.ensure_future(self.cmd_server.command_listener()) asyncio.ensure_future(self.cmd_server.event_sender()) - + self.cmd_server.showMyself() loop.run_forever() logger.info('done') diff --git a/hugvey/communication.py b/hugvey/communication.py index cc8bcac..5094cb6 100644 --- a/hugvey/communication.py +++ b/hugvey/communication.py @@ -7,11 +7,11 @@ def getTopic(hugvey_id): return "hv{}".format(hugvey_id) -async def zmqSend(socket, hugvey_id, msg): +def zmqSend(socket, hugvey_id, msg): msgData = json.dumps(msg) topic = getTopic(hugvey_id) logger.info("Send 0mq to {} containing {}".format(topic, msg)) - await socket.send_multipart([topic.encode(), msgData.encode()]) + socket.send_multipart([topic.encode(), msgData.encode()]) async def zmqReceive(socket): topic, msg = await socket.recv_multipart() diff --git a/test_pub.py b/test_pub.py index d9b5f7a..a7747e4 100644 --- a/test_pub.py +++ b/test_pub.py @@ -3,6 +3,12 @@ import random import sys import time import hugvey.communication +import coloredlogs, logging + +coloredlogs.install( + level=logging.DEBUG + ) + port = "5555" if len(sys.argv) > 1: @@ -11,7 +17,7 @@ if len(sys.argv) > 1: context = zmq.Context() socket = context.socket(zmq.PUB) -socket.bind("tcp://*:%s" % port) +socket.connect("tcp://127.0.0.1:%s" % port) while True: # topic = random.randrange(9999,10005)