Upgrades for 2.0

* Added support for /uuid REST method
* Now the http server is relocated on IP Change detection
This commit is contained in:
Adolfo Gómez García 2016-03-14 12:12:56 +01:00
parent 135392d245
commit 2e96074961
2 changed files with 28 additions and 3 deletions

View File

@ -172,7 +172,10 @@ class HTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def get_information(self, params):
# TODO: Return something useful? :)
return 'Information'
return 'Up and running'
def get_uuid(self, params):
return self.service.api.uuid
def log_error(self, fmt, *args):
logger.error('HTTP ' + fmt % args)
@ -188,9 +191,13 @@ class HTTPServerThread(threading.Thread):
if HTTPServerHandler.uuid is None:
HTTPServerHandler.uuid = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(48))
self.certFile = createSelfSignedCert()
HTTPServerHandler.service = service
self.certFile = createSelfSignedCert()
self.initiateServer(address)
def initiateServer(self, address):
self.server = socketserver.TCPServer(address, HTTPServerHandler)
self.server.socket = ssl.wrap_socket(self.server.socket, certfile=self.certFile, server_side=True)
@ -201,5 +208,14 @@ class HTTPServerThread(threading.Thread):
logger.debug('Stopping REST Service')
self.server.shutdown()
def restart(self, address=None):
if address is None:
address = self.server.server_address
self.stop()
self.initiateServer(address)
def run(self):
self.server.serve_forever()

View File

@ -206,8 +206,17 @@ class CommonService(object):
try:
# Notifies all interfaces IPs
self.api.notifyIpChanges(((v.mac, v.ip) for v in netInfo))
# Regenerates Known ips
self.knownIps = dict(((v.mac, v.ip) for v in netInfo))
# And notify new listening address to broker
address = (self.knownIps[self.api.mac], random.randrange(43900, 44000))
# And new listening address
self.httpServer.restart(address)
# sends notification
self.api.notifyComm(self.httpServer.getServerUrl())
except Exception as e:
logger.warn('Got an error notifiying IPs to broker: {} (will retry in a bit)'.format(e.message.decode('windows-1250', 'ignore')))
@ -237,7 +246,7 @@ class CommonService(object):
self.ipc.start()
if self.api.mac in self.knownIps:
address = (self.knownIps[self.api.mac], random.randrange(40000, 44000))
address = (self.knownIps[self.api.mac], random.randrange(43900, 44000))
logger.info('Starting REST listener at {}'.format(address))
self.httpServer = httpserver.HTTPServerThread(address, self)
self.httpServer.start()