Commits

Ginés Martínez Sánchez committed 9f10a49 Draft

router back way done?!

Comments (0)

Files changed (1)

ginsfsm/router.py

         'message_type',
         'my_gaplic_name',
         'my_roles',
-        'my_roles_back_way',
     ]
     identity_card_ack_fields = [
         'message_type',
             origin_gobj=None,
             my_gaplic_name=None,
             my_roles=None,
-            my_roles_back_way=None,
             kw=None,
             error_message=None,
             reference=None):
         self.origin_gobj = origin_gobj
         self.my_gaplic_name = my_gaplic_name
         self.my_roles = my_roles
-        self.my_roles_back_way = my_roles_back_way
         if kw:
             self.kw = kw
         else:
 class StaticRoute(object):
     """ Static route (when we are clients)
     """
-    def __init__(self, name, roles, urls, connex_mode=None):
+    def __init__(self, name, roles, urls, connex):
         self.gaplic_name = name
         if isinstance(roles, (list, tuple,)):
             self.roles = roles
                 'StaticRoute needs urls'
             )
 
-        self.connex_mode = connex_mode
+        connex.route = self
+        self.connex = connex
         self.idx_url = 0        # current url connection if client
         self.dl_output_events = deque()  # queue of pending output events
         self.cur_pending_event = None
-        self.connex_handler = None
         self.write = None
-        self.route_ref = None
+        self.route_ref = hash(connex)
         self.identity_ack = False
         self.serial = 0
         self.ws = None
                 self.gaplic_name,
                 self.roles,
                 self.route_ref,
-                self.connex_handler.config.destinations,
+                self.connex.config.destinations,
                 self.their_roles,
             )
 
             self.roles = []
         self.dl_output_events = deque()  # queue of pending output events
         self.cur_pending_event = None
-        self.connex_handler = None
+        self.connex = None
         self.write = write
         self.gsock = gsock
-        self.route_ref = None
+        self.route_ref = hash(gsock)
         self.identity_ack = True
         self.serial = 0
         self.ws = None
         destinations = get_host_port_from_urls(urls)
         if not destinations:
             return None
-        r = StaticRoute(name, roles, urls, connex_mode)
-        k = random_key()
-        connex_handler = self.router.create_gobj(
+        connex = self.router.create_gobj(
             'static_route',
             GConnex,
             self.router,
             transmit_ready_event_name=None,
             destinations=destinations,
         )
-        connex_handler.route_ref = k
-        r.route_ref = k
-        r.connex_handler = connex_handler
-        self.static_routes[k] = r
+        r = StaticRoute(name, roles, urls, connex)
+        self.static_routes[r.route_ref] = r
         return r
 
     def add_dynamic_route(self, name, role, write, gsock):
         r = DynamicRoute(name, role, write, gsock)
-        k = random_key()
-        r.route_ref = k
-        self.dynamic_routes[k] = r
+        self.dynamic_routes[r.route_ref] = r
         return r
 
     def get_route_by_ref(self, route_ref):
         return tuple(map(lambda x: '^' + x, found_roles))
 
     def check_broadcast_roles_back_way(self, roles):
-        broadcasted_roles = self.broadcasted_roles
+        #broadcasted_roles = self.broadcasted_roles  # TODO: must be per route
+        broadcasted_roles = []
         to_broadcast = []
 
         pure_roles = list(
         static_routes = registry.static_routes
         for route_ref in static_routes:
             route = static_routes[route_ref]
-            if route.connex_handler.is_disconnected():
+            if route.connex.is_disconnected():
                 continue
             self.router.send_identity_card(route, roles_back_way)
 
     if hasattr(event, 'connex'):
         # client connection
         connex = event.connex
-        route_ref = connex.route_ref
+        route_ref = hash(connex)
         route = self.registry.get_route_by_ref(route_ref)
         # all urls must have the same path!!
         resource = get_path_from_url(route.urls[0])
         # Register a dynamic router
         gaplic_name = intra_event.my_gaplic_name
         roles = intra_event.my_roles
-        this_route = registry.add_dynamic_route(
-            gaplic_name,
-            roles,
-            write,
-            gsock,
-        )
+        if isinstance(this_route, EmptyRoute):
+            this_route = registry.add_dynamic_route(
+                gaplic_name,
+                roles,
+                write,
+                gsock,
+            )
+            case = 'NEW'
+        else:
+            case = 'UPDATE'
         if self.config.trace_router:
-            self.logger.info("%s: NEW DYNAMIC route: %s: %s: %s: %s" % (
+            self.logger.info("%s: %s DYNAMIC route: %s: %s: %s: %s" % (
                 registry.my_gaplic_name,
+                case,
                 this_route.route_ref,
                 this_route.gaplic_name,
                 this_route.roles,
         if intra_event.my_gaplic_name:
             if this_route.gaplic_name != intra_event.my_gaplic_name:
                 self.logger.info(
-                    "WARNING: updating route %r, GAPLIC_NAME to %r" % (
+                    "WARNING: updating route %r, GAPLIC_NAME! to %r" % (
                     this_route.route_ref,
                     intra_event.my_gaplic_name))
                 this_route.gaplic_name = intra_event.my_gaplic_name
             serial=None,
             my_gaplic_name=my_gaplic_name,
             my_roles=list(my_roles) + list(my_roles_back_way),
-            #my_roles_back_way=my_roles_back_way,
             kw={},
         )
 
             registry.fire_pending_events(route)
         return True
 
-    def mt_send_event_to_external_by_route(
-            self,
-            gaplic_name,
-            role,
-            route_ref,
-            gobj_name,
-            event_name,
-            subscriber_gobj,
-            kw):
-        registry = self.registry
-        route = registry.get_route_by_ref(route_ref)
-        if not route:
-            self.logger.error("ERROR route_ref NOT FOUND: %r" % route_ref)
-            return False
-
-        intra_event = IntraEvent(
-            message_type='__event__',
-            serial=route.incr_serial(),
-            event_name=event_name,
-            destination_gaplic=gaplic_name if gaplic_name else '',
-            destination_role=route if route else '',
-            destination_gobj=gobj_name,
-            origin_gaplic=registry.my_gaplic_name,
-            origin_gobj=subscriber_gobj,
-            kw=kw)
-
-        registry.enqueue_pending_event(route, intra_event)
-        if not route.cur_pending_event:
-            registry.fire_pending_events(route)
-        return True
-
     def make_external_subscription(self, intra_event, route_ref):
         k = random_key()
         subscription = ExternalSubscription(intra_event, route_ref)