在 Java 中使用 Jade ACLMessage 发送字节
Send bytes with Jade ACLMessage in Java
我正在尝试发送带有 Jade ACL 消息的字节数组。基本上我想做这个人做的事: 但我不想将密码转换成字符串而是直接发送字节。有一个名为 setByteSequence 的函数,我 could/should 使用它,但是当我 运行 我的代码时,它会抛出一个错误,指出不支持 base64 并引用没有说明如何使用它的文档.只是它受支持。我正在使用 Jade 网站上的 jade.jar。
发件人:
public class Alice extends Agent {
private static final long serialVersionUID = 725326296709140752L;
protected void setup() {
ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
AID recipient1 = new AID();
recipient1.setName(String.format("Bob@%s:1099/JADE", Main.IPBob));
recipient1.addAddresses(String.format("http://%s:7778/acc", Main.IPBob));
msg.addReceiver(recipient1);
// byte[] mBytes = "bla".getBytes(); // this doesn't work
byte[] mBytes = Base64.getEncoder().encode("bla".getBytes()); // neither does this
msg.setByteSequenceContent(mBytes);
send(msg);
}
}
接收者:
public class Bob extends Agent {
private static final long serialVersionUID = 2028682217881039710L;
protected void setup() {
addBehaviour(new CyclicBehaviour(this) {
private static final long serialVersionUID = 1L;
public void action() {
ACLMessage msg = myAgent.receive();
if (msg != null) {
System.out.println(String.format("Got Message %s", DatatypeConverter.printBase64Binary(msg.getByteSequenceContent())));
} else {
block();
}
}
});
}
}
主要:
public class Main {
public static final String IPAlice = "...";
public static final String IPBob = "...";
public static void main(String[] args) {
int port = 1099;
int mtpPort = 7778;
String hostIP = "...";
Profile profile = new ProfileImpl(hostIP, port, null, true);
profile.setParameter(Profile.MTPS, "jade.mtp.http.MessageTransportProtocol(http://"+hostIP+":"+mtpPort+"/acc)");
Runtime runtime = Runtime.instance();
AgentContainer container = runtime.createMainContainer(profile);
try {
// AgentController bob = container.createNewAgent("Bob", agent.Bob.class.getName(), null);
// bob.start();
AgentController alice = container.createNewAgent("Alice", agent.Alice.class.getName(), null);
alice.start();
} catch (StaleProxyException e) {
e.printStackTrace();
}
}
}
}
base64 示例也没有显示任何内容。它只是将一个对象设置为消息的内容,并表示它将进行 base64 编码……可能是我遗漏了明显的内容,但我看不到它。非常感谢您的帮助。
尝试使用 soures 而不是 jar,发现 org.apache.commons.codec.binary.Base64
无法解析,这意味着我必须将 Apache Commons Codec 添加到我的项目中。
这是您发送代理的样子。
////////////////////////////// 发件人 /////////// ////////////////////
Path path = Paths.get("Address of file");
byte[] data = null;
try {
data = Files.readAllBytes(path);
} catch (IOException ex) {
Logger.getLogger(DataAgent.class.getName()).log(Level.SEVERE, null, ex);
}
ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
msg.addReceiver(<receiver-aid>);
msg.addReceiver(new AID("Agent-name", AID.ISLOCALNAME));
msg.setByteSequenceContent(data);
msg.addUserDefinedParameter("file-name", filename);
send(msg);
System.out.println("Message sent");
现在您的接收代理应该是这样的:
///////////////////// 接收器 //////////////////////
ACLMessage msg = receive();
System.out.println("Received msg is "+msg.getContent());
String fileName = msg.getUserDefinedParameter("file-name");
File f = new File(fileName);
byte[] fileContent = msg.getByteSequenceContent();
//write to file
我正在尝试发送带有 Jade ACL 消息的字节数组。基本上我想做这个人做的事:
发件人:
public class Alice extends Agent {
private static final long serialVersionUID = 725326296709140752L;
protected void setup() {
ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
AID recipient1 = new AID();
recipient1.setName(String.format("Bob@%s:1099/JADE", Main.IPBob));
recipient1.addAddresses(String.format("http://%s:7778/acc", Main.IPBob));
msg.addReceiver(recipient1);
// byte[] mBytes = "bla".getBytes(); // this doesn't work
byte[] mBytes = Base64.getEncoder().encode("bla".getBytes()); // neither does this
msg.setByteSequenceContent(mBytes);
send(msg);
}
}
接收者:
public class Bob extends Agent {
private static final long serialVersionUID = 2028682217881039710L;
protected void setup() {
addBehaviour(new CyclicBehaviour(this) {
private static final long serialVersionUID = 1L;
public void action() {
ACLMessage msg = myAgent.receive();
if (msg != null) {
System.out.println(String.format("Got Message %s", DatatypeConverter.printBase64Binary(msg.getByteSequenceContent())));
} else {
block();
}
}
});
}
}
主要:
public class Main {
public static final String IPAlice = "...";
public static final String IPBob = "...";
public static void main(String[] args) {
int port = 1099;
int mtpPort = 7778;
String hostIP = "...";
Profile profile = new ProfileImpl(hostIP, port, null, true);
profile.setParameter(Profile.MTPS, "jade.mtp.http.MessageTransportProtocol(http://"+hostIP+":"+mtpPort+"/acc)");
Runtime runtime = Runtime.instance();
AgentContainer container = runtime.createMainContainer(profile);
try {
// AgentController bob = container.createNewAgent("Bob", agent.Bob.class.getName(), null);
// bob.start();
AgentController alice = container.createNewAgent("Alice", agent.Alice.class.getName(), null);
alice.start();
} catch (StaleProxyException e) {
e.printStackTrace();
}
}
}
}
base64 示例也没有显示任何内容。它只是将一个对象设置为消息的内容,并表示它将进行 base64 编码……可能是我遗漏了明显的内容,但我看不到它。非常感谢您的帮助。
尝试使用 soures 而不是 jar,发现 org.apache.commons.codec.binary.Base64
无法解析,这意味着我必须将 Apache Commons Codec 添加到我的项目中。
这是您发送代理的样子。
////////////////////////////// 发件人 /////////// ////////////////////
Path path = Paths.get("Address of file");
byte[] data = null;
try {
data = Files.readAllBytes(path);
} catch (IOException ex) {
Logger.getLogger(DataAgent.class.getName()).log(Level.SEVERE, null, ex);
}
ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
msg.addReceiver(<receiver-aid>);
msg.addReceiver(new AID("Agent-name", AID.ISLOCALNAME));
msg.setByteSequenceContent(data);
msg.addUserDefinedParameter("file-name", filename);
send(msg);
System.out.println("Message sent");
现在您的接收代理应该是这样的:
///////////////////// 接收器 //////////////////////
ACLMessage msg = receive();
System.out.println("Received msg is "+msg.getContent());
String fileName = msg.getUserDefinedParameter("file-name");
File f = new File(fileName);
byte[] fileContent = msg.getByteSequenceContent();
//write to file