import java.io.*; import java.util.Vector; //Currently The VM Monitor, Pipe Manager, Process Manager, and Stream Manager are all in this single class //Will later separate and create a SYSAPI class that exposes certain methods to VMs. 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.compareTo("")==0) { strdata = new String(filedata); } return strdata; } catch(Exception ex) { System.out.println(ex); return null; } } 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 { recieved = inpipe.read(); data[0] = (byte) recieved; buffer[count] = data[0]; datastr = new String(data); count++; //System.out.println(datastr); }while(inpipe.available() > 0 && datastr.compareTo("\n")!=0); System.out.println("TEST2"); //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"); } } }