Please review any and all PUBLIC repositories, groups and associate files. These allow anyone on the Internet to access without authentication. Repository and group owners are responsible for their content and permission settings. Go to your project(s), click on Settings > General and expand the "Visibility, project features, permissions" to change this setting.

Renamed the folder to BrennenstuhlTobias

parent bad46cf7
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package DISTools;
import static java.lang.Math.pow;
import static java.lang.Math.sqrt;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;
import java.util.TreeMap;
/**
*
* @author tobia
*/
public class Compressor {
private LinkedHashMap<Double, Coordinates> localMap;
public Compressor(LinkedHashMap<Double, Coordinates> localHashMap) {
//copy the given HashMap to a localMap
this.localMap = new LinkedHashMap<>();
Set<Double> keys = localHashMap.keySet();
for (Double k : keys) {
localMap.put(k, localHashMap.get(k));
}
System.out.println("DISTools.Regression.<init>()");
}
public TreeMap<Double, Coordinates> doCompression() {
System.out.println("DISTools.Regression.doRegression()");
//Check whether points could be deleted to compress the stream
//https://www.crashkurs-statistik.de/einfache-lineare-regression/
TreeMap<Double, Coordinates> streamMap = new TreeMap<>();
Set<Double> keys = localMap.keySet();
//Copy LinkedHashMap into TreeMap to be able to pull the first element.
streamMap.putAll(localMap);
TreeMap<Double, Coordinates> returnMap = new TreeMap<>();
//TreeMap of slidingWindows will store all of the points that are currently processed
//use .pullFirstEntry() to get rid of the points at the beginning.
TreeMap<Double, Coordinates> slidingWindow = new TreeMap<>();
boolean addToWindow = true;
while (streamMap.size() > 0) {
slidingWindow.put(streamMap.firstEntry().getKey(), streamMap.get(streamMap.firstEntry().getKey()));
streamMap.pollFirstEntry();
//Calculate the mean and SD
Set<Double> slidingWindowKeys = slidingWindow.keySet();
if (slidingWindow.size() >= 3) {
List<Double> tList = new ArrayList<>();
List<Double> xList = new ArrayList<>();
List<Double> yList = new ArrayList<>();
List<Double> zList = new ArrayList<>();
Double[] k = new Double[slidingWindowKeys.size()];
slidingWindowKeys.toArray(k);
for (int i = 0; i < slidingWindow.size(); i++) {
//Fix Loop to fill Arrays
tList.add(i, k[i]);
xList.add(i, slidingWindow.get(k[i]).getX());
yList.add(i, slidingWindow.get(k[i]).getY());
zList.add(i, slidingWindow.get(k[i]).getZ());
}
//calculate triangle according to the homepage
for (int i = 0; i < slidingWindow.size(); i++) {
//Calculate Area of Triangle
//http://www.ambrsoft.com/TrigoCalc/Line3D/LineColinear.htm
double a = sqrt(pow(xList.get(1) - xList.get(0), 2) + pow(yList.get(1) - yList.get(0), 2) + pow(zList.get(1) - zList.get(0), 2));
double b = sqrt(pow(xList.get(i) - xList.get(0), 2) + pow(yList.get(i) - yList.get(0), 2) + pow(zList.get(i) - zList.get(0), 2));
double c = sqrt(pow(xList.get(i) - xList.get(1), 2) + pow(yList.get(i) - yList.get(1), 2) + pow(zList.get(i) - zList.get(1), 2));
double s = (a + b + c) / 2;
double areaA = sqrt(s * (s - a) * (s - b) * (s - c));
if ((areaA >= 0.1) || (tList.get(i) - tList.get(0) >= 4.0)) {
System.out.println("Threshold met. Break");
//grab the first and the last point from the regression test and push it to the returnMap
Coordinates firstPoint = new Coordinates();
firstPoint.setX(xList.get(0));
firstPoint.setY(yList.get(0));
firstPoint.setZ(zList.get(0));
Coordinates lastPoint = new Coordinates(xList.get(i), yList.get(i), zList.get(i));
returnMap.put(tList.get(0), firstPoint);
returnMap.put(tList.get(i), lastPoint);
slidingWindow.clear();
tList.clear();
xList.clear();
yList.clear();
zList.clear();
break;
}
if ((areaA <= 0.1) && (tList.get(i) - tList.get(0) <= 4.0) && streamMap.size() == 0) {
System.out.println("StreamMap empty. All points left will be added. Break");
//grab the first and the last point from the regression test and push it to the returnMap
for (int j = 0; j < slidingWindow.size(); j++) {
Coordinates leftPoints = new Coordinates(xList.get(j), yList.get(j), zList.get(j));
returnMap.put(tList.get(j), leftPoints);
}
break;
}
System.out.println("Area of Triangle: " + areaA);
}
}
};
return returnMap;
}
;
private void calcValues() {
}
;
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package DISTools;
/**
*
* @author tobia
*/
public class Coordinates {
private double x;
private double y;
private double z;
public Coordinates(double x, double y, double z) {
this.setX(x);
this.setY(y);
this.setZ(z);
}
public Coordinates() {
this.setX(0.0);
this.setY(0.0);
this.setZ(0.0);
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getZ() {
return z;
}
public void setZ(double z) {
this.z = z;
}
}
/**
* Copyright (c) 2008-2019, MOVES Institute, Naval Postgraduate School. All rights reserved.
* This work is licensed under the BSD open source license, available at https://www.movesinstitute.org/licenses/bsd.html
*/
package PduStreamTools;
import java.io.IOException;
import java.util.Scanner;
/**
* PduSaver.java created on Aug 21, 2019 MOVES Institute Naval Postgraduate
* School, Monterey, CA, USA www.nps.edu
*
* @author Mike Bailey, jmbailey@nps.edu
* @version $Id$
*/
public class PduListenerSaver {
private final static String DEFAULT_OUTPUTDIR = "pduLog";
private final static String MCAST_ADDR = "239.1.2.3";
private final static int DIS_PORT = 3000;
private enum mystate {
RUNNING,
PAUSED;
}
public static void main(String[] args) throws IOException {
String outDir = DEFAULT_OUTPUTDIR;
String mcast = MCAST_ADDR;
int port = DIS_PORT;
System.out.println("DisExamplesOpenDis7.PduListenerSaver started...");
switch (args.length) {
case 0:
break;
case 1:
outDir = args[0];
break;
case 3:
outDir = args[0];
mcast = args[1];
port = Integer.parseInt(args[2]);
break;
default:
System.err.println("Usage: PduListener() or PduListener(\"outputdir\") or PduListener(\"outputdir\",\"multicast address\", ipPort");
System.exit(1);
}
System.out.println("Beginning pdu save to directory " + outDir);
//RecorderPlainText stores an unencoded PduLog
//RecorderPlainText recorder = new RecorderPlainText(outDir, mcast, port);
//RecorderBase64 stores a BASE64 encoded PduLog
//RecorderBase64 recorder = new RecorderBase64(outDir, mcast, port);
//RecorderBinary stores a Binary PduLog
RecorderBinary recorder = new RecorderBinary(outDir, mcast, port);
recorder.startResume();
mystate state = mystate.RUNNING;
System.out.println("* recorder.startResume(), state=RUNNING, recording in progress...");
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("Warning: you must quit when complete, otherwise recorded PDUs are lost!");
System.out.println("Type p/enter to pause, r/enter to resume, q/enter to stop recording, save and quit");
String line = scan.nextLine();
if (line.equalsIgnoreCase("p") && state == mystate.RUNNING) {
recorder.stopPause();
state = mystate.PAUSED;
System.out.println("* recorder.stopPause(), state=PAUSED, recording paused...");
} else if (line.equalsIgnoreCase("r") && state == mystate.PAUSED) {
recorder.startResume();
state = mystate.RUNNING;
System.out.println("* recorder.startResume(), state=RUNNING, recording in progress...");
} else if (line.equalsIgnoreCase("q")) {
recorder.end();
System.out.println("* recorder.end(), recording complete.");
break;
}
}
System.out.println("Ending pdu save to " + recorder.getLogFile());
}
}
/**
* Copyright (c) 2008-2019, MOVES Institute, Naval Postgraduate School. All rights reserved.
* This work is licensed under the BSD open source license, available at https://www.movesinstitute.org/licenses/bsd.html
*/
package PduStreamTools;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
/**
* PduSaver.java created on Aug 21, 2019 MOVES Institute Naval Postgraduate
* School, Monterey, CA, USA www.nps.edu
*
* @author Mike Bailey, jmbailey@nps.edu
* @version $Id$
*/
public class PduReaderPlayer {
private final static String DEFAULT_OUTPUTDIR = "pduLog";
private final static String MCAST_ADDR = "239.1.2.3";
private final static int DIS_PORT = 3000;
private enum mystate {
RUNNING,
PAUSED;
}
public static void main(String[] args) {
String outDir = DEFAULT_OUTPUTDIR;
String mcast = MCAST_ADDR;
int port = DIS_PORT;
System.out.println("DisExamplesOpenDis7.PduReaderPlayer started...");
switch (args.length) {
case 0:
break;
case 1:
outDir = args[0];
break;
case 3:
outDir = args[0];
mcast = args[1];
port = Integer.parseInt(args[2]);
break;
default:
System.err.println("Usage: PduReaderPlayer() or PduReaderPlayer(\"outputdir\") or PduReaderPlayer(\"outputdir\",\"multicast address\", ipPort");
System.exit(1);
}
System.out.println("Beginning pdu playback from directory " + outDir);
try {
//Use Tobis Code without decryption
//Does only work with unecrypted PduLogs
//PlayerPlainText player = new PlayerPlainText(mcast, port, new File(outDir).toPath());
//Use Mike Baileys Code to store a file in BASE64 format
PlayerBase64 player = new PlayerBase64(mcast, port, new File(outDir).toPath());
player.startResume();
mystate state = mystate.RUNNING;
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("Type p/enter to pause, r/enter to resume, q/enter to quit");
String line = scan.nextLine();
if (line.equalsIgnoreCase("p") && state == mystate.RUNNING) {
player.stopPause();
state = mystate.PAUSED;
} else if (line.equalsIgnoreCase("r") && state == mystate.PAUSED) {
player.startResume();
state = mystate.RUNNING;
} else if (line.equalsIgnoreCase("q")) {
player.end();
break;
}
}
System.out.println("Ending pdu files playback for directory " + outDir);
System.out.println("DisExamplesOpenDis7.PduReaderPlayer complete.");
System.exit(0); // not sure why this is necessary with Netbeans...
} catch (IOException ex) {
System.err.println("Exception: " + ex.getClass().getSimpleName() + ": " + ex.getLocalizedMessage());
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package PduStreamTools;
/**
* Copyright (c) 2008-2019, MOVES Institute, Naval Postgraduate School. All rights reserved.
* This work is licensed under the BSD open source license, available at https://www.movesinstitute.org/licenses/bsd.html
*/
import static PduStreamTools.RecorderBase64.COMMENT_MARKER;
import static PduStreamTools.RecorderBase64.START_COMMENT_MARKER;
import static PduStreamTools.RecorderBase64.STOP_COMMENT_MARKER;
import com.google.common.primitives.Longs;
import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Base64;
public class PlayerBase64
{
private Path disLogDirectory;
private String ip;
private int port;
private Thread thrd;
public PlayerBase64(String ip, int port, Path disLogDirectory) throws IOException
{
this.disLogDirectory = disLogDirectory;
this.ip = ip;
this.port = port;
thrd = new Thread(() -> begin());
thrd.setPriority(Thread.NORM_PRIORITY);
thrd.setName("PlayerThread");
thrd.setDaemon(true);
thrd.start();
}
private Integer scenarioPduCount = null;
private boolean showPduCountsOneTime = false;
private int pduCount = 0;
private DatagramSocket dsock;
private BufferedReader brdr;
private Long startNanoTime = null;
private boolean paused = false;
@SuppressWarnings("StatementWithEmptyBody")
public void begin()
{
try {
System.out.println("Replaying DIS logs.");
InetAddress addr = InetAddress.getByName(ip);
FilenameFilter filter = (dir, name) -> name.endsWith(RecorderBase64.DISLOG_FILE_TAIL) && !name.startsWith(".");
File[] fs = disLogDirectory.toFile().listFiles(filter);
if (fs == null)
fs = new File[0];
Arrays.sort(fs, (f1, f2) -> {
return f1.getName().compareTo(f2.getName());
});
//Arrays.sort(fs, Comparator.comparing(File::getName));
dsock = new DatagramSocket();
Base64.Decoder decdr = Base64.getDecoder();
for (File f : fs) {
System.out.println("Replaying " + f.getAbsolutePath());
brdr = new BufferedReader(new FileReader(f), 1024 * 200); // 200kb buffer
String line = brdr.readLine();
while (line != null && !Thread.interrupted()) {
while (paused) {
sleep(500l); // half sec
}
if (line.length() <= 0)
; // blank lines ok
else if (line.startsWith(COMMENT_MARKER)) {
if (handleComment(line, f)) {
// here if we got an end comment
break; // out of read loop
}
}
else {
String[] sa = line.split(",");
if (sa.length != 2) {
System.err.println("Error: parsing error. Line follows.");
System.err.println(line);
byebye();
}
if (startNanoTime == null)
startNanoTime = System.nanoTime();
byte[] pduTimeBytes = decdr.decode(sa[0]);
long pduTimeInterval = Longs.fromByteArray(pduTimeBytes);
// This is a relative number in nanoseconds of the time of packet reception minus first packet reception for scenario.
long targetSimTime = startNanoTime + pduTimeInterval; // when we should send the packet
long now = System.nanoTime();
long sleepTime = targetSimTime - now; //System.nanoTime(); // the difference between then and now
if (sleepTime > 20000000) { // 20 ms //
//System.out.println("sim interval = " + pduTimeInterval + ", sleeping for " + sleepTime/1000000l + " ms");
sleep(sleepTime / 1000000L, (int) (sleepTime % 1000000L));
}
byte[] buffer = decdr.decode(sa[1]);
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, addr, port);
dsock.send(packet);
pduCount++;
if (scenarioPduCount != null)
scenarioPduCount++;
if (showPduCountsOneTime || pduCount % 5 == 0)
showCounts();
}
line = brdr.readLine();
}
brdr.close();
}
}
catch (Exception ex) {
System.err.println("Exception reading/writing pdus: "+ex.getClass().getSimpleName()+": "+ex.getLocalizedMessage());
thrd = null;
closer();
}
}
private void sleep(long ms)
{
try {
Thread.sleep(ms);
}
catch (InterruptedException ex) {
}
}
private void showCounts()
{
if (scenarioPduCount != null)
System.out.print(pduCount + " " + ++scenarioPduCount + "\r");
else
System.out.print(pduCount + "\r");
showPduCountsOneTime = false;
}
private void byebye() throws IOException
{
System.out.println("Replay stopped.");
closer();
throw new IOException("Dis Replayer parsing error");
}
private void closer()
{
try {
if (dsock != null) {
dsock.close();
dsock = null;
}
if (brdr != null) {
brdr.close();
brdr = null;
}
}
catch (Exception ioex) {
System.err.println("IOException closing reader in Player");
}
}
private boolean handleComment(String s, File f) //true if we're done
{
boolean ret = false;
if (s.startsWith(START_COMMENT_MARKER)) {
//System.out.println();
s = s.substring(START_COMMENT_MARKER.length());
System.out.println(s + " ");
showPduCountsOneTime = true; // get the first one in there
}
else if (s.startsWith(STOP_COMMENT_MARKER)) {
showCounts();
System.out.println();
System.out.println("End of replay from " + f.getName());
System.out.println(s.substring(STOP_COMMENT_MARKER.length()));
scenarioPduCount = 0;
startNanoTime = null;