//File: Sysapi.java //CS 471 - Operating Systems Project //Dennis Pereira, Ely Soto, John Cavalieri //The Sysapi java class is the main Application programing interface for //the virtual machine modules. It contains redirections to the appropriate functions that //the virtual machines should have access to. //The Pipe Manager, File Manager, and Virtual Machine Monitor are also //contained in this file as non-public classes to prevent access by a module. import java.io.*; import java.util.Vector; class StreamManager { public StreamManager() { } public Handle OpenFile(String filename) { try { RandomAccessFile rafile; File thefile = new File(filename); thefile.createNewFile(); rafile = new RandomAccessFile(thefile,"rw"); Handle filehandle = new Handle(rafile,3,"file-" + rafile.hashCode()); return filehandle; } catch(IOException ex) { System.out.println(ex); return null; } } public String ReadFile(RandomAccessFile rafile, int readcount) { try { Long readlength; String strdata = ""; byte[] filedata = new byte[0]; if(readcount <= 0) {// Two versions shown here for this case // Reads from current file pointer to end of file //readlength = new Long(rafile.length() - rafile.getFilePointer()); //filedata = new byte[readlength.intValue()]; //rafile.read(filedata,0,readlength.intValue()); //Reads a Line from the file strdata = rafile.readLine(); } else { filedata = new byte[readcount]; //Gets available bytes readlength = new Long(rafile.length() - rafile.getFilePointer()); if(readcount <= readlength.intValue()) { filedata = new byte[readcount]; rafile.read(filedata,0,readcount); } else { filedata = new byte[readlength.intValue()]; rafile.read(filedata,0,readlength.intValue()); } } if(strdata == null) { return ""; } if(strdata.compareTo("")==0) { strdata = new String(filedata); } return strdata; } catch(Exception ex) { System.out.println("ReadFile threw Exception: " + ex); return ""; } } public synchronized void WriteFile(RandomAccessFile rafile, String strdata, int writecount) { // Will write min(strdata.length,writecount) number of bytes from strdata after the position of the // filepointer try { byte[] filedata; if(writecount < strdata.length() || writecount == 0) { filedata = new byte[strdata.length()]; } else { filedata = new byte[writecount]; } filedata = strdata.getBytes(); rafile.write(filedata,0,filedata.length); try { notifyAll(); } catch(Exception ex) { System.out.println(ex); } } catch(Exception ex) { System.out.println(ex); } } } class PipeManager { public PipeManager() { } public Handle CreatePipe() { Pipe newpipe = new Pipe(); Handle newhandle = new Handle(newpipe,3,"pipe-" + newpipe.hashCode()); return newhandle; } } class Pipe { private PipedOutputStream outpipe; private PipedInputStream inpipe; public Pipe() { try { outpipe = new PipedOutputStream(); inpipe = new PipedInputStream(outpipe); } catch(IOException ex) { System.out.println("Error while creating pipe."); } } public synchronized void WritePipe(String strdata, int writecount) { try { byte[] pipedata; if(writecount < strdata.length() || writecount == 0) { pipedata = new byte[strdata.length()]; } else { pipedata = new byte[writecount]; } pipedata = strdata.getBytes(); outpipe.write(pipedata,0,pipedata.length); outpipe.flush(); try { notifyAll(); } catch(Exception ex) { System.out.println(ex); } } catch(Exception ex) { System.out.println(ex); } } public String ReadPipe(int readcount) throws IOException { // Reads readcount bytes from inpipe // Reads all available if readcount == 0 try { byte[] pipedata; int count = 0; if(readcount <= 0) { //Reads all available bytes //pipedata = new byte[inpipe.available()]; //inpipe.read(pipedata,0,inpipe.available()); //Reads bytes until newline or End of available Bytes found int recieved; String datastr; byte[] buffer = new byte[10000]; byte[] data = new byte[1]; do { try { recieved = inpipe.read(); } catch(IOException ioex1) {//Found end of pipe stream //System.out.println(ioex1); return ""; } if(recieved == -1) {//Found end of stream return ""; } data[0] = (byte) recieved; buffer[count] = data[0]; datastr = new String(data); count++; }while(inpipe.available() > 0 && datastr.compareTo("\n")!=0); //discards last byte pipedata = new byte[count]; for(int i=0;i= UserArray.size() || i >= ConsoleArray.size()) { console = new ConsoleWindow("OSVM - " + currentUser); ConsoleArray.add(console); } else { console = ((ConsoleWindow)ConsoleArray.get(i)); } ThreadID = console.getClass().getName() + "-" + console.hashCode(); Handle chandle = new Handle(console,1,ThreadID);//Create method for unique IDs return chandle; } if(device == "display") { for(i=0;i < UserArray.size() && getUser(i).getName() != currentUser;i++); if(i >= UserArray.size() || i >= ConsoleArray.size()) { console = new ConsoleWindow("OSVM - " + currentUser); ConsoleArray.add(console); } else { console = ((ConsoleWindow)ConsoleArray.get(i)); } ThreadID = console.getClass().getName() + "-" + console.hashCode(); Handle chandle = new Handle(console,2,ThreadID);//Create method for unique IDs return chandle; } else { return new Handle(null,0,""); } } public void setTitle(String username) {// Sets the title of the console owned by the specified user int i; for(i=0;i < UserArray.size() && getUser(i).getName() != username;i++); ((ConsoleWindow)ConsoleArray.get(i)).setTitle("OSVM - " + username); } // ---------- Public User Search Functions ------------------- public User getUser(int index) { return ((User)UserArray.get(index)); } public User findUser(String username) { int i; for(i=0;i0) {// Parent VM waits until its children are finished wait(); } } } catch(InterruptedException ex) { System.out.println("Exception while stopping parent VM"); } } public String getProcessMsg(String ThreadID) {//Gets Oldest Message for Thread calling this function int i = 0; for(i = 0;i=VMList.size()) { return "Process Not Found.\n"; } else { return ((ProcessMsgBox)VMMsgBox.get(i)).getMsg(); } } public boolean writeProcessMsg(String ThreadID, String msg) {// Writes Msg to specified Thread, returns true if successful, false if ThreadID not found. int i = 0; for(i = 0;i=VMList.size()) { return false; } else { ((ProcessMsgBox)VMMsgBox.get(i)).writeMsg(Thread.currentThread().getName(),msg); return true; } } class ProcessMsgBox { private Vector msgbox = new Vector(5,1); public ProcessMsgBox() { } public void writeMsg(String ID, String msg) { msgbox.add(ID + " - " + msg); } public String getMsg() {// Retrieves Oldest Process Message String msg = ""; if(msgbox.size()>0) { msg = (String)msgbox.remove(0); } else { msg = ""; } return msg; } } }