2017-01-17 29 views
2

我正在嘗試使用Jade ACL消息發送字節數組。基本上我想要做這個人的工作:Decryption Error bad padding但我不想將密碼轉換爲字符串,但直接發送字節。有一個名爲setByteSequence的函數,我可以/應該使用該函數,但是當我運行我的代碼時,它會拋出一個錯誤,指出base64不受支持,並提及沒有說明如何使用它的文檔。只是它被支持。我使用Jade網站的jade.jar。使用Java以Jade ACLMessage發送字節

發件人:

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("[email protected]%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(); 
    } 
} 
} 
} 

的基於64例並不說明任何事情無論是。它只是設置一個對象作爲消息的內容,並表示它將以base64編碼......可能是因爲我錯過了明顯但我看不到的東西。我很感激你的幫助。

回答

0

試着用soures代替罐子,發現org.apache.commons.codec.binary.Base64無法解決,這意味着我必須在我的項目中添加Apache Commons Codec

0

以下是發件人代理的外觀。

///////////////////////////////// SENDER //////////// ///////////////////

    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"); 

現在這是你的接收器代理應該如何看起來像:

///////// ///////////// RECEIVER //////////////////////

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