From fd803affb606b29482ff09302a52b7034be357f2 Mon Sep 17 00:00:00 2001
From: Letitia Li <letitia.li@telecom-paristech.fr>
Date: Mon, 24 Jul 2017 15:07:23 +0200
Subject: [PATCH] automatic backtracing for latency

---
 src/main/java/ui/MainGUI.java                 | 16 ++++++++
 .../JFrameAvatarInteractiveSimulation.java    |  3 +-
 .../ui/avatarsmd/AvatarSMDReceiveSignal.java  | 11 ++++--
 .../ui/avatarsmd/AvatarSMDSendSignal.java     | 32 ++++++++++++++-
 .../JFrameInteractiveSimulation.java          |  2 +
 src/main/java/ui/tmlad/TMLADReadChannel.java  |  8 +++-
 src/main/java/ui/tmlad/TMLADWriteChannel.java | 39 ++++++++++++++++++-
 7 files changed, 102 insertions(+), 9 deletions(-)

diff --git a/src/main/java/ui/MainGUI.java b/src/main/java/ui/MainGUI.java
index 9984a21907..3187ab3924 100644
--- a/src/main/java/ui/MainGUI.java
+++ b/src/main/java/ui/MainGUI.java
@@ -308,6 +308,7 @@ public  class MainGUI implements ActionListener, WindowListener, KeyListener, Pe
     private ArrayList<RunningInfo> runningIDs;
     private ArrayList<LoadInfo> loadIDs;
     private ConcurrentHashMap<Integer, ArrayList<SimulationTransaction>> transactionMap = new ConcurrentHashMap<Integer, ArrayList<SimulationTransaction>>();
+	private ConcurrentHashMap<Integer, String[]> latencyMap = new ConcurrentHashMap<Integer, String[]>();
     private ConcurrentHashMap<String, String> statusMap = new ConcurrentHashMap<String, String>();
     private JFrameInteractiveSimulation jfis;
     private JFrameAvatarInteractiveSimulation jfais;
@@ -7646,6 +7647,21 @@ public  class MainGUI implements ActionListener, WindowListener, KeyListener, Pe
 
         return transactionMap.get(id);
     }
+
+	public synchronized void addLatencyVals(int id, String[] latency){
+		if (latencyMap!=null){
+			latencyMap.put(id, latency);
+		}
+	//	System.out.println(latencyMap);
+	}
+
+	public synchronized String[] getLatencyVals(int id){
+	//	System.out.println(id + " " + latencyMap);
+		if (latencyMap!=null){
+			return latencyMap.get(id);
+		}
+		return null;
+	}
     public synchronized ConcurrentHashMap<String,String> getStatus(int id){
         if (statusMap == null) {
             return null;
diff --git a/src/main/java/ui/avatarinteractivesimulation/JFrameAvatarInteractiveSimulation.java b/src/main/java/ui/avatarinteractivesimulation/JFrameAvatarInteractiveSimulation.java
index cb7b3bac55..f83804b469 100755
--- a/src/main/java/ui/avatarinteractivesimulation/JFrameAvatarInteractiveSimulation.java
+++ b/src/main/java/ui/avatarinteractivesimulation/JFrameAvatarInteractiveSimulation.java
@@ -1482,7 +1482,7 @@ public  class JFrameAvatarInteractiveSimulation extends JFrame implements Avatar
 									minTimes.add(time);
 								}
 							}
-							System.out.println(transTimes.get(st1) + " " + transTimes.get(st2) + " " + minTimes);
+						//	System.out.println(transTimes.get(st1) + " " + transTimes.get(st2) + " " + minTimes);
 							if (minTimes.size()>0){
                                     int sum=0;
                                     sl.setMinTime(Integer.toString(Collections.min(minTimes)));
@@ -1499,6 +1499,7 @@ public  class JFrameAvatarInteractiveSimulation extends JFrame implements Avatar
                                     stdev = Math.sqrt(stdev);
                                     sl.setAverageTime(String.format("%.1f",average));
                                     sl.setStDev(String.format("%.1f",stdev));
+									mgui.addLatencyVals(Integer.valueOf(st2.split(":")[1]), new String[]{st1, Integer.toString(Collections.max(minTimes))});
                             }
                             	latencies.add(sl);
 							
diff --git a/src/main/java/ui/avatarsmd/AvatarSMDReceiveSignal.java b/src/main/java/ui/avatarsmd/AvatarSMDReceiveSignal.java
index 4c8a0abb97..f4924f21ce 100644
--- a/src/main/java/ui/avatarsmd/AvatarSMDReceiveSignal.java
+++ b/src/main/java/ui/avatarsmd/AvatarSMDReceiveSignal.java
@@ -100,7 +100,7 @@ public class AvatarSMDReceiveSignal extends AvatarSMDBasicComponent implements C
         
         myImageIcon = IconManager.imgic908;
 		latencyVals = new HashMap<String, String>();
-		latencyVals.put("sendChannel: distanceData", "15");
+		//latencyVals.put("sendChannel: sensorData", "15");
     }
     
 
@@ -166,8 +166,13 @@ public class AvatarSMDReceiveSignal extends AvatarSMDBasicComponent implements C
 		   
         //g.drawString("sig()", x+(width-w) / 2, y);
         g.drawString(value, x + linebreak + textX1, y + textY);
+		//System.out.println(getDIPLOID());
 		if (getCheckLatency()){
-			drawLatencyInformation(g);
+			String[] latency =tdp.getMGUI().getLatencyVals(getAVATARID());
+			if (latency!=null){
+				addLatency(latency[0], latency[1]);
+				drawLatencyInformation(g);
+			}
 		}
 		
     }
@@ -178,7 +183,7 @@ public class AvatarSMDReceiveSignal extends AvatarSMDBasicComponent implements C
 			g.drawRect(x-latencyX-w, y-latencyY-textHeight, w+4, textHeight); 
 			g.drawLine(x,y,x-latencyX, y-latencyY);
 			Color c = g.getColor();
-			g.setColor(Color.ORANGE);
+			g.setColor(Color.RED);
 			g.drawString(latencyVals.get(s), x-latencyX/2, y-latencyY/2);
 			g.setColor(c);
 		}
diff --git a/src/main/java/ui/avatarsmd/AvatarSMDSendSignal.java b/src/main/java/ui/avatarsmd/AvatarSMDSendSignal.java
index ce9a985719..8716a22c9e 100644
--- a/src/main/java/ui/avatarsmd/AvatarSMDSendSignal.java
+++ b/src/main/java/ui/avatarsmd/AvatarSMDSendSignal.java
@@ -50,6 +50,7 @@ import javax.swing.*;
 import java.awt.*;
 import java.awt.geom.Line2D;
 import java.util.LinkedList;
+import java.util.HashMap;
 
 /**
    * Class AvatarSMDSendSignal
@@ -65,6 +66,12 @@ public class AvatarSMDSendSignal extends AvatarSMDBasicComponent implements Chec
     protected int arc = 5;
     protected int linebreak = 10;
 
+	private HashMap<String, String> latencyVals;
+	
+	protected int latencyX=30;
+	protected int latencyY=10;
+	protected int textWidth=10;
+	protected int textHeight=20;
 
     protected int stateOfError = 0; // Not yet checked
 
@@ -91,8 +98,11 @@ public class AvatarSMDSendSignal extends AvatarSMDBasicComponent implements Chec
         //makeValue();
 
         myImageIcon = IconManager.imgic904;
+		latencyVals = new HashMap<String, String>();
     }
-
+	public void addLatency(String name, String num){
+		latencyVals.put(name,num);
+	}
     public void internalDrawing(Graphics g) {
 
         int w  = g.getFontMetrics().stringWidth(value);
@@ -152,9 +162,29 @@ public class AvatarSMDSendSignal extends AvatarSMDBasicComponent implements Chec
         //g.drawString("sig()", x+(width-w) / 2, y);
         g.drawString(value, x + (width - w) / 2 , y + textY);
 
+		if (getCheckLatency()){
 
+			String[] latency =tdp.getMGUI().getLatencyVals(getAVATARID());
+			if (latency!=null){
+				addLatency(latency[0], latency[1]);
+				drawLatencyInformation(g);
+			}
+		}
     }
 
+	public void drawLatencyInformation(Graphics g){
+		for (String s:latencyVals.keySet()){
+			int w  = g.getFontMetrics().stringWidth(s);
+			g.drawString(s, x-latencyX-w+1, y-latencyY-2);
+			g.drawRect(x-latencyX-w, y-latencyY-textHeight, w+4, textHeight); 
+			g.drawLine(x,y,x-latencyX, y-latencyY);
+			Color c = g.getColor();
+			g.setColor(Color.RED);
+			g.drawString(latencyVals.get(s), x-latencyX/2, y-latencyY/2);
+			g.setColor(c);
+		}
+	}
+
     public TGComponent isOnMe(int _x, int _y) {
         if (GraphicLib.isInRectangle(_x, _y, x, y, width, height)) {
             return this;
diff --git a/src/main/java/ui/interactivesimulation/JFrameInteractiveSimulation.java b/src/main/java/ui/interactivesimulation/JFrameInteractiveSimulation.java
index d6423ca5a6..0772c328fb 100755
--- a/src/main/java/ui/interactivesimulation/JFrameInteractiveSimulation.java
+++ b/src/main/java/ui/interactivesimulation/JFrameInteractiveSimulation.java
@@ -2671,6 +2671,8 @@ public class JFrameInteractiveSimulation extends JFrame implements ActionListene
                                     stdev = Math.sqrt(stdev);
                                     sl.setAverageTime(String.format("%.1f",average));
                                     sl.setStDev(String.format("%.1f",stdev));
+									
+									mgui.addLatencyVals(Integer.valueOf(st2), new String[]{sl.getTransaction1(), Integer.toString(Collections.max(minTimes))}); 
                                 }
                             }
 
diff --git a/src/main/java/ui/tmlad/TMLADReadChannel.java b/src/main/java/ui/tmlad/TMLADReadChannel.java
index 6992df5278..b65ff5fe14 100755
--- a/src/main/java/ui/tmlad/TMLADReadChannel.java
+++ b/src/main/java/ui/tmlad/TMLADReadChannel.java
@@ -108,7 +108,7 @@ public class TMLADReadChannel extends TGCWithoutInternalComponent implements Che
 
         myImageIcon = IconManager.imgic906;
 		latencyVals = new HashMap<String, String>();
-		latencyVals.put("sendChannel: distanceData", "3");
+		//latencyVals.put("sendChannel: sensorData", "3");
 
     }
 
@@ -171,7 +171,11 @@ public class TMLADReadChannel extends TGCWithoutInternalComponent implements Che
 	}
 		drawReachabilityInformation(g);
 		if (getCheckLatency()){
-			drawLatencyInformation(g);	
+			String[] latency =tdp.getMGUI().getLatencyVals(getDIPLOID());
+			if (latency!=null){
+				addLatency(latency[0], latency[1]);
+				drawLatencyInformation(g);	
+			}
 		}
     }
 
diff --git a/src/main/java/ui/tmlad/TMLADWriteChannel.java b/src/main/java/ui/tmlad/TMLADWriteChannel.java
index b7b49c457a..758e861ad3 100755
--- a/src/main/java/ui/tmlad/TMLADWriteChannel.java
+++ b/src/main/java/ui/tmlad/TMLADWriteChannel.java
@@ -54,6 +54,7 @@ import javax.swing.*;
 import java.awt.*;
 import java.awt.geom.Line2D;
 import java.util.ArrayList;
+import java.util.HashMap;
 
 /**
    * Class TMLADWriteChannel
@@ -69,6 +70,15 @@ public class TMLADWriteChannel extends TGCWithoutInternalComponent implements Ch
     protected int arc = 5;
     protected int linebreak = 10;
 
+
+	private HashMap<String, String> latencyVals;
+		
+	protected int latencyX=30;
+	protected int latencyY=10;
+	protected int textWidth=10;
+	protected int textHeight=20;
+
+
     protected String channelName = "ch";
     protected String nbOfSamples= "1";
     public String securityContext = "";
@@ -87,6 +97,8 @@ public class TMLADWriteChannel extends TGCWithoutInternalComponent implements Ch
         height = 20;
         minWidth = 30;
 
+
+
         nbConnectingPoint = 2;
         connectingPoint = new TGConnectingPoint[2];
         connectingPoint[0] = new TGConnectingPointTMLAD(this, 0, -lineLength, true, false, 0.5, 0.0);
@@ -100,6 +112,7 @@ public class TMLADWriteChannel extends TGCWithoutInternalComponent implements Ch
         name = "write channel";
 
         myImageIcon = IconManager.imgic900;
+		latencyVals = new HashMap<String, String>();
     }
 
     public void internalDrawing(Graphics g) {
@@ -155,10 +168,32 @@ public class TMLADWriteChannel extends TGCWithoutInternalComponent implements Ch
 	if (!securityContext.equals("")){
 	    g.drawString("sec:"+securityContext, x+3*width/4, y+height+textY);
 	}
-	drawReachabilityInformation(g);
-
+				
+		if (getCheckLatency()){
+			String[] latency =tdp.getMGUI().getLatencyVals(getDIPLOID());
+			if (latency!=null){
+				addLatency(latency[0], latency[1]);
+				drawLatencyInformation(g);	
+			}
+		}
+		drawReachabilityInformation(g);
     }
 
+	public void drawLatencyInformation(Graphics g){
+		for (String s:latencyVals.keySet()){
+			int w  = g.getFontMetrics().stringWidth(s);
+			g.drawString(s, x-latencyX-w+1, y-latencyY-2);
+			g.drawRect(x-latencyX-w, y-latencyY-textHeight, w+4, textHeight); 
+			g.drawLine(x,y,x-latencyX, y-latencyY);
+			g.drawString(latencyVals.get(s), x-latencyX/2, y-latencyY/2);
+		}
+	}
+
+
+	public void addLatency(String name, String num){
+		latencyVals.put(name,num);
+	}
+
     public void drawReachabilityInformation(Graphics g) {
         if (reachabilityInformation > 0) {
 
-- 
GitLab