Пример #1
0
def prune():
    requests = RAMCollection('cascade requests')
    responses = RAMCollection('cascade responses')

    cscuuids = []

    for objuuid in requests.list_objuuids():
        request = requests.get_object(objuuid)

        try:
            if time() - request.object['timestamp'] > ASYNC_CASCADE_TIMEOUT:
                request.destroy()
            else:
                cscuuids.append(request.object['cscuuid'])
        except:
            request.destroy()

    for objuuid in responses.list_objuuids():
        response = responses.get_object(objuuid)

        try:
            if response.object['cscuuid'] not in cscuuids:
                response.destroy()
        except:
            response.destroy()
Пример #2
0
def __forward(message):
    peers = RAMCollection('peers').find(agtuuid=message['dest'])

    if message['dest'] == cherrypy.config.get('agtuuid'):
        process(message)
        ctr_increment('messages forwarded')
    elif len(peers) > 0:
        try:
            if peers[0].object['url'] != None:
                MPIClient(peers[0].object['url'],
                          cherrypy.config.get(
                              'server.secret_digest')).send_json(message)

                ctr_increment('messages forwarded')
            else:
                push_message(message)
        except:
            ctr_increment('messages dropped')
    else:
        weight = None
        best_route = None

        for route in RAMCollection('routes').find(agtuuid=message['dest']):
            if weight == None or float(route.object['weight']) < float(weight):
                weight = route.object['weight']
                best_route = route

        if best_route is not None:
            gtwuuid = best_route.object['gtwuuid']
        else:
            gtwuuid = None

        peers = RAMCollection('peers').find(agtuuid=gtwuuid)
        if len(peers) > 0:
            try:
                if peers[0].object['url'] != None:
                    MPIClient(peers[0].object['url'],
                              cherrypy.config.get(
                                  'server.secret_digest')).send_json(message)

                    ctr_increment('messages forwarded')
                else:
                    push_message(message)
            except:
                ctr_increment('messages dropped')
        else:
            ctr_increment('messages dropped')

    ctr_decrement('threads (forwarding)')
Пример #3
0
def process_cascade_request(message):
    ctr_increment('cascades processed')

    cascade_requests = RAMCollection('cascade requests')

    try:
        if len(cascade_requests.find_objuuids(
                cscuuid=message['cscuuid'])) == 0:
            cascade_request = cascade_requests.get_object()
            cascade_request.object = message
            cascade_request.set()

            if len(message['ftags']) > 0:
                if len(list(set(message['ftags'])
                            & set(get_tag_values()))) > 0:
                    Thread(target=forward_cascade_request,
                           args=(message, )).start()
            else:
                Thread(target=forward_cascade_request,
                       args=(message, )).start()

            if len(message['etags']) > 0:
                if len(list(set(message['etags'])
                            & set(get_tag_values()))) > 0:
                    Thread(target=execute_cascade_request,
                           args=(message, )).start()
            else:
                Thread(target=execute_cascade_request,
                       args=(message, )).start()
    except:
        pass
Пример #4
0
def wait_on_cascade_responses(cscuuid, timeout=None):
    if timeout == None:
        sleep(SYNC_CASCADE_TIMEOUT)
    else:
        sleep(timeout)

    response_objects = []

    for response in RAMCollection('cascade responses').find(cscuuid=cscuuid):
        response_objects.append(response.object)
        response.destroy()

    for request in RAMCollection('cascade requests').find(cscuuid=cscuuid):
        request.destroy()

    return response_objects
Пример #5
0
def get_cascade_responses(cscuuid):
    response_objects = []

    for response in RAMCollection('cascade responses').find(cscuuid=cscuuid):
        response_objects.append(response.object)

    return response_objects
Пример #6
0
def create_route(agtuuid, gtwuuid, weight, timestamp=None):
    collection = RAMCollection('routes')

    routes = collection.find(agtuuid=agtuuid, gtwuuid=gtwuuid)

    if len(routes) > 1:
        for route in routes:
            route.destroy()

        route = collection.get_object()
        route.object = {
            'gtwuuid': gtwuuid,
            'agtuuid': agtuuid,
            'weight': weight
        }
        route.set()
    elif len(routes) == 1:
        route = routes[0]
        if route.object['weight'] > weight:
            route.object['weight'] = weight
            route.set()
    else:
        route = collection.get_object()
        route.object = {
            'gtwuuid': gtwuuid,
            'agtuuid': agtuuid,
            'weight': weight
        }
        route.set()
Пример #7
0
def init_peers():
    ram_peers = RAMCollection('peers')
    peers = SQLCollection('peers')

    for objuuid in peers.list_objuuids():
        ram_peer = ram_peers.get_object(objuuid)
        ram_peer.object = peers.get_object(objuuid).object
        ram_peer.set()
Пример #8
0
def touch_peer(agtuuid):
    if cherrypy.config.get('agtuuid') != agtuuid:
        peers = RAMCollection('peers').find(agtuuid=agtuuid)

        if len(peers) == 0:
            create_peer(agtuuid, ttl=PEER_TIMEOUT)
        else:
            if 'refresh time' in peers[0].object:
                if peers[0].object['refresh time'] < time():
                    create_peer(agtuuid, ttl=PEER_TIMEOUT)
Пример #9
0
def delete_peers():
    peers = SQLCollection('peers')

    for peer in peers.find():
        peer.destroy()

    peers = RAMCollection('peers')

    for peer in peers.find():
        peer.destroy()
Пример #10
0
def process_route_advertisement(advertisement):
    peers = RAMCollection('peers')
    routes = RAMCollection('routes')

    ignored_peers = [cherrypy.config.get('agtuuid')]
    for peer in peers.find():
        try:
            ignored_peers.append(peer.object['agtuuid'])
        except:
            pass

    for route in advertisement['routes']:
        try:
            if route['agtuuid'] not in ignored_peers:
                create_route(route['agtuuid'], advertisement['agtuuid'],
                             route['weight'] + 1)
        except:
            pass

    prune()
Пример #11
0
def pop_cascade_responses(cscuuid):
    responses = RAMCollection('cascade responses')

    response_objects = []

    for objuuid in responses.find_objuuids(cscuuid=cscuuid):
        response = responses.get_object(objuuid)

        response_objects.append(response.object)
        response.destroy()

    return response_objects
Пример #12
0
def age_routes(v):
    routes = RAMCollection('routes')

    for route in routes.find():
        try:
            if route.object['weight'] > MAX_WEIGHT:
                route.destroy()
            else:
                route.object['weight'] = route.object['weight'] + v
                route.set()
        except:
            route.destroy()
Пример #13
0
def prune():
    routes = RAMCollection('routes')
    peers = RAMCollection('peers')

    peer_agtuuids = []

    for peer in peers.find():
        try:
            if 'destroy time' in peer.object:
                if peer.object['destroy time'] < time():
                    peer.destroy()
                else:
                    peer_agtuuids.append(peer.object['agtuuid'])
            else:
                peer_agtuuids.append(peer.object['agtuuid'])
        except:
            peer.destroy()

    peers = SQLCollection('peers')

    for peer in peers.find():
        try:
            if 'destroy time' in peer.object:
                if peer.object['destroy time'] < time():
                    peer.destroy()
                else:
                    peer_agtuuids.append(peer.object['agtuuid'])
            else:
                peer_agtuuids.append(peer.object['agtuuid'])
        except:
            peer.destroy()

    for route in routes.find():
        try:
            if (len(peers.find(agtuuid=route.object['agtuuid'])) > 0 or
                    route.object['agtuuid'] == cherrypy.config.get('agtuuid')
                    or route.object['gtwuuid'] not in peer_agtuuids):
                route.destroy()
        except:
            route.destroy()
Пример #14
0
def create_route_advertisement():
    prune()

    routes = RAMCollection('routes')
    peers = RAMCollection('peers')

    advertisement = {}
    advertisement['type'] = 'route advertisement'
    advertisement['agtuuid'] = cherrypy.config.get('agtuuid')
    advertisement['routes'] = []

    for route in routes.find():
        try:
            if 'agtuuid' not in route.object:
                raise Exception('Invalid Route')

            if 'weight' not in route.object:
                raise Exception('Invalid Route')

            temp = {}
            temp['agtuuid'] = route.object['agtuuid']
            temp['weight'] = route.object['weight']
            temp['gtwuuid'] = cherrypy.config.get('agtuuid')

            advertisement['routes'].append(temp)
        except:
            route.destroy()

    for peer in peers.find():
        try:
            temp = {}
            temp['agtuuid'] = peer.object['agtuuid']
            temp['weight'] = 0
            temp['gtwuuid'] = cherrypy.config.get('agtuuid')

            advertisement['routes'].append(temp)
        except:
            peer.destroy()

    return advertisement
Пример #15
0
def pull_messages(agtuuid):
    agtuuids = []
    agtuuids.append(agtuuid)

    routes = RAMCollection('routes')

    routes_dict = {}

    for rteuuid in routes.list_objuuids():
        route = routes.get_object(rteuuid)

        try:
            if route.object['agtuuid'] in routes_dict:
                if float(routes_dict[route.object['agtuuid']]['weight']) > \
                   float(route.object['weight']):
                    routes_dict[route.object['agtuuid']] = {
                        'weight': route.object['weight'],
                        'gtwuuid': route.object['gtwuuid']
                    }
            else:
                routes_dict[route.object['agtuuid']] = {
                    'weight': route.object['weight'],
                    'gtwuuid': route.object['gtwuuid']
                }
        except:
            route.destroy()

    for k, v in routes_dict.items():
        try:
            if v['gtwuuid'] == agtuuid:
                agtuuids.append(k)
        except:
            pass

    messages = []
    for agtuuid in agtuuids:
        messages += pop_messages(dest=agtuuid)

    return messages
Пример #16
0
def create_peer(agtuuid, url=None, ttl=None, polling=False):
    collection = SQLCollection('peers')

    peers = collection.find(agtuuid=agtuuid)

    if len(peers) == 1:
        peer = peers[0]
    else:
        peer = collection.get_object()

    peer.object = {'agtuuid': agtuuid, 'url': url, 'polling': polling}

    if ttl != None:
        peer.object['destroy time'] = time() + ttl
        peer.object['refresh time'] = time() + PEER_REFRESH

    peer.set()

    collection = RAMCollection('peers')

    peers = collection.find(agtuuid=agtuuid)

    if len(peers) == 1:
        peer = peers[0]
    else:
        peer = collection.get_object()

    peer.object = {'agtuuid': agtuuid, 'url': url, 'polling': polling}

    if ttl != None:
        peer.object['destroy time'] = time() + ttl
        peer.object['refresh time'] = time() + PEER_REFRESH

    peer.set()

    return peer
Пример #17
0
def execute_cascade_request(message):
    ctr_increment('cascades executed')

    request = message['request']
    response = {}

    try:
        if request['type'] == 'process sync':
            if 'timeout' in request:
                status, stdout, stderr = process_sync(
                    request['command'], timeout=request['timeout'])
            else:
                status, stdout, stderr = process_sync(request['command'])

            response['type'] = request['type']
            response['stdout'] = b64encode(stdout).decode()
            response['stderr'] = b64encode(stderr).decode()
            response['status'] = status

            if message['anonymous'] == False:
                push_message({
                    'type': 'cascade response',
                    'dest': message['src'],
                    'cscuuid': message['cscuuid'],
                    'response': response,
                    'src': kvstore.get(name='agtuuid')
                })

        elif request['type'] == 'file read':
            response['type'] = request['type']
            response['b64data'] = b64encode(file_read(
                request['filename'])).decode()

            push_message({
                'type': 'cascade response',
                'dest': message['src'],
                'cscuuid': message['cscuuid'],
                'response': response,
                'src': kvstore.get(name='agtuuid')
            })

        elif request['type'] == 'file write':
            file_write(request['filename'], b64decode(request['b64data']))

        elif request['type'] == 'delete route':
            for route in RAMCollection('routes').find(
                    agtuuid=request['agtuuid']):
                route.destroy()

            for route in RAMCollection('routes').find(
                    gtwuuid=request['agtuuid']):
                route.destroy()

        elif request['type'] == 'find collection objects':
            collection = SQLCollection(request['name'])

            response = []

            for objuuid in collection.find_objuuids(**request['query']):
                response.append(collection.get_object(objuuid).object)

            if len(response) > 0:
                push_message({
                    'type': 'cascade response',
                    'dest': message['src'],
                    'cscuuid': message['cscuuid'],
                    'response': response,
                    'src': kvstore.get(name='agtuuid')
                })

        elif request['type'] == 'execute python':
            response['status'], response['stdout'], response[
                'stderr'] = interpret(request['body'])

            if message['anonymous'] == False:
                push_message({
                    'type': 'cascade response',
                    'dest': message['src'],
                    'cscuuid': message['cscuuid'],
                    'response': response,
                    'src': kvstore.get(name='agtuuid')
                })
    except:
        pass
Пример #18
0
def get_routes():
    route_list = []
    routes = RAMCollection('routes')
    for route in routes.find():
        route_list.append(route.object)
    return route_list
Пример #19
0
        try:
            if time() - request.object['timestamp'] > ASYNC_CASCADE_TIMEOUT:
                request.destroy()
            else:
                cscuuids.append(request.object['cscuuid'])
        except:
            request.destroy()

    for objuuid in responses.list_objuuids():
        response = responses.get_object(objuuid)

        try:
            if response.object['cscuuid'] not in cscuuids:
                response.destroy()
        except:
            response.destroy()


def worker():
    register_timer(name='cascade_worker', target=worker, timeout=60).start()

    prune()


collection = RAMCollection('cascade requests')
collection.create_attribute('cscuuid', "['cscuuid']")

collection = RAMCollection('cascade responses')
collection.create_attribute('cscuuid', "['cscuuid']")

Thread(target=worker).start()
Пример #20
0
def get_peers():
    peer_list = []
    peers = RAMCollection('peers')
    for peer in peers.find():
        peer_list.append(peer.object)
    return peer_list
Пример #21
0
def service_cascade_request(message):
    ctr_increment('cascades serviced')

    cascade_response = RAMCollection('cascade responses').get_object()
    cascade_response.object = message
    cascade_response.set()
Пример #22
0
def delete_peer(agtuuid):
    for peer in RAMCollection('peers').find(agtuuid=agtuuid):
        peer.destroy()

    for peer in SQLCollection('peers').find(agtuuid=agtuuid):
        peer.destroy()
Пример #23
0
def delete_route(agtuuid, gtwuuid):
    for route in RAMCollection('routes').find(agtuuid=agtuuid,
                                              gtwuuid=gtwuuid):
        route.destroy()
Пример #24
0
            temp['gtwuuid'] = cherrypy.config.get('agtuuid')

            advertisement['routes'].append(temp)
        except:
            peer.destroy()

    return advertisement


def init_peers():
    ram_peers = RAMCollection('peers')
    peers = SQLCollection('peers')

    for objuuid in peers.list_objuuids():
        ram_peer = ram_peers.get_object(objuuid)
        ram_peer.object = peers.get_object(objuuid).object
        ram_peer.set()


collection = SQLCollection('peers')
collection.create_attribute('agtuuid', "['agtuuid']")

collection = RAMCollection('peers')
collection.create_attribute('agtuuid', "['agtuuid']")

collection = RAMCollection('routes')
collection.create_attribute('agtuuid', "['agtuuid']")
collection.create_attribute('gtwuuid', "['gtwuuid']")
collection.create_attribute('weight', "['weight']")

init_peers()