# Path: pyactor/context.py
# def set_context(module_name='thread'):
#     """
#     This function initializes the execution context deciding which
#     type of threads are being used: classic python threads or green
#     threads, provided by Gevent.
# 
#     This should be called first of all in every execution, otherwise,
#     the library would not work.
# 
#     The default module is 'thread'.
# 
#     :param str. module_name: Name of the module you want to use
#         ('thread' or 'green_thread').
#     """
#     global core_type
#     if core_type is None and module_name in available_types:
#         core_type = module_name
#         util.core_type = core_type
#         global actor_module
#         actor_module = import_module('pyactor.' + module_name + '.actor')
#         global intervals
#         intervals = import_module('pyactor.' + module_name + '.intervals')
#         global parallels
#         parallels = import_module('pyactor.' + module_name + '.parallels')
#         global future
#         future = import_module('pyactor.' + module_name + '.future')
#         set_actor(module_name)
#         global rpcactor
#         rpcactor = import_module('pyactor.' + module_name + '.rpcactor')
#         global signal
#         if module_name == 'green_thread':
#             signal = import_module('gevent')
#         else:
#             signal = import_module('signal')
#     else:
#         if core_type is not None:
#             raise Exception("The core type was previously configured.")
#         raise Exception("Bad core type.")
# 
# def create_host(url="local://local:6666/host"):
#     """
#     This is the main function to create a new Host to which you can
#     spawn actors. It will be set by default at local address if no
#     parameter *url* is given. This function should be called once
#     for execution or after calling :meth:`~.shutdown` to the previous
#     host.
# 
#     However, it is possible to create locally more than one host
#     and simulate a remote communication between them if they are of some
#     remote type (`http` or `amqp`), but the first one created will
#     be the main host, which is the one hosting the queries from
#     the main function.
#     Of course, every host must be initialized with a different URL(port).
#     Although that, more than one host should not be required for any real
#     project.
# 
#     :param str. url: URL where to start and bind the host.
#     :return: :class:`~.Proxy` to the new host created.
#     :raises: Exception if there is a host already created with that URL.
#     """
#     if url in util.hosts.keys():
#         raise HostError("Host already created. Only one host can"
#                         " be ran with the same url.")
#     else:
#         if not util.hosts:
#             util.main_host = Host(url)
#             util.hosts[url] = util.main_host
#         else:
#             util.hosts[url] = Host(url)
#         return util.hosts[url].proxy
# 
# def serve_forever():
#     """
#     This allows the host (main host) to keep alive indefinitely so its actors
#     can receive queries at any time.
#     The main thread stays blocked forever.
#     To kill the execution, press Ctrl+C.
# 
#     See usage example in :ref:`sample6`.
#     """
#     if not util.main_host.alive:
#         raise Exception("This host is already shut down.")
#     util.main_host.serving = True
#     signal.signal(SIGINT, signal_handler)
#     print("Press Ctrl+C to kill the execution")
#     while util.main_host is not None and util.main_host.serving:
#         try:
#             sleep(1)
#         except Exception:
#             pass
#     print("BYE!")
# 
# def interval(host, time, actor, method, *args, **kwargs):
#     """
#     Creates an Event attached to the host for management that will
#     execute the *method* of the *actor* every *time* seconds.
# 
#     See example in :ref:`sample_inter`
# 
#     :rtype:
#     :param Proxy host: host that will manage the interval, commonly the
#         host of the actor.
#     :param float time: seconds for the intervals.
#     :param Proxy actor: actor to which make the call every *time* seconds.
#     :param Str. method: method of the *actor* to be called.
#     :param list args: arguments for *method*.
#     :return: :class:`Event` instance of the interval.
#     """
#     call = getattr(actor, method, None)
#     if not callable(call):
#         raise IntervalError(
#             f"The actor {actor.get_id()} does not have the method {method}.")
#     if call.__class__.__name__ in ["TellWrapper", "TellRefWrapper"]:
#         # If the method is a normal tell, the interval thread can send
#         # the calls normally.
#         # It it is a Ref Tell, the proxies in the args would be parsed
#         # during the call to this very method. So the call can be made
#         # as a normal Tell. The actor will do the loads normally on the
#         # receive as it has its methods marked as ref.
#         if call.__class__.__name__ is "TellRefWrapper":
#             call.__call__ = TellWrapper.__call__
# 
#         return intervals.interval_host(host, time, call, *args, **kwargs)
#     else:
#         raise IntervalError("The callable for the interval must be a tell" +
#                             " method of the actor.")


from pyactor.context import set_context, create_host, serve_forever, interval
@author: Daniel Barcelona Pons
"""

CLIENTS = 100


class Connecter(object):
    _tell = {'send_message', 'init_start', 'set_server'}
    _ref = {'set_server'}

    def set_server(self, srvr):
        self.server = srvr

    def init_start(self):
        self.interval1 = interval(self.host, 0.1, self.proxy, 'send_message')

    def send_message(self):
        self.server.work('abcdefghijklmnopqrstuvwxyz' +
                         'abcdefghijklmnopqrstuvwxyz')
        # self.server.work(1)


class Show(object):
    _tell = {'send_message', 'init_start', 'set_server'}
    _ref = {'set_server'}

    def set_server(self, srvr):
        self.server = srvr

    def init_start(self):

# Path: pyactor/context.py
# def set_context(module_name='thread'):
#     """
#     This function initializes the execution context deciding which
#     type of threads are being used: classic python threads or green
#     threads, provided by Gevent.
# 
#     This should be called first of all in every execution, otherwise,
#     the library would not work.
# 
#     The default module is 'thread'.
# 
#     :param str. module_name: Name of the module you want to use
#         ('thread' or 'green_thread').
#     """
#     global core_type
#     if core_type is None and module_name in available_types:
#         core_type = module_name
#         util.core_type = core_type
#         global actor_module
#         actor_module = import_module('pyactor.' + module_name + '.actor')
#         global intervals
#         intervals = import_module('pyactor.' + module_name + '.intervals')
#         global parallels
#         parallels = import_module('pyactor.' + module_name + '.parallels')
#         global future
#         future = import_module('pyactor.' + module_name + '.future')
#         set_actor(module_name)
#         global rpcactor
#         rpcactor = import_module('pyactor.' + module_name + '.rpcactor')
#         global signal
#         if module_name == 'green_thread':
#             signal = import_module('gevent')
#         else:
#             signal = import_module('signal')
#     else:
#         if core_type is not None:
#             raise Exception("The core type was previously configured.")
#         raise Exception("Bad core type.")
# 
# def create_host(url="local://local:6666/host"):
#     """
#     This is the main function to create a new Host to which you can
#     spawn actors. It will be set by default at local address if no
#     parameter *url* is given. This function should be called once
#     for execution or after calling :meth:`~.shutdown` to the previous
#     host.
# 
#     However, it is possible to create locally more than one host
#     and simulate a remote communication between them if they are of some
#     remote type (`http` or `amqp`), but the first one created will
#     be the main host, which is the one hosting the queries from
#     the main function.
#     Of course, every host must be initialized with a different URL(port).
#     Although that, more than one host should not be required for any real
#     project.
# 
#     :param str. url: URL where to start and bind the host.
#     :return: :class:`~.Proxy` to the new host created.
#     :raises: Exception if there is a host already created with that URL.
#     """
#     if url in util.hosts.keys():
#         raise HostError("Host already created. Only one host can"
#                         " be ran with the same url.")
#     else:
#         if not util.hosts:
#             util.main_host = Host(url)
#             util.hosts[url] = util.main_host
#         else:
#             util.hosts[url] = Host(url)
#         return util.hosts[url].proxy
# 
# def serve_forever():
#     """
#     This allows the host (main host) to keep alive indefinitely so its actors
#     can receive queries at any time.
#     The main thread stays blocked forever.
#     To kill the execution, press Ctrl+C.
# 
#     See usage example in :ref:`sample6`.
#     """
#     if not util.main_host.alive:
#         raise Exception("This host is already shut down.")
#     util.main_host.serving = True
#     signal.signal(SIGINT, signal_handler)
#     print("Press Ctrl+C to kill the execution")
#     while util.main_host is not None and util.main_host.serving:
#         try:
#             sleep(1)
#         except Exception:
#             pass
#     print("BYE!")
# 
# def interval(host, time, actor, method, *args, **kwargs):
#     """
#     Creates an Event attached to the host for management that will
#     execute the *method* of the *actor* every *time* seconds.
# 
#     See example in :ref:`sample_inter`
# 
#     :rtype:
#     :param Proxy host: host that will manage the interval, commonly the
#         host of the actor.
#     :param float time: seconds for the intervals.
#     :param Proxy actor: actor to which make the call every *time* seconds.
#     :param Str. method: method of the *actor* to be called.
#     :param list args: arguments for *method*.
#     :return: :class:`Event` instance of the interval.
#     """
#     call = getattr(actor, method, None)
#     if not callable(call):
#         raise IntervalError(
#             f"The actor {actor.get_id()} does not have the method {method}.")
#     if call.__class__.__name__ in ["TellWrapper", "TellRefWrapper"]:
#         # If the method is a normal tell, the interval thread can send
#         # the calls normally.
#         # It it is a Ref Tell, the proxies in the args would be parsed
#         # during the call to this very method. So the call can be made
#         # as a normal Tell. The actor will do the loads normally on the
#         # receive as it has its methods marked as ref.
#         if call.__class__.__name__ is "TellRefWrapper":
#             call.__call__ = TellWrapper.__call__
# 
#         return intervals.interval_host(host, time, call, *args, **kwargs)
#     else:
#         raise IntervalError("The callable for the interval must be a tell" +
#                             " method of the actor.")


# Path: tests/other/stress.py
from pyactor.context import set_context, create_host, serve_forever, interval

@author: Daniel Barcelona Pons
"""

CLIENTS = 100


class Connecter(object):
    _tell = {'send_message', 'init_start', 'set_server'}
    _ref = {'set_server'}

    def set_server(self, srvr):
        self.server = srvr

    def init_start(self):
        self.interval1 = interval(self.host, 0.1, self.proxy, 'send_message')

    def send_message(self):
        self.server.work('abcdefghijklmnopqrstuvwxyz' +
                         'abcdefghijklmnopqrstuvwxyz')
        # self.server.work(1)


class Show(object):
    _tell = {'send_message', 'init_start', 'set_server'}
    _ref = {'set_server'}

    def set_server(self, srvr):
        self.server = srvr

    def init_start(self):

        self.interval1 = interval(self.host, 1, self.proxy, 'send_message')