Revamp xmpp and rearranging code

Rework XMPP to support iq and sasl auth
Organize and rework the run code
Support for threading
This commit is contained in:
Brian Martin 2019-02-15 23:12:33 -05:00
parent a1b1b1a23b
commit acbcb8707a
5 changed files with 564 additions and 204 deletions

View file

@ -6,9 +6,10 @@ import sys, socket
import time
import platform
bumperlog = logging.getLogger("bumper")
def main():
args = sys.argv
if len(args) > 0:
if '--debug' in args:
logging.basicConfig(level=logging.DEBUG,
@ -18,14 +19,7 @@ def main():
format="[%(asctime)s] :: %(levelname)s :: %(name)s :: %(message)s")
#format="[%(asctime)s] :: %(levelname)s :: %(name)s :: %(module)s :: %(funcName)s :: %(lineno)d :: %(message)s")
# A default bot could be set here to automatically add it as available
# dbot = bumper.VacBotDevice("did", "class", "resource", "name","nick" )
# bclient = bumper.bumper_bots_var
# bclienttemp = bclient.get()
# bclienttemp.append(dbot.asdict())
# bclient.set(bclienttemp)
if platform.system() == "Darwin":
if platform.system() == "Darwin": #If a Mac, use 0.0.0.0 for listening
listen_host = "0.0.0.0"
else:
listen_host = socket.gethostbyname(socket.gethostname())
@ -35,36 +29,46 @@ def main():
conf_address_8007 = (listen_host, 8007)
xmpp_address = (listen_host, 5223)
mqtt_address = (listen_host, 8883)
xmpp_server = bumper.XMPPServer(xmpp_address)
mqtt_server = bumper.MQTTServer(mqtt_address,bumper_bots=bumper.bumper_bots_var,bumper_clients=bumper.bumper_clients_var,remove_clients=bumper.bumper_removeclients_var)
mqtt_helperbot = bumper.MQTTHelperBot(mqtt_address, bumper_bots=bumper.bumper_bots_var,bumper_clients=bumper.bumper_clients_var)
conf_server = bumper.ConfServer(conf_address_443, usessl=True, bumper_bots=bumper.bumper_bots_var,bumper_clients=bumper.bumper_clients_var, remove_clients=bumper.bumper_removeclients_var,helperbot=mqtt_helperbot)
conf_server_2 = bumper.ConfServer(conf_address_8007, usessl=False, bumper_bots=bumper.bumper_bots_var,bumper_clients=bumper.bumper_clients_var, helperbot=mqtt_helperbot,remove_clients=bumper.bumper_removeclients_var)
# start xmpp server on port 5223 (sync)
xmpp_server.run(run_async=True) #Start in new thread
# start mqtt server on port 8883 (async)
startmqttserver = "Starting MQTT Server at {}".format(mqtt_address)
bumperlog.info("{}".format(startmqttserver))
print("{}".format(startmqttserver))
mqtt_server = bumper.MQTTServer(mqtt_address, run_async=True,bumper_bots=bumper.bumper_bots_var,bumper_clients=bumper.bumper_clients_var)
# start mqtt server on port 8883 (async)
mqtt_server.run(run_async=True) #Start in new thread
time.sleep(1.5) #Wait for broker startup
# start mqtt_helperbot (async)
bumperlog.info("Starting MQTT HelperBot")
print("Starting MQTT HelperBot")
mqtt_helperbot = bumper.MQTTHelperBot(mqtt_address, run_async=True,bumper_bots=bumper.bumper_bots_var,bumper_clients=bumper.bumper_clients_var)
# start mqtt_helperbot (async)
mqtt_helperbot.run(run_async=True) #Start in new thread
# start conf server on port 443 (async) - Used for most https calls
startconf443 = "Starting Main ConfServer at {}".format(conf_address_443)
bumperlog.info("{}".format(startconf443))
print("{}".format(startconf443))
conf_server = bumper.ConfServer(conf_address_443, usessl=True, run_async=True,bumper_bots=bumper.bumper_bots_var,bumper_clients=bumper.bumper_clients_var, helperbot=mqtt_helperbot)
# start conf server on port 443 (async) - Used for most https calls
conf_server.run(run_async=True) #Start in new thread
# start conf server on port 8007 (async) - Used for a load balancer request
startconf8007 = "Starting LoadBalancer ConfServer at {}".format(conf_address_8007)
bumperlog.info("{}".format(startconf8007))
print("{}".format(startconf8007))
conf_server_2 = bumper.ConfServer(conf_address_8007, usessl=False, run_async=True,bumper_bots=bumper.bumper_bots_var,bumper_clients=bumper.bumper_clients_var, helperbot=mqtt_helperbot)
# start conf server on port 8007 (async) - Used for a load balancer request
conf_server_2.run(run_async=True) #Start in new thread
# start xmpp server on port 5223 (sync)
startxmpp = "Starting XMPP Server at {}".format(xmpp_address)
bumperlog.info("{}".format(startxmpp))
print("{}".format(startxmpp))
xmpp_server = bumper.XMPPServer(xmpp_address)
while True:
try:
time.sleep(0.25)
# WIP: Remove clients that have disconnected
remove_clients = bumper.bumper_removeclients_var.get()
if len(remove_clients) > 0:
for uid in remove_clients:
if uid != "":
xmpp_server.remove_client_byuid(uid) #Remove clients from xmpp server
remove_clients.remove(uid)
bumper.bumper_removeclients_var.set(remove_clients)
except KeyboardInterrupt:
print("Bumper Exiting")
if __name__ == "__main__":
main()