Compare commits

...

3 Commits

2 changed files with 23 additions and 8 deletions

View File

@ -9,11 +9,13 @@ import java.net.URL;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
public class Main {
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
boolean help_enqueued = false;
boolean allow_networking = true;
String matrix_file_name = "";
int verbosity = 1;
int p = 0;
@ -32,8 +34,7 @@ public class Main {
p += 2;
}
else {
System.err.println("[ERROR] Unknown argument found: " + args[p] + "\n[ERROR] Please, use 'java Main -h' for help");
System.exit(1);
matrix_file_name = args[p++];
}
}
catch (Exception e) {
@ -47,6 +48,20 @@ public class Main {
printHelp(allow_networking);
System.exit(0);
}
if (Objects.equals("", matrix_file_name)) {
System.err.println("[WARN] No filename specified to read from, using default one");
matrix_file_name = "random_matrix.txt";
}
File matrix_file = new File(matrix_file_name);
if (!matrix_file.exists()) {
if (allow_networking) {
System.err.println("[WARN] File is missing, downloading random matrix from the server");
fetchResource("http://lab2.kpi.dev:16554/matrix.py", matrix_file_name);
}
}
}
private static void fetchResource(String remote_url, String output_filename) {
@ -72,7 +87,7 @@ public class Main {
System.err.println("[WARN] Help file is missing.");
if (allow_net) {
System.err.println("[INFO] Trying to recover it from the git server");
fetchResource("http://139.162.162.130:3000/dymik739/oop-labs-collection/raw/branch/lab2-dev/labs/2/src/help.txt", "src/help.txt");
fetchResource("http://lab2.kpi.dev:3000/dymik739/oop-labs-collection/raw/branch/lab2-dev/labs/2/src/help.txt", "src/help.txt");
} else {
System.err.println("[INFO] Networking is disabled, not recovering");
System.exit(1);
@ -80,14 +95,12 @@ public class Main {
}
Scanner help_file_scanner = new Scanner(help_file);
String help_text = "";
while (help_file_scanner.hasNextLine()) {
help_text += help_file_scanner.nextLine();
System.out.print(help_file_scanner.nextLine());
}
help_file_scanner.close();
System.out.println(help_text);
} catch (Exception e) {
System.out.println("[ERROR] Failed to read help due to the following exception: " + e);
System.exit(1);

2
labs/2/matrix.py Normal file
View File

@ -0,0 +1,2 @@
import random
print("Content-Type: text/plain\n\n" + "\n".join([" ".join(list(map(str, [random.randint(-40, 40) for i in range(7)]))) for i in range(7)]))