| package cloudTrading.seller; |
| import jade.core.Agent; |
| import jade.core.behaviours.; |
| import jade.lang.acl.ACLMessage; |
| import jade.lang.acl.MessageTemplate; |
| import jade.domain.; |
| import jade.domain.FIPAAgentManagement.; |
| import java.util.; |
| public class CloudSellerAgent extends Agent |
|
| protected void setup() |
| System.out.println("Seller-agent "+getAID().getName()+" is ready."); |
| myGui = new CloudSellerGuiImpl(); |
| myGui.setAgent(this); |
| myGui.show(); |
| addBehaviour(new CallForOfferServer()); |
| DFAgentDescription dfd = new DFAgentDescription(); |
| dfd.setName(getAID()); |
| ServiceDescription sd = new ServiceDescription(); |
| sd.setType("Cloud-selling"); |
| sd.setName(getLocalName()+"-cloud-selling"); |
| dfd.addServices(sd); |
| DFService.register(this, dfd); |
| protected void takeDown() |
| if (myGui != null) |
| myGui.dispose(); |
| System.out.println("Seller-agent "+getAID().getName()+"terminating."); |
| DFService.deregister(this); |
| public void putForSale(String title, float initPrice, float minPrice, Date deadline) |
| addBehaviour(new PriceManager(this, title, initPrice, minPrice, deadline)); |
| public class PriceManager extends TickerBehaviour |
| private PriceManager(Agent a, String t, float ip, float mp, Date d) |
| super(a, 60000); // tick every minute |
| title = t; |
| initPrice = ip; |
| currentPrice = initPrice; |
| deltaP = initPrice - mp; |
| deadline = d.getTime(); |
| initTime = System.currentTimeMillis(); |
| deltaT = ((deadline - initTime) > 0 ? (deadline - initTime): 60000); |
| bestPrice = initPrice; |
| public void onStart() |
| catalogue.put(title, this); |
| super.onStart(); |
| public void onTick() |
|
| long currentTime = System.currentTimeMillis(); |
| if (currentTime > deadline) |
| myGui.notifyUser("Cannot sell cloud"+title); |
| catalogue.remove(title); |
| stop(); |
| public float getCurrentPrice() |
| return currentPrice; |
| private class CallForOfferServer extends CyclicBehaviour |
|
| private MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.CFP); |
| public void action() |
| ACLMessage msg = myAgent.receive(mt); |
| if (msg != null) |
| cPrice = Float.valueOf(msg.getContent()); |
| myGui.notifyUser("Received Proposal to buy service at price"+cPrice); |
| ACLMessage reply = msg.createReply(); |
| pPrice = pcurrentPrice; |
| if (cPrice != 0.0) |
| if (msg.getPerformative() == ACLMessage.ACCEPT_PROPOSAL) |
| ACLMessage commit = new ACLMessage(ACLMessage.ACCEPT_PROPOSAL); |
| myAgent.send(commit); |
| myGui.notifyUser("SLA Commit at Price =" + bestPrice); |
| myGui.notifyUser("Cloud service successfully provisioned"); |
| elseif (cPrice < pPrice) |
| reply.setPerformative(ACLMessage.PROPOSE); |
| reply.setContent(String.valueOf(pPrice)); |
| else |
| reply.setPerformative(ACLMessage.ACCEPT_PROPOSAL); |
| reply.setContent(String.valueOf(cPrice)); |
| myGui.notifyUser("Accept Proposal at price"+ cPrice); |
| else |
| reply.setPerformative(ACLMessage.REFUSE); |
| myGui.notifyUser("Reject Proposal at price"+ cPrice); |
| myAgent.send(reply); |
| myGui.notifyUser(cPrice != 0.0 ? "Sent Proposal to sell at price "+reply.getContent(): |
| "Refused Proposal as the cloud is not for sale"); |
|