/* -------------------------------------------------------- * * Aaron Morris * * CS567 * * Object-Oriented Event-Driven Simulation * * * * Class representing Basic Server Object * * -------------------------------------------------------- */ //package datasend; import java.awt.*; public class Server extends DataHandler { private int MaxFileSize; private int PERBYTES; // Basic Constructor, set Idle to True public Server(String Name, int ID, int Delay,int PerBytes,EventQueue eq, int MaxFile,RandNum r,Stats s) { PERBYTES = PerBytes; // Related to delay, this is bytes/time super.CurrentLoad = 0; // Current Load on the server init 0 super.Delay = Delay; // Delay for getting a file super.randGen = r; // Pointer to RandNum object super.stats = s; // statistics object pointer super.eventList = eq; // EventList object pointer super.Idle = true; // Server initialized Idle super.Name = Name; // Name of Server super.ID = ID; // ID of Server super.requests = new MessageQueue(); // Personal Message Queue Initialized this.MaxFileSize = MaxFile; // Max File Size to return } // Methods to return/set necessary attributes public int getCurrentLoad() { return this.requests.getNumQueued(); } public int getQueueLength() { return this.requests.getNumQueued(); } // Create new Finish Event at Time and correlate Arrival ID public ResponseEvent genResponse(int Time,Message m) { int RandFileSize; int PropDelay; // Generate Random File Size and From that get propogation Delay RandFileSize = super.randGen.getJavaInt(MaxFileSize)+1; stats.avgFileSize.update(RandFileSize); m.setHeaderName("RESPONSE"); m.setHeaderSize(RandFileSize); m.setBody("File of Size " + RandFileSize); m.setCurrentLoad(CurrentLoad); m.setSource(this); PropDelay = Delay + (RandFileSize/PERBYTES); ResponseEvent newEvent = new ResponseEvent("RESPONSE", Time+PropDelay,m,this); newEvent.setID(m.getMsgID()); return newEvent; } public void receive(int time, Message m) { // If server Idle, Generate Response Event. Response event will if (this.isIdle()) { CurrentLoad = 1; setBusy(); ResponseEvent NRE = this.genResponse(time, m); stats.report.write(this.getName() + " generated Response for Time " + NRE.getArrivalTime()); eventList.enQueue(NRE); } // Else Put in request Queue else { //stats.report.write("Message " + m.getMsgID() + " Put in " + // "Queue of " + this.Name); CurrentLoad = CurrentLoad + 1; setBusy(); requests.enQueue(m); } } }