/*****************************************************************
JADE - Java Agent DEvelopment Framework is a framework to develop 
multi-agent systems in compliance with the FIPA specifications.
Copyright (C) 2000 CSELT S.p.A. 

GNU Lesser General Public License

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, 
version 2.1 of the License. 

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA  02111-1307, USA.
*****************************************************************/

package examples.Base64;

import jade.lang.acl.ACLMessage;
import jade.core.Agent;
import jade.domain.AgentManagementOntology;

import java.util.*;
import java.io.*;

/**
This agent makes the following task:
1. searches in the DF for an ObjectReaderAgent;
2. sends an ACLMessage with a content encoded in Base64 to the 
   ObjectReaderAgent.
@author Fabio Bellifemine - CSELT S.p.A
@version $Date$ $Revision$
*/

public class ObjectWriterAgent extends Agent {


protected void setup() {

  System.out.println(getLocalName()+" agent sends an ACLMessage whose content is a set of Java objects");

  /** Search with the DF for the name of the ObjectReaderAgent **/
  String reader = new String();
  AgentManagementOntology.DFAgentDescriptor dfd = new AgentManagementOntology.DFAgentDescriptor();    
  dfd.setType("ObjectReaderAgent"); 
  try {
    while (true) {
      System.out.println(getLocalName()+ " waiting for an ObjectReaderAgent registering with the DF");
      AgentManagementOntology.DFSearchResult result;
      Vector vc = new Vector(1);
      AgentManagementOntology.Constraint c = new AgentManagementOntology.Constraint();
      c.setName(AgentManagementOntology.Constraint.DFDEPTH);
      c.setFn(AgentManagementOntology.Constraint.MAX); // MIN
      c.setArg(3);
      vc.addElement(c);
      result = searchDF("DF",dfd,vc);
      Enumeration e = result.elements();
      if (e.hasMoreElements()) {
	dfd = (AgentManagementOntology.DFAgentDescriptor)e.nextElement();
	reader = dfd.getName();
	break;
      } 
      Thread.sleep(10000);
    }
  } catch (Exception fe) {
    System.err.println(getLocalName()+" search with DF is not succeeded because of " + fe.getMessage());
    doDelete();
    }

    try {
      ACLMessage msg = new ACLMessage(ACLMessage.INFORM);

      msg.addDest(reader);

      ByteArrayOutputStream c = new ByteArrayOutputStream();
      ObjectOutputStream oos = new ObjectOutputStream(c);

      Person p = new Person("JADE", "CSELT", new Date(), 2);
      oos.writeObject(p);
      System.out.println(getLocalName()+" written "+p.toString());
      oos.writeObject("Today");
      System.out.println(getLocalName()+" written Today");
      oos.writeObject(new Integer(12345));
      System.out.println(getLocalName()+" written 12345");
      Date d = new Date();
      oos.writeObject(d);
      System.out.println(getLocalName()+" written "+d.toString());
      oos.flush();
      
      msg.setContentBase64(c.toByteArray());
      send(msg);
      System.out.println(getLocalName()+" sent a message to "+reader);
    } catch (IOException e ) {
      e.printStackTrace();
    }
    doDelete();
  }
}


@@ -1560,15 +1560,8 @@
 s a 
-set of 
 Java
@@ -1567,17 +1567,16 @@
 a object
-s
 %22);%0A%0A  /
@@ -2692,17 +2692,16 @@
   %7D%0A%0A   
- 
 try %7B%0A  
@@ -2795,659 +2795,99 @@
     
-ByteArrayOutputStream c = new ByteArrayOutputStream();%0A      ObjectOutputStream oos = new ObjectOutputStream(c);%0A%0A      Person p = new Person(%22JADE%22, %22CSELT%22, new Date(), 2);%0A      oos.writeObject(p);%0A      System.out.println(getLocalName()+%22 written %22+p.toString());%0A      oos.writeObject(%22Today%22);%0A      System.out.println(getLocalName()+%22 written Today%22);%0A      oos.writeObject(new Integer(12345));%0A      System.out.println(getLocalName()+%22 written 12345%22);%0A      Date d = new Date();%0A      oos.writeObject(d);%0A      System.out.println(getLocalName()+%22 written %22+d.toString());%0A      oos.flush();%0A      %0A      msg.setContentBase64(c.toByteArray());
+Person p = new Person(%22JADE%22, %22CSELT%22, new Date(), 2);%0A      msg.setContentObject(p);%0A     
 %0A   
@@ -2971,18 +2971,16 @@
 eader);%0A
-  
   %7D catc
@@ -3004,18 +3004,16 @@
 ) %7B%0A    
-  
 e.printS
@@ -3024,21 +3024,16 @@
 Trace();
-%0A    
 %7D%0A    do
@@ -3047,10 +3047,8 @@
 );%0A  %7D%0A%7D
-%0A%0A

update the example in order to use the new method setContentObject()