queue peering requests and send again
This commit is contained in:
parent
255bb6ce5c
commit
e4ca454c41
11 changed files with 157 additions and 97 deletions
|
|
@ -10,7 +10,7 @@ import state
|
|||
from websocket import trigger_event
|
||||
|
||||
import logging
|
||||
logger = logging.getLogger('oml.node.api')
|
||||
logger = logging.getLogger('oml.node.nodeapi')
|
||||
|
||||
def api_pullChanges(app, remote_id, user_id=None, from_=None, to=None):
|
||||
if user_id and not from_ and not to:
|
||||
|
|
@ -69,6 +69,7 @@ def api_acceptPeering(app, user_id, username, message):
|
|||
user.info['message'] = message
|
||||
user.update_peering(True, username)
|
||||
trigger_event('peering', user.json())
|
||||
state.nodes.queue('add', user.id)
|
||||
return True
|
||||
return False
|
||||
|
||||
|
|
@ -5,6 +5,7 @@ import os
|
|||
import tornado
|
||||
from tornado.web import Application
|
||||
from tornado.httpserver import HTTPServer
|
||||
from tornado.ioloop import PeriodicCallback
|
||||
|
||||
import settings
|
||||
|
||||
|
|
@ -14,7 +15,7 @@ import user
|
|||
|
||||
import json
|
||||
from utils import valid, get_public_ipv6
|
||||
import api
|
||||
import nodeapi
|
||||
import cert
|
||||
|
||||
import logging
|
||||
|
|
@ -46,7 +47,7 @@ class NodeHandler(tornado.web.RequestHandler):
|
|||
content = {}
|
||||
if valid(key, data, sig):
|
||||
action, args = json.loads(data)
|
||||
logger.debug('%s action %s %s', key, action, args)
|
||||
logger.debug('NODE action %s %s (%s)', action, args, key)
|
||||
if action == 'ping':
|
||||
content = {
|
||||
'ip': request.remote_addr
|
||||
|
|
@ -57,13 +58,17 @@ class NodeHandler(tornado.web.RequestHandler):
|
|||
if action in (
|
||||
'requestPeering', 'acceptPeering', 'rejectPeering', 'removePeering'
|
||||
) or (u and u.peered):
|
||||
content = getattr(api, 'api_' + action)(self.app, key, *args)
|
||||
content = getattr(nodeapi, 'api_' + action)(self.app, key, *args)
|
||||
else:
|
||||
logger.debug('PEER %s IS UNKNOWN SEND 403', key)
|
||||
self.set_status(403)
|
||||
content = {
|
||||
'status': 'not peered'
|
||||
}
|
||||
if u and u.pending:
|
||||
logger.debug('ignore request from pending peer[%s] %s (%s)', key, action, args)
|
||||
content = {}
|
||||
else:
|
||||
logger.debug('PEER %s IS UNKNOWN SEND 403', key)
|
||||
self.set_status(403)
|
||||
content = {
|
||||
'status': 'not peered'
|
||||
}
|
||||
content = json.dumps(content)
|
||||
sig = settings.sk.sign(content, encoding='base64')
|
||||
self.set_header('X-Ed25519-Signature', sig)
|
||||
|
|
@ -103,13 +108,28 @@ class ShareHandler(tornado.web.RequestHandler):
|
|||
self.finish()
|
||||
|
||||
|
||||
def publish_node():
|
||||
def publish_node(app):
|
||||
host = get_public_ipv6()
|
||||
state.online = directory.put(settings.sk, {
|
||||
'host': host,
|
||||
'port': settings.server['node_port'],
|
||||
'cert': settings.server['cert']
|
||||
})
|
||||
if state.online:
|
||||
with app.app_context():
|
||||
for u in user.models.User.query.filter_by(queued=True):
|
||||
logger.debug('adding queued node... %s', u.id)
|
||||
state.nodes.queue('add', u.id)
|
||||
state.check_nodes = PeriodicCallback(lambda: check_nodes(app), 60000)
|
||||
state.check_nodes.start()
|
||||
|
||||
def check_nodes(app):
|
||||
if state.online:
|
||||
with app.app_context():
|
||||
for u in user.models.User.query.filter_by(queued=True):
|
||||
if not state.nodes.check_online(u.id):
|
||||
logger.debug('queued peering message for %s trying to connect...', u.id)
|
||||
state.nodes.queue('add', u.id)
|
||||
|
||||
def start(app):
|
||||
application = Application([
|
||||
|
|
@ -124,5 +144,5 @@ def start(app):
|
|||
"keyfile": settings.ssl_key_path
|
||||
})
|
||||
http_server.listen(settings.server['node_port'], settings.server['node_address'])
|
||||
state.main.add_callback(publish_node)
|
||||
state.main.add_callback(publish_node, app)
|
||||
return http_server
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue