opensistemas-hub / osbrain

osBrain - A general-purpose multi-agent system module written in Python
https://osbrain.readthedocs.io/en/stable/
Apache License 2.0
176 stars 43 forks source link

Remote access simple not working #317

Closed daviduarte closed 5 years ago

daviduarte commented 5 years ago

I'm doing a distributed system, and I'm trying remote access from an agent to a nameserver.

I ran the following code:

client.py

import time

import osbrain
from osbrain import run_agent
from osbrain import run_nameserver

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

def reply_late(agent, message):
    time.sleep(1)
    return 'Hello, Bob!'

if __name__ == '__main__':

    #ns = run_nameserver()
    alice = run_agent('Alice', nsaddr="EXTERNAL_IP_SERVER:4444")
    #bob = run_agent('Bob')

    addr = alice.bind('ASYNC_REP', handler=reply_late, alias="main")
    #bob.connect(addr, alias='alice', handler=process_reply)

    #bob.send('alice', 'Hello, Alice!')
    #bob.log_info('I am done!')

    #bob.log_info('Waiting for Alice to reply...')
    time.sleep(2)

    #ns.shutdown()

server.py

import time

import osbrain

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

from osbrain import run_agent
from osbrain import run_nameserver
from osbrain.proxy import Proxy

# Armazena as conexões atuais
connections = {}

def process_reply(agent, message):
    agent.log_info('Processed reply: %s' % message)

ns = run_nameserver("127.0.0.1:4444")

agentServer = run_agent('AgentServer')
#agentServer.connect(addr, alias='alice', handler=process_reply)

while 1:
    for alias in ns.agents():
        if alias != "AgentServer":
            print(alias)

            if alias not in connections:

                # Primeiro, coloca o novo agente na lista de conexões 
                proxy = Proxy(alias)
                connections[alias] = proxy

                print(Proxy(alias))
                agentServer.connect(proxy.addr('main'), alias='alice', handler=process_reply)
                agentServer.send('alice', 'Hello, Alice!')
                agentServer.log_info('I am done!')
                agentServer.log_info('Waiting for Alice to reply...')

    time.sleep(1)

ns.shutdown()

In the server.py, I already tried:

ns = run_nameserver("127.0.0.1:4444")    # localhost
ns = run_nameserver("192.168.1.8:4444")  # My internal IP
ns = run_nameserver("MY_EXTERNAL_IP:4444") But this raises an exception

I tried the following scenarios: 1- server.py and client.py in my current machine = WORKS! 2- server.py and client.py in my internal LAN = NOT WORK 3- server.py in my machine and client.py in a cloud = NOT WORK 4- client.py in my machine and server.py in a cloud = NOT WORK

I already configured port forwarding to 4444 and disabled UFW. I'm running in Ubuntu 16.04.

I tried a simple socket connection over my internal lan in the same port (situation 2 above), and works perfectly.

There is something i'm missing?

Peque commented 5 years ago

There is something i'm missing?

I am not sure. I would guess it is a problem not directly related to osBrain, but rather with your IP configurations or firewalls.

If it is already working in your localhost then, before going to the cloud, I would first try to make it work in your LAN:

Peque commented 5 years ago

Also, you may want to try with 192.168.1.8:4444 on both client and server but running them from the same machine (the one with that IP). That could indicate the problem is in your LAN/firewalls.

Peque commented 5 years ago

Closing this as outdated. Feel free to reopen if the problem still persists.