多代理问题

发布于 2024-11-06 01:12:47 字数 435 浏览 0 评论 0原文

假设您有多个代理(每个代理都是它自己的进程),它们可以相互通信(代理只能成对通信);如何确保一旦两个代理开始通信,没有其他代理可以中断他们?

这是代码的重要部分:

class Agent {
    private void send(int to, byte[] message) {...};
    private void receive(int from, byte[] message) {...};
}

send 方法将消息发送到指定的代理,而 receive 方法处理收到的来自其他代理的消息。因此,假设 id=1 和 id=2 的代理交换一些消息:如何确保两个代理在交换期间都不会处理(在其接收方法中)来自任何其他代理的消息?我尝试根据代理的 id 过滤消息(通过将它们存储在 int 变量中),但它似乎无法正常工作?

say you have multiple agents(each of them is it's own process) which can communicate to each other(agents communicate only in pairs); how do you ensure that once two agents start communicating, no other agent can interrupt them?

Here's the important part of the code:

class Agent {
    private void send(int to, byte[] message) {...};
    private void receive(int from, byte[] message) {...};
}

The send method sends the message to the specified agent and receive method processes the received message, that comes from other agents. So let's say agents with id=1 and id=2 exchange a few messages: how do I ensure that neither of the agents processes(in it's receive method) messages from any other agent during their exchange? I tried filtering messages based on agent's id(by storing them in int variable) but it doesn't seems to work properly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月染过的梦 2024-11-13 01:12:47

通过进程并使用 TCP 进行 IPC,任何时候让每个代理最多打开一个套接字。那么第二个代理将永远无法与已经通话的代理通话。

With processes and using TCP for the IPC, at any time let every agent have at most one socket open. Then a second agent will never be able to talk with an already talking agent.

许久 2024-11-13 01:12:47

您可以给接收者一个令牌。当代理想要发送给接收者时,它应该首先获取接收者的令牌。整个通信结束后,发送方将释放令牌,然后其他人就可以向接收方发送令牌。

You can give receivers a token. When an agent want to send to a receiver, it should get the receiver's token at first. After finishing the whole communication, the sender will release the token, and then other's can send to the receiver.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文