commit f0602812cfdffced6284a1525daa7ff5c2d28bd2 Author: Apostolof Date: Fri Dec 29 13:10:43 2017 +0200 Init diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..e51bb91 --- /dev/null +++ b/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..75d2c48 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + finalCode + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/bin/ACK.class b/bin/ACK.class new file mode 100644 index 0000000..4aad709 Binary files /dev/null and b/bin/ACK.class differ diff --git a/bin/Camera.class b/bin/Camera.class new file mode 100644 index 0000000..86050a5 Binary files /dev/null and b/bin/Camera.class differ diff --git a/bin/ComputerNetworks.class b/bin/ComputerNetworks.class new file mode 100644 index 0000000..7f15992 Binary files /dev/null and b/bin/ComputerNetworks.class differ diff --git a/bin/GPS.class b/bin/GPS.class new file mode 100644 index 0000000..07d076b Binary files /dev/null and b/bin/GPS.class differ diff --git a/bin/Ping.class b/bin/Ping.class new file mode 100644 index 0000000..c66994e Binary files /dev/null and b/bin/Ping.class differ diff --git a/bin/Utils.class b/bin/Utils.class new file mode 100644 index 0000000..c0b8f7f Binary files /dev/null and b/bin/Utils.class differ diff --git a/libs/ithakimodem.jar b/libs/ithakimodem.jar new file mode 100644 index 0000000..4229d48 Binary files /dev/null and b/libs/ithakimodem.jar differ diff --git a/src/ACK.java b/src/ACK.java new file mode 100644 index 0000000..9f8f28d --- /dev/null +++ b/src/ACK.java @@ -0,0 +1,144 @@ +import java.io.FileNotFoundException; +import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.Scanner; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import ithakimodem.Modem; + +public class ACK { + private final static String regex = ".*PSTART.*<(.*)> ([0-9]*) PSTOP."; + private final static String subst = "$1,$2"; + private final static Pattern pattern = Pattern.compile(regex, Pattern.DOTALL); + + private static final Scanner input = new Scanner(System.in); + + public static void ack() { + System.out.print("Type request code for ack: "); + String ackRequestCode = input.nextLine(); + System.out.print("Type request code for nack: "); + String nackRequestCode = input.nextLine(); + System.out.print("Time to run: "); + int timeToRun = input.nextInt(); + input.nextLine(); + + getACK(ackRequestCode, nackRequestCode, timeToRun); + } + + private static int getACK(String ackRequestCode, String nackRequestCode, int numberOfSecondsToRun) { + long endTime = 0; + boolean lastCallHadErrors = false; + int packageNumber = 0, errorFreePackages = 0, errorPackages = 0, BER = 0; + ArrayList packageBytesWithErrors = new ArrayList(); + ArrayList packageResponseTime = new ArrayList(); + + PrintWriter writer = null; + try { + writer = new PrintWriter("ARQ_2.txt", "UTF-8"); + } catch (FileNotFoundException e) { + e.printStackTrace(); + return -1; + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + return -1; + } + + writer.println("[0] #No, [1] HasError, [2] Package, [3] Response time\n\n[0]\t[1]\t\t\t\t\t\t\t\t[2]\t\t\t\t\t\t\t\t[3]"); + + Modem modem = new Modem(); + modem.setSpeed(Utils.modemSpeed); + modem.setTimeout(Utils.modemTimeout); + modem.open(Utils.modemOpen); + + endTime = System.currentTimeMillis() + numberOfSecondsToRun * 1000; + while(System.currentTimeMillis() < endTime) { + long packageStartTime = 0, packageEndTime = 0; + String response = ""; + + packageStartTime = System.currentTimeMillis(); + if (!lastCallHadErrors) { + response = Utils.makeRequest(modem, ackRequestCode); + packageEndTime = System.currentTimeMillis(); + packageResponseTime.add((int)(packageEndTime - packageStartTime - Utils.modemTimeout)); + } else { + response = Utils.makeRequest(modem, nackRequestCode); + packageEndTime = System.currentTimeMillis(); + packageResponseTime.add((int)(packageEndTime - packageStartTime - Utils.modemTimeout)); + } + + Matcher matcher = pattern.matcher(response); + String[] packageAndFCS = (matcher.replaceAll(subst)).split(","); + if (packageAndFCS.length != 2 || packageAndFCS[0] == null || packageAndFCS[0].isEmpty() + || packageAndFCS[1] == null || packageAndFCS[1].isEmpty()) { + System.out.println("Malformed response: " + response); + continue; + } + byte[] packageBytes = packageAndFCS[0].getBytes(); + byte xor = packageBytes[0]; + for (int i=1; i\t"); + writer.print(packageBytes[0]); + for (int i=1; i " + (errorFreePackages / allPackages * 100)); + System.out.println("Error packages = " + errorPackages + " --> " + (errorPackages / allPackages * 100)); + System.out.println("Error bits = " + BER); + System.out.println("BER = " + ((float) BER)/((float)((allPackages) * 16 * 8))); + + return 1; + } +} diff --git a/src/Camera.java b/src/Camera.java new file mode 100644 index 0000000..227c91c --- /dev/null +++ b/src/Camera.java @@ -0,0 +1,66 @@ +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Scanner; + +import ithakimodem.Modem; + +public class Camera { + private static final Scanner input = new Scanner(System.in); + + public static void camera() { + System.out.print("Type request code: "); + String requestCode = input.nextLine(); + System.out.print("Image filename (if a file with the same name already exists" + + " it will be overwritten): "); + String filename = input.nextLine(); + + getImage(requestCode, filename); + } + + private static boolean getImage(String requestCode, String imageFilaname) { + String imageString; + + Modem modem = new Modem(); + modem.setSpeed(Utils.modemSpeed); + modem.setTimeout(Utils.modemTimeout); + modem.open(Utils.modemOpen); + + { + String response = ""; + response = Utils.makeRequest(modem, requestCode); + if (response == null || response.isEmpty()) { + return false; + } + int startIndex = response.indexOf("" + (char) 255 + (char) 216), + endIndex = response.lastIndexOf("" + (char) 255 + (char) 217) + 2; + if (startIndex == -1 || endIndex == -1) { + return false; + } + imageString = response.substring(startIndex, endIndex); + } + + FileOutputStream fileOutputStream; + try { + fileOutputStream = new FileOutputStream(imageFilaname, false); + + char imageCharArray[] = imageString.toCharArray(); + byte imageByteArray[] = new byte[imageCharArray.length]; + for (int i=0; i coordinates = getCoordinates(requestCode, requestFlags, numberOfCoordinates); + + if (coordinates == null || coordinates.isEmpty()) { + System.out.println("Came with an empty List!"); + return; + } + + input.nextLine(); //Clear input buffer + System.out.print("Should try to fetch the image? (y/n): "); + String fetchImage = input.nextLine(); + while(true) { + if ("y".equals(fetchImage)) { + System.out.print("Image filename (if a file with the same name already exists" + + " it will be overwritten): "); + String filename = input.nextLine(); + + getImage(requestCode, coordinates, filename); + + return; + } else if ("n".equals(fetchImage)) { + return; + } + System.out.print("Input malformed. Try again: "); + fetchImage = input.nextLine(); + } + } + + private static List getCoordinates(String requestCode, String requestFlag, int coordinatesToExtract) { + List coordinates, returnList = new ArrayList(); + String response = ""; + + PrintWriter writer = null; + try { + writer = new PrintWriter("gps.txt", "UTF-8"); + } catch (FileNotFoundException e) { + e.printStackTrace(); + return null; + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + return null; + } + + Modem modem = new Modem(); + modem.setSpeed(Utils.modemSpeed); + modem.setTimeout(Utils.modemTimeout); + modem.open(Utils.modemOpen); + + response = Utils.makeRequest(modem, requestCode + requestFlag); + modem.close(); + + if (response == null || response.isEmpty()) { + writer.close(); + return null; + } + + final Matcher extractGPGGALineMatcher = extractGPGGALinePattern.matcher(response); + coordinates = new ArrayList(Arrays.asList(extractGPGGALineMatcher.replaceAll(extractGPGGALineSubst).split("\n"))); + for (String coordinate: coordinates) { + writer.println(coordinate); + } + writer.close(); + writer.println(response); + + for (int i=0; i coordinates, String imageFilename) { + String imageString; + + for (String coordinate : coordinates) { + requestCode += "T=" + coordinate; + } + + Modem modem = new Modem(); + modem.setSpeed(Utils.modemSpeed); + modem.setTimeout(Utils.modemTimeout); + modem.open(Utils.modemOpen); + + { + String response = ""; + response = Utils.makeRequest(modem, requestCode); + if (response == null || response.isEmpty()) { + return false; + } + int startIndex = response.indexOf("" + (char) 255 + (char) 216), + endIndex = response.lastIndexOf("" + (char) 255 + (char) 217) + 2; + if (startIndex == -1 || endIndex == -1) { + return false; + } + imageString = response.substring(startIndex, endIndex); + } + + FileOutputStream fileOutputStream; + try { + fileOutputStream = new FileOutputStream(imageFilename, false); + + char imageCharArray[] = imageString.toCharArray(); + byte imageByteArray[] = new byte[imageCharArray.length]; + for (int i=0; i