多代理问题
假设您有多个代理(每个代理都是它自己的进程),它们可以相互通信(代理只能成对通信);如何确保一旦两个代理开始通信,没有其他代理可以中断他们?
这是代码的重要部分:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过进程并使用 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.
您可以给接收者一个令牌。当代理想要发送给接收者时,它应该首先获取接收者的令牌。整个通信结束后,发送方将释放令牌,然后其他人就可以向接收方发送令牌。
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.