Client now publishes itself

This commit is contained in:
Ruben van de Ven 2019-01-16 09:00:49 +01:00
parent ad9a4b8f13
commit 7bce8fd318
4 changed files with 37 additions and 8 deletions

View File

@ -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

View File

@ -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')

View File

@ -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()

View File

@ -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)