# 2
Now I'll post my code, If anyone knows how to serialize (and then deseserialize) the class "AgentMessage" , and its subclasses "StaticPart" and "DynElement" from the main class , I'll be grateful.. It's about 2 days I'm working on it and I continue getting "nonserialializable exception" ...
thank you guys
package agentsLibrary;
//some imports
public class AgentMessage implements Serializable{
private static final long serialVersionUID = 1;
private StaticPart sp;
private DynElement de;
public AgentMessage(String agent,byte[] code,String mainclass,Certificate signerId,PrivateKey priv,String configuration,Serializable dclear,byte[] dsecret,PathEl[] dpath,byte[] c){ //costruttore
sp=new StaticPart(agent,code,mainclass,signerId,priv,configuration);
de=new DynElement(dclear,dsecret,c,dpath);
}
public StaticPart getSp(){
return sp;
}
public DynElement getDe(){
return de;
}
private void writeObject(java.io.ObjectOutputStream out) throws IOException{
System.out.println("class implements writeObject( )");
//depends on the method to store out all the important state
//out.defaultWriteObject();//perform the default serialization(va sempre fatto il default)
out.writeObject(sp);
out.writeObject(de);
}
private void readObject(java.io.ObjectInputStream in)throws ClassNotFoundException, IOException {
System.out.println("class implements readObject( )");
//in.defaultReadObject();
sp=(StaticPart)in.readObject();
de=(DynElement)in.readObject();
}
}
*******************************************************
package agentsLibrary;
public class StaticPart implements Serializable{
private static final long serialVersionUID = 1;
private String agent="";
private byte[] code=null;
private String mainclass="";
private Certificate signerid=null;
private PrivateKey priv=null;
private Calendar timestamp=null;
private Signature sig=null;
private byte[] buffertotale=null;
private byte[] firma=null;
private String configuration="";
public StaticPart(String agent, byte[] code, String mainclass, Certificate signerid, PrivateKey priv,String configuration){
this.configuration=configuration;
this.code=code;
this.mainclass=mainclass;
this.agent=agent;
this.priv=priv;
this.signerid=signerid;
timestamp=Calendar.getInstance();
Date time=new Date();
time=timestamp.getTime();//Gets this Calendar's current time.
try {
byte[] nomeagent=agent.getBytes("8859_1");//converto stringa
byte[] mainclas=mainclass.getBytes("8859_1");//converto stringa
byte[] signer=signerid.getEncoded();
byte[] priva=priv.getEncoded();
byte[] dat=null;
dat=time.toString().getBytes("8859_1");
buffertotale=new byte[nomeagent.length+mainclas.length+signer.length+priva.length+dat.length+code.length];
System.arraycopy(nomeagent, 0, buffertotale, 0, nomeagent.length);
System.arraycopy(code, 0, buffertotale, nomeagent.length, code.length);
System.arraycopy(mainclas, 0, buffertotale, code.length, mainclas.length);
System.arraycopy(signer, 0, buffertotale, mainclas.length, signer.length);
System.arraycopy(priva, 0, buffertotale, signer.length, priva.length);
System.arraycopy(dat, 0, buffertotale, priva.length, dat.length);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
System.exit(1);
} catch (CertificateEncodingException e) {
e.printStackTrace();
System.exit(1);
}
try {
sig = Signature.getInstance(priv.getAlgorithm());
sig.initSign(priv);
sig.update(buffertotale, 0, buffertotale.length);
firma=sig.sign();
} catch (SignatureException e) {
e.printStackTrace();
System.exit(1);
} catch (InvalidKeyException e) {
e.printStackTrace();
System.exit(1);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
System.exit(1);
}
}
public boolean verify() {
try{
PublicKey pub=signerid.getPublicKey();
Signature sig = Signature.getInstance(pub.getAlgorithm());
sig.initVerify(pub);
sig.update(buffertotale, 0, buffertotale.length);
return sig.verify(firma);
} catch (SignatureException e) {
e.printStackTrace();
System.exit(1);
} catch (InvalidKeyException e) {
e.printStackTrace();
System.exit(1);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
System.exit(1);
}
return false;
}
public String getAgentName() {
return agent;
}
public byte[] getCode() {
return code;
}
public String getClassName() {
return mainclass;
}
public PrivateKey getPrivate() {
return priv;
}
public Certificate getId() {
return signerid;
}
public byte[] getSignature(){
return firma;
}
public String getConfiguration(){
return configuration;
}
}
***********************************************
package agentsLibrary;
import java.io.Serializable;
public class DynElement implements java.io.Serializable{
private static final long serialVersionUID = 1;
private Serializable dclear=null;
private byte[] dsecret=null;
private PathEl[] dpath=null;
private byte[]c=null;
public DynElement(Serializable dclear,byte[] dsecret,byte[]c,PathEl[] dpath){
this.dclear=dclear;
this.dsecret=dsecret;
this.c=c;
this.dpath=dpath;
}
public byte[] getC() {
return c;
}
public Serializable getDclear() {
return dclear;
}
public PathEl[] getDpath() {
return dpath;
}
public byte[] getDsecret() {
return dsecret;
}
}
*********************************************
//finally the following is the main class that should serialize
....
AgentMessage msg=new AgentMessage(name,code,mainclass,signerId,privata,configuration,dclear,dsecret,dpath,c);
try {
System.out.println("Sending Agent Message to Server "+ip+":"+port);
Socket s = new Socket(ip,port);
ObjectOutputStream out=new ObjectOutputStream(s.getOutputStream());
out.writeObject(msg);
out.flush();
s.close();
} catch (Exception e) {
return false;
}
return true;
}