Commits

Ginés Martínez Sánchez committed f72197e Draft

writting inter event example

Comments (0)

Files changed (6)

ginsfsm/examples/inter_event/ap1_banco/banco/pyramid_app/static/cliente.js

         pon_texto(self, "", self.$acciones_cantidad);
         pon_texto(self, "", self.$prestamo_total);
         pon_texto(self, "", self.$acciones_total);
-        pon_texto(self, "", self.$saldo_total);
+        pon_texto(self, "", self.$regalo_resultado);
 
         pon_texto(self, "Escriba su nombre", self.$registro_nombre);
     }
         self.$acciones_button = $('#acciones_button');
         self.$acciones_total = $('#acciones_total');
 
-        self.$saldo_total = $('#saldo_total');
+        self.$regalo_button = $('#regalo_button');
+        self.$regalo_resultado = $('#regalo_resultado');
 
         /*
          *  Configure events
             self.send_event(self, 'EV_COMPRAR_ACCIONES', kw);
         });
 
+        self.$regalo_button.on("click", self, function(event){
+            event.stopPropagation();
+            var self = event.data;
+            var kw = {
+            };
+            self.send_event(self, 'EV_SUBSCRIBE_REGALO', kw);
+        });
+
         return 1;
     }
 
         return 1;
     }
 
+    function ac_subscribe_regalo(self, event) {
+        pon_texto(self, "Espere por favor...", self.$regalo_resultado);
+
+        //self.gaplic.send_event_outside(
+        //    '',                 // gaplic
+        //    'publicidad',       // role
+        //    'sorteo',           // gobj_name
+        //    'EV_REGALO',        // event_name
+        //    self,               // subscriber_gobj
+        //    null,               // origin_role
+        //    {                   // kw
+        //        user_name: self.user_name
+        //    }
+        //);
+
+        self.gaplic.subscribe_event_outside(
+            '',             // gaplic_name
+            'publicidad',   // role
+            'sorteo',       // gobj_name
+            'EV_REGALO',    // event_name
+            self,           // subscriber_gobj
+            null,           // origin_role
+            {               // kw
+                user_name: self.user_name
+            }
+        );
+        return 1;
+    }
+
+    function ac_regalo(self, event) {
+        var regalo = event.kw.regalo;
+
+        pon_texto(self, regalo, self.$regalo_resultado);
+
+        return 1;
+    }
+
     /********************************************
      *      Automata
      ********************************************/
             'EV_ACCIONES_ACK',
             'EV_PEDIR_PRESTAMO',
             'EV_COMPRAR_ACCIONES',
+            'EV_SUBSCRIBE_REGALO',
+            'EV_REGALO',
             'EV_REGISTER_USER'
         ],
         'state_list': [
                 ['EV_REGISTER_USER',    ac_unregister_user,  'ST_INIT'],
                 ['EV_PEDIR_PRESTAMO',   ac_pedir_prestamo,   undefined],
                 ['EV_COMPRAR_ACCIONES', ac_comprar_acciones, undefined],
+                ['EV_SUBSCRIBE_REGALO', ac_subscribe_regalo, undefined],
+                ['EV_REGALO',           ac_regalo,           undefined],
                 ['EV_PRESTAMO_ACK',     ac_prestamo_ack,     undefined],
                 ['EV_ACCIONES_ACK',     ac_acciones_ack,     undefined]
             ]

ginsfsm/examples/inter_event/ap1_banco/banco/pyramid_app/templates/mytemplate.pt

         <tr>
           <td></td>
           <td></td>
-          <td></td>
-          <td>Saldo</td>
-          <td><input id="saldo_total" disabled="disabled" readonly="readonly" type="text" /></td>
+          <td><input id="regalo_button" value="Subscribirse a regalos!" type="button" /></td>
+          <td>Regalo</td>
+          <td><input id="regalo_resultado" disabled="disabled" readonly="readonly" type="text" /></td>
         </tr>
         </tbody></table>
 

ginsfsm/examples/inter_event/ap4_publicidad/publicidad.ini

 GObj.trace_mach = false
 GObj.trace_creation = false
 GObj.trace_traverse = false
-GObj.trace_subscription = false
+GObj.trace_subscription = true
 GSock.trace_dump = false
 
 

ginsfsm/examples/inter_event/ap4_publicidad/publicidad/c_sorteo.py

 """
 
 from ginsfsm.gobj import GObj
+from ginsfsm.c_timer import GTimer
 
 
-def ac_sample(self, event):
+def ac_regalo(self, event):
     """ Event attributes:
-        * :attr:`data`: example.
+        * :attr:`sorteo`: .
 
     """
-    if self.output_event_name is not None:
-        self.broadcast_event(self.output_event_name, data=event.data)
+    user_name = event.user_name
+
+    if event.__intra_event__:
+        origin_gaplic = event.__intra_event__.origin_gaplic
+        origin_role = event.__intra_event__.origin_role
+        origin_gobj = event.__intra_event__.origin_gobj
+        self.user_sorteo[user_name] = {
+            'gaplic': origin_gaplic,
+            'role': origin_role,
+            'gobj': origin_gobj
+        }
+
+    self.set_timeout(3)
+
+        #self.gaplic.send_event_outside(
+        #    origin_gaplic,  # gaplic_name
+        #    origin_role,  # role
+        #    origin_gobj,  # gobj_name
+        #    'EV_REGALO',  # event_name
+        #    self,  # subscriber_gobj
+        #    regalo=22,
+        #)
+
+
+def ac_sorteo(self, event):
+    self.broadcast_event('EV_REGALO')
+    self.set_timeout(3)
 
 
 GSORTEO_FSM = {
     'event_list': (
-        'EV_INPUT_EVENT:top input',
-        'EV_OUTPUT_EVENT:top output',
+        'EV_REGALO:top input output',
+        'EV_TIMEOUT:bottom input',
     ),
     'state_list': (
         'ST_IDLE',
     'machine': {
         'ST_IDLE':
         (
-            ('EV_INPUT_EVENT',      ac_sample,      None),
+            ('EV_REGALO',      ac_regalo,      None),
+            ('EV_TIMEOUT',     ac_sorteo,      None),
         ),
     }
 }
         "subcriber of all output-events."
         "Default is ``None``, i.e., the parent"
     ],
-
-    # If some name is None then the parent don't want receive it.
-    'output_event_name': [
-        str, 'EV_OUTPUT_EVENT', 0, None,
-        "Name of the *sample output* event."
-        " ``None`` if you want ignore the event"
-    ],
 }
 
 
        :gconfig: GSORTEO_GCONFIG
 
     *Input-Events:*
-        * :attr:`'EV_INPUT_EVENT'`: sample input event.
+        * :attr:`'EV_REGALO'`: sample input event.
 
           Event attributes:
 
               * ``data``: sample event attribute.
 
     *Output-Events:*
-        * :attr:`'EV_OUTPUT_EVENT'`: sample output event.
+        * :attr:`'EV_REGALO_ACK'`: sample output event.
 
           Event attributes:
 
     """
     def __init__(self):
         GObj.__init__(self, GSORTEO_FSM, GSORTEO_GCONFIG)
+        self.user_sorteo = {}
 
     def start_up(self):
         """ Initialization zone.
         """
-        if self.config.subscriber is None:
-            self.config.subscriber = self.parent
-        self.subscribe_event(None, self.config.subscriber)
+        self.timer = self.create_gobj(
+            None,
+            GTimer,
+            self,
+            timeout_event_name='EV_TIMEOUT',
+        )
+        self.set_timeout(3)
+
+    def set_timeout(self, seconds):
+        self.send_event(self.timer, 'EV_SET_TIMER', seconds=seconds)
+
+    def clear_timeout(self):
+        self.send_event(self.timer, 'EV_SET_TIMER', seconds=-1)

ginsfsm/gaplic.py

 * :meth:`GAplic.find_unique_gobj`
 * :meth:`GAplic.send_event_outside`
 * :meth:`GAplic.send_event_to_external_role`
-* :meth:`GAplic.subscribe_event_from_external_gaplic`
+* :meth:`GAplic.subscribe_event_outside`
 * :meth:`GAplic.subscribe_event_from_external_role`
 * :meth:`GAplic.unsubscribe_event_from_external_gaplic`
 * :meth:`GAplic.unsubscribe_event_from_external_role`
         find_unique_gobj,
         send_event_outside,
         send_event_to_external_role,
-        subscribe_event_from_external_gaplic,
+        subscribe_event_outside,
         subscribe_event_from_external_role,
         unsubscribe_event_from_external_gaplic,
         unsubscribe_event_from_external_role,
         #    kw
         #)
 
-    def subscribe_event_from_external_gaplic(
-            self, gaplic_name, role, gobj_name, event_name, subscriber_gobj, **kw):
+    def subscribe_event_outside(
+            self,
+            gaplic_name,
+            role,
+            gobj_name,
+            event_name,
+            subscriber_gobj,
+            origin_role=None,
+            **kw):
         """ Subscribe an event of an external gaplic by name.
         """
         kw.update({'__subscribe_event__': True})
             gobj_name,
             event_name,
             subscriber_gobj,
+            origin_role=origin_role,
             **kw
         )
 
-    def unsubscribe_event_from_external_gaplic(
+    def unsubscribe_event_outside(
             self, gaplic_name, role, gobj_name, event_name, subscriber_gobj, **kw):
         """ Subscribe an event of an external gaplic by name.
         """
            the :term:`event-filter` cannot be applied.
         """
         if self.config.trace_subscription and self.logger:
-            self.logger.info('BROADCAST %r' % event)
+            self.logger.info('BROADCAST event: %r, gobj: %r' % (event, self))
 
         if not self._some_subscriptions:
             return
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.