• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't initiate a Grasshopper agent PLEASE HELP!!  RSS feed

 
Cathy Lo
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm quite new to Grasshopper mobile agent pltaform and trying to write a customer agent of type SmartServer. This agent should get the price and descritption of an Item to be purchased as input arguments. It then loads a keystore called "customer" with a password "project" to obtain the required keys (the keystore is in the same directory as the agent) . It should then prepare a purchase request and creates another agent (called CustomerAgent) to search for this item on vendors' sites and purchase if suitable offer found. To test the agent, I initiated a region registry and then I initiated an agency that is registered to the region. I then tried to create the agent within agency with (30 jumper) as input arguments. The problem is that I'm getting "exception in agent initialisation" every time I try to create the customer agent!! can anyone help me on that please?? I can't go further in my project without solving this problem and I'm running out of time!! the code for Customer agent is attached .Thank you very much in advance.

package customer;

import de.ikv.grasshopper.agency.IAgentSystem;
import de.ikv.grasshopper.agent.SmartServer;
import de.ikv.grasshopper.type.AgentInfo;
import de.ikv.grasshopper.type.Identifier;
import java.io.*;
import java.math.BigInteger;
import java.security.*;
import java.security.cert.X509Certificate;
import java.security.interfaces.DSAPrivateKey;
import java.security.interfaces.DSAPublicKey;
import util.*;

// Referenced classes of package project.agents:
// CustomerAgentI, CustomerI

public class Customer extends SmartServer
implements CustomerI
{

public Customer()
{
pw = new char[7];
}

public synchronized void init(Object creationArgs[])
{
try
{
ks = KeyStore.getInstance("JKS");
FileInputStream fis = new FileInputStream("customer");
pw[0] = 'p';
pw[1] = 'r';
pw[2] = 'o';
pw[3] = 'j';
pw[4] = 'e';
pw[5] = 'c';
pw[6] = 't';
BufferedInputStream bis = new BufferedInputStream(fis);
ks.load(bis, pw);
bis.close();
state = 0;
}
catch(Exception e)
{
throw new RuntimeException(e.getMessage());
}
price = ((Integer)creationArgs[0]).intValue();
description = (String) creationArgs[1];
}

private int price;
private String description;

public synchronized void live()
{
switch(state)
{
default:
break;

case 0: // '\0'
sig = null;
try
{
Security.addProvider(new ProjectProvider());
sig = Signature.getInstance("SDSS1");
}
catch(Exception e)
{
log(e.getMessage());
return;
}
getKeys();
state = 1;
// fall through

case 1: // '\001'
getReqs();
log("in await req.");
live();
break;

case 2: // '\002'
preparePurchase();
live();
break;

case 5: // '\005'
KeyRes keyR = new KeyRes(transId, expiry, req, idAgent);
byte keyRes[] = keyR.getBytes();
byte mySig[] = sign(keyRes);
if(mySig == null)
{
log("Error while signing key restrictions.");
live();
break;
}
pair = KeySharePairGenerator.generateKeyShares(proxy, idAgent, idTTP);
agentItem = new Item(myCert, keyR, mySig, pair.getAgentShare());
ttpItem = new Item(myCert, keyR, mySig, pair.getTTPShare());
byte agentBytes[] = agentItem.getBytes();
byte ttpBytes[] = signcrypt(ttpItem.getBytes());
if(ttpBytes == null)
{
log("Error while signcrypting ttp message.");
live();
} else
{
agentProxy.setRequirements(agentBytes, ttpBytes, ttpKey, myCert);
state = 6;
live();
}
break;

case 6: // '\006'
notify();
try
{
wait();
}
catch(Exception e)
{
System.err.println(e.getMessage());
e.printStackTrace();
state = 3;
live();
break;
}
live();
break;

case 4: // '\004'
log(errorMessage);
state = 3;
live();
break;

case 3: // '\003'
log("Bye bye...");
removeMe();
break;
}
}

private void preparePurchase()
{
proxy = new ProxyKey(myKey);
IAgentSystem system = getAgentSystem();
AgentInfo aInfo = null;
agentProxy = null;
try
{
Object args[] = new Object[1];
args[0] = getInfo();
aInfo = system.createAgent("project.agents.CustomerAgent", "", "", args);
agentProxy = (CustomerAgentI)getAgentProxy(aInfo, CustomerAgentI.class);
}
catch(Exception e)
{
state = 4;
errorMessage = e.getMessage();
e.printStackTrace();
return;
}
idAgent = aInfo.getIdentifier().toByteArray();
idTTP = null;
try
{
idTTP = (new String("buendgf1@cs.man.ac.uk")).getBytes("UTF-8");
}
catch(Exception e)
{
state = 4;
errorMessage = e.getMessage();
return;
}
expiry = System.currentTimeMillis();
transId = new String("" + expiry);
expiry += 0xdbba0L;
state = 5;
}

private void getReqs()
{
byte myId[] = getIdentifier().toByteArray();
req = new Requirements(description, price, myId);
state = 2;
log("reqs made");
}

private byte[] signcrypt(byte[] input){
try{
Signcryption signcryption = new Signcryption();
signcryption.initSigncryption(ttpKey, myKey);
return signcryption.signcrypt(input);
}
catch(Exception e){
state = CustomerStates.INTERNAL_ERROR;
errorMessage = e.getMessage();
return null;
}
}




private byte[] sign(byte[] input){
try{
sig.initSign(myKey);
sig.update(input, 0, input.length);
return sig.sign();
}
catch(Exception e){
state = CustomerStates.INTERNAL_ERROR;
errorMessage = e.getMessage();
return null;
}
}


private void getKeys()
{
try
{
myCert = (X509Certificate)ks.getCertificate("purchase");
ttpCert = (X509Certificate)ks.getCertificate("ttp");
ttpKey = (DSAPublicKey)ttpCert.getPublicKey();
myKey = (DSAPrivateKey)ks.getKey("purchase", pw);
}
catch(Exception e)
{
state = 4;
errorMessage = e.getMessage();
}
}

public synchronized void setTransaction(byte transaction[], byte ttpSig[], BigInteger R, X509Certificate bCert)
{
do
{
if(state == 6)
break;
try
{
wait();
}
catch(Exception e)
{
state = 4;
notify();
return;
}
} while(true);
if(transaction == null)
{
state = 3;
System.err.println("No verification token received.");
notify();
return;
}
DSAPublicKey verKey;
byte signcryption[];
boolean genuine;
PartialSigncryption partSigncryption;
byte plainText[];
Contract contract;
try
{
verKey = PartialSigncryption.getVerificationKey((TTPKeyShare)ttpItem.getKeyShare(), (DSAPublicKey)myCert.getPublicKey());
VerificationToken token = VerificationToken.getVerificationToken(transaction);
signcryption = token.getSigncryption();
sig.initVerify(ttpCert);
sig.update(transaction);
boolean verified = sig.verify(ttpSig);
if(!verified)
{
System.err.println("TTP signatire on received token invalid. ");
state = 3;
notify();
return;
}
sig.initVerify(verKey);
sig.update(PartialSigncryption.getC(signcryption));
genuine = sig.verify(PartialSigncryption.getSignature(signcryption));
if(!genuine)
{
System.err.println("Proxy signature not valid");
state = 3;
notify();
return;
}
partSigncryption = new PartialSigncryption();
plainText = partSigncryption.getPlainText((DSAPublicKey)bCert.getPublicKey(), R, signcryption);
contract = Contract.getContract(plainText);
System.err.println(contract.toString());
state = 3;
notify();
return;
}
catch(Exception e)
{
System.err.println(e.getMessage());
e.printStackTrace();
state = 3;
notify();
return;
}



}

private KeyStore ks;
private DSAPublicKey ttpKey;
private DSAPrivateKey myKey;
private ProxyKey proxy;
private KeySharePair pair;
private Item agentItem;
private Item ttpItem;
private Signature sig;
private byte state;
private char pw[];
private X509Certificate myCert;
private X509Certificate ttpCert;
private String des;
private int maxPrice;
private Requirements req;
private String errorMessage;
private CustomerAgentI agentProxy;
byte idAgent[];
byte idTTP[];
private long expiry;
private String transId;
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!