⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 auction.java

📁 中間件開發详细说明:清华大学J2EE教程讲义(ppt)-Tsinghua University J2EE tutorial lectures (ppt) [上载源码成为会员下载此源码] [成为VIP会
💻 JAVA
字号:
/*
 * 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
 *
 * $Id: Auction.java,v 1.4 2006/03/23 03:40:56 anthony Exp $
 */
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++;
	}
	
	/**
	 * 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();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -