Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package examples.message;
- import jade.core.AID;
- import jade.core.Agent;
- import jade.core.behaviours.*;
- import jade.domain.DFService;
- import jade.domain.FIPAAgentManagement.DFAgentDescription;
- import jade.domain.FIPAAgentManagement.ServiceDescription;
- import jade.domain.FIPAException;
- import jade.lang.acl.*;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.util.zip.GZIPInputStream;
- import java.util.zip.GZIPOutputStream;
- import org.apache.commons.codec.binary.Base64;
- /**
- *
- * @author U
- */
- public class CalcSender extends Agent
- {
- static final Base64 base64 = new Base64();
- String operator = "";
- int operand1 = 0;
- int operand2 = 0;
- //object to string
- public String serializeObjectToString(Object object) throws IOException
- {
- String s = null;
- try
- {
- ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
- GZIPOutputStream gzipOutputStream = new GZIPOutputStream(arrayOutputStream);
- ObjectOutputStream objectOutputStream = new ObjectOutputStream(gzipOutputStream);
- objectOutputStream.writeObject(object);
- objectOutputStream.flush();
- gzipOutputStream.close();
- objectOutputStream.flush();
- objectOutputStream.close();
- s = new String(base64.encode(arrayOutputStream.toByteArray()));
- arrayOutputStream.flush();
- arrayOutputStream.close();
- }
- catch(Exception ex){}
- return s;
- }
- //string to object
- public Object deserializeObjectFromString(String objectString) throws IOException, ClassNotFoundException
- {
- Object obj = null;
- try
- {
- ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(base64.decode(objectString));
- GZIPInputStream gzipInputStream = new GZIPInputStream(arrayInputStream);
- ObjectInputStream objectInputStream = new ObjectInputStream(gzipInputStream);
- obj = objectInputStream.readObject();
- objectInputStream.close();
- gzipInputStream.close();
- arrayInputStream.close();
- }
- catch(Exception ex){}
- return obj;
- }
- protected void setup()
- {
- //for receiving calculation result
- addBehaviour(new SimpleBehaviour(this)
- {
- boolean done = false;
- public void action()
- {
- ACLMessage msg = blockingReceive();
- if (msg != null) {
- System.out.println("\n[CalcSender] Message received!");
- System.out.println("[CalcSender] Sender Agent : " + msg.getSender());
- String msgContent = msg.getContent();
- System.out.println("[CalcSender] Message content [Base64 string]: " + msgContent);
- try
- {
- Calculation calc = (Calculation)deserializeObjectFromString(msgContent);
- if (calc.isSuccess()) {
- System.out.println("\n[CalcSender] Calculation - operation: " + calc.getOperation());
- System.out.println("[CalcSender] Calculation - operand1 : " + calc.getOperand1());
- System.out.println("[CalcSender] Calculation - operand2 : " + calc.getOperand2());
- System.out.println("[CalcSender] Calculation - result : " + calc.getResult());
- } else {
- System.out.println("\n[CalcSender] Calculation - operation: " + calc.getInfo());
- System.out.println("\n[CalcSender] Msg performative: " + ACLMessage.getPerformative(msg.getPerformative()));
- }
- }
- catch(Exception ex)
- {
- System.out.println("\n[CalcSender] StrToObj conversion error: " + ex.getMessage());
- }
- done = true;
- }
- else
- {
- System.out.println("\n[CalcSender] Msg null");
- }
- }
- public boolean done()
- {
- if (done)
- {
- System.out.println("\n[CalcSender] SimpleBehaviour terminated!");
- }
- return done;
- }
- });
- Object[] args = getArguments();
- String operation = (String)args[0];
- int operand1 = Integer.parseInt((String)args[1]);
- int operand2 = Integer.parseInt((String)args[2]);
- //Send messages to "cap - CalcAgentPlus"
- //
- Calculation calc = new Calculation();
- calc.setOperation(operation);
- calc.setOperand1(operand1);
- calc.setOperand2(operand2);
- String strObj = "";
- try
- {
- strObj = serializeObjectToString(calc);
- }
- catch (Exception ex)
- {
- System.out.println("\n[CalcSender] ObjToStr conversion error: " + ex.getMessage());
- }
- ACLMessage msg = new ACLMessage(ACLMessage.REQUEST);
- msg.setContent(strObj);
- msg.addReceiver(new AID("cap", AID.ISLOCALNAME));
- send(msg);
- System.out.println("\n[CalcSender] Sending Message!");
- System.out.println("[CalcSender] Receiver Agent : cap");
- System.out.println("[CalcSender] Message content [Base64 string]: " + strObj);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement