scheduler-simulator

git clone git://git.codymlewis.com/scheduler-simulator.git
Log | Files | Refs | README

Dispatcher.java (5858B)


      1 import java.util.LinkedList;
      2 import java.util.Map;
      3 import java.util.HashMap;
      4 import java.util.Scanner;
      5 import java.util.PriorityQueue;
      6 import java.io.File;
      7 import java.io.FileNotFoundException;
      8 
      9 /**
     10  * <h1>Dispatcher - Comp2240A1</h1>
     11  *
     12  * A class for the Dispatcher, it manages the process execution simulation and
     13  * is composed of a scheduler.
     14  *
     15  * @author Cody Lewis (c3283349)
     16  * @version 1
     17  * @since 2019-08-17
     18  */
     19 
     20 public class Dispatcher {
     21     private Scheduler scheduler;
     22     private int switchProcessTime;
     23     private Map<Integer, LinkedList<Process>> processes;
     24 
     25     /**
     26      * Input constructor
     27      *
     28      * @param schedulerAlgorithm Name of the scheduler algorithm to use
     29      * @param filename Name of the data file
     30      */
     31     public Dispatcher(String schedulerAlgorithm, String filename) {
     32         processes = parseDataFile(filename);
     33         scheduler = schedulerFactory(schedulerAlgorithm);
     34     }
     35 
     36     /**
     37      * Read the data file and construct a map between the time of the process
     38      * and the list of processes.
     39      *
     40      * @param filename Name of the file to read
     41      * @return Map between time of process and the list of processes
     42      */
     43     private Map<Integer, LinkedList<Process>> parseDataFile(String filename) {
     44         Map<Integer, LinkedList<Process>> data = null;
     45 
     46         try (Scanner fstream = new Scanner(new File(filename))) {
     47             String curLine = fstream.nextLine();
     48             // Check that the file is formated correctly
     49             if (!curLine.contains("BEGIN")) {
     50                 throw new InvalidDataException();
     51             }
     52             fstream.nextLine();
     53             curLine = fstream.nextLine();
     54             Scanner sstream = new Scanner(curLine);
     55             if (!sstream.next().contains("DISP:")) {
     56                 throw new InvalidDataException();
     57             }
     58             switchProcessTime = sstream.nextInt();
     59             if (!fstream.nextLine().contains("END")) {
     60                 throw new InvalidDataException();
     61             }
     62             data = fstreamToMap(fstream);
     63         } catch (FileNotFoundException fnfe) {
     64             System.err.format(
     65                 "Error: File %s was not found. Continuing run with no processes.",
     66                 filename
     67             );
     68         } catch (InvalidDataException ide) {
     69             System.err.format(
     70                 "Error: %s Continuing run with no processes.",
     71                 ide.getMessage()
     72             );
     73         }
     74 
     75         return data == null ?
     76             new HashMap<Integer, LinkedList<Process>>() :
     77             data;
     78     }
     79 
     80     /**
     81      * Put the file contents from the stream into Map
     82      *
     83      * @param fstream A scanner stream iterated to the process content
     84      * @return Map from the time of arrival to the list of processes
     85      */
     86     private Map<Integer, LinkedList<Process>> fstreamToMap(Scanner fstream) {
     87         String pid = "", curLine = "";
     88         Scanner sstream;
     89         int arrivalTime = 0;
     90         int serviceTime = 0;
     91         Map<Integer, LinkedList<Process>> data = new HashMap<>();
     92 
     93         while (!(curLine = fstream.nextLine()).contains("EOF")) {
     94             if (curLine.length() > 1) {
     95                 sstream = new Scanner(curLine);
     96                 switch (sstream.next()) {
     97                     case "ID:":
     98                         pid = sstream.next();
     99                         break;
    100                     case "Arrive:":
    101                         arrivalTime = sstream.nextInt();
    102                         break;
    103                     case "ExecSize:":
    104                         serviceTime = sstream.nextInt();
    105                         break;
    106                     case "END":
    107                         if (data.get(arrivalTime) == null) {
    108                             data.put(
    109                                 arrivalTime,
    110                                 new LinkedList<Process>()
    111                             );
    112                         }
    113                         data.get(arrivalTime).add(
    114                             new Process(pid, arrivalTime, serviceTime)
    115                         );
    116                 }
    117             }
    118         }
    119 
    120         return data;
    121     }
    122 
    123     /**
    124      * Factory method to choose the appropriate scheduler to compose this
    125      *
    126      * @param algorithm Name of the algorithm
    127      * @return A scheduler running the chosen algorithm
    128      */
    129     private Scheduler schedulerFactory(String algorithm) {
    130         switch (algorithm) {
    131             case "RR":
    132                 return new RR(switchProcessTime);
    133             case "NRR":
    134                 return new NRR(switchProcessTime);
    135             case "FB (constant)":
    136                 return new FB(switchProcessTime);
    137             default:
    138                 return new FCFS(switchProcessTime);
    139         }
    140     }
    141 
    142     /**
    143      * Run through the scheduled processes
    144      *
    145      * @return Results of the simulation
    146      */
    147     public String run() {
    148         int time = 0;
    149         int timeProcessingTo = 0;
    150         PriorityQueue<Integer> eventTimes = new PriorityQueue<>();
    151         processes.keySet().stream().forEach(e -> eventTimes.add(e));
    152 
    153         while (!eventTimes.isEmpty()) {
    154             time = eventTimes.poll();
    155             if (processes.get(time) != null) {
    156                 processes.get(time).stream().forEach(p -> scheduler.add(p));
    157                 processes.remove(time);
    158             }
    159             if (time >= timeProcessingTo) {
    160                 int processingTime = scheduler.process(time);
    161                 if (processingTime != 0) {
    162                     eventTimes.add(time + processingTime);
    163                     timeProcessingTo = time + processingTime;
    164                 }
    165             }
    166         }
    167 
    168         return scheduler.results();
    169     }
    170 
    171     /**
    172      * Give summary data of the simulation
    173      *
    174      * @return The average turnaroundTime and average waitingTime
    175      */
    176     public String summary() {
    177         return scheduler.summary();
    178     }
    179 }