Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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 src.Coordinator;
- import java.io.*;
- import java.lang.reflect.Type;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.ResourceBundle;
- import java.util.Optional;
- import java.util.Scanner;
- import javafx.fxml.FXML;
- import javafx.fxml.Initializable;
- import javafx.scene.control.*;
- import javafx.scene.image.ImageView;
- import javafx.scene.control.Alert.AlertType;
- import javafx.scene.input.MouseEvent;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Line;
- import javafx.util.Pair;
- import src.Utils.Coord2PeerControlMeasurement;
- import src.Utils.Coord2PeerControlSpecial;
- import src.Coordinator.CoordinatorDataHandler;
- import src.Utils.TypeID;
- /**
- * FXML Controller class
- *
- * @author
- */
- public class MainPaneController implements Initializable, Serializable {
- private List speedValues;
- private List layer4Values;
- private boolean startBasic;
- private boolean startAdvanced;
- private boolean connected;
- private boolean fastScan;
- //--------- BASIC PANEL ---------
- @FXML
- private Button peer_1;
- @FXML
- private Line line_1_2;
- @FXML
- private ImageView peer_loading;
- @FXML
- private ComboBox layer4;
- @FXML
- private TextField packetNumber;
- @FXML
- private TextField packetSize;
- @FXML
- private TextField simulationTime;
- @FXML
- private TextField portNumber;
- @FXML
- private TextField startDelay;
- @FXML
- private TextArea advancedResult;
- @FXML
- private ChoiceBox probingChoiceBox;
- @FXML
- private ChoiceBox measurementChoiceBox;
- @FXML
- private TextArea probingTextArea;
- @FXML
- private TextArea alarmTextArea;
- @FXML
- private TabPane tabPane;
- @FXML
- private Tab resultsTab;
- private int refreshRate = 5000;
- private static HashMap<TypeID, Pair<String, String>> resultsMap = new HashMap<>();
- public MainPaneController() {
- }
- /**
- * Initializes the controller class.
- */
- @Override
- public void initialize(URL url, ResourceBundle rb) {
- peer_loading.setVisible(false);
- }
- //--------- BASIC PANEL ---------
- private String destPeer(){
- TextInputDialog dialog = new TextInputDialog("10.0.0.2");
- dialog.setTitle("Select a peer IP from the topology");
- dialog.setHeaderText("Destination src.Peer Selection");
- dialog.setContentText("Please enter the destination peer ip:");
- Optional<String> result = dialog.showAndWait();
- if (result.isPresent()){
- System.out.println("Dest peer: " + result.get());
- }
- return result.get();
- //result.ifPresent(name -> System.out.println("Your name: " + name));
- }
- private int probeID(){
- TextInputDialog dialog = new TextInputDialog("1");
- dialog.setTitle("Choose a probe id from the current probings");
- dialog.setHeaderText("Probe ID Selection");
- dialog.setContentText("Please enter the probe id:");
- Optional<String> result = dialog.showAndWait();
- if (result.isPresent()){
- System.out.println("Probe id: " + result.get());
- }
- return Integer.parseInt(result.get());
- //result.ifPresent(name -> System.out.println("Your name: " + name));
- }
- private char probeTYPE(){
- TextInputDialog dialog = new TextInputDialog("R");
- dialog.setTitle("Choose a probe type from the current probings");
- dialog.setHeaderText("Probe Type Selection");
- dialog.setContentText("Please enter the probe type (R, r, J or P):");
- Optional<String> result = dialog.showAndWait();
- if (result.isPresent()){
- System.out.println("Probe type: " + result.get());
- }
- return result.get().charAt(0);
- //result.ifPresent(name -> System.out.println("Your name: " + name));
- }
- private String numPackets(){
- TextInputDialog dialog = new TextInputDialog("5");
- dialog.setTitle("Choose the number of packets you wish to send");
- dialog.setHeaderText("Number of Packets Selection");
- dialog.setContentText("Please enter the number of packets to send:");
- Optional<String> result = dialog.showAndWait();
- if (result.isPresent()){
- System.out.println("Nr of packets: " + result.get());
- }
- return result.get();
- //result.ifPresent(name -> System.out.println("Your name: " + name));
- }
- private String packetSize(){
- TextInputDialog dialog = new TextInputDialog("56");
- dialog.setTitle("Choose the size of the packets you wish to send");
- dialog.setHeaderText("Size of Packets Selection");
- dialog.setContentText("Please enter the size of the packets:");
- Optional<String> result = dialog.showAndWait();
- if (result.isPresent()){
- System.out.println("Size of packets: " + result.get());
- }
- return result.get();
- //result.ifPresent(name -> System.out.println("Your name: " + name));
- }
- private String feedbackTime(){
- TextInputDialog dialog = new TextInputDialog("20");
- dialog.setTitle("Choose the feedback time of the probing");
- dialog.setHeaderText("Feedback Time Selection");
- dialog.setContentText("Please enter the feedback time you want:");
- Optional<String> result = dialog.showAndWait();
- if (result.isPresent()){
- System.out.println("Feedback Time: " + result.get());
- }
- return result.get();
- //result.ifPresent(name -> System.out.println("Your name: " + name));
- }
- private char alarmDef(){
- List<String> choices = new ArrayList<>();
- choices.add("With Alarm");
- choices.add("No Alarm");
- ChoiceDialog<String> dialog2 = new ChoiceDialog<>("With Alarm", choices);
- dialog2.setTitle("Choice Dialog");
- dialog2.setHeaderText("Alarm definition");
- dialog2.setContentText("Alarm: ");
- // Traditional way to get the response value.
- Optional<String> result2 = dialog2.showAndWait();
- if (result2.isPresent()){
- System.out.println("Alarm: " + result2.get());
- }
- char choice = '0';
- switch (result2.get()) {
- case "With Alarm": {
- choice = '1';
- break;
- }
- case "No Alarm": {
- choice = '0';
- break;
- }
- default: {
- System.out.println("ERROR! Invalid Alarm!");
- break;
- }
- }
- return choice;
- }
- private char selectProtocol(){
- List<String> choices = new ArrayList<>();
- choices.add("ICMP");
- choices.add("NO ICMP");
- ChoiceDialog<String> dialog2 = new ChoiceDialog<>("ICMP", choices);
- dialog2.setTitle("Choice Dialog");
- dialog2.setHeaderText("Measurement definition");
- dialog2.setContentText("Protocol: ");
- // Traditional way to get the response value.
- Optional<String> result2 = dialog2.showAndWait();
- if (result2.isPresent()){
- System.out.println("Protocol selected: " + result2.get());
- }
- char choice = '0';
- switch (result2.get()) {
- case "ICMP": {
- choice = '1';
- break;
- }
- case "NO ICMP": {
- choice = '0';
- break;
- }
- default: {
- System.out.println("ERROR! Invalid Protocol!");
- break;
- }
- }
- return choice;
- }
- private char measurementMode(){
- List<String> choices2 = new ArrayList<>();
- choices2.add("Atomic");
- choices2.add("Normal Loop");
- choices2.add("Statistical Loop");
- ChoiceDialog<String> dialog3 = new ChoiceDialog<>("Atomic", choices2);
- dialog3.setTitle("Choice Dialog");
- dialog3.setHeaderText("Measurement definition");
- dialog3.setContentText("Measurement Mode: ");
- // dialog3.getDialogPane().getStylesheets().add(getClass().getResource("../style/myDialogs.css").toExternalForm());
- // dialog3.getDialogPane().getStyleClass().add("myDialog");
- Optional<String> result3 = dialog3.showAndWait();
- if (result3.isPresent()){
- System.out.println("Your choice: " + result3.get());
- }
- char choice = '0';
- switch (result3.get()) {
- case "Atomic": {
- choice = '0';
- break;
- }
- case "Normal Loop": {
- choice = '1';
- break;
- }
- case "Statistical Loop": {
- choice = '2';
- break;
- }
- default: {
- System.out.println("ERROR! Invalid Measurement Mode!");
- break;
- }
- }
- return choice;
- }
- private char measurementType(){
- List<String> choices2 = new ArrayList<>();
- choices2.add("RTT");
- choices2.add("Packet Loss");
- choices2.add("Jitter");
- choices2.add("Route Path");
- ChoiceDialog<String> dialog4 = new ChoiceDialog<>("RTT", choices2);
- dialog4.setTitle("Choice Dialog");
- dialog4.setHeaderText("Measurement definition");
- dialog4.setContentText("Measurement Type: ");
- // dialog3.getDialogPane().getStylesheets().add(getClass().getResource("../style/myDialogs.css").toExternalForm());
- // dialog3.getDialogPane().getStyleClass().add("myDialog");
- Optional<String> result3 = dialog4.showAndWait();
- if (result3.isPresent()){
- System.out.println("Your choice: " + result3.get());
- }
- char choice = 'R';
- switch (result3.get()) {
- case "RTT": {
- choice = 'R';
- break;
- }
- case "Packet Loss": {
- choice = 'P';
- break;
- }
- case "Jitter": {
- choice = 'J';
- break;
- }
- case "Route Path": {
- choice = 'r';
- break;
- }
- default: {
- System.out.println("ERROR! Invalid Measurement Type!");
- break;
- }
- }
- return choice;
- }
- private String alarmConditions(){
- TextInputDialog dialog = new TextInputDialog(">0.5");
- dialog.setTitle("Select the alarm contidions you want");
- dialog.setHeaderText("Alarm Condition Selection");
- dialog.setContentText("Please enter the alarm condition (>, <, <=, >=, = or !=)");
- Optional<String> result = dialog.showAndWait();
- if (result.isPresent()){
- System.out.println("Alarm condition: " + result.get());
- }
- return result.get();
- //result.ifPresent(name -> System.out.println("Your name: " + name));
- }
- @FXML
- private void peer_1Handler(MouseEvent event) throws IOException, ClassNotFoundException {
- ServerSocket dataServerSocket = null;
- Socket sv_peer_control_socket = null;
- int opt=-1, opt2=-1, opt3=-1;
- ObjectInputStream in = null;
- ObjectOutputStream out = null;
- Scanner s = new Scanner(System.in);
- Object request = null;
- String peerOrigem = null;
- Socket socket = null;
- HashMap<Character,Integer> probeMap = new HashMap<>();
- //socket = new Socket(event.getPickResult().getIntersectedNode().getId(), 8081);
- socket = new Socket("localhost", 8081);
- out = new ObjectOutputStream(socket.getOutputStream());
- in = new ObjectInputStream(socket.getInputStream());
- /* Enviar o endereco do host para o peer saber onde enviar os dados*/
- //out.writeObject(getHostAddress(peerOrigem));
- out.writeObject("localhost");
- out.flush();
- //MUDAR O NOME DELES PARA OS IPS
- //System.out.println(System.out.println(event.getClass().getName().toString()));
- System.out.println(event.getPickResult().getIntersectedNode());
- System.out.println(event.getPickResult().getIntersectedNode().getId());
- Alert alert = new Alert(AlertType.CONFIRMATION);
- alert.setTitle("src/Peer");
- alert.setHeaderText("Create a new probing or cancel a probing.");
- alert.setContentText("Choose your option.");
- ButtonType buttonTypeOne = new ButtonType("New Probing");
- ButtonType buttonTypeTwo = new ButtonType("Cancel a Probing");
- alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeTwo);
- Optional<ButtonType> result = alert.showAndWait();
- if (result.get() == buttonTypeOne){
- String peerDest = destPeer();
- char measurementType = measurementType();
- char ICMP = selectProtocol();
- char loop = measurementMode();
- int probeID = 1;
- int nrpackets = 1;
- int packetsize = 56;
- if(loop == '0' || (measurementType == 'P' && loop == '1')){
- nrpackets = Integer.parseInt(numPackets());
- packetsize = Integer.parseInt(packetSize());
- }
- else{
- packetsize = Integer.parseInt(packetSize());
- }
- char ALARM = alarmDef();
- String feedbackTime;
- if(measurementType == 'r'){
- probeID = incrementProbe_ID(probeMap, 'r');
- if(loop == '0') {
- request = new Coord2PeerControlMeasurement(probeID, 1, 'r', peerOrigem, peerDest, loop);
- }
- if(loop == '1'){
- feedbackTime = feedbackTime();
- request = new Coord2PeerControlMeasurement(probeID,1,'r', peerOrigem, peerDest, loop, feedbackTime, ALARM, "!!");
- }
- if(loop == '2') {
- request = new Coord2PeerControlMeasurement(probeID, 1, 'r', peerOrigem, peerDest, loop, ALARM, "!!");
- }
- }
- if(ALARM == '0' && loop == '0') {
- probeID = incrementProbe_ID(probeMap, measurementType);
- request = new Coord2PeerControlMeasurement(probeID, 1, measurementType, peerOrigem, peerDest, nrpackets, packetsize, loop, ICMP);
- }
- if(ALARM == '1' && loop == '0'){
- probeID = incrementProbe_ID(probeMap, measurementType);
- String alarmCondition = alarmConditions();
- request = new Coord2PeerControlMeasurement(probeID,1,measurementType,peerOrigem, peerDest, nrpackets, packetsize, loop, ICMP, ALARM, alarmCondition);
- }
- if(ALARM == '0' && loop == '1'){
- probeID = incrementProbe_ID(probeMap, measurementType);
- feedbackTime = feedbackTime();
- request = new Coord2PeerControlMeasurement(probeID,1,measurementType,peerOrigem, peerDest, nrpackets, packetsize, loop, ICMP, feedbackTime);
- }
- if(ALARM == '1' && loop == '1'){
- probeID = incrementProbe_ID(probeMap, measurementType);
- feedbackTime = feedbackTime();
- String alarmCondition = alarmConditions();
- request = new Coord2PeerControlMeasurement(probeID,1,measurementType, peerOrigem, peerDest, nrpackets, packetsize, loop, ICMP, feedbackTime, ALARM, alarmCondition);
- }
- if(ALARM == '0' && loop == '2'){
- probeID = incrementProbe_ID(probeMap, measurementType);
- request = new Coord2PeerControlMeasurement(probeID,1,measurementType, peerOrigem, peerDest, 1, packetsize, loop, ICMP);
- }
- if(ALARM == '1' && loop == '2'){
- probeID = incrementProbe_ID(probeMap, measurementType);
- String alarmCondition = alarmConditions();
- request = new Coord2PeerControlMeasurement(probeID,1,measurementType, peerOrigem, peerDest, 1, packetsize, loop, ICMP, ALARM, alarmCondition);
- }
- out.writeObject(request);
- out.flush();
- byte[] buffer = new byte[2048];
- StringBuilder sb = new StringBuilder();
- System.out.println(in.readObject().toString());
- /* Atribuir request a null para o novo request ser preenchido */
- request = null;
- // Refresh na lista de probes e measurements na aba results
- this.resultsMap.put(new TypeID(measurementType,probeID), new Pair<>("", ""));
- // Adicionar às choice boxes
- measurementChoiceBox.getItems().add(measurementType);
- probingChoiceBox.getItems().add(probeID);
- // fechar socket
- socket.close();
- line_1_2.setStroke(Color.GREEN);
- peer_loading.setVisible(true);
- } else if (result.get() == buttonTypeTwo) {
- peer_loading.setVisible(false);
- line_1_2.setStroke(Color.WHITE);
- // CANCEL
- request = new Coord2PeerControlSpecial(probeID(), 'C', probeTYPE());
- out.writeObject(request);
- out.flush();
- byte[] buffer = new byte[2048];
- StringBuilder sb = new StringBuilder();
- System.out.println(in.readObject().toString());
- /* Atribuir request a null para o novo request ser preenchido */
- request = null;
- socket.close();
- } else {
- // ... CLOSED DIALOG
- }
- }
- public static String getHostAddress(String peerOrigem){
- String res=null,sb;
- try{
- Process p = Runtime.getRuntime().exec( "ip route get " + peerOrigem);
- BufferedReader stdInput = new BufferedReader(new
- InputStreamReader(p.getInputStream()));
- if((sb = stdInput.readLine()) != null){
- res = sb.split("src ")[1].split(" +")[0];
- }
- }catch(Exception e){ e.printStackTrace(); }
- return res;
- }
- public int incrementProbe_ID(HashMap<Character, Integer> map, char type){
- if(map.containsKey(type)){
- int id = map.get(type)+1;
- map.put(type,id);
- return id;
- }
- else{
- map.put(type,1);
- return 1;
- }
- }
- public static boolean isNum(String str){
- try{
- int d = Integer.parseInt(str);
- }
- catch(NumberFormatException e){
- return false;
- }
- return true;
- }
- /******************************************************************************************************************/
- /** EVENTS AND RESULTS HANDLING**/
- public Pair<String,String> refreshResults(int probeID,char type){
- Pair <String,String> p;
- String res = new String("");
- String alarm = new String("");
- if (probeID != -1 && type != 'Z') {
- p = this.resultsMap.get(new TypeID(type, probeID));
- }
- else if(probeID != -1 && type == 'Z'){
- for (TypeID typeID : this.resultsMap.keySet()) {
- if(typeID.getPROBE_ID() == probeID) {
- res = res.concat(this.resultsMap.get(typeID).getKey());
- alarm = alarm.concat(this.resultsMap.get(typeID).getValue());
- }
- }
- p = new Pair<>(res,alarm);
- }
- else if(probeID == -1 && type != 'Z'){
- for (TypeID typeID : this.resultsMap.keySet()) {
- if(typeID.getTYPE() == type) {
- res = res.concat(this.resultsMap.get(typeID).getKey());
- alarm = alarm.concat(this.resultsMap.get(typeID).getValue());
- }
- }
- p = new Pair<>(res,alarm);
- }
- else {
- for (Pair<String, String> aux: this.resultsMap.values()) {
- res = res.concat(aux.getKey());
- alarm = alarm.concat(aux.getValue());
- }
- p = new Pair<>(res,alarm);
- }
- return p;
- }
- public static void putOnMap(TypeID id, Pair<String, String> pair){
- String alarm;
- String res;
- System.out.println("PROBE_ID: "+id.getPROBE_ID());
- System.out.println("PAIR KEY: "+pair.getKey());
- System.out.println("SIZE: " + resultsMap.size());
- if(resultsMap.containsKey(id)){
- System.out.println(resultsMap.get(id).getKey());
- res = resultsMap.get(id).getKey() + pair.getKey();
- alarm = resultsMap.get(id).getValue() + pair.getValue();
- resultsMap.put(id, new Pair<>(res,alarm));
- }
- else{
- resultsMap.put(id, pair);
- }
- }
- @FXML
- private void resultsRefreshEvent(){
- int probeID = -1;
- char type = 'Z';
- // Receber os dados dos resultados do Coordinator e colocar no Map
- System.out.println("SIZE: "+resultsMap.size());
- if(probingChoiceBox.getSelectionModel().getSelectedItem() != null)
- probeID = (int) probingChoiceBox.getSelectionModel().getSelectedItem();
- if(measurementChoiceBox.getSelectionModel().getSelectedItem() != null)
- type = (char) measurementChoiceBox.getSelectionModel().getSelectedItem();
- Pair<String, String> p = refreshResults(probeID, type);
- // Key == RESULT
- probingTextArea.setText(p.getKey());
- // Value == ALARM
- alarmTextArea.setText(p.getValue());
- }
- // EVENTO DE ATUALIZAR A PROBING CHOICE BOX E A MEASUREMENT CHOICE BOX QUANDO UMA PROBE É EFETUADA
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement