Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.16 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 CalcSender extends Agent
  32. {
  33. static final Base64 base64 = new Base64();
  34. String operator = "";
  35. int operand1 = 0;
  36. int operand2 = 0;
  37.  
  38. //object to string
  39. public String serializeObjectToString(Object object) throws IOException
  40. {
  41. String s = null;
  42.  
  43. try
  44. {
  45. ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
  46. GZIPOutputStream gzipOutputStream = new GZIPOutputStream(arrayOutputStream);
  47. ObjectOutputStream objectOutputStream = new ObjectOutputStream(gzipOutputStream);
  48.  
  49. objectOutputStream.writeObject(object);
  50. objectOutputStream.flush();
  51. gzipOutputStream.close();
  52.  
  53. objectOutputStream.flush();
  54. objectOutputStream.close();
  55.  
  56. s = new String(base64.encode(arrayOutputStream.toByteArray()));
  57. arrayOutputStream.flush();
  58. arrayOutputStream.close();
  59. }
  60. catch(Exception ex){}
  61.  
  62. return s;
  63. }
  64.  
  65. //string to object
  66. public Object deserializeObjectFromString(String objectString) throws IOException, ClassNotFoundException
  67. {
  68. Object obj = null;
  69. try
  70. {
  71. ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(base64.decode(objectString));
  72. GZIPInputStream gzipInputStream = new GZIPInputStream(arrayInputStream);
  73. ObjectInputStream objectInputStream = new ObjectInputStream(gzipInputStream);
  74. obj = objectInputStream.readObject();
  75.  
  76. objectInputStream.close();
  77. gzipInputStream.close();
  78. arrayInputStream.close();
  79. }
  80. catch(Exception ex){}
  81. return obj;
  82. }
  83.  
  84. protected void setup()
  85. {
  86. //for receiving calculation result
  87. addBehaviour(new SimpleBehaviour(this)
  88. {
  89. boolean done = false;
  90.  
  91. public void action()
  92. {
  93. ACLMessage msg = blockingReceive();
  94.  
  95. if (msg != null) {
  96. System.out.println("\n[CalcSender] Message received!");
  97. System.out.println("[CalcSender] Sender Agent : " + msg.getSender());
  98.  
  99. String msgContent = msg.getContent();
  100. System.out.println("[CalcSender] Message content [Base64 string]: " + msgContent);
  101.  
  102. try
  103. {
  104. Calculation calc = (Calculation)deserializeObjectFromString(msgContent);
  105.  
  106. if (calc.isSuccess()) {
  107. System.out.println("\n[CalcSender] Calculation - operation: " + calc.getOperation());
  108. System.out.println("[CalcSender] Calculation - operand1 : " + calc.getOperand1());
  109. System.out.println("[CalcSender] Calculation - operand2 : " + calc.getOperand2());
  110. System.out.println("[CalcSender] Calculation - result : " + calc.getResult());
  111. } else {
  112. System.out.println("\n[CalcSender] Calculation - operation: " + calc.getInfo());
  113. System.out.println("\n[CalcSender] Msg performative: " + ACLMessage.getPerformative(msg.getPerformative()));
  114. }
  115. }
  116. catch(Exception ex)
  117. {
  118. System.out.println("\n[CalcSender] StrToObj conversion error: " + ex.getMessage());
  119. }
  120.  
  121. done = true;
  122. }
  123. else
  124. {
  125. System.out.println("\n[CalcSender] Msg null");
  126. }
  127. }
  128.  
  129. public boolean done()
  130. {
  131. if (done)
  132. {
  133. System.out.println("\n[CalcSender] SimpleBehaviour terminated!");
  134. }
  135.  
  136. return done;
  137. }
  138. });
  139.  
  140. Object[] args = getArguments();
  141. String operation = (String)args[0];
  142. int operand1 = Integer.parseInt((String)args[1]);
  143. int operand2 = Integer.parseInt((String)args[2]);
  144. //Send messages to "cap - CalcAgentPlus"
  145. //
  146. Calculation calc = new Calculation();
  147. calc.setOperation(operation);
  148. calc.setOperand1(operand1);
  149. calc.setOperand2(operand2);
  150.  
  151. String strObj = "";
  152. try
  153. {
  154. strObj = serializeObjectToString(calc);
  155. }
  156. catch (Exception ex)
  157. {
  158. System.out.println("\n[CalcSender] ObjToStr conversion error: " + ex.getMessage());
  159. }
  160. ACLMessage msg = new ACLMessage(ACLMessage.REQUEST);
  161. msg.setContent(strObj);
  162. msg.addReceiver(new AID("cap", AID.ISLOCALNAME));
  163. send(msg);
  164.  
  165. System.out.println("\n[CalcSender] Sending Message!");
  166. System.out.println("[CalcSender] Receiver Agent : cap");
  167. System.out.println("[CalcSender] Message content [Base64 string]: " + strObj);
  168. }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement