Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distributed agents #363

Closed
brownbytes opened this issue May 24, 2021 · 7 comments
Closed

Distributed agents #363

brownbytes opened this issue May 24, 2021 · 7 comments
Labels
Milestone

Comments

@brownbytes
Copy link

brownbytes commented May 24, 2021

Hi,

I am trying out dploying agents on multiple physical devices. Below are two approaches that I took:

Approach 1--- Agentns.py

import time
import osbrain
from osbrain.nameserver import NameServer as ns
from osbrain import run_nameserver
from osbrain.proxy import locate_ns
from osbrain import run_agent
from agent1 import Agent1
from agent2 import Agent2
from agent3 import Agent3
def main():
    ns_sock = '127.0.0.1:2000'
    osbrain.config['TRANSPORT'] = 'tcp'
    ns_proxy = run_nameserver(ns_sock)  # we will use this to access other agents
    ns_addr = locate_ns(ns_sock)
    #running agents using their addresses and creating agent proxies
    print("running agents...")
    agent1 = run_agent('agent1',addr='127.0.0.1:3010',base=Agent1)
    agent2 = run_agent('agent2', addr='127.0.0.1:3020', base=Agent2)
    #setting up bi-directional channels
    agent1.connect(agent2.addr('ag21'),handler='handle_ag2')
    agent2.connect(agent1.addr('ag12'),handler='handle_ag1')
    agents_in_NS = ns.agents(ns_proxy)
    print(agents_in_NS)
    print("All agents have joined!")
    agent1.send_hello('ag12')
if __name__ == '__main__':
    main()

Agent1.py

from osbrain import Agent
from osbrain import NSProxy
from osbrain import run_agent
import osbrain
import Pyro4
import time
class Agent1(Agent):
    def on_init(self):
        self.bind('PUSH',alias='ag12') #agent12 channel
    def send_hello(self,ch):
        print("Sending hello")
        self.send(ch,'agent1 says hello!') #ch is 'agent12' or 'agent13'
    def handle_ag2(self,message):
        self.log_info('Received:%s from agent2'%message)

Agent2.py

from osbrain import Agent
from osbrain import NSProxy
from osbrain import run_agent
import osbrain
import Pyro4
import time
class Agent2(Agent):
    def on_init(self):
        self.bind('PUSH', alias='ag21')  # agent12 channel
    def send_hello(self, ch):
        print("Sending hello")
        self.send(ch, 'agent2 says hello!')  # ch is 'agent12' or 'agent13'
    def handle_ag1(self, message):
        self.log_info('Received:%s from agent1' % message)

Result:

Broadcast server running on 0.0.0.0:9091
NS running on 127.0.0.1:2000 (127.0.0.1)
URI = PYRO:[email protected]:2000
running agents...
['agent1', 'agent2']
All agents have joined!
Sending hello
INFO [2021-05-24 00:34:35.355122] (agent2): Recieved:agent1 says hello! from agent1
Sending hello
INFO [2021-05-24 00:34:35.356071] (agent1): Recieved:agent2 says hello! from agent2

Approach 2 : Taking cue from #341

Agentns.py

import osbrain
from osbrain.nameserver import NameServer as ns
from osbrain import run_nameserver
from osbrain.proxy import locate_ns

def main():
    ns_sock = '127.0.0.1:2000'
    osbrain.config['TRANSPORT'] = 'tcp'
    ns_proxy = run_nameserver(ns_sock)  # we will use this to access other agents
    ns_addr = locate_ns(ns_sock)
    agents_in_NS = ns.agents(ns_proxy)

    while(len(agents_in_NS) < 3):
        agents_in_NS = osbrain.nameserver.NameServer.agents(ns_proxy)
        for agent in agents_in_NS:
            agent_proxy = ns_proxy.proxy(agent)
            print(agent_proxy)
        print(agents_in_NS)

    print("All agents have joined!")
if __name__ == '__main__':
    main()

Agent1.py

from osbrain import Agent
from osbrain import NSProxy
from osbrain import run_agent
import osbrain
import Pyro4
import time

class Agent1(Agent):
    def on_init(self):
        self.bind('PUSH',alias='agent12') #agent12 channel
    def set_ns_proxy(self,ns_proxy):
        self.ns_proxy = ns_proxy
    def connect_to(self): #to listen to agent2's and agent3's pushed messages
        #find remote agents proxies
        print("connecting to agent2")
        a2_proxy = self.ns_proxy.proxy('agent2')
        a2_addr = a2_proxy.addr('agent21')#addr takes alias of a socket
        self.connect(a2_addr,handler=self.custom_log) # listen to agent2

    def send_hello(self,ch):
        print("Sending hello")
        self.send(ch,'agent1 says hello!') #ch is 'agent12' or 'agent13'

    def custom_log(self,message):
        self.log_info('Recieved:%s'%message)

def conn_NS():  # https://github.com/opensistemas-hub/osbrain/issues/341
    ns_addr = '127.0.0.1:2000'
    self_addr = '127.0.0.1:3010' #this agent's address
    while True:  # look for nameserver
        try:
            ns_proxy = NSProxy(ns_addr)
            break
        except Pyro4.errors.CommunicationError:
            print('Pyr04.errors.CommunicationError,no nameserver found')
            print('Trying again...')
            pass

    while True:  # join nameserver
        try:
            # register with server
            print("registering agent with server...")
            a1_proxy = run_agent('agent1', addr=self_addr, base= Agent1, nsaddr=ns_addr)
            a1_proxy.set_ns_proxy(ns_proxy)
            # wait for proxy to set up
            checkstatus = a1_proxy.get_attr('_running')

            while checkstatus == False:
                checkstatus = a1_proxy.get_attr('_running')

            # verify that agent has registered successfully
            agents_in_NS = osbrain.nameserver.NameServer.agents(ns_proxy)
            print('The following agents are in the server: %s' % agents_in_NS)
            break

        except KeyError:
            print('Key Error')
            print('Trying again...')
            pass

        except Pyro4.errors.CommunicationError:
            print('Pyro4.errors.CommunicationError, no nameserver found')
            print('Trying again...')
            pass

        except TimeoutError:
            print('TimeoutError, no nameserver found')
            print('Trying again...')
            pass

    print('ofpagent has joined the nameserver')
    return a1_proxy

if __name__ == '__main__':
    ns_addr = '127.0.0.1:2000'
    ns_proxy = NSProxy(ns_addr)
    a1_proxy = conn_NS()
    time.sleep(5)
    agents_in_NS = osbrain.nameserver.NameServer.agents(ns_proxy)
    print(agents_in_NS)
    a1_proxy.connect_to()
    time.sleep(1)
    for _ in range(3):
        time.sleep(2)
        a1_proxy.send_hello('agent12')  # send hello on 'agent12' channel

Agent2.py

from osbrain import Agent
from osbrain import NSProxy
from osbrain import run_agent
import osbrain
import Pyro4
import time

class Agent2(Agent):
    def on_init(self):
        self.bind('PUSH',alias='agent21')

    def set_ns_proxy(self,ns_proxy):
        self.ns_proxy = ns_proxy

    def connect_to(self):
        print("connecting to agent1")
        a1_proxy = self.ns_proxy.proxy('agent1')#find a1's proxy
        a1_addr = a1_proxy.addr('agent12') # find a1's addr
        self.connect(a1_addr, handler=self.custom_log)  # listen to agent1

    def send_hello(self,ch):
        print("Sending hello")
        self.send(ch,'agent2 says hello!') #ch is 'agent21' or 'agent23'

    def custom_log(self,message):
        self.log_info('Recieved:%s'%message)

def conn_NS():  # https://github.com/opensistemas-hub/osbrain/issues/341
    ns_addr = '127.0.0.1:2000'
    self_addr = '127.0.0.1:3020' #this agent's address
    while True:  # look for nameserver
        try:
            ns_proxy = NSProxy(ns_addr)
            break
        except Pyro4.errors.CommunicationError:
            print('Pyr04.errors.CommunicationError,no nameserver found')
            print('Trying again...')
            pass

    while True:  # join nameserver
        try:
            # register with server
            print("registering agent with server...")
            a2_proxy = run_agent('agent2', addr=self_addr, base= Agent2, nsaddr=ns_addr)  
            a2_proxy.set_ns_proxy(ns_proxy)
            # wait for proxy to set up
            checkstatus = a2_proxy.get_attr('_running')

            while checkstatus == False:
                checkstatus = a2_proxy.get_attr('_running')

            # verify that agent has registered successfully
            agents_in_NS = osbrain.nameserver.NameServer.agents(ns_proxy)
            print('The following agents are in the server: %s' % agents_in_NS)
            break

        except KeyError:
            print('Key Error')
            print('Trying again...')
            pass

        except Pyro4.errors.CommunicationError:
            print('Pyro4.errors.CommunicationError, no nameserver found')
            print('Trying again...')
            pass

        except TimeoutError:
            print('TimeoutError, no nameserver found')
            print('Trying again...')
            pass

    print('ofpagent has joined the nameserver')
    return a2_proxy

if __name__ == '__main__':
    ns_addr = '127.0.0.1:2000'
    ns_proxy = NSProxy(ns_addr)
    a2_proxy = conn_NS()
    time.sleep(5)
    agents_in_NS = osbrain.nameserver.NameServer.agents(ns_proxy)
    #agents_in_NS = ['agent1', 'agent3']
    print(agents_in_NS)
    time.sleep(1)
    for _ in range(3):
        time.sleep(2)
        a2_proxy.send_hello('agent21')

Output:

registering agent with server...
The following agents are in the server: ['agent1', 'agent2']
ofpagent has joined the nameserver
['agent1', 'agent2']
Sending hello
Sending hello
Sending hello

As can be seen , I dont see hellos exchanged if I take approach-2.
I can't quite figure out why approach-2 fails.
Also, can I proceed with approach1. That is - assign IP addresses to agents and start entire MAS setup in NS?

Thank you.

@Peque
Copy link
Member

Peque commented May 24, 2021

Currently you cannot spawn agents from a NameServer running in one machine to a different machine (remote spawning). So I don't think approach 1 will work.

Note that you can use ns_proxy.agents() to list agents directly using the proxy (to simplify your scripts). That means, try to avoid using osbrain.nameserver.NameServer.agents(ns_proxy).

It seems you may be missing the a2_proxy.connect_to() call. That could be the reason why it is not working.

@Peque Peque added the question label May 24, 2021
@Peque Peque added this to the 0.7.0 milestone May 24, 2021
@brownbytes
Copy link
Author

@ Peque

I can confirm approach 1 did not work :-(. I tested approach 1 using 3 Raspberry Pi's and the approach failed.
I am testing approach 2 and will recheck my scripts for missing connect calls?

Thank you

@brownbytes
Copy link
Author

brownbytes commented May 24, 2021

I feel so stupid.
I did indeed did not call a2_proxy.connect_to(), this works ok now.
Will still go and test this on Rpi and close the issue .

AgentAddress(ipc, /run/user/1000/.osbrain_ipc/ebb913321bd04de28d3f5762841ea74d, PUSH, server, pickle)
Sending hello
INFO [2021-05-24 09:03:59.318338] (agent1): Recieved:agent2 says hello!
Sending hello

@brownbytes
Copy link
Author

brownbytes commented May 24, 2021

Hi Peque,
ns_proxy.proxy(agent_alias) ran just fine when all agents were running on same machine.
But when I ran the agents on different machines, I hit n exception.
This has occurred while querying agent's proxy using nameserver.
so, when agent1 tries to connect to agent2 , in connect_to() a2_proxy = self.ns_proxy.proxy('agent2') hit an exception, and nameserver.py hit an exception in step

        for agent in agents_in_NS:
            agent_proxy = ns_proxy.proxy(agent)

exception as below:


Traceback (most recent call last):
  File "/home/durga/Desktop/ns.py", line 32, in <module>
    main()
  File "/home/durga/Desktop/ns.py", line 25, in main
    ns_proxy.proxy(agent)
  File "/usr/local/lib/python3.6/dist-packages/osbrain/proxy.py", line 355, in proxy
    return Proxy(name, nsaddr=self.addr(), timeout=timeout)
  File "/usr/local/lib/python3.6/dist-packages/osbrain/proxy.py", line 82, in __init__
    while not self._ready_or_timeout(time0, timeout):
  File "/usr/local/lib/python3.6/dist-packages/osbrain/proxy.py", line 98, in _ready_or_timeout
    self.unsafe.ping()
  File "/usr/local/lib/python3.6/dist-packages/osbrain/proxy.py", line 156, in __getattr__
    return super().__getattr__(name)
  File "/usr/local/lib/python3.6/dist-packages/Pyro4/core.py", line 275, in __getattr__
    self._pyroGetMetadata()
  File "/usr/local/lib/python3.6/dist-packages/Pyro4/core.py", line 615, in _pyroGetMetadata
    self.__pyroCreateConnection()
  File "/usr/local/lib/python3.6/dist-packages/Pyro4/core.py", line 596, in __pyroCreateConnection
    connect_and_handshake(conn)
  File "/usr/local/lib/python3.6/dist-packages/Pyro4/core.py", line 549, in connect_and_handshake
    raise ce
Pyro4.errors.CommunicationError: cannot connect to ('localhost', 40729): [Errno 111] Connection refused

@brownbytes
Copy link
Author

brownbytes commented May 25, 2021

Update:
I could fix the above error by passing the IP address in run_agent().

Though the above issue is now fixed - I still need some advice as the agents dont seem to exchange 'hellos' .

below are agents

Agent1.py

from osbrain import Agent
from osbrain import NSProxy
from osbrain import run_agent
import osbrain
import Pyro4
import time
import sys

class Agent1(Agent):
    def on_init(self):
        addr12=self.bind('PUSH',alias='agent12') #agent12 channel
        
    def set_ns_proxy(self,ns_proxy):
        self.ns_proxy = ns_proxy

    def connect_to(self): #to listen to agent2's and agent3's pushed messages
        #find remote agents proxies
        print("connecting to agent2")
        try:
            a2_proxy = self.ns_proxy.proxy('agent2')
        except:
            print("Exception::ns_proxy.proxy('agent2')")
        finally :
            print(sys.exc_info()[0])
        a2_addr = a2_proxy.addr('agent21')#addr takes alias of a socket
        print(a2_proxy)
        print(a2_addr)
        self.connect(a2_addr,handler=self.custom_log) # listen to agent2

    def send_hello(self,ch):
        print("Sending hello")
        self.send(ch,'agent1 says hello!') #ch is 'agent12' or 'agent13'

    def custom_log(self,message):
        self.log_info('Recieved:%s'%message)

def conn_NS():  # https://github.com/opensistemas-hub/osbrain/issues/341
    ns_addr = '192.168.0.7:5020'
    while True:  # look for nameserver
        try:
            ns_proxy = NSProxy(ns_addr)
            break
        except Pyro4.errors.CommunicationError:
            print('Pyr04.errors.CommunicationError,no nameserver found')
            print('Trying again...')
            pass

    while True:  # join nameserver
        try:
            # register with server
            print("registering agent with server...")
            a1_proxy = run_agent('agent1', base= Agent1, nsaddr=ns_addr, addr = '192.168.0.101')
            a1_proxy.set_ns_proxy(ns_proxy)
            # wait for proxy to set up
            checkstatus = a1_proxy.get_attr('_running')

            while checkstatus == False:
                checkstatus = a1_proxy.get_attr('_running')

            # verify that agent has registered successfully
            agents_in_NS = ns_proxy.agents()
            print('The following agents are in the server: %s' % agents_in_NS)
            break

        except KeyError:
            print('Key Error')
            print('Trying again...')
            pass

        except Pyro4.errors.CommunicationError:
            print('Pyro4.errors.CommunicationError, no nameserver found')
            print('Trying again...')
            pass

        except TimeoutError:
            print('TimeoutError, no nameserver found')
            print('Trying again...')
            pass

    print('a1 has joined the nameserver')
    return a1_proxy


if __name__ == '__main__':
    ns_addr = '192.168.0.7:5020'
    ns_proxy = NSProxy(ns_addr)
    a1_proxy = conn_NS()
    agents_in_NS = ns_proxy.agents()
    #agents_in_NS = ['agent2', 'agent3']
    print(agents_in_NS)
    a1_proxy.connect_to()

    time.sleep(1)
    for _ in range(3):
        time.sleep(1)
        a1_proxy.send_hello('agent12')  # send hello on 'agent12' channel


Agent2.py

from osbrain import Agent
from osbrain import NSProxy
from osbrain import run_agent
import osbrain
import Pyro4
import time
from osbrain.proxy import Proxy

class Agent2(Agent):

    def on_init(self):
        addr21 = self.bind('PUSH',transport='tcp',alias='agent21')

    def set_ns_proxy(self,ns_proxy):
        self.ns_proxy = ns_proxy

    def connect_to(self):
        print("connecting to agent1 and agent2")
        a1_proxy = self.ns_proxy.proxy('agent1')#find a1's proxy
        a1_addr = a1_proxy.addr('agent12') # find a1's addr
        self.connect(a1_addr, handler=self.custom_log)  # listen to agent1

    def send_hello(self,ch):
        print("Sending hello")
        self.send(ch,'agent2 says hello!') #ch is 'agent21' or 'agent23'

    def custom_log(self,message):
        self.log_info('Recieved:%s'%message)

def conn_NS():  # https://github.com/opensistemas-hub/osbrain/issues/341
    ns_addr = '192.168.0.7:5020'
    while True:  # look for nameserver
        try:
            ns_proxy = NSProxy(ns_addr)
            
            break
        except Pyro4.errors.CommunicationError:
            print('Pyr04.errors.CommunicationError,no nameserver found')
            print('Trying again...')
            pass

    while True:  # join nameserver
        try:
            # register with server
            print("registering agent with server...")
            a2_proxy = run_agent('agent2', base= Agent2, nsaddr=ns_addr, addr='192.168.0.12')  
            a2_proxy.set_ns_proxy(ns_proxy)
            # wait for proxy to set up
            checkstatus = a2_proxy.get_attr('_running')
            print(a2_proxy)

            while checkstatus == False:
                checkstatus = a2_proxy.get_attr('_running')

            # verify that agent has registered successfully
            agents_in_NS = ns_proxy.agents()
            print('The following agents are in the server: %s' % agents_in_NS)
            break

        except KeyError:
            print('Key Error')
            print('Trying again...')
            pass

        except Pyro4.errors.CommunicationError:
            print('Pyro4.errors.CommunicationError, no nameserver found')
            print('Trying again...')
            pass

        except TimeoutError:
            print('TimeoutError, no nameserver found')
            print('Trying again...')
            pass

    print('a2 agent has joined the nameserver')
    return a2_proxy

if __name__ == '__main__':
    ns_addr = '192.168.0.7:5020'
    ns_proxy = NSProxy(ns_addr)
    a2_proxy = conn_NS()
    time.sleep(5)
    agents_in_NS = ns_proxy.agents()
    #agents_in_NS = ['agent1']
    print(agents_in_NS)
    a2_proxy.connect_to()
    time.sleep(1)
    for _ in range(3):
        time.sleep(1)
        a2_proxy.send_hello('agent21')





    



ns.py

from osbrain import run_nameserver
from osbrain.proxy import locate_ns,Proxy
import osbrain
import time

def main():
    # Windows
    # ns_sock = '127.0.0.1:1125'

    # # Linux
    ns_sock = '192.168.0.7:5020'
    #ns_sock = '0.0.0.0:5020'

    osbrain.config['TRANSPORT'] = 'tcp'

    ns_proxy = run_nameserver(ns_sock)
    ns_addr = locate_ns(ns_sock)

    agents_in_NS = ns_proxy.agents()

    while(len(agents_in_NS)<2):
        agents_in_NS = ns_proxy.agents()
        print('Current agents in Nameserver are: %s' %agents_in_NS)
        for agent in agents_in_NS:
            print(Proxy(agent))
        time.sleep(1)

    print('All agents have joined!')

if __name__ == '__main__':

    main()

The agents are stuck at 'sending hello' state as before.
agent1

registering agent with server...
The following agents are in the server: ['agent1']
a1 has joined the nameserver
['agent1', 'agent2']
connecting to agent2
error:: None
<osbrain.proxy.Proxy at 0x75dfd9b0; connected IPv4; for PYRONAME:[email protected]:5020>
AgentAddress(tcp, 192.168.0.12:44081, PUSH, server, pickle)
Sending hello

@brownbytes
Copy link
Author

brownbytes commented May 25, 2021

OKay!!!
This is now fixed. I realised since transport wasn't set to 'tcp' while binding agent1, Agent2 was using IPC .
Once I set both agent1 and agent2 to use transport='tcp', I can now see communication across devices.

Sorry, about spamming . Please delete the thread if it's unnecessary.
Final(working) versions:
agent1.py

from osbrain import Agent
from osbrain import NSProxy
from osbrain import run_agent
import osbrain
import Pyro4
import time
import sys

class Agent1(Agent):
    def on_init(self):
        addr12=self.bind('PUSH',alias='agent12',transport='tcp') #agent12 channel
        
    def set_ns_proxy(self,ns_proxy):
        self.ns_proxy = ns_proxy

    def connect_to(self): #to listen to agent2's and agent3's pushed messages
        #find remote agents proxies
        print("connecting to agent2")
        try:
            a2_proxy = self.ns_proxy.proxy('agent2')
            print(a2_proxy)
        except:
            print("Exception::ns_proxy.proxy('agent2')")
        try:
            a2_addr = a2_proxy.addr('agent21')#addr takes alias of a socket
            print(a2_addr)
        except:
            print("failed to fetch a2_addr")
        try:
            self.connect(a2_addr,handler=self.custom_log) # listen to agent2
        except:
            print("failure to connect in connect_to method")

    def send_hello(self,ch):
        print("Sending hello")
        self.send(ch,'agent1 says hello!') #ch is 'agent12' or 'agent13'

    def custom_log(self,message):
        self.log_info('Recieved:%s'%message)

def conn_NS():  # https://github.com/opensistemas-hub/osbrain/issues/341
    ns_addr = '192.168.0.7:5020'
    while True:  # look for nameserver
        try:
            ns_proxy = NSProxy(ns_addr)
            break
        except Pyro4.errors.CommunicationError:
            print('Pyr04.errors.CommunicationError,no nameserver found')
            print('Trying again...')
            pass

    while True:  # join nameserver
        try:
            # register with server
            print("registering agent with server...")
            a1_proxy = run_agent('agent1', base= Agent1, nsaddr=ns_addr, addr = '192.168.0.101')
            a1_proxy.set_ns_proxy(ns_proxy)
            # wait for proxy to set up
            checkstatus = a1_proxy.get_attr('_running')

            while checkstatus == False:
                checkstatus = a1_proxy.get_attr('_running')

            # verify that agent has registered successfully
            agents_in_NS = ns_proxy.agents()
            print('The following agents are in the server: %s' % agents_in_NS)
            break

        except KeyError:
            print('Key Error')
            print('Trying again...')
            pass

        except Pyro4.errors.CommunicationError:
            print('Pyro4.errors.CommunicationError, no nameserver found')
            print('Trying again...')
            pass

        except TimeoutError:
            print('TimeoutError, no nameserver found')
            print('Trying again...')
            pass

    print('a1 has joined the nameserver')
    return a1_proxy


if __name__ == '__main__':
    ns_addr = '192.168.0.7:5020'
    ns_proxy = NSProxy(ns_addr)
    a1_proxy = conn_NS()
    time.sleep(5)
    agents_in_NS = ns_proxy.agents()
    #agents_in_NS = ['agent2', 'agent3']
    
    print(agents_in_NS)
    a1_proxy.connect_to()
    time.sleep(10)
    for _ in range(3):
        time.sleep(2)
        a1_proxy.send_hello('agent12')  # send hello on 'agent12' channel

agent2.py

from osbrain import Agent
from osbrain import NSProxy
from osbrain import run_agent
import osbrain
import Pyro4
import time
from osbrain.proxy import Proxy
import sys

class Agent2(Agent):

    def on_init(self):
        addr21 = self.bind('PUSH',transport='tcp',alias='agent21')

    def set_ns_proxy(self,ns_proxy):
        self.ns_proxy = ns_proxy

    def connect_to(self):
        print("connecting to agent1")
        try:
            a1_proxy = self.ns_proxy.proxy('agent1')#find a1's proxy
            print(a1_proxy)
        except:
            print("exception::ns_proxy.proxy('agent1')")
        try:
            a1_addr = a1_proxy.addr('agent12') # find a1's addr
            print(a1_addr)
        except:
            print("failed to fetch addr")
        try:
            self.connect(a1_addr, handler=self.custom_log)  # listen to agent1
        except:
            print("failed to connect in connect_to()")

    def send_hello(self,ch):
        print("Sending hello")
        self.send(ch,'agent2 says hello!') #ch is 'agent21' or 'agent23'

    def custom_log(self,message):
        self.log_info('Recieved:%s'%message)

def conn_NS():  # https://github.com/opensistemas-hub/osbrain/issues/341
    ns_addr = '192.168.0.7:5020'
    while True:  # look for nameserver
        try:
            ns_proxy = NSProxy(ns_addr)
            
            break
        except Pyro4.errors.CommunicationError:
            print('Pyr04.errors.CommunicationError,no nameserver found')
            print('Trying again...')
            pass

    while True:  # join nameserver
        try:
            # register with server
            print("registering agent with server...")
            a2_proxy = run_agent('agent2', base= Agent2, nsaddr=ns_addr, addr='192.168.0.12')  
            a2_proxy.set_ns_proxy(ns_proxy)
            # wait for proxy to set up
            checkstatus = a2_proxy.get_attr('_running')
            print(a2_proxy)

            while checkstatus == False:
                checkstatus = a2_proxy.get_attr('_running')

            # verify that agent has registered successfully
            agents_in_NS = ns_proxy.agents()
            print('The following agents are in the server: %s' % agents_in_NS)
            break

        except KeyError:
            print('Key Error')
            print('Trying again...')
            pass

        except Pyro4.errors.CommunicationError:
            print('Pyro4.errors.CommunicationError, no nameserver found')
            print('Trying again...')
            pass

        except TimeoutError:
            print('TimeoutError, no nameserver found')
            print('Trying again...')
            pass

    print('a2 agent has joined the nameserver')
    return a2_proxy

if __name__ == '__main__':
    ns_addr = '192.168.0.7:5020'
    ns_proxy = NSProxy(ns_addr)
    a2_proxy = conn_NS()
    time.sleep(5)
    agents_in_NS = ns_proxy.agents()
    #agents_in_NS = ['agent1']
    print(agents_in_NS)
    a2_proxy.connect_to()
    time.sleep(10)
    for _ in range(3):
        time.sleep(2)
        a2_proxy.send_hello('agent21')

@Peque
Copy link
Member

Peque commented May 25, 2021

Good to know! 😊

@Peque Peque closed this as completed May 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants