From: <sul...@us...> - 2008-08-31 07:16:40
|
Revision: 238 http://gridsim.svn.sourceforge.net/gridsim/?rev=238&view=rev Author: sulistio Date: 2008-08-31 07:16:49 +0000 (Sun, 31 Aug 2008) Log Message: ----------- change from DOS formatting to UNIX Modified Paths: -------------- trunk/source/gridsim/auction/Auction.java trunk/source/gridsim/auction/AuctionObserver.java trunk/source/gridsim/auction/AuctionTags.java trunk/source/gridsim/auction/Auctioneer.java trunk/source/gridsim/auction/ContinuousDoubleAuction.java trunk/source/gridsim/auction/DoubleAuction.java trunk/source/gridsim/auction/DutchAuction.java trunk/source/gridsim/auction/EnglishAuction.java trunk/source/gridsim/auction/FirstPriceSealedBidAuction.java trunk/source/gridsim/auction/Message.java trunk/source/gridsim/auction/MessageAsk.java trunk/source/gridsim/auction/MessageBid.java trunk/source/gridsim/auction/MessageCallForBids.java trunk/source/gridsim/auction/MessageInformOutcome.java trunk/source/gridsim/auction/MessageInformStart.java trunk/source/gridsim/auction/MessageRejectBid.java trunk/source/gridsim/auction/MessageRejectCallForBid.java trunk/source/gridsim/auction/OneSidedAuction.java trunk/source/gridsim/auction/Responder.java trunk/source/gridsim/auction/ReverseDutchAuction.java trunk/source/gridsim/auction/ReverseEnglishAuction.java trunk/source/gridsim/auction/ReverseFirstPriceSealedBidAuction.java trunk/source/gridsim/datagrid/index/AbstractRC.java trunk/source/gridsim/datagrid/index/RegionalRC.java trunk/source/gridsim/net/RateControlledScheduler.java Modified: trunk/source/gridsim/auction/Auction.java =================================================================== --- trunk/source/gridsim/auction/Auction.java 2008-08-23 06:55:34 UTC (rev 237) +++ trunk/source/gridsim/auction/Auction.java 2008-08-31 07:16:49 UTC (rev 238) @@ -1,359 +1,359 @@ -/* - * Title: GridSim Toolkit - * Description: GridSim (Grid Simulation) Toolkit for Modeling and Simulation - * of Parallel and Distributed Systems such as Clusters and Grids - * Licence: GPL - http://www.gnu.org/copyleft/gpl.html - * - * Copyright (c) 2006, The University of Melbourne, Australia - */ -package gridsim.auction; - -import eduni.simjava.Sim_event; -import eduni.simjava.Sim_port; -import eduni.simjava.Sim_system; -import gridsim.GridSim; -import gridsim.GridSimTags; -import gridsim.IO_data; - -import java.util.Hashtable; -import java.util.LinkedList; - -/** - * This class represents an auction. This is class has generic - * attributes common to all auctions - * - * @author Marcos Dias de Assuncao - * @since GridSim Toolkit 4.0 - * @see gridsim.auction.OneSidedAuction - * @see gridsim.auction.DoubleAuction - * @see gridsim.auction.AuctionTags - */ -public abstract class Auction extends GridSim { - /* - * TODO: Maybe this class does not need to extend GridSim. It could extend - * just Sim_entity. However, to make my experiments easier, I ended up - * extending GridSim. - * - */ - - private int auctionID = -1; - - //starting simulation time - private double startingTime; - - //code of the kind of auction protocol - private int auctionProtocol; - - //additional attributes that need to be assigned to the auction - private Hashtable attributes; - - //list of current bidders engaged in the auction - private LinkedList bidders; - - //used to auto generate auction id - private static int currentID = 0; - - /* The AuctionPolicy will send messages on behalf of the auctioneer, therefore - * it is necessary to specify the auxtioneer's id, so the reply messages - * can be returned to the auctioneer - */ - private int auctioneerID = -1; - - /** - * The Auction output port. This port is mainly used to send - * messages generated by this Auction class. - * This is because an Auction class has to send messages - * on the auctioneer's behalf - */ - protected Sim_port outputPort = null; - - /** - * Default constructor - * @param auctionName name for the auction - * @param auctioneerID the ID of the auctioner because the auction sends messages - * on the auctioneer's behalf - * @param auctionProtocol an int representing the auction protocol - * @param output the auctioneer's output port - * @throws Exception - * @see gridsim.GridSim - */ - public Auction( String auctionName, int auctioneerID, - int auctionProtocol, Sim_port output)throws Exception { - super(auctionName); - this.auctioneerID = auctioneerID; - this.attributes = new Hashtable(); - this.outputPort = output; - setAuctionProtocol(auctionProtocol); - this.auctionID = Auction.genID(); - /* - * TODO: To create a report with information about the auction - */ - } - - /** - * Constructor - * @param auctionName name for the auction - * on the auctioneer's behalf - * @param auctionProtocol an int representing the auction protocol - * @throws Exception - * @see gridsim.GridSim - */ - public Auction( String auctionName, int auctionProtocol)throws Exception { - super(auctionName); - this.attributes = new Hashtable(); - setAuctionProtocol(auctionProtocol); - this.auctionID = Auction.genID(); - /* - * TODO: To create a report with information about the auction - */ - } - - /** - * Returns the auctioneer ID - * @return the ID of a GridSim entity - */ - public int getAuctioneerID(){ - return auctioneerID; - } - - /** - * Sets the id of the auctioneer responsible for this auction - * @param auctioneerID the auctioneer ID - * @return <tt>true</tt> if the id was properly set - */ - public boolean setAuctioneerID(int auctioneerID){ - if(auctioneerID >= 0){ - this.auctioneerID = auctioneerID; - return true; - } - return false; - } - - /** - * Sets the output port to be used by this auction. The output port - * is the auctioneer's output port since the auction sends messages - * on the auctioneer's behalf - * @param output the port to be used - * @return <tt>true</tt> if the output port was properly set - */ - public boolean setOutputPort(Sim_port output){ - if(output!=null){ - this.outputPort = output; - return true; - } - return false; - } - - // Used just to generate a unique ID - //TODO: It might not be necessary, the ID could be the entity's ID - private synchronized static int genID(){ - return currentID++; - } - - /** - * Sets the auction's ID manually - * @param id the id to be used by the auction - * @return <tt>true</tt> if the ID has been set; <tt>false</tt> otherwise. - */ - public boolean setAuctionID(int id) { - if(id < 0) - return false; - - auctionID = id; - return true; - } - - /** - * Returns the ID of this auction - * @return auction ID - */ - public int getAuctionID(){ - return auctionID; - } - - /** - * Sets a list of the bidders associated with this auction - * @param list of bidders. The IDs must be entities' ids - * @pre list != null - * @return <tt>true</tt> if the bidders were correctly set - */ - public boolean setBidders(LinkedList list){ - if(list == null) - return false; - - synchronized(this){ - this.bidders = list; - } - return true; - } - - /** - * Returns the list of bidders - * @return list of GridSim entities' IDs - */ - public LinkedList getBidders(){ - synchronized(this){ - return this.bidders; - } - } - - /** - * Returns the output port used by this auction - * to send messages - * @return the output port - */ - protected Sim_port getOutputPort(){ - return this.outputPort; - } - - /** - * Sets an attribute to this auction. Anything additional has to - * be set as an attribute of the auction. - * @param key the key used to retrieve the value of the attribute - * @param value the value of the attribute - * @pre key != null && value !=null - * @return <tt>true</tt> if the value of the attribute was correctly set - */ - public boolean setAttribute(Object key, Object value){ - if(key == null || value == null) - return false; - - attributes.put(key, value); - return true; - } - - /** - * Returns a Hashtable with the attributes defined in the - * auction. - * @return the attributes defined in this auction - */ - protected Hashtable getAttributes(){ - return attributes; - } - - /** - * Returns a given attribute of the auction - * @param key used to retrieve the attribute - * @return the attribute - */ - public Object getAttribute(Object key){ - return attributes.get(key); - } - - /** - * Sets the auction protocol used by the auction - * @param protocol - * @pre protocol > 0 - * @return <tt>true</tt> if the the auction protocol was properly set - */ - protected boolean setAuctionProtocol(int protocol){ - if(protocol <= 0) - return false; - - auctionProtocol = protocol; - return true; - } - - /** - * Returns the auction protocol - * @return int representing the auction protocol - */ - protected int getAuctionProtocol(){ - return auctionProtocol; - } - - /** - * Sets the initial time of the auction - * @param time is the simulation time - * @pre time >= 0.0D - * @return <tt>true</tt> if the starting time was properly set - */ - protected boolean setStartingTime(double time){ - if(time < 0) - return false; - - startingTime = time; - return true; - } - - /** - * Returns the initial time of the auction - * @return the simulation time in which the auction started - */ - public double getStartingTime(){ - return startingTime; - } - - /** - * Brodcasts a message to all bidders engaged in the auction - * @param msg Message to be broadcast - * @pre Message != null - * @return <tt>true</tt> if the message was properly broadcast - */ - protected synchronized boolean broadcastMessage(Message msg){ - if(msg == null) - return false; - - msg.setSourceID(this.auctioneerID); - msg.setDestinationID(Message.TO_ALL_BIDDERS); - msg.setAuctionID(this.auctionID); - - int tag = -1; - int nBidders = bidders.size(); - if( msg instanceof MessageCallForBids){ - tag = AuctionTags.AUCTION_CFP; - } - else if( msg instanceof MessageInformStart){ - tag = AuctionTags.AUCTION_INFORM_START; - } - else if( msg instanceof MessageInformOutcome){ - tag = AuctionTags.AUCTION_INFORM_OUTCOME; - } - - /* TODO: - * For now, we are assuming that every message has a size of about 100 bytes. - * It would be better to consider some FIPA's encoding schema, for example. - * Please see: www.fipa.org - */ - for(int i=0; i<nBidders; i++){ - int destId = ((Integer)bidders.get(i)).intValue(); - super.sim_schedule(this.outputPort, GridSimTags.SCHEDULE_NOW, - tag, new IO_data(msg, 100, destId)); - } - - return true; - } - - /** - * - */ - public void body(){ - // Process events until END_OF_SIMULATION is received from the - // GridSimShutdown Entity - - Sim_event ev = new Sim_event(); - while ( Sim_system.running() ) - { - super.sim_get_next(ev); - - // if the simulation finishes then exit the loop - if (ev.get_tag() == GridSimTags.END_OF_SIMULATION || - ev.get_tag() == AuctionTags.END_OF_AUCTION){ - break; - } - - // process the received event - processEvent(ev); - } - - // remove I/O entities created during construction of this entity - super.terminateIOEntities(); - } - - protected abstract void processEvent(Sim_event ev); - - /** - * - */ - public abstract void startAuction(); -} +/* + * Title: GridSim Toolkit + * Description: GridSim (Grid Simulation) Toolkit for Modeling and Simulation + * of Parallel and Distributed Systems such as Clusters and Grids + * Licence: GPL - http://www.gnu.org/copyleft/gpl.html + * + * Copyright (c) 2006, The University of Melbourne, Australia + */ +package gridsim.auction; + +import eduni.simjava.Sim_event; +import eduni.simjava.Sim_port; +import eduni.simjava.Sim_system; +import gridsim.GridSim; +import gridsim.GridSimTags; +import gridsim.IO_data; + +import java.util.Hashtable; +import java.util.LinkedList; + +/** + * This class represents an auction. This is class has generic + * attributes common to all auctions + * + * @author Marcos Dias de Assuncao + * @since GridSim Toolkit 4.0 + * @see gridsim.auction.OneSidedAuction + * @see gridsim.auction.DoubleAuction + * @see gridsim.auction.AuctionTags + */ +public abstract class Auction extends GridSim { + /* + * TODO: Maybe this class does not need to extend GridSim. It could extend + * just Sim_entity. However, to make my experiments easier, I ended up + * extending GridSim. + * + */ + + private int auctionID = -1; + + //starting simulation time + private double startingTime; + + //code of the kind of auction protocol + private int auctionProtocol; + + //additional attributes that need to be assigned to the auction + private Hashtable attributes; + + //list of current bidders engaged in the auction + private LinkedList bidders; + + //used to auto generate auction id + private static int currentID = 0; + + /* The AuctionPolicy will send messages on behalf of the auctioneer, therefore + * it is necessary to specify the auxtioneer's id, so the reply messages + * can be returned to the auctioneer + */ + private int auctioneerID = -1; + + /** + * The Auction output port. This port is mainly used to send + * messages generated by this Auction class. + * This is because an Auction class has to send messages + * on the auctioneer's behalf + */ + protected Sim_port outputPort = null; + + /** + * Default constructor + * @param auctionName name for the auction + * @param auctioneerID the ID of the auctioner because the auction sends messages + * on the auctioneer's behalf + * @param auctionProtocol an int representing the auction protocol + * @param output the auctioneer's output port + * @throws Exception + * @see gridsim.GridSim + */ + public Auction( String auctionName, int auctioneerID, + int auctionProtocol, Sim_port output)throws Exception { + super(auctionName); + this.auctioneerID = auctioneerID; + this.attributes = new Hashtable(); + this.outputPort = output; + setAuctionProtocol(auctionProtocol); + this.auctionID = Auction.genID(); + /* + * TODO: To create a report with information about the auction + */ + } + + /** + * Constructor + * @param auctionName name for the auction + * on the auctioneer's behalf + * @param auctionProtocol an int representing the auction protocol + * @throws Exception + * @see gridsim.GridSim + */ + public Auction( String auctionName, int auctionProtocol)throws Exception { + super(auctionName); + this.attributes = new Hashtable(); + setAuctionProtocol(auctionProtocol); + this.auctionID = Auction.genID(); + /* + * TODO: To create a report with information about the auction + */ + } + + /** + * Returns the auctioneer ID + * @return the ID of a GridSim entity + */ + public int getAuctioneerID(){ + return auctioneerID; + } + + /** + * Sets the id of the auctioneer responsible for this auction + * @param auctioneerID the auctioneer ID + * @return <tt>true</tt> if the id was properly set + */ + public boolean setAuctioneerID(int auctioneerID){ + if(auctioneerID >= 0){ + this.auctioneerID = auctioneerID; + return true; + } + return false; + } + + /** + * Sets the output port to be used by this auction. The output port + * is the auctioneer's output port since the auction sends messages + * on the auctioneer's behalf + * @param output the port to be used + * @return <tt>true</tt> if the output port was properly set + */ + public boolean setOutputPort(Sim_port output){ + if(output!=null){ + this.outputPort = output; + return true; + } + return false; + } + + // Used just to generate a unique ID + //TODO: It might not be necessary, the ID could be the entity's ID + private synchronized static int genID(){ + return currentID++; + } + + /** + * Sets the auction's ID manually + * @param id the id to be used by the auction + * @return <tt>true</tt> if the ID has been set; <tt>false</tt> otherwise. + */ + public boolean setAuctionID(int id) { + if(id < 0) + return false; + + auctionID = id; + return true; + } + + /** + * Returns the ID of this auction + * @return auction ID + */ + public int getAuctionID(){ + return auctionID; + } + + /** + * Sets a list of the bidders associated with this auction + * @param list of bidders. The IDs must be entities' ids + * @pre list != null + * @return <tt>true</tt> if the bidders were correctly set + */ + public boolean setBidders(LinkedList list){ + if(list == null) + return false; + + synchronized(this){ + this.bidders = list; + } + return true; + } + + /** + * Returns the list of bidders + * @return list of GridSim entities' IDs + */ + public LinkedList getBidders(){ + synchronized(this){ + return this.bidders; + } + } + + /** + * Returns the output port used by this auction + * to send messages + * @return the output port + */ + protected Sim_port getOutputPort(){ + return this.outputPort; + } + + /** + * Sets an attribute to this auction. Anything additional has to + * be set as an attribute of the auction. + * @param key the key used to retrieve the value of the attribute + * @param value the value of the attribute + * @pre key != null && value !=null + * @return <tt>true</tt> if the value of the attribute was correctly set + */ + public boolean setAttribute(Object key, Object value){ + if(key == null || value == null) + return false; + + attributes.put(key, value); + return true; + } + + /** + * Returns a Hashtable with the attributes defined in the + * auction. + * @return the attributes defined in this auction + */ + protected Hashtable getAttributes(){ + return attributes; + } + + /** + * Returns a given attribute of the auction + * @param key used to retrieve the attribute + * @return the attribute + */ + public Object getAttribute(Object key){ + return attributes.get(key); + } + + /** + * Sets the auction protocol used by the auction + * @param protocol + * @pre protocol > 0 + * @return <tt>true</tt> if the the auction protocol was properly set + */ + protected boolean setAuctionProtocol(int protocol){ + if(protocol <= 0) + return false; + + auctionProtocol = protocol; + return true; + } + + /** + * Returns the auction protocol + * @return int representing the auction protocol + */ + protected int getAuctionProtocol(){ + return auctionProtocol; + } + + /** + * Sets the initial time of the auction + * @param time is the simulation time + * @pre time >= 0.0D + * @return <tt>true</tt> if the starting time was properly set + */ + protected boolean setStartingTime(double time){ + if(time < 0) + return false; + + startingTime = time; + return true; + } + + /** + * Returns the initial time of the auction + * @return the simulation time in which the auction started + */ + public double getStartingTime(){ + return startingTime; + } + + /** + * Brodcasts a message to all bidders engaged in the auction + * @param msg Message to be broadcast + * @pre Message != null + * @return <tt>true</tt> if the message was properly broadcast + */ + protected synchronized boolean broadcastMessage(Message msg){ + if(msg == null) + return false; + + msg.setSourceID(this.auctioneerID); + msg.setDestinationID(Message.TO_ALL_BIDDERS); + msg.setAuctionID(this.auctionID); + + int tag = -1; + int nBidders = bidders.size(); + if( msg instanceof MessageCallForBids){ + tag = AuctionTags.AUCTION_CFP; + } + else if( msg instanceof MessageInformStart){ + tag = AuctionTags.AUCTION_INFORM_START; + } + else if( msg instanceof MessageInformOutcome){ + tag = AuctionTags.AUCTION_INFORM_OUTCOME; + } + + /* TODO: + * For now, we are assuming that every message has a size of about 100 bytes. + * It would be better to consider some FIPA's encoding schema, for example. + * Please see: www.fipa.org + */ + for(int i=0; i<nBidders; i++){ + int destId = ((Integer)bidders.get(i)).intValue(); + super.sim_schedule(this.outputPort, GridSimTags.SCHEDULE_NOW, + tag, new IO_data(msg, 100, destId)); + } + + return true; + } + + /** + * + */ + public void body(){ + // Process events until END_OF_SIMULATION is received from the + // GridSimShutdown Entity + + Sim_event ev = new Sim_event(); + while ( Sim_system.running() ) + { + super.sim_get_next(ev); + + // if the simulation finishes then exit the loop + if (ev.get_tag() == GridSimTags.END_OF_SIMULATION || + ev.get_tag() == AuctionTags.END_OF_AUCTION){ + break; + } + + // process the received event + processEvent(ev); + } + + // remove I/O entities created during construction of this entity + super.terminateIOEntities(); + } + + protected abstract void processEvent(Sim_event ev); + + /** + * + */ + public abstract void startAuction(); +} Modified: trunk/source/gridsim/auction/AuctionObserver.java =================================================================== --- trunk/source/gridsim/auction/AuctionObserver.java 2008-08-23 06:55:34 UTC (rev 237) +++ trunk/source/gridsim/auction/AuctionObserver.java 2008-08-31 07:16:49 UTC (rev 238) @@ -1,197 +1,197 @@ -/* - * Title: GridSim Toolkit - * Description: GridSim (Grid Simulation) Toolkit for Modeling and Simulation - * of Parallel and Distributed Systems such as Clusters and Grids - * Licence: GPL - http://www.gnu.org/copyleft/gpl.html - * - * Copyright (c) 2006, The University of Melbourne, Australia - */ -package gridsim.auction; - -import eduni.simjava.Sim_event; -import eduni.simjava.Sim_port; -import eduni.simjava.Sim_system; -import gridsim.GridSim; -import gridsim.GridSimTags; -import gridsim.IO_data; - -/** - * This class is used by entities that want to participate - * as bidders in auctions. - * <p> - * To use this class, you need to redirect the events that - * the entity cannot treat. You need to call - * {@link #processEvent(Sim_event)} passing the event that - * the entity is not able to process. If the event is related - * to the auctions (ie. has a tag code corresponding to an auction event) - * the observer is able to process it by passing the message - * to its Responder. - * - * @author Marcos Dias de Assuncao - * @since GridSim Toolkit 4.0 - * @see gridsim.auction.Responder - */ -public class AuctionObserver extends GridSim { - private Responder responder; - private int bidderID; - private Object syncSteps = new Object(); - - /** The Bidder output port. This port is mainly used to send - * messages generated by this AuctionObserver class. - * This is because an AuctionObserver class doesn't have networked - * entities (Input and Output). - */ - protected Sim_port outputPort; - - /** - * Constructor - * @param bidderID the bidder if, since it sends messages on the bidder's behalf - * @param entityName a name for this entity - * @param port the port to be used as output of messages - * @throws Exception - */ - public AuctionObserver(int bidderID, String entityName, Sim_port port) throws Exception { - super(bidderID + "_" + entityName); - this.bidderID = bidderID; - this.outputPort = port; - } - - /** - * Constructor - * @param bidderID the bidder if, since it sends messages on the bidder's behalf - * @param entityName entityName a name for this entity - * @param responder the responder which will deal with the messages - * that this responder receives - * @param port the port to be used as output of messages - * @throws Exception - */ - public AuctionObserver(int bidderID, String entityName, Sim_port port, - Responder responder) throws Exception { - this(bidderID, entityName, port); - this.responder = responder; - } - - /** - * Sets a responder to this observer - * @param responder the responder - * @pre responder != null - * @return <tt>true</tt> if the responder was correctly set - */ - public boolean setResponder(Responder responder){ - if(responder == null) - return false; - - this.responder = responder; - return true; - } - - /** - * Returns the responder that the observer is using - * @return the responder - */ - public Responder getResponder(){ - return responder; - } - - /** - * - */ - public void body(){ - // Process events until END_OF_SIMULATION is received from the - // GridSimShutdown Entity - - Sim_event ev = new Sim_event(); - while ( Sim_system.running() ) - { - super.sim_get_next(ev); - - // if the simulation finishes then exit the loop - if (ev.get_tag() == GridSimTags.END_OF_SIMULATION){ - break; - } - - // process the received event - processEvent(ev); - } - - // remove I/O entities created during construction of this entity - super.terminateIOEntities(); - - } - - - /** - * Process an event. - * @param ev - * @return <tt>true</tt> if the event was treated; <tt>false</tt> otherwise. - */ - public boolean processEvent(Sim_event ev){ - int src_id = -1; - Message msg = null; - Message respMsg = null; - - if (responder == null){ - System.out.println("No responder to deal with auction messages!"); - return false; - } - - switch ( ev.get_tag() ){ - case AuctionTags.AUCTION_INFORM_START: - msg = (Message)ev.get_data(); - src_id = msg.getSourceID(); - synchronized(syncSteps){ - respMsg = responder.onReceiveStartAuction((MessageInformStart)msg); - } - break; - - case AuctionTags.AUCTION_CFP: - msg = (Message)ev.get_data(); - src_id = msg.getSourceID(); - synchronized(syncSteps){ - respMsg = responder.onReceiveCfb((MessageCallForBids)msg); - } - break; - - case AuctionTags.AUCTION_INFORM_OUTCOME: - msg = (Message)ev.get_data(); - src_id = msg.getSourceID(); - synchronized(syncSteps){ - respMsg = responder.onReceiveInformOutcome((MessageInformOutcome)msg); - } - break; - - case AuctionTags.AUCTION_REJECT_PROPOSAL: - msg = (Message)ev.get_data(); - src_id = msg.getSourceID(); - synchronized(syncSteps){ - respMsg = responder.onReceiveRejectProposal((MessageRejectBid)msg); - } - break; - - // other unknown tags are processed by this method - default: - return false; - } - - if(respMsg!=null){ - respMsg.setDestinationID(src_id); - - int tag = - 1; - if(respMsg instanceof MessageBid){ - tag = AuctionTags.AUCTION_PROPOSE; - ((MessageBid)respMsg).setBidder(this.bidderID); - } - else if(respMsg instanceof MessageRejectCallForBid){ - tag = AuctionTags.AUCTION_REJECT_CALL_FOR_BID; - ((MessageRejectCallForBid)respMsg).setBidder(this.bidderID); - } - - double scheduleAt = (respMsg.getScheduleTime() > 0.0) ? - respMsg.getScheduleTime() : GridSimTags.SCHEDULE_NOW; - - super.sim_schedule(this.outputPort, scheduleAt, - tag, new IO_data(respMsg, 100, src_id)); - } - return true; - } -} +/* + * Title: GridSim Toolkit + * Description: GridSim (Grid Simulation) Toolkit for Modeling and Simulation + * of Parallel and Distributed Systems such as Clusters and Grids + * Licence: GPL - http://www.gnu.org/copyleft/gpl.html + * + * Copyright (c) 2006, The University of Melbourne, Australia + */ +package gridsim.auction; + +import eduni.simjava.Sim_event; +import eduni.simjava.Sim_port; +import eduni.simjava.Sim_system; +import gridsim.GridSim; +import gridsim.GridSimTags; +import gridsim.IO_data; + +/** + * This class is used by entities that want to participate + * as bidders in auctions. + * <p> + * To use this class, you need to redirect the events that + * the entity cannot treat. You need to call + * {@link #processEvent(Sim_event)} passing the event that + * the entity is not able to process. If the event is related + * to the auctions (ie. has a tag code corresponding to an auction event) + * the observer is able to process it by passing the message + * to its Responder. + * + * @author Marcos Dias de Assuncao + * @since GridSim Toolkit 4.0 + * @see gridsim.auction.Responder + */ +public class AuctionObserver extends GridSim { + private Responder responder; + private int bidderID; + private Object syncSteps = new Object(); + + /** The Bidder output port. This port is mainly used to send + * messages generated by this AuctionObserver class. + * This is because an AuctionObserver class doesn't have networked + * entities (Input and Output). + */ + protected Sim_port outputPort; + + /** + * Constructor + * @param bidderID the bidder if, since it sends messages on the bidder's behalf + * @param entityName a name for this entity + * @param port the port to be used as output of messages + * @throws Exception + */ + public AuctionObserver(int bidderID, String entityName, Sim_port port) throws Exception { + super(bidderID + "_" + entityName); + this.bidderID = bidderID; + this.outputPort = port; + } + + /** + * Constructor + * @param bidderID the bidder if, since it sends messages on the bidder's behalf + * @param entityName entityName a name for this entity + * @param responder the responder which will deal with the messages + * that this responder receives + * @param port the port to be used as output of messages + * @throws Exception + */ + public AuctionObserver(int bidderID, String entityName, Sim_port port, + Responder responder) throws Exception { + this(bidderID, entityName, port); + this.responder = responder; + } + + /** + * Sets a responder to this observer + * @param responder the responder + * @pre responder != null + * @return <tt>true</tt> if the responder was correctly set + */ + public boolean setResponder(Responder responder){ + if(responder == null) + return false; + + this.responder = responder; + return true; + } + + /** + * Returns the responder that the observer is using + * @return the responder + */ + public Responder getResponder(){ + return responder; + } + + /** + * + */ + public void body(){ + // Process events until END_OF_SIMULATION is received from the + // GridSimShutdown Entity + + Sim_event ev = new Sim_event(); + while ( Sim_system.running() ) + { + super.sim_get_next(ev); + + // if the simulation finishes then exit the loop + if (ev.get_tag() == GridSimTags.END_OF_SIMULATION){ + break; + } + + // process the received event + processEvent(ev); + } + + // remove I/O entities created during construction of this entity + super.terminateIOEntities(); + + } + + + /** + * Process an event. + * @param ev + * @return <tt>true</tt> if the event was treated; <tt>false</tt> otherwise. + */ + public boolean processEvent(Sim_event ev){ + int src_id = -1; + Message msg = null; + Message respMsg = null; + + if (responder == null){ + System.out.println("No responder to deal with auction messages!"); + return false; + } + + switch ( ev.get_tag() ){ + case AuctionTags.AUCTION_INFORM_START: + msg = (Message)ev.get_data(); + src_id = msg.getSourceID(); + synchronized(syncSteps){ + respMsg = responder.onReceiveStartAuction((MessageInformStart)msg); + } + break; + + case AuctionTags.AUCTION_CFP: + msg = (Message)ev.get_data(); + src_id = msg.getSourceID(); + synchronized(syncSteps){ + respMsg = responder.onReceiveCfb((MessageCallForBids)msg); + } + break; + + case AuctionTags.AUCTION_INFORM_OUTCOME: + msg = (Message)ev.get_data(); + src_id = msg.getSourceID(); + synchronized(syncSteps){ + respMsg = responder.onReceiveInformOutcome((MessageInformOutcome)msg); + } + break; + + case AuctionTags.AUCTION_REJECT_PROPOSAL: + msg = (Message)ev.get_data(); + src_id = msg.getSourceID(); + synchronized(syncSteps){ + respMsg = responder.onReceiveRejectProposal((MessageRejectBid)msg); + } + break; + + // other unknown tags are processed by this method + default: + return false; + } + + if(respMsg!=null){ + respMsg.setDestinationID(src_id); + + int tag = - 1; + if(respMsg instanceof MessageBid){ + tag = AuctionTags.AUCTION_PROPOSE; + ((MessageBid)respMsg).setBidder(this.bidderID); + } + else if(respMsg instanceof MessageRejectCallForBid){ + tag = AuctionTags.AUCTION_REJECT_CALL_FOR_BID; + ((MessageRejectCallForBid)respMsg).setBidder(this.bidderID); + } + + double scheduleAt = (respMsg.getScheduleTime() > 0.0) ? + respMsg.getScheduleTime() : GridSimTags.SCHEDULE_NOW; + + super.sim_schedule(this.outputPort, scheduleAt, + tag, new IO_data(respMsg, 100, src_id)); + } + return true; + } +} Modified: trunk/source/gridsim/auction/AuctionTags.java =================================================================== --- trunk/source/gridsim/auction/AuctionTags.java 2008-08-23 06:55:34 UTC (rev 237) +++ trunk/source/gridsim/auction/AuctionTags.java 2008-08-31 07:16:49 UTC (rev 238) @@ -1,91 +1,91 @@ -/* - * Title: GridSim Toolkit - * Description: GridSim (Grid Simulation) Toolkit for Modeling and Simulation - * of Parallel and Distributed Systems such as Clusters and Grids - * Licence: GPL - http://www.gnu.org/copyleft/gpl.html - * - * Copyright (c) 2006, The University of Melbourne, Australia - */ -package gridsim.auction; - -/** - * Contains various static command tags that indicate a type of action that - * needs to be undertaken by auction entities when they receive or send events. - * - * @author Marcos Dias de Assuncao - * @since GridSim Toolkit 4.0 - * @see gridsim.auction.OneSidedAuction - * @see gridsim.auction.DoubleAuction - * @see gridsim.auction.AuctionTags - */ -public class AuctionTags { - - private static final int BASE = 900; - - /** Event used by messages that inform the start of an auction */ - public static final int AUCTION_INFORM_START = BASE + 1; - - /** Represents the rejects of a proposal */ - public static final int AUCTION_REJECT_CALL_FOR_BID = BASE + 2; - - /** It means a call for bids or proposals */ - public static final int AUCTION_CFP = BASE + 3; - - /** Proposal or bid */ - public static final int AUCTION_PROPOSE = BASE + 4; - - /** Represents an ask sent to an auctioneer */ - public static final int AUCTION_ASK = BASE + 5; - - /** Used to inform that a bid has been accepted */ - public static final int AUCTION_ACCEPT_PROPOSAL = BASE + 6; - - /** It is used to reject a proposal */ - public static final int AUCTION_REJECT_PROPOSAL = BASE + 7; - - /** This code is used to events that inform the final outcome of an auction */ - public static final int AUCTION_INFORM_OUTCOME = BASE + 8; - - /** Used to inform that a match for an ask has been found */ - public static final int AUCTION_MATCH_TO_ASK = BASE + 9; - - /** An auction must be post to an auctioneer. This event has this purpose. */ - public static final int AUCTION_POST = BASE + 10; - - /** Event code used to trigger or start an auction */ - public static final int AUCTION_START = BASE + 11; - - /** Event code used to inform auctioneer that an auction has finished */ - public static final int AUCTION_FINISHED = BASE + 12; - - /** Used to inform the auctioneer that an auction must be deleted */ - public static final int AUCTION_DELETE = BASE + 13; - - /** Used to stop an auction and to stop the execution of its <tt>body()</tt> method. */ - public static final int END_OF_AUCTION = BASE + 14; - - /** Internal event code internally by auctions to control timeout of rounds and auctions */ - public static final int AUCTION_TIMEOUT = BASE + 15; - - //Code for some kinds of auctions - /** This code is used by First-Price Sealed Bid auctions */ - public static final int FIRST_PRICE_SEALED_AUCTION = 1; - - /** This code is used by Reverse First-Price Sealed Bid auctions */ - public static final int REVERSE_FIRST_PRICE_SEALED_AUCTION = 2; - - /** This code is used by English auctions */ - public static final int ENGLISH_AUCTION = 3; - - /** This code is used by Reverse English auctions */ - public static final int REVERSE_ENGLISH_AUCTION = 4; - - /** This code is used by Dutch auctions */ - public static final int DUTCH_AUCTION = 5; - - /** This code is used by Reverse Dutch auctions */ - public static final int REVERSE_DUTCH_AUCTION = 6; - - /** This code is used by Continuous Double auctions */ - public static final int CONTINUOUS_DOUBLE_AUCTION = 7; -} +/* + * Title: GridSim Toolkit + * Description: GridSim (Grid Simulation) Toolkit for Modeling and Simulation + * of Parallel and Distributed Systems such as Clusters and Grids + * Licence: GPL - http://www.gnu.org/copyleft/gpl.html + * + * Copyright (c) 2006, The University of Melbourne, Australia + */ +package gridsim.auction; + +/** + * Contains various static command tags that indicate a type of action that + * needs to be undertaken by auction entities when they receive or send events. + * + * @author Marcos Dias de Assuncao + * @since GridSim Toolkit 4.0 + * @see gridsim.auction.OneSidedAuction + * @see gridsim.auction.DoubleAuction + * @see gridsim.auction.AuctionTags + */ +public class AuctionTags { + + private static final int BASE = 900; + + /** Event used by messages that inform the start of an auction */ + public static final int AUCTION_INFORM_START = BASE + 1; + + /** Represents the rejects of a proposal */ + public static final int AUCTION_REJECT_CALL_FOR_BID = BASE + 2; + + /** It means a call for bids or proposals */ + public static final int AUCTION_CFP = BASE + 3; + + /** Proposal or bid */ + public static final int AUCTION_PROPOSE = BASE + 4; + + /** Represents an ask sent to an auctioneer */ + public static final int AUCTION_ASK = BASE + 5; + + /** Used to inform that a bid has been accepted */ + public static final int AUCTION_ACCEPT_PROPOSAL = BASE + 6; + + /** It is used to reject a proposal */ + public static final int AUCTION_REJECT_PROPOSAL = BASE + 7; + + /** This code is used to events that inform the final outcome of an auction */ + public static final int AUCTION_INFORM_OUTCOME = BASE + 8; + + /** Used to inform that a match for an ask has been found */ + public static final int AUCTION_MATCH_TO_ASK = BASE + 9; + + /** An auction must be post to an auctioneer. This event has this purpose. */ + public static final int AUCTION_POST = BASE + 10; + + /** Event code used to trigger or start an auction */ + public static final int AUCTION_START = BASE + 11; + + /** Event code used to inform auctioneer that an auction has finished */ + public static final int AUCTION_FINISHED = BASE + 12; + + /** Used to inform the auctioneer that an auction must be deleted */ + public static final int AUCTION_DELETE = BASE + 13; + + /** Used to stop an auction and to stop the execution of its <tt>body()</tt> method. */ + public static final int END_OF_AUCTION = BASE + 14; + + /** Internal event code internally by auctions to control timeout of rounds and auctions */ + public static final int AUCTION_TIMEOUT = BASE + 15; + + //Code for some kinds of auctions + /** This code is used by First-Price Sealed Bid auctions */ + public static final int FIRST_PRICE_SEALED_AUCTION = 1; + + /** This code is used by Reverse First-Price Sealed Bid auctions */ + public static final int REVERSE_FIRST_PRICE_SEALED_AUCTION = 2; + + /** This code is used by English auctions */ + public static final int ENGLISH_AUCTION = 3; + + /** This code is used by Reverse English auctions */ + public static final int REVERSE_ENGLISH_AUCTION = 4; + + /** This code is used by Dutch auctions */ + public static final int DUTCH_AUCTION = 5; + + /** This code is used by Reverse Dutch auctions */ + public static final int REVERSE_DUTCH_AUCTION = 6; + + /** This code is used by Continuous Double auctions */ + public static final int CONTINUOUS_DOUBLE_AUCTION = 7; +} Modified: trunk/source/gridsim/auction/Auctioneer.java =================================================================== --- trunk/source/gridsim/auction/Auctioneer.java 2008-08-23 06:55:34 UTC (rev 237) +++ trunk/source/gridsim/auction/Auctioneer.java 2008-08-31 07:16:49 UTC (rev 238) @@ -1,295 +1,295 @@ -/* - * Title: GridSim Toolkit - * Description: GridSim (Grid Simulation) Toolkit for Modeling and Simulation - * of Parallel and Distributed Systems such as Clusters and Grids - * Licence: GPL - http://www.gnu.org/copyleft/gpl.html - * - * Copyright (c) 2006, The University of Melbourne, Australia - */ -package gridsim.auction; - -import eduni.simjava.Sim_event; -import eduni.simjava.Sim_system; -import gridsim.GridSim; -import gridsim.GridSimTags; -import gridsim.net.Link; - -import java.util.Hashtable; -import java.util.LinkedList; - -/** - * This class defines the basic behavious of an auctioneer - * - * @author Marcos Dias de Assuncao - * @since GridSim Toolkit 4.0 - * @see gridsim.GridSim - */ -public abstract class Auctioneer extends GridSim { - private Hashtable auctions; - private Object syncSteps = new Object(); - - /** - * Constructor - * @param name - * @throws Exception - */ - public Auctioneer(String name) throws Exception { - super(name); - auctions = new Hashtable(); - } - - /** - * Constructor - * @param name - * @param baudRate - * @throws Exception - */ - public Auctioneer(String name, double baudRate) throws Exception { - super(name, baudRate); - auctions = new Hashtable(); - } - - /** - * @param name - * @param link - * @throws Exception - */ - public Auctioneer(String name, Link link) throws Exception { - super(name, link); - auctions = new Hashtable(); - } - - /** - * Adds an auction to this auctioneer - * @param auction - */ - public void addAuction(Auction auction){ - Integer key = new Integer(auction.getAuctionID()); - synchronized(syncSteps){ - auctions.put(key, auction); - } - } - - /* - * Removes an auction that has finished - * @param auction the auction ID - */ - private void removeAuction(int auctionID){ - Integer key = new Integer(auctionID); - synchronized(syncSteps){ - Auction auc = (Auction)auctions.get(key); - auctions.remove(key); - super.send(auc.get_id(), - GridSimTags.SCHEDULE_NOW, - AuctionTags.END_OF_AUCTION); - } - } - - /** - * Starts a given auction already added to the auctioneer - * @param auctionID The auction's id - */ - public void startAuction(int auctionID){ - Integer key = new Integer(auctionID); - synchronized(syncSteps){ - Auction auction = (Auction)auctions.get(key); - if(auction!=null){ - super.send(auction.get_id(), - GridSimTags.SCHEDULE_NOW, - AuctionTags.AUCTION_START); - } - else - System.err.println("Auctioneer.startAution(): "+ - "This auction does not exist. Auction ID = " + auctionID); - } - } - - /** - * Handles external events that are coming to this Auctioneer entity. - * <p> - * The services or tags available for this resource are: - * <ul> - * <li> {@link gridsim.auction.AuctionTags#AUCTION_POST} </li> - * <li> {@link gridsim.auction.AuctionTags#AUCTION_START} </li> - * <li> {@link gridsim.auction.AuctionTags#AUCTION_DELETE} </li> - * <li> {$link gridsim.auction.AuctionTags#AUCTION_FINISHED} </li> - * <li> {@link gridsim.auction.AuctionTags#AUCTION_PROPOSE} </li> - * <li> {@link gridsim.auction.AuctionTags#AUCTION_REJECT_CALL_FOR_BID} </li> - * <li> {@link gridsim.auction.AuctionTags#AUCTION_ASK} </li> - * <li> {@link gridsim.auction.AuctionTags#AUCTION_MATCH_TO_ASK} </li> - * </ul> - * <br> - * This method also calls these methods in the following order: - * <ol> - * <li> {@link #processOtherEvent(Sim_event)} method - * </ol> - * - * @pre $none - * @post $none - */ - public void body(){ - // Process events until END_OF_SIMULATION is received from the - // GridSimShutdown Entity - - Sim_event ev = new Sim_event(); - while ( Sim_system.running() ) - { - super.sim_get_next(ev); - - // if the simulation finishes then exit the loop - if (ev.get_tag() == GridSimTags.END_OF_SIMULATION){ - break; - } - - // process the received event - processEvent(ev); - } - - // remove I/O entities created during construction of this entity - super.terminateIOEntities(); - } - - /** - * Processes events or services that are available to this Auctioneer - * @param ev a Sim_event object - * @pre ev != null - * @post $none - */ - private void processEvent(Sim_event ev) - { - Auction auc = null; - Message msg = null; - Integer auctionID = null; - - switch ( ev.get_tag() ) - { - case AuctionTags.AUCTION_POST: - auc = (Auction)ev.get_data(); - addAuction(auc); // just adds the auction in the hashtable - break; - - case AuctionTags.AUCTION_START: - auctionID = (Integer)ev.get_data(); - startAuction(auctionID.intValue()); // starts the auction that was previously added - break; - - case AuctionTags.AUCTION_DELETE: - auctionID = (Integer)ev.get_data(); - removeAuction(auctionID.intValue()); - break; - - case AuctionTags.AUCTION_FINISHED: - auc = (Auction)auctions.get((Integer)ev.get_data()); - synchronized(syncSteps){ - this.onAuctionClose(auc); - // trigger the event to delete this auction from the list - super.send(get_id(), GridSimTags.SCHEDULE_NOW, - AuctionTags.AUCTION_DELETE, new Integer(auc.getAuctionID())); - } - break; - - // deal with proposal that has been sent - case AuctionTags.AUCTION_PROPOSE: - Auction auction_p = null; - msg = (Message)ev.get_data(); - MessageBid bid = (MessageBid)msg; - auction_p = (Auction)auctions.get(new Integer(bid.getAuctionID())); - synchronized(syncSteps){ - if(auction_p != null){ - if(auction_p instanceof OneSidedAuction){ - if(((OneSidedAuction)auction_p).currentRound() == bid.getRound()){ - super.send(auction_p.get_id(), GridSimTags.SCHEDULE_NOW, - AuctionTags.AUCTION_PROPOSE, bid); - } - } - else{ - super.send(auction_p.get_id(), GridSimTags.SCHEDULE_NOW, - AuctionTags.AUCTION_PROPOSE, bid); - } - } - } - break; - - case AuctionTags.AUCTION_REJECT_CALL_FOR_BID: - msg = (Message)ev.get_data(); - MessageRejectCallForBid rej = (MessageRejectCallForBid)msg; - auc = (Auction)auctions.get(new Integer(rej.getAuctionID())); - synchronized(syncSteps){ - if(auc != null){ - if(auc instanceof OneSidedAuction) - if(((OneSidedAuction)auc).currentRound() == rej.getRound()){ - super.send(auc.get_id(), GridSimTags.SCHEDULE_NOW, - AuctionTags.AUCTION_REJECT_CALL_FOR_BID, rej); - } - } - } - break; - - // deal with ask that has been sent - case AuctionTags.AUCTION_ASK: - msg = (Message)ev.get_data(); - MessageAsk ask = (MessageAsk)msg; - auc = (Auction)auctions.get(new Integer(ask.getAuctionID())); - if(auc != null){ - if(auc instanceof DoubleAuction){ - super.send(auc.get_id(), GridSimTags.SCHEDULE_NOW, - AuctionTags.AUCTION_ASK, ask); - } - } - break; - - case AuctionTags.AUCTION_MATCH_TO_ASK: - LinkedList mat = (LinkedList)ev.get_data(); - synchronized(syncSteps){ - MessageAsk a = (MessageAsk)mat.get(0); - MessageBid b = (MessageBid)mat.get(1); - double p = ((Double)mat.get(2)).doubleValue(); - // call the method to process match to ask - this.onResponseToAsk(a,b,p); - } - break; - - // other unknown tags are processed by this method - default: - processOtherEvent(ev); - break; - } - } - - /** - * Overrides this method when making a new and different type of auctioneer. - * This method is called by {@link #body()} for incoming unknown tags. - * - * @param ev a Sim_event object - * @pre ev != null - * @post $none - */ - protected void processOtherEvent(Sim_event ev){ - if (ev == null){ - System.out.println("Auctioneer.processEvent(): " + super.get_name() - + " is has asked to process a null event."); - return; - } - } - - /** - * This method should be implemented to perform some auction after - * some auction has been finished. This method will be called whenever - * one of the auctions that were initiated by this Auctionerr has been concluded - * @param auction is the auction that has been concluded - */ - protected abstract void onAuctionClose(Auction auction); - - /** - * This method is called when a match for an ask was found by a double - * auction. The auction passes the ask, the bid that matches it and the - * price that they will use to trade - * @param ask the ask previously sent to the auctioneer - * @param bid the bid that matches the ask - * @param price the price used to trade - * @post the bid can be null if a match was not found - */ - protected abstract void onResponseToAsk(MessageAsk ask, MessageBid bid, double price); - - -} +/* + * Title: GridSim Toolkit + * Description: GridSim (Grid Simulation) Toolkit for Modeling and Simulation + * of Parallel and Distributed Systems such as Clusters and Grids + * Licence: GPL - http://www.gnu.org/copyleft/gpl.html + * + * Copyright (c) 2006, The University of Melbourne, Australia + */ +package gridsim.auction; + +import eduni.simjava.Sim_event; +import eduni.simjava.Sim_system; +import gridsim.GridSim; +import gridsim.GridSimTags; +import gridsim.net.Link; + +import java.util.Hashtable; +import java.util.LinkedList; + +/** + * This class defines the basic behavious of an auctioneer + * + * @author Marcos Dias de Assuncao + * @since GridSim Toolkit 4.0 + * @see gridsim.GridSim + */ +public abstract class Auctioneer extends GridSim { + private Hashtable auctions; + private Object syncSteps = new Object(); + + /** + * Constructor + * @param name + * @throws Exception + */ + public Auctioneer(String name) throws Exception { + super(name); + auctions = new Hashtable(); + } + + /** + * Constructor + * @param name + * @param baudRate + * @throws Exception + */ + public Auctioneer(String name, double baudRate) throws Exception { + super(name, baudRate); + auctions = new Hashtable(); + } + + /** + * @param name + * @param link + * @throws Exception + */ + public Auctioneer(String name, Link link) throws Exception { + super(name, link); + auctions = new Hashtable(); + } + + /** + * Adds an auction to this auctioneer + * @param auction + */ + public void addAuction(Auction auction){ + Integer key = new Integer(auction.getAuctionID()); + synchronized(syncSteps){ + auctions.put(key, auction); + } + } + + /* + * Removes an auction that has finished + * @param auction the auction ID + */ + private void removeAuction(int auctionID){ + Integer key = new Integer(auctionID); + synchronized(syncSteps){ + Auction auc = (Auction)auctions.get(key); + auctions.remove(key); + super.send(auc.get_id(), + GridSimTags.SCHEDULE_NOW, + AuctionTags.END_OF_AUCTION); + } + } + + /** + * Starts a given auction already added to the auctioneer + * @param auctionID The auction's id + */ + public void startAuction(int auctionID){ + Integer key = new Integer(auctionID); + synchronized(syncSteps){ + Auction auction = (Auction)auctions.get(key); + if(auction!=null){ + super.send(auction.get_id(), + GridSimTags.SCHEDULE_NOW, + AuctionTags.AUCTION_START); + } + else + System.err.println("Auctioneer.startAution(): "+ + "This auction does not exist. Auction ID = " + auctionID); + } + } + + /** + * Handles external events that are coming to this Auctioneer entity. + * <p> + * The services or tags available for this resource are: + * <ul> + * <li> {@link gridsim.auction.AuctionTags#AUCTION_POST} </li> + * <li> {@link gridsim.auction.AuctionTags#AUCTION_START} </li> + * <li> {@link gridsim.auction.AuctionTags#AUCTION_DELETE} </li> + * <li> {$link gridsim.auction.AuctionTags#AUCTION_FINISHED} </li> + * <li> {@link gridsim.auction.AuctionTags#AUCTION_PROPOSE} </li> + * <li> {@link gridsim.auction.AuctionTags#AUCTION_REJECT_CALL_FOR_BID} </li> + * <li> {@link gridsim.auction.AuctionTags#AUCTION_ASK} </li> + * <li> {@link gridsim.auction.AuctionTags#AUCTION_MATCH_TO_ASK} </li> + * </ul> + * <br> + * This method also calls these methods in the following order: + * <ol> + * <li> {@link #processOtherEvent(Sim_event)} method + * </ol> + * + * @pre $none + * @post $none + */ + public void body(){ + // Process events until END_OF_SIMULATION is received from the + // GridSimShutdown Entity + + Sim_event ev = new Sim_event(); + while ( Sim_system.running() ) + { + super.sim_get_next(ev); + + // if the simulation finishes then exit the loop + if (ev.get_tag() == GridSimTags.END_OF_SIMULATION){ + break; + } + + // process the received event + processEvent(ev); + } + + // remove I/O entities created during construction of this entity + super.terminateIOEntities(); + } + + /** + * Processes events or services that are available to this Auctioneer + * @param ev a Sim_event object + * @pre ev != null + * @post $none + */ + private void processEvent(Sim_event ev) + { + Auction auc = null; + Message msg = null; + Integer auctionID = null; + + switch ( ev.get_tag() ) + { + case AuctionTags.AUCTION_POST: + auc = (Auction)ev.get_data(); + addAuction(auc); // just adds the auction in the hashtable + break; + + case AuctionTags.AUCTION_START: + auctionID = (Integer)ev.get_data(); + startAuction(auctionID.intValue()); // starts the auction that was previously added + break; + + case AuctionTags.AUCTION_DELETE: + auctionID = (Integer)ev.get_data(); + removeAuction(auctionID.intValue()); + break; + + case AuctionTags.AUCTION_FINISHED: + auc = (Auction)auctions.get((Integer)ev.get_data()); + synchronized(syncSteps){ + this.onAuctionClose(auc); + // trigger the event to delete this auction from the list + super.send(get_id(), GridSimTags.SCHEDULE_NOW, + AuctionTags.AUCTION_DELETE, new Integer(auc.getAuctionID())); + } + break; + + // deal with proposal that has been sent + case AuctionTags.AUCTION_PROPOSE: + Auction auction_p = null; + msg = (Message)ev.get_data(); + MessageBid bid = (MessageBid)msg; + auction_p = (Auction)auctions.get(new Integer(bid.getAuctionID())); + synchronized(syncSteps){ + if(auction_p != null){ + if(auction_p instanceof OneSidedAuction){ + if(((OneSidedAuction)auction_p).currentRound() == bid.getRound()){ + super.send(auction_p.get_id(), GridSimTags.SCHEDULE_NOW, + AuctionTags.AUCTION_PROPOSE, bid); + } + } + else{ + super.send(auction_p.get_id(), GridSimTags.SCHEDULE_NOW, + AuctionTags.AUCTION_PROPOSE, bid); + } + } + } + break; + + case AuctionTags.AUCTION_REJECT_CALL_FOR_BID: + msg = (Message)ev.get_data(); + MessageRejectCallForBid rej = (MessageRejectCallForBid)msg; + auc = (Auction)auctions.get(new Integer(rej.getAuctionID())); + synchronized(syncSteps){ + if(auc != null){ + if(auc instanceof OneSidedAuction) + if(((OneSidedAuction)auc).currentRound() == rej.getRound()){ + super.send(auc.get_id(), GridSimTags.SCHEDULE_NOW, + AuctionTags.AUCTION_REJECT_CALL_FOR_BID, rej); + } + } + } + break; + + // deal with ask that has been sent + case AuctionTags.AUCTION_ASK: + msg = (Message)ev.get_data(); + MessageAsk ask = (MessageAsk)msg; + auc = (Auction)auctions.get(new Integer(ask.getAuctionID())); + if(auc != null){ + if(auc instanceof DoubleAuction){ + super.send(auc.get_id(), GridSimTags.SCHEDULE_NOW, + AuctionTags.AUCTION_ASK, ask); + } + } + break; + + case AuctionTags.AUCTION_MATCH_TO_ASK: + LinkedList mat = (LinkedList)ev.get_data(); + synchronized(syncSteps){ + MessageAsk a = (MessageAsk)mat.get(0); + MessageBid b = (MessageBid)mat.get(1); + double p = ((Double)mat.get(2)).doubleValue(); + // call the method to process match to ask + this.onResponseToAsk(a,b,p); + } + break; + + // other unknown tags are processed by this method + default: + processOtherEvent(ev); + break; + } + } + + /** + * Overrides this method when making a new and different type of auctioneer. + * This method is called by {@link #body()} for incoming unknown tags. + * + * @param ev a Sim_event object + * @pre ev != null + * @post $none + */ + protected void processOtherEvent(Sim_event ev){ + if (ev == null){ + System.out.println("Auctioneer.processEvent(): " + super.get_name() + + " is has asked to process a null event."); + return; + } + } + + /** + * This method should be implemented to perform some auction after + * some auction h... [truncated message content] |