Download Steam Net Simulation with Real Control System

Transcript
com.ms.wfc.io.FileMode.CREATE,
com.ms.wfc.io.FileAccess.READWRITE,
com.ms.wfc.io.FileShare.READWRITE);
String contents = "PCS7" + CR + LF;
for (int i = 1; i < fromProcessToPCS7.length; i++)
contents += (String) fromProcessToPCS7[i][0] + CR + LF;
for (int i = 1; i < toProcessFromPCS7.length; i++)
contents += (String) toProcessFromPCS7[i][0] + CR + LF;
fileStartPosition = file.getPosition();
file.setPosition(fileStartPosition);
file.setLength(contents.length());
for (int i = 0; i< contents.length(); i++)
file.write(contents.charAt(i));
file.flush();
file.close();
}
/* Private method that returns a String with the components of the file. Before
reading from the file it tries to lock the contents of the file. If the
atte mpt is unsuccesfull it sleeps and tries again and so on. */
private String getContentsFromFile()
{
boolean ourFile = true;
try{ file.lock(0,4000);}
catch(Exception wioe){System.out.println(wioe);ourFile = false;}
while (!ourFile)
{
ourFile = true;
try{myThread.sleep(5);}
catch(InterruptedException ie){System.out.println(ie);}
try{file.lock(0,4000);}
catch(Exception wioe){System.out.println(wioe);ourFile = false;}
}
file.setPosition(fileStartPosition);
file.flush();
String temp = new String("");
long fileLength = file.getLength();
for (int i = 0; i < fileLength; i++)
temp += (char) file.read();
return temp;
}
/* Private method that performs the communication between the file and the
control system. It reads process values from the file, waits for the control
system to tick to the next sample and then writes the control signals to the
file. This is performed repeatedly. */
private void performWriteandRead()
{
file = new com.ms.wfc.io.File(fileName,
com.ms.wfc.io.FileMode.OPEN,
com.ms.wfc.io.FileAccess.READWRITE,
com.ms.wfc.io.FileShare.READWRITE); //open the file "unsafe"
fileStartPosition = file.getPosition();
while (true)
{
String fileContents = getContentsFromFile();
int currentIndex = 0;
String wroteLast = fileContents.substring(currentIndex,
fileContents.indexOf(CR, currentIndex));
while (wroteLast.equals("PCS7"))
{
file.unlock(0,4000);
try{myThread.sleep(80);}
catch(InterruptedException ie){System.out.println(ie);}
fileContents = getContentsFromFile();
wroteLast = fileContents.substring(currentIndex,
fileContents.indexOf(CR,currentIndex));
50