Commits

Ginés Martínez Sánchez  committed 895d30c Draft

developing http server

  • Participants
  • Parent commits df507eb

Comments (0)

Files changed (5)

File ginsfsm/examples/http/http_server_echo.py

             subscriber=self,
             inactivity_timeout=2,
             responseless_timeout=2,
+            maximum_simultaneous_requests=1,
         )
 
         if self.verbose > 0:

File ginsfsm/gobj.py

 
         self._dl_subscriptions = set()      # uauuu, how many fans!!
         self._some_subscriptions = False
-
-        self._destroyed = False  # mark as destroyed when destroy_gobj()
+        self._gobj_destroyed = False  # mark as destroyed when destroy_gobj()
 
         gconfig = add_gconfig(gconfig, GOBJ_GCONFIG)
         GConfig.__init__(self, gconfig)
                 pass  # "Set changed size during iteration" is OK
 
         gobj.delete_all_subscriptions()
-        gobj._destroyed = True
+        gobj._gobj_destroyed = True
+        SMachine._machine_destroyed = True
         del gobj
 
     def start_up(self):
             You must consult the attributes supported by each machine's event.
         """
         destination = self._resolv_destination(destination)
-        if destination._destroyed:
-            logging.error("ERROR internal: "
+        if destination._gobj_destroyed:
+            logging.error("GObj ERROR internal: "
                 "sending an event to a destroyed gobj",
             )
             return -1

File ginsfsm/protocols/http/server/c_http_clisrv.py

     data = event.data
     if not data:
         return
-    some_request = False
     new_request = self.current_request
     while data:
         if new_request is None:
             self.current_request = None
             if not new_request.empty:
                 self.enqueue_request(new_request)
-                some_request = True
             new_request = None
         else:
             self.current_request = new_request
             break
         data = data[n:]
 
-    if some_request:
+    if len(self.dl_requests):
         self.send_event(self, 'EV_DEQUEUE_REQUEST')
 
 
     )
     request.error = InternalServerError(body)
     task = HttpErrorResponse(self.identity, self.gsock, request)
-    task.service()  # must not fail
+    task.service()
 
 
 GHTTPCLISRV_FSM = {
     'gsock': [None, None, 0, None,
         "partner gsock."
         ],
+    'maximum_simultaneous_requests': [int, 10, 0, None,
+        "maximum simultaneous requests."
+        ],
     'identity': [str, 'ginsfsm', 0, None,
                  "server identity (sent in Server: header)"],
     'url_scheme': [str, 'http', 0, None, "default ``http`` value"],
     def enqueue_request(self, new_request):
         # TODO: check limit queue size
         self.dl_requests.append(new_request)
+        if len(self.dl_requests) >= self.maximum_simultaneous_requests:
+            # Close the channel by maximum simultaneous requests reached.
+            body = 'Please change your behavior.' \
+                ' You have reached the maximum simultaneous requests (%d).' % (
+                    self.maximum_simultaneous_requests)
+            request = HTTPRequestParser(
+                url_scheme=self.url_scheme
+            )
+            request.error = InternalServerError(body)
+            task = HttpErrorResponse(self.identity, self.gsock, request)
+            task.service()
+            self.dl_requests.clear()
 
     def start_inactivity_timer(self):
         self.send_event(

File ginsfsm/protocols/http/server/c_http_server.py

         gsock=gsock,
         subscriber=self.subscriber,
         identity=self.identity,
+        maximum_simultaneous_requests=self.maximum_simultaneous_requests,
         url_scheme=self.url_scheme,
         inactivity_timeout=self.inactivity_timeout,
         responseless_timeout=self.responseless_timeout,
     'origins': [None, None, 0, None,
         "TODO:list of (host, port) tuples allowed to connect from"],
 
+    'maximum_simultaneous_requests': [int, 10, 0, None,
+        "maximum simultaneous requests."
+        ],
     'identity': [str, 'ginsfsm', 0, None,
-                 "server identity (sent in Server: header)"],
+        "server identity (sent in Server: header)"
+        ],
     'url_scheme': [str, 'http', 0, None, "default ``http`` value"],
     'inactivity_timeout': [int, 5 * 60 * 60, 0, None,
         "Inactivity timeout in seconds."

File ginsfsm/smachine.py

 
 """
 
+import logging
 import datetime
 from ginsfsm.compat import (
     string_types,
         self._current_state = 1
         self.__trace_mach = False
         self.logger = None
+        self._machine_destroyed = False  # mark as destroyed when destroy_gobj()
 
         # check state names
         state_names = list(self._state_list)
         by :meth:`inject_event` method it will be the returned value by
         the executed :term:`action`.
         """
+
+        if self._machine_destroyed:
+            logging.error("SMachine ERROR internal: "
+                "sending an event to a destroyed gobj",
+            )
+            return -1
+
         result = None
         logger = self.logger
         trace_mach = self.trace_mach