This repository has been archived on 2025-06-04. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
fl0w-old/Shared/Routing.py
Philip Trauner ab6444f118 Overhaul to work with Highway
create_routes now doesn't launch them automatically because routes are put together before the socket actually becomes avaliable.
create_exchange_map previously used the wrong index for the route list.
There is now a universal Route class from which ServerRoute and ClientRoute inherit.
2016-09-18 19:49:53 +02:00

70 lines
No EOL
1.5 KiB
Python

BROADCAST = 0
ROUTE = 1
SOCK = 2
class InvalidRouteSetup(AttributeError):
def __init__(self, msg):
super(AttributeError, self).__init__(msg)
class InvalidRouteLength(AttributeError):
def __init__(self, msg):
super(AttributeError, self).__init__(msg)
class Route:
def run(self, data, handler):
pass
def start(self, handler):
pass
class ServerRoute(Route):
REQUIRED = []
PATCHED = False
class ClientRoute(Route):
pass
def create_routes(routes, handler):
routes = routes.copy()
reverse_routes = {}
for prefix in routes:
if type(routes[prefix]) is tuple or type(routes[prefix]) is list:
routes[prefix] = routes[prefix][0](**routes[prefix][1])
reverse_routes[routes[prefix]] = prefix
for prefix in routes:
attrs = dir(routes[prefix])
if not routes[prefix].PATCHED:
for required in routes[prefix].REQUIRED:
if required == BROADCAST:
routes[prefix].broadcast = handler.broadcast
elif required == ROUTE:
routes[prefix].route = reverse_routes[routes[prefix]]
routes[prefix].PATCHED = True
return routes
def launch_routes(created_routes, handler):
for prefix in created_routes:
created_routes[prefix].start(handler)
def create_exchange_map(routes):
exchange_map = {-1 : "meta"}
exchange_id = 0
for route in routes:
exchange_map[exchange_id] = route
exchange_id += 1
return exchange_map
def validate_exchange_map(routes):
for key in routes:
if not type(key) is int and type(routes[key]) is str:
return False
return True