Commits

Ginés Martínez Sánchez committed 65f19f2 Draft

added post_event_from_outside, to send events form non-gobj world to gobj world.

Comments (0)

Files changed (3)

ginsfsm/__init__.py

 """
 A python library to do development based in Finite State Machines.
 """
-__version__ = '0.6.7'
+__version__ = '0.6.8'
 __title__ = 'ginsfsm'
 __author__ = 'Ginés Martínez'
 __license__ = 'MIT License'

ginsfsm/globals.py

 
 def global_get_gobj(gobj_name, gaplic_name=None):
     """ Search a :term:`named-gobj` in global_main, threads and subprocesses.
-    TODO: with subprocesses send_event() will work??
+    TODO: with subprocesses will work??
     """
     global_main_gaplic = get_global_main_gaplic()
     if global_main_gaplic:
     return None
 
 
+def global_get_gaplic(gaplic_name):
+    """ Search a :term:`named-gobj` in global_main, threads and subprocesses.
+    TODO: with subprocesses will work??
+    """
+    global_main_gaplic = get_global_main_gaplic()
+    if global_main_gaplic:
+        if global_main_gaplic.name == gaplic_name:
+            return global_main_gaplic
+
+    global_threads = get_global_threads()
+    for wkr in global_threads:
+        if wkr.gaplic.name == gaplic_name:
+            return wkr.gaplic
+
+    # TODO: will work???
+    global_subprocesses = get_global_subprocesses()
+    for wkr in global_subprocesses:
+        if wkr.gaplic.name == gaplic_name:
+            return wkr.gaplic
+
+    return None
+
+
 def get_gaplic_by_thread_ident(thread_ident=None):
     """ Return the gaplic by the thread ident.
         If :param:`thread_ident` is None,
 * :meth:`GObj.destroy_gobj`
 * :meth:`GObj.send_event`
 * :meth:`GObj.post_event`
+* :meth:`GObj.post_event_from_outside`
 * :meth:`GObj.broadcast_event`
 * :meth:`GObj.subscribe_event`
 * :meth:`GObj.delete_subscription`
 
     * sending events by direct delivery: :meth:`GObj.send_event`.
     * sending events by queues: :meth:`GObj.post_event`.
+    * sending events from outside world: :meth:`GObj.post_event_from_outside`.
     * sending events to subscribers: :meth:`GObj.broadcast_event`.
 
+
 * receiving events:
 
     * directly from another :term:`gobj`'s who knows you.
 
 .. autoclass:: GObj
     :members: start_up, create_gobj, destroy_gobj,
-        send_event, post_event, broadcast_event,
+        send_event, post_event, broadcast_event, post_event_from_outside,
         subscribe_event, delete_subscription, set_owned_event_filter,
         overwrite_parameters, overwrite_few_parameters
 
     add_gconfig,
 )
 from ginsfsm.deferred import Deferred
+from ginsfsm.globals import global_get_gobj
 
 
 class GObjError(Exception):
         ret = destination.inject_event(event)
         return ret
 
+    @staticmethod
+    def post_event_from_outside(gaplic_name, gobj_name, event, **kw):
+        """ Same funcionality as :func:`post_event`,
+        but the event is sent from non-gobj world.
+        """
+        gaplic = ginsfsm.globals.global_get_gaplic(gaplic_name)
+        if not gaplic:
+            raise Exception("CHECK NAMES!! gaplic %s  doesn't exist" % (
+                gaplic_name,))
+        gobj = global_get_gobj(gobj_name, gaplic_name)
+        if not gobj:
+            raise Exception("CHECK NAMES!! gobj %s  doesn't exist" % (
+                gobj_name,))
+        gaplic.post_event(gobj, event, **kw)
+
     def post_event(self, destination, event, **kw):
         """ Same funcionality as :func:`send_event`,
         but the event is processed by gaplic.
         If the destination is inside of the current gaplic the event will be
         sent in the next loop cycle.
-        If the destination is in another gaplic, the event will be processed
-        by the gaplic's router.
         """
         event = self._event_factory(destination, event, **kw)
         if not self.gaplic: