-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainClass.java
More file actions
288 lines (237 loc) · 8.42 KB
/
MainClass.java
File metadata and controls
288 lines (237 loc) · 8.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import java.io.*;
import java.util.Hashtable;
class Disk {
static final int NUM_SECTORS = 2048;
static final int DISK_DELAY = 80; // 80 for Gradescope
int id;
StringBuffer sectors[] = new StringBuffer[NUM_SECTORS];
Disk(int newId){
this.id = newId;
}
void write(int sector, StringBuffer data){
this.sectors[sector] = data;
try{
Thread.sleep(DISK_DELAY);
}catch (Exception e) {e.printStackTrace();}
}
void printDisk(){
for(int i=0; i<sectors.length; i++){
if (sectors[i] != null){
System.out.println(i + " " + sectors[i]);
}
}
}
void read(int sector, StringBuffer data){
data.append(this.sectors[sector]);
try{
Thread.sleep(DISK_DELAY);
}catch (Exception e) {e.printStackTrace();}
}
}
class Printer {
static final int PRINT_DELAY = 275; // 275 for Gradescope
int id;
Printer(int newId){
this.id = newId;
}
void print(StringBuffer data){
try{
File f = new File("PRINTER" + this.id);
boolean appendMode = f.exists();
FileWriter myWriter = new FileWriter("PRINTER" + this.id, appendMode);
myWriter.write(data.toString() + "\n");
myWriter.close();
}catch (Exception e) {e.printStackTrace();}
}
}
class UserThread extends Thread{
// Read from Useri file and create a printJobThread for each .print
// .save and .end indicate a saving operation to the disk
int id;
UserThread(int newId){
this.id = newId;
}
public void run(){
try{
FileInputStream inputStream = new FileInputStream("USER" + this.id);
BufferedReader myReader = new BufferedReader(new InputStreamReader(inputStream));
for (String line; (line = myReader.readLine()) != null; ){
String[] tokens = line.split(" ");
if (tokens[0].equals(".print")){
PrintJobThread newJob = new PrintJobThread(tokens[1]);
newJob.start();
}else if(tokens[0].equals(".save")){
this.SaveFile(tokens[1], myReader);
}
}
myReader.close();
}catch (Exception e) {e.printStackTrace();}
}
public void SaveFile(String name, BufferedReader myReader){
int diskNumber = MainClass.GlobalDiskManager.request();
int offset = MainClass.GlobalDiskManager.getNextFree(diskNumber);
int filelength = 0;
try{
for (String line; (line = myReader.readLine()) != null; ){
if (line.equals(".end")){
MainClass.GlobalDirectoryManager.enter(new StringBuffer(name), new FileInfo(diskNumber, offset, filelength));
break;
}else{
MainClass.GlobalDiskManager.disks[diskNumber].write(offset + filelength, new StringBuffer(line));
}
filelength++;
}
//MainClass.GlobalDiskManager.setNextFree(diskNumber, offset+filelength); Might need to implement this
MainClass.GlobalDiskManager.release(diskNumber);
}catch (Exception e) {e.printStackTrace();}
}
}
class PrintJobThread extends Thread{
StringBuffer line; // only allowed one line to reuse for read from disk and print to printer
FileInfo f;
int start;
int d;
PrintJobThread(String fileToPrint)
{
this.line = new StringBuffer(fileToPrint);
this.f = MainClass.GlobalDirectoryManager.lookup(line);
this.start = f.startingSector;
this.d = f.diskNumber;
}
public void run()
{
int p = MainClass.GlobalPrinterManager.request();
line.setLength(0);
for (int i=0; i<f.fileLength; i++){
MainClass.GlobalDiskManager.disks[d].read(start+i, line);
MainClass.GlobalPrinterManager.printers[p].print(line);
line.setLength(0);
}
MainClass.GlobalPrinterManager.release(p);
}
}
class FileInfo{
// DirectoryManager will use this metainfo to hold info about files after saving to disk
int diskNumber;
int startingSector;
int fileLength;
FileInfo(int newDiskNumber, int newStartingSector, int newFileLength){
this.diskNumber = newDiskNumber;
this.startingSector = newStartingSector;
this.fileLength = newFileLength;
}
}
class DirectoryManager{
// Table that knows where files are stored on disk via mapping file names to disk sectors
private Hashtable<String, FileInfo> T;
DirectoryManager()
{
T = new Hashtable<String, FileInfo>();
}
void enter(StringBuffer fileName, FileInfo file){
T.put(fileName.toString(), file);
}
FileInfo lookup(StringBuffer fileName){
return T.get(fileName.toString());
}
}
class ResourceManager {
/*A ResourceManager gives a specific Thread exclusive access rights to a resource, either a Disk or a Printer. */
boolean isFree[];
ResourceManager(int numberOfItems) {
isFree = new boolean[numberOfItems];
for (int i=0; i<isFree.length; ++i)
isFree[i] = true;
}
synchronized int request() {
while (true) {
for (int i = 0; i < isFree.length; ++i){
if ( isFree[i] ) {
isFree[i] = false;
return i;
}
try{
this.wait(); // block until someone releases Resource
} catch (Exception e) {e.printStackTrace();}
}
}
}
synchronized void release( int index ) {
isFree[index] = true;
this.notify(); // let a blocked thread run
}
}
class DiskManager extends ResourceManager{
/*The DiskManager is derived from ResourceManager and also keeps track of the next free sector on each disk,
which is useful for saving files.
The DiskManager should contain the DirectoryManager for finding file sectors on Disk. */
Disk[] disks;
DiskManager(int numberOfItems) {
super(numberOfItems);
disks = new Disk[numberOfItems];
for(int i=0; i<numberOfItems; i++){
this.disks[i] = new Disk(i);
}
}
int getNextFree(int d){
for(int i=0; i<Disk.NUM_SECTORS; i++){
if (this.disks[d].sectors[i] == null){
return i;
}
}
return -1;
}
void printDisks(){
for (int i=0; i<disks.length; i++){
disks[i].printDisk();
}
}
}
class PrinterManager extends ResourceManager{
Printer[] printers;
int currentPrinter;
PrinterManager(int numberOfItems) {
super(numberOfItems);
printers = new Printer[numberOfItems];
for(int i=0; i<numberOfItems; i++){
this.printers[i] = new Printer(i);
}
this.currentPrinter = 0;
}
synchronized int request() {
while (true) {
for (int i = 0; i < printers.length; ++i) {
int printerToAllocate = (currentPrinter + i) % printers.length; // Round-robin allocation
if (isFree[printerToAllocate]) {
isFree[printerToAllocate] = false;
currentPrinter = (printerToAllocate + 1) % printers.length; // Update the current printer
return printerToAllocate;
}
}
try {
this.wait(); // block until someone releases a printer
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public class MainClass{
public static DiskManager GlobalDiskManager;
public static PrinterManager GlobalPrinterManager;
public static DirectoryManager GlobalDirectoryManager;
public static void main(String[] args){
// Initialize the disk and printer managers along with their lists
GlobalDiskManager = new DiskManager(Math.abs(Integer.parseInt(args[1])));
GlobalPrinterManager = new PrinterManager(Math.abs(Integer.parseInt(args[2])));
GlobalDirectoryManager = new DirectoryManager();
UserThread[] users = new UserThread[Math.abs(Integer.parseInt(args[0]))];
for(int i=0; i<users.length; i++){
UserThread newUser = new UserThread(i);
users[i] = newUser;
}
for(int i=0; i<users.length; i++){
users[i].start();
}
}
}