/*
 * 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 packetcapture;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import javafx.fxml.FXML;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;

/**
 * FXML Controller class
 *
 * @author senor
 */
public class PacketCaptureController {
    String timestamp = null;
    Boolean capture = false;
    Thread thingThread = null;
    
    @FXML
    private AnchorPane root;
    
    @FXML
    private Button btnStartCapture;
    
    @FXML
    private Button btnEndCapture;
    
    @FXML
    private TextField txtStorageDirectory;
    
    @FXML
    private Button btnSelectStorageDirectory;
    
    @FXML
    private TextArea txtOutput;
    
    public void initialize() {
        txtStorageDirectory.setText(System.getProperty("user.home"));
        resetAll();
    }
    
    @FXML
    private void selectStorageDirectory() {
        Stage stage = (Stage) root.getScene().getWindow();
        DirectoryChooser directoryChooser = new DirectoryChooser();
        directoryChooser.setTitle("Open Telemetry Storage Directory");
        directoryChooser.setInitialDirectory(
                new File(System.getProperty("user.home")));
        File directory = directoryChooser.showDialog(stage);
        if (directory != null && directory.isDirectory()) {
            try {
                String directoryName = directory.getCanonicalPath();
                txtStorageDirectory.setText(directoryName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } 
    }
    
    @FXML
    private void resetAll() {
        btnStartCapture.setDisable(false);
        btnStartCapture.setDefaultButton(true);
        btnEndCapture.setDisable(true);
        btnEndCapture.setDefaultButton(false);
        txtOutput.setEditable(false);
        txtOutput.setText("Ready to capture...");
    }
    
    @FXML
    private void startCapture() throws IOException {
        btnStartCapture.setDisable(true);
        btnStartCapture.setDefaultButton(false);
        btnEndCapture.setDisable(false);
        btnEndCapture.setDefaultButton(true);
        txtOutput.setText(txtOutput.getText()+"\nBeginning capture.");
        
        LocalDateTime date = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss");
        timestamp = date.format(formatter);
        
        File directory = new File(txtStorageDirectory.getText()+File.separator+timestamp);
        boolean success = directory.mkdir();
        if (success) {
            txtOutput.setText(txtOutput.getText()+"\nSaving packets to "+directory.toString());   
            getPacket(timestamp);
        } else {
            txtOutput.setText(txtOutput.getText()+"\nError creating "+directory.toString()+". Packet capture aborted");
            btnEndCapture.setDisable(true);
            btnEndCapture.setDefaultButton(false);
            btnStartCapture.setDisable(false);
            btnStartCapture.setDefaultButton(true);
        }
    }

    @FXML
    private void getPacket(String directory) throws SocketException, IOException {
        DatagramSocket socket = new DatagramSocket(5606, InetAddress.getByName("0.0.0.0"));

        Runnable captureThread = new Runnable() {
            @Override
            public void run() {
                Integer i = new Integer(1);
                while (capture) {
                    byte[] buf = new byte[2048];
                    DatagramPacket packet = new DatagramPacket(buf, buf.length);
                    try {
                        socket.receive(packet);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    try {
                        FileWriter file = new FileWriter(txtStorageDirectory.getText() + File.separator + directory + File.separator + "pdata" + i.toString());
                        file.write(new String(packet.getData(), 0, packet.getLength(), "UTF-8"));
                        file.flush();
                        file.close();
                        i += 1;
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        capture = true;
        thingThread = new Thread(captureThread);
        thingThread.start();
    }
            
    @FXML
    private void endCapture() {
        btnEndCapture.setDisable(true);
        btnEndCapture.setDefaultButton(false);
        btnStartCapture.setDisable(false);
        btnStartCapture.setDefaultButton(true);
        txtOutput.setText(txtOutput.getText()+"\nEnding capture.");

        capture = false;
    }
}

@@ -228,115 +228,9 @@
 .io.
-File;%0D%0Aimport java.io.FileWriter;%0D%0Aimport java.io.IOException;%0D%0Aimport java.io.UnsupportedEncodingException
+*
 ;%0D%0Ai
@@ -3861,22 +3861,9 @@
 i = 
-new Integer(1)
+0
 ;%0D%0A 
@@ -4277,22 +4277,28 @@
     File
-Writer
+OutputStream
  file = 
@@ -4309,14 +4309,20 @@
 File
-Writer
+OutputStream
 (txt
@@ -4461,40 +4461,118 @@
 ite(
-new String(packet.getData(), 0, 
+buf, 0, packet.getLength());%0D%0A                        txtOutput.setText(txtOutput.getText()+%22%5CnPacket Size: %22+
 pack
@@ -4589,18 +4589,8 @@
 th()
-, %22UTF-8%22)
 );%0D%0A

Fix for packet storage.