# Path: osbrain/agent.py
# def run_agent(
#     name='',
#     nsaddr=None,
#     addr=None,
#     base=Agent,
#     serializer=None,
#     transport=None,
#     safe=None,
#     attributes=None,
# ):
#     """
#     Ease the agent creation process.
# 
#     This function will create a new agent, start the process and then run
#     its main loop through a proxy.
# 
#     Parameters
#     ----------
#     name : str, default is ''
#         Agent name or alias.
#     nsaddr : SocketAddress, default is None
#         Name server address.
#     addr : SocketAddress, default is None
#         New agent address, if it is to be fixed.
#     transport : str, AgentAddressTransport, default is None
#         Transport protocol.
#     safe : bool, default is None
#         Use safe calls by default from the Proxy.
#     attributes : dict, default is None
#         A dictionary that defines initial attributes for the agent.
# 
#     Returns
#     -------
#     proxy
#         A proxy to the new agent.
#     """
#     if not nsaddr:
#         nsaddr = os.environ.get('OSBRAIN_NAMESERVER_ADDRESS')
#     agent = AgentProcess(
#         name=name,
#         nsaddr=nsaddr,
#         addr=addr,
#         base=base,
#         serializer=serializer,
#         transport=transport,
#         attributes=attributes,
#     )
#     agent_name = agent.start()
# 
#     proxy = Proxy(agent_name, nsaddr, safe=safe)
#     proxy.run()
#     proxy.wait_for_running()
# 
#     return proxy
# 
# Path: osbrain/nameserver.py
# def run_nameserver(addr=None, base=NameServer):
#     """
#     Ease the name server creation process.
# 
#     This function will create a new nameserver, start the process and then run
#     its main loop through a proxy.
# 
#     Parameters
#     ----------
#     addr : SocketAddress, default is None
#         Name server address.
# 
#     Returns
#     -------
#     proxy
#         A proxy to the name server.
#     """
#     if not addr:
#         addr = random_nameserver_process(base=base).addr
#     else:
#         NameServerProcess(addr, base=base).start()
#     return NSProxy(addr)


import time
from osbrain import run_agent
from osbrain import run_nameserver



def heavy_processing(agent, message):
    agent.log_info('%s seconds task...' % message)
    time.sleep(message)
    agent.send('results', '%s finished with %s' % (agent.name, message))


def append_received(agent, message):
    agent.log_info(message)
    agent.received.append(message)


if __name__ == '__main__':

    ns = run_nameserver()


# Path: osbrain/agent.py
# def run_agent(
#     name='',
#     nsaddr=None,
#     addr=None,
#     base=Agent,
#     serializer=None,
#     transport=None,
#     safe=None,
#     attributes=None,
# ):
#     """
#     Ease the agent creation process.
# 
#     This function will create a new agent, start the process and then run
#     its main loop through a proxy.
# 
#     Parameters
#     ----------
#     name : str, default is ''
#         Agent name or alias.
#     nsaddr : SocketAddress, default is None
#         Name server address.
#     addr : SocketAddress, default is None
#         New agent address, if it is to be fixed.
#     transport : str, AgentAddressTransport, default is None
#         Transport protocol.
#     safe : bool, default is None
#         Use safe calls by default from the Proxy.
#     attributes : dict, default is None
#         A dictionary that defines initial attributes for the agent.
# 
#     Returns
#     -------
#     proxy
#         A proxy to the new agent.
#     """
#     if not nsaddr:
#         nsaddr = os.environ.get('OSBRAIN_NAMESERVER_ADDRESS')
#     agent = AgentProcess(
#         name=name,
#         nsaddr=nsaddr,
#         addr=addr,
#         base=base,
#         serializer=serializer,
#         transport=transport,
#         attributes=attributes,
#     )
#     agent_name = agent.start()
# 
#     proxy = Proxy(agent_name, nsaddr, safe=safe)
#     proxy.run()
#     proxy.wait_for_running()
# 
#     return proxy
# 
# Path: osbrain/nameserver.py
# def run_nameserver(addr=None, base=NameServer):
#     """
#     Ease the name server creation process.
# 
#     This function will create a new nameserver, start the process and then run
#     its main loop through a proxy.
# 
#     Parameters
#     ----------
#     addr : SocketAddress, default is None
#         Name server address.
# 
#     Returns
#     -------
#     proxy
#         A proxy to the name server.
#     """
#     if not addr:
#         addr = random_nameserver_process(base=base).addr
#     else:
#         NameServerProcess(addr, base=base).start()
#     return NSProxy(addr)


# Path: examples/ventilator.py
import time
from osbrain import run_agent
from osbrain import run_nameserver




def heavy_processing(agent, message):
    agent.log_info('%s seconds task...' % message)
    time.sleep(message)
    agent.send('results', '%s finished with %s' % (agent.name, message))


def append_received(agent, message):
    agent.log_info(message)
    agent.received.append(message)


if __name__ == '__main__':

    ns = run_nameserver()


    results = run_agent('Results')