Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.58 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6.  
  7. package examples.message;
  8.  
  9. import jade.core.AID;
  10. import jade.core.Agent;
  11. import jade.core.behaviours.*;
  12. import jade.domain.DFService;
  13. import jade.domain.FIPAAgentManagement.DFAgentDescription;
  14. import jade.domain.FIPAAgentManagement.ServiceDescription;
  15. import jade.domain.FIPAException;
  16. import jade.lang.acl.*;
  17.  
  18. import java.io.ByteArrayInputStream;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.IOException;
  21. import java.io.ObjectInputStream;
  22. import java.io.ObjectOutputStream;
  23. import java.util.zip.GZIPInputStream;
  24. import java.util.zip.GZIPOutputStream;
  25. import org.apache.commons.codec.binary.Base64;
  26.  
  27. /**
  28. *
  29. * @author U
  30. */
  31. public class CalcAgentPlus extends Agent
  32. {
  33. static final Base64 base64 = new Base64();
  34.  
  35. public String serializeObjectToString(Object object) throws IOException
  36. {
  37. String s = null;
  38.  
  39. try
  40. {
  41. ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
  42. GZIPOutputStream gzipOutputStream = new GZIPOutputStream(arrayOutputStream);
  43. ObjectOutputStream objectOutputStream = new ObjectOutputStream(gzipOutputStream);
  44.  
  45. objectOutputStream.writeObject(object);
  46. objectOutputStream.flush();
  47. gzipOutputStream.close();
  48.  
  49. objectOutputStream.flush();
  50. objectOutputStream.close();
  51.  
  52. s = new String(base64.encode(arrayOutputStream.toByteArray()));
  53. arrayOutputStream.flush();
  54. arrayOutputStream.close();
  55. }
  56. catch(Exception ex){}
  57.  
  58. return s;
  59. }
  60.  
  61. public Object deserializeObjectFromString(String objectString) throws IOException, ClassNotFoundException
  62. {
  63. Object obj = null;
  64. try
  65. {
  66. ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(base64.decode(objectString));
  67. GZIPInputStream gzipInputStream = new GZIPInputStream(arrayInputStream);
  68. ObjectInputStream objectInputStream = new ObjectInputStream(gzipInputStream);
  69. obj = objectInputStream.readObject();
  70.  
  71. objectInputStream.close();
  72. gzipInputStream.close();
  73. arrayInputStream.close();
  74. }
  75. catch(Exception ex){}
  76. return obj;
  77. }
  78.  
  79. protected void setup()
  80. {
  81. //First set-up answering behaviour
  82. addBehaviour(new CyclicBehaviour(this)
  83. {
  84. public void action() {
  85. ACLMessage msg = receive();
  86. Calculation calc = new Calculation();
  87.  
  88. if (msg != null)
  89. {
  90. String msgContent = msg.getContent();
  91.  
  92. System.out.println("\n[CalcAgentPlus] Message Received");
  93. System.out.println("[CalcAgentPlus] Sender Agent : " + msg.getSender());
  94. System.out.println("[CalcAgentPlus] Message content [Base64 string]: " + msgContent);
  95.  
  96. try
  97. {
  98. calc = (Calculation)deserializeObjectFromString(msgContent);
  99. }
  100. catch(Exception ex)
  101. {
  102. System.out.println("\n[CalcAgentPlus] StrToObj conversion error: " + ex.getMessage());
  103. }
  104.  
  105. int result = 0;
  106. if (calc.getOperation().equals("plus")){
  107. result = calc.getOperand1() + calc.getOperand2();
  108. calc.setResult(result);
  109. calc.setSuccess(true);
  110.  
  111. System.out.println("[CalcAgentPlus] Calculation result: " +
  112. calc.getOperand1() + " + " + calc.getOperand2() + " = " + calc.getResult());
  113.  
  114. }else if (calc.getOperation().equals("minus")){
  115. result = calc.getOperand1() - calc.getOperand2();
  116. calc.setResult(result);
  117. calc.setSuccess(true);
  118.  
  119. System.out.println("[CalcAgentPlus] Calculation result: " +
  120. calc.getOperand1() + " - " + calc.getOperand2() + " = " + calc.getResult());
  121.  
  122. }else if (calc.getOperation().equals("divide")){
  123. result = calc.getOperand1() / calc.getOperand2();
  124. calc.setResult(result);
  125. calc.setSuccess(true);
  126.  
  127. System.out.println("[CalcAgentPlus] Calculation result: " +
  128. calc.getOperand1() + " / " + calc.getOperand2() + " = " + calc.getResult());
  129.  
  130. }else if (calc.getOperation().equals("multiply")){
  131. result = calc.getOperand1() * calc.getOperand2();
  132. calc.setResult(result);
  133. calc.setSuccess(true);
  134.  
  135. System.out.println("[CalcAgentPlus] Calculation result: " +
  136. calc.getOperand1() + " x " + calc.getOperand2() + " = " + calc.getResult());
  137. }else{
  138. System.out.println("Undefined operation: "+ calc.getOperation());
  139. calc.setSuccess(false);
  140. }
  141.  
  142.  
  143.  
  144. String strObj = "";
  145. try
  146. {
  147. strObj = serializeObjectToString(calc);
  148. }
  149. catch (Exception ex)
  150. {
  151. System.out.println("\n[CalcAgentPlus] ObjToStr conversion error: " + ex.getMessage());
  152. }
  153.  
  154. if (calc.isSuccess()){
  155. ACLMessage reply = msg.createReply();
  156. reply.setPerformative(ACLMessage.INFORM);
  157. reply.setContent(strObj);
  158. send(reply);
  159. }else{
  160. ACLMessage reply = msg.createReply();
  161. reply.setPerformative(ACLMessage.REFUSE);
  162. reply.setContent(strObj);
  163. send(reply);
  164. }
  165.  
  166. ACLMessage reply = new ACLMessage(ACLMessage.INFORM);
  167.  
  168. reply.addReceiver(msg.getSender()); //get from envelope
  169.  
  170. reply.setContent(strObj);
  171. send(reply);
  172.  
  173. System.out.println("\n[CalcAgentPlus] Sending Message!");
  174. System.out.println("[CalcAgentPlus] Receiver Agent : " + msg.getSender());
  175. System.out.println("[CalcAgentPlus] Message content [Base64 string]: " + msg.getContent());
  176. }
  177.  
  178. System.out.println("[CalcAgentPlus] CyclicBehaviour Block");
  179. block();
  180. }
  181. });
  182. }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement