Skip to content

Commit

Permalink
- Added java
Browse files Browse the repository at this point in the history
- Massively Optimized
- Increased Stability
- Updated README
  • Loading branch information
wacksientrist committed Jun 5, 2024
0 parents commit a2d37ce
Show file tree
Hide file tree
Showing 16 changed files with 1,019 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
130 changes: 130 additions & 0 deletions Instance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class Instance implements Runnable {
private final int UUID;
private final String instrcRReadyPath;
private final String instrcRPath;
private final String instrcSPath;
private final String instrcRCompletePath;

public Instance(int UUID) {
this.UUID = UUID;
this.instrcRReadyPath = "Comp" + UUID + "/Instrc_r_ready.txt";
this.instrcRPath = "Comp" + UUID + "/Instrc_r.txt";
this.instrcSPath = "Comp" + UUID + "/Instrc_s.txt";
this.instrcRCompletePath = "Comp" + UUID + "/Instrc_r_complete.txt";
}

public void processCommands() {
try {
while (true) {
// Check if there's a new command to process
String readyFlag = readStringFromFile(instrcRReadyPath);
if (!readyFlag.trim().equals("ready")) {
Thread.sleep(1);
continue;
}

// Read the command from Instrc_r.txt
String command = readStringFromFile(instrcRPath).trim();
String[] parts = command.split(" ");
if (parts.length < 3) {
// Handle invalid command format
System.err.println("Error: Invalid command format in Instrc_r.txt");
continue;
}

// Parse command parameters
double A, B;
try {
A = Double.parseDouble(parts[0]);
B = Double.parseDouble(parts[1]);
} catch (NumberFormatException e) {
// Handle invalid numeric input
System.err.println("Error: Invalid numeric input in command file.");
continue;
}
String type = parts[2];

// Process the command
String resultStr = processCommand(A, B, type);

// Write the result to Instrc_s.txt
writeStringToFile(instrcSPath, resultStr);

// Signal completion of command processing
writeStringToFile(instrcRCompletePath, "complete");

// Clear readiness for the next command
writeStringToFile(instrcRReadyPath, "");
}
} catch (Exception e) {
e.printStackTrace();
}
}

private String processCommand(double A, double B, String type) {
switch (type) {
case "A":
return String.valueOf(A + B);
case "S":
return String.valueOf(A - B);
case "M":
return String.valueOf(A * B);
case "D":
if (B != 0) {
return String.valueOf(A / B);
} else {
System.err.println("Error: Division by zero.");
return "";
}
case "IF=":
return A == B ? "T" : "F";
case "IF!":
return A != B ? "T" : "F";
case "IF>":
return A > B ? "T" : "F";
case "IF<":
return A < B ? "T" : "F";
default:
System.err.println("Error: Unknown command type.");
return "";
}
}

private String readStringFromFile(String filePath) {
try {
return new String(Files.readAllBytes(Paths.get(filePath))).trim();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}

private void writeStringToFile(String filePath, String content) {
try {
Files.write(Paths.get(filePath), content.getBytes(), StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING);
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void run() {
processCommands();
}

public static void main(String[] args) {
if (args.length < 1) {
System.err.println("Usage: java Instance <UUID>");
System.exit(1);
}

int UUID = Integer.parseInt(args[0]);
Instance instance = new Instance(UUID);
instance.run();
}
}
Loading

0 comments on commit a2d37ce

Please sign in to comment.