def __init__( self, oef_addr: str, oef_port: int, loop: Optional[asyncio.AbstractEventLoop] = None, ): """ Initialize. :param oef_addr: IP address of the OEF node. :param oef_port: Port of the OEF node. """ self.oef_addr = oef_addr self.oef_port = oef_port self._result = False self._stop = False self._core = AsyncioCore() self.agent = OEFAgent("check", core=self._core, oef_addr=self.oef_addr, oef_port=self.oef_port) self.agent.on_connect_success = self.on_connect_ok self.agent.on_connection_terminated = self.on_connect_terminated self.agent.on_connect_failed = self.exception_handler
def __init__(self, public_key, oef_addr, oef_port, data_model_json, service_description_json, data_to_send_json, price): OEFAgent.__init__(self, public_key, oef_addr, oef_port) self.data_model = modlify(data_model_json) self.service = Description(service_description_json, self.data_model) self.data = data_to_send_json self.price = price
def test_that_two_connect_attempts_work_correctly(self): """Test that two call to the :func:'~agents.Agent.connect()' method work correctly. Use the local implementation of the OEF.""" with NetworkOEFNode(): agent_1 = OEFAgent("two_connect_attempt", "127.0.0.1", 3333) first_status = agent_1.connect() second_status = agent_1.connect() assert first_status assert second_status
def __init__(self): ''' MAIN/TEST ADDRESSES NOT RELEASED YET net = os.getenv('NET', '') oef = 'oef.economicagents.com' if (net == 'MAIN' or net == 'TEST') else '127.0.0.1' ''' oef = '127.0.0.1' OEFAgent.__init__(self, public_key = 'DataBridge', oef_addr = oef, oef_port = 10000) self.data = {} self.price = 0 self.open_proposals = [] self.search = []
def test_connection_error_public_key_already_in_use(self): """Test that a OEFConnectionError is raised when we try to connect two agents with the same public key.""" with pytest.raises(OEFConnectionError, match="Public key already in use."): with NetworkOEFNode(): agent_1 = OEFAgent("the_same_public_key", "127.0.0.1", 3333) agent_2 = OEFAgent(agent_1.public_key, "127.0.0.1", 3333) agent_1.connect() agent_2.connect()
def test_competition_stops_too_few_registered_agents(self, network_node): """Test that if the controller agent does not receive enough registrations, it stops.""" controller_agent = ControllerAgent(version=1) controller_agent.connect() parameters = TACParameters(min_nb_agents=2, start_time=datetime.datetime.now(), registration_timeout=5) job = Thread(target=controller_agent.handle_competition, args=(parameters, )) job.start() crypto = Crypto() agent1 = OEFAgent(crypto.public_key, "127.0.0.1", 10000, loop=asyncio.new_event_loop()) agent1.connect() agent1.send_message( 0, 0, controller_agent.public_key, Register(agent1.public_key, crypto, 'agent_name').serialize()) job.join() assert len(controller_agent.game_handler.registered_agents) == 1
def test_disconnect(self): """Test that the disconnect method works correctly.""" with NetworkOEFNode(): agent_1 = OEFAgent("disconnect", "127.0.0.1", 3333) assert not agent_1._oef_proxy.is_connected() agent_1.connect() assert agent_1._oef_proxy.is_connected() agent_1.disconnect() assert not agent_1._oef_proxy.is_connected()
class OEFHealthCheck(object): """A health check class.""" def __init__( self, oef_addr: str, oef_port: int, loop: Optional[asyncio.AbstractEventLoop] = None, ): """ Initialize. :param oef_addr: IP address of the OEF node. :param oef_port: Port of the OEF node. """ self.oef_addr = oef_addr self.oef_port = oef_port self._result = False self._stop = False self._core = AsyncioCore() self.agent = OEFAgent("check", core=self._core, oef_addr=self.oef_addr, oef_port=self.oef_port) self.agent.on_connect_success = self.on_connect_ok self.agent.on_connection_terminated = self.on_connect_terminated self.agent.on_connect_failed = self.exception_handler def exception_handler(self, url=None, ex=None): """Handle exception during a connection attempt.""" print("An error occurred. Exception: {}".format(ex)) self._stop = True def on_connect_ok(self, url=None): """Handle a successful connection.""" print("Connection OK!") self._result = True self._stop = True def on_connect_terminated(self, url=None): """Handle a connection failure.""" print("Connection terminated.") self._stop = True def run(self) -> bool: """ Run the check, asynchronously. :return: True if the check is successful, False otherwise. """ self._result = False self._stop = False def stop_connection_attempt(self): if self.agent.state == "connecting": self.agent.state = "failed" t = Timer(1.5, stop_connection_attempt, args=(self, )) try: print("Connecting to {}:{}...".format(self.oef_addr, self.oef_port)) self._core.run_threaded() t.start() self._result = self.agent.connect() self._stop = True if self._result: print("Connection established. Tearing down connection...") self.agent.disconnect() t.cancel() else: print("A problem occurred. Exiting...") return self._result except Exception as e: print(str(e)) return self._result finally: t.join(1.0) self.agent.stop() self.agent.disconnect() self._core.stop()
def __init__(self, public_key, oef_addr, oef_port, max_price): OEFAgent.__init__(self, public_key, oef_addr, oef_port) self.price_threshold = max_price
# Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # ------------------------------------------------------------------------------ """ A short script that show how an agent can register/unregister to the OEF. """ from oef.schema import Description from oef.agents import OEFAgent if __name__ == '__main__': agent = OEFAgent("agent_1", oef_addr="127.0.0.1", oef_port=3333) agent.connect() print("Agent successfully connected.") # specify message id to receive any error messages from the OEF Node. # look at the 'onOEFError' agent method. message_id = 0 agent.register_agent(message_id, Description({})) print("Agent registered to the OEF Node.") agent.unregister_agent(message_id + 1) print("Agent {} unregistered".format(agent.public_key)) agent.disconnect()