Skip to content

Commit

Permalink
Merge pull request #114 from maurerle/acl_msg
Browse files Browse the repository at this point in the history
use AgentAddress in create_acl for receiver_addr and sender_addr
  • Loading branch information
rcschrg authored Oct 21, 2024
2 parents d8d9735 + 59a5e31 commit a599f28
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 41 deletions.
50 changes: 26 additions & 24 deletions mango/messages/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
from enum import Enum
from typing import Any

from ..messages.acl_message_pb2 import ACLMessage as ACLProto
from ..messages.mango_message_pb2 import MangoMessage as MangoMsg
from ..agent.core import AgentAddress
from .acl_message_pb2 import ACLMessage as ACLProto
from .mango_message_pb2 import MangoMessage as MangoMsg


class Message(ABC):
Expand Down Expand Up @@ -245,41 +246,42 @@ class Performatives(Enum):

def create_acl(
content,
receiver_addr: str | tuple[str, int],
sender_addr: str | tuple[str, int],
receiver_id: None | str = None,
receiver_addr: AgentAddress,
sender_addr: AgentAddress,
acl_metadata: None | dict[str, Any] = None,
is_anonymous_acl=False,
):
acl_metadata = {} if acl_metadata is None else acl_metadata.copy()
# analyse and complete acl_metadata
if "receiver_addr" not in acl_metadata.keys():
acl_metadata["receiver_addr"] = receiver_addr
elif acl_metadata["receiver_addr"] != receiver_addr:
if (
"receiver_addr" in acl_metadata.keys()
and acl_metadata["receiver_addr"] != receiver_addr.protocol_addr
):
warnings.warn(
f"The argument receiver_addr ({receiver_addr}) is not equal to "
f"The argument receiver_addr ({receiver_addr.protocol_addr}) is not equal to "
f"acl_metadata['receiver_addr'] ({acl_metadata['receiver_addr']}). \
For consistency, the value in acl_metadata['receiver_addr'] "
f"was overwritten with receiver_addr.",
UserWarning,
)
acl_metadata["receiver_addr"] = receiver_addr
if receiver_id:
if "receiver_id" not in acl_metadata.keys():
acl_metadata["receiver_id"] = receiver_id
elif acl_metadata["receiver_id"] != receiver_id:
warnings.warn(
f"The argument receiver_id ({receiver_id}) is not equal to "
f"acl_metadata['receiver_id'] ({acl_metadata['receiver_id']}). \
For consistency, the value in acl_metadata['receiver_id'] "
f"was overwritten with receiver_id.",
UserWarning,
)
acl_metadata["receiver_id"] = receiver_id
if (
"receiver_id" in acl_metadata.keys()
and acl_metadata["receiver_id"] != receiver_addr.aid
):
warnings.warn(
f"The argument receiver_id ({receiver_addr.aid}) is not equal to "
f"acl_metadata['receiver_id'] ({acl_metadata['receiver_id']}). \
For consistency, the value in acl_metadata['receiver_id'] "
f"was overwritten with receiver_id.",
UserWarning,
)
acl_metadata["receiver_addr"] = receiver_addr.protocol_addr
acl_metadata["receiver_id"] = receiver_addr.aid

# add sender_addr if not defined and not anonymous
if not is_anonymous_acl:
if "sender_addr" not in acl_metadata.keys() and sender_addr is not None:
acl_metadata["sender_addr"] = sender_addr
acl_metadata["sender_addr"] = sender_addr.protocol_addr
acl_metadata["sender_id"] = sender_addr.aid

message = ACLMessage()
message.content = content
Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests/core/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async def test_send_acl_message():

async with activate(c) as c:
await agent.send_message(
create_acl("", receiver_addr=agent2.addr, sender_addr=c.addr),
create_acl("", receiver_addr=agent2.addr, sender_addr=agent.addr),
receiver_addr=agent2.addr,
)
msg = await agent2.inbox.get()
Expand Down Expand Up @@ -96,7 +96,7 @@ async def test_schedule_acl_message():

async with activate(c) as c:
await agent.schedule_instant_message(
create_acl("", receiver_addr=agent2.addr, sender_addr=c.addr),
create_acl("", receiver_addr=agent2.addr, sender_addr=agent.addr),
receiver_addr=agent2.addr,
)

Expand Down
22 changes: 13 additions & 9 deletions tests/unit_tests/core/test_container.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from mango import activate, create_acl, create_tcp_container
from mango.agent.core import Agent
from mango.agent.core import Agent, AgentAddress


class LooksLikeAgent:
Expand Down Expand Up @@ -153,10 +153,9 @@ async def test_create_acl_no_modify():
common_acl_q = {}
actual_acl_message = create_acl(
"",
receiver_addr="",
receiver_id="",
receiver_addr=AgentAddress("", ""),
acl_metadata=common_acl_q,
sender_addr=c.addr,
sender_addr=AgentAddress(c.addr, ""),
)

assert "reeiver_addr" not in common_acl_q
Expand All @@ -170,7 +169,10 @@ async def test_create_acl_no_modify():
async def test_create_acl_anon():
c = create_tcp_container(addr=("127.0.0.1", 5555))
actual_acl_message = create_acl(
"", receiver_addr="", receiver_id="", is_anonymous_acl=True, sender_addr=c.addr
"",
receiver_addr=AgentAddress("", ""),
is_anonymous_acl=True,
sender_addr=AgentAddress(c.addr, ""),
)

assert actual_acl_message.sender_addr is None
Expand All @@ -181,7 +183,10 @@ async def test_create_acl_anon():
async def test_create_acl_not_anon():
c = create_tcp_container(addr=("127.0.0.1", 5555))
actual_acl_message = create_acl(
"", receiver_addr="", receiver_id="", is_anonymous_acl=False, sender_addr=c.addr
"",
receiver_addr=AgentAddress("", ""),
is_anonymous_acl=False,
sender_addr=AgentAddress(c.addr, ""),
)

assert actual_acl_message.sender_addr is not None
Expand Down Expand Up @@ -227,10 +232,9 @@ async def test_create_acl_diff_receiver():
with pytest.warns(UserWarning) as record:
actual_acl_message = create_acl(
"",
receiver_addr="A",
receiver_id="A",
receiver_addr=AgentAddress("A", "A"),
acl_metadata={"receiver_id": "B", "receiver_addr": "B"},
sender_addr=c.addr,
sender_addr=AgentAddress(c.addr, ""),
is_anonymous_acl=False,
)

Expand Down
10 changes: 4 additions & 6 deletions tests/unit_tests/core/test_external_scheduling_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,8 @@ async def test_step_with_cond_task():
# create and send message in next step
message = create_acl(
content="",
receiver_addr=external_scheduling_container.addr,
receiver_id=agent_1.aid,
sender_addr=external_scheduling_container.addr,
receiver_addr=AgentAddress(external_scheduling_container.addr, agent_1.aid),
sender_addr=AgentAddress(external_scheduling_container.addr, agent_1.aid),
)
encoded_msg = external_scheduling_container.codec.encode(message)
print("created message")
Expand Down Expand Up @@ -204,9 +203,8 @@ async def test_send_internal_messages():
async with activate(external_scheduling_container) as c:
message = create_acl(
content="",
receiver_addr=external_scheduling_container.addr,
receiver_id=agent_1.aid,
sender_addr=external_scheduling_container.addr,
receiver_addr=AgentAddress(external_scheduling_container.addr, agent_1.aid),
sender_addr=AgentAddress(external_scheduling_container.addr, agent_1.aid),
)
encoded_msg = external_scheduling_container.codec.encode(message)
return_values = await external_scheduling_container.step(
Expand Down

0 comments on commit a599f28

Please sign in to comment.