home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 21 / IOPROG_21.ISO / SOFT / JSL.ZIP / JSL20 / examples / computer / Server.java < prev   
Encoding:
Java Source  |  1998-02-20  |  2.3 KB  |  94 lines

  1.  
  2. package examples.computer;
  3.  
  4.  
  5. import simula.*;
  6. import simula.simset.* ;
  7. import simula.simset.simulation.*;
  8. import simula.random.* ;
  9. /**
  10.  * The server executes jobs and, if needed, reschedules them.
  11.  * @author Andrea Poltronieri
  12.  * @version 1.0 12 Feb 1998
  13.  */
  14.  
  15. public class Server extends simula.simset.simulation.Process {
  16.  
  17.  
  18.  
  19. /**
  20. * @param _sim The active Simulation
  21. */
  22.  
  23.         public Server (Simulation _sim){
  24.           super(_sim);
  25.         }
  26. /**     
  27.   * Executes actions for this object. Called by simulation on activate
  28.   * @see simula.simset.simulation.Simulation
  29. */
  30.  
  31.  
  32.  
  33. public void run() {
  34.     ComputerSimulation sim = (ComputerSimulation)this.sim ;
  35.  
  36.     
  37.     Job next ;
  38.     Job first_outer ;
  39.     simula.simset.Head tmp ;
  40.     Uniform uniform = new Uniform (sim.seed,0,1) ;
  41.     try {
  42.         // Main loop
  43.         while (true)  {
  44.             tmp=new Head() ;
  45.             // Scan inner queue and serve jobs
  46.             while (! sim.inner.empty()) {
  47.                 next=(Job)(sim.inner.first()) ;
  48.                 next.out() ;
  49.                 
  50.                 sim.result += (sim.inner.cardinal()) * (sim.time() - sim.lastevent);
  51.                 sim.lastevent = sim.time();
  52.                 sim.hold(sim.s) ;
  53.                 
  54.                 // Decide if the job needs another slice.
  55.                 if (uniform.draw().asReal()<=sim.q) {
  56.                     // Job not finished -- reschedule
  57.                     next.into(sim.inner) ;
  58.                 }    
  59.                 else{
  60.                     sim.free+=next.req; 
  61.                     tmp=new Head() ;
  62.                     // Job in inner queue finished. Select a new job from outer
  63.                     // Scan outer queue to schedule new jobs
  64.                     //System.err.println(">> Scannig outer...") ;
  65.                     while (! (sim.outer.empty())) {
  66.                         
  67.                         next=(Job)(sim.outer.first()) ;
  68.                         //System.err.println(">>  Before out  in outer : "+sim.outer.cardinal()) ;
  69.                         //Lang.inint() ;
  70.                         next.out() ;
  71.                         //System.err.println(">>  After out  in outer : "+sim.outer.cardinal()) ;
  72.                         // If the job cant be served, move it to a temporary queue.
  73.                         // Not elegant, but i can read only the first element of the queue...
  74.                         if (next.req<=sim.free) {
  75.                             next.into(sim.inner);
  76.                             sim.free-=next.req ;
  77.                         }    
  78.                         else
  79.                             next.into(tmp);
  80.                     }    
  81.                     sim.outer= tmp ;
  82.                     
  83.                 }    
  84.             }        
  85.             // Inner queue is now empty
  86.             // If outer is not empty, there is jobs wich need more memory than existant.
  87.             sim.passivate() ;                
  88.         }
  89.     } catch (Exception e) {
  90.     System.err.println ("Removals exception caught: " + e) ;
  91.     System.exit(1) ;
  92.     }
  93. }
  94. }