FiveGVisualization.java 4.54 KB
Newer Older
Björn Richerzhagen's avatar
Björn Richerzhagen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
 *
 * This file is part of PeerfactSim.KOM.
 * 
 * PeerfactSim.KOM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * PeerfactSim.KOM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PeerfactSim.KOM.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package de.tud.kom.p2psim.impl.topology.views.visualization.world;

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

import javax.swing.JComponent;

import de.tud.kom.p2psim.impl.topology.views.FiveGTopologyView;
import de.tud.kom.p2psim.impl.topology.views.VisualizationTopologyView.VisualizationInjector;
import de.tud.kom.p2psim.impl.topology.views.fiveg.AbstractGridBasedTopologyDatabase;
import de.tud.kom.p2psim.impl.topology.views.fiveg.FiveGTopologyDatabase.Entry;
import de.tudarmstadt.maki.simonstrator.api.Time;

/**
 * Visualization for the {@link FiveGTopologyView}
 * 
 * @author Bjoern Richerzhagen
 * @version 1.0, Nov 5, 2015
 */
public class FiveGVisualization extends JComponent {

	protected BufferedImage image;

	protected volatile boolean needsRedraw = true;

	private final AbstractGridBasedTopologyDatabase database;


	public FiveGVisualization(AbstractGridBasedTopologyDatabase database) {
		setBounds(0, 0, VisualizationInjector.getWorldX(),
				VisualizationInjector.getWorldY());
		setOpaque(true);
		setVisible(true);

		this.database = database;
		image = new BufferedImage(VisualizationInjector.getWorldX(),
				VisualizationInjector.getWorldY(), BufferedImage.TYPE_INT_ARGB);
	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);

		if (needsRedraw) {
			redraw();
		}

		Graphics2D g2 = (Graphics2D) g;
		g2.drawImage(image, 0, 0, null);
	}

	protected void redraw() {
		needsRedraw = false;

		Graphics2D g2 = (Graphics2D) image.getGraphics();

		Composite c = g2.getComposite();
		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
		Rectangle2D.Double rect = new Rectangle2D.Double(0, 0,
				VisualizationInjector.getWorldX(),
				VisualizationInjector.getWorldY());
		g2.fill(rect);
		g2.setComposite(c);

		try {
			drawEntries(g2);
		} catch (RuntimeException e) {
			needsRedraw = true;
		}
	}

	private void drawEntries(Graphics2D g2) {
		// Iterate over grid coordinates
		int stepSize = database.getGridSize();

		double maxLatency = 0;
		double minLatency = Double.MAX_VALUE;
		boolean isUpload = false;

		for (int x = 0; x < VisualizationInjector.getWorldX(); x += stepSize) {
			for (int y = 0; y < VisualizationInjector
					.getWorldY(); y += stepSize) {
				// TODO add checkbox for cloudlets?
				Entry entry = database.getEntryFor(database.getSegmentID(x, y),
						false);
				if (entry.getLatency(isUpload) > maxLatency) {
					maxLatency = entry.getLatency(isUpload);
				}
				if (entry.getLatency(isUpload) < minLatency) {
					minLatency = entry.getLatency(isUpload);
				}
			}
		}
		
		for (int x = 0; x < VisualizationInjector.getWorldX(); x += stepSize) {
			for (int y = 0; y < VisualizationInjector
					.getWorldY(); y += stepSize) {

				// TODO add checkbox for cloudlets?
				Entry entry = database.getEntryFor(database.getSegmentID(x, y),
						false);

				// TODO add checkbox for upload/download toggle?

				// Latency
				double latencyFactor = (entry.getLatency(isUpload) - minLatency)
						/ (maxLatency - minLatency);
				g2.setColor(
						new Color(255, 0, 0, 10 + (int) (40 * latencyFactor)));
				g2.fillRect(x, y, stepSize, stepSize);

				// Drop-Prob
				g2.setColor(new Color(255, 0, 0,
						10 + (int) (100 * entry.getDropProbability(isUpload))));
				g2.setStroke(new BasicStroke(
						(float) (10 * entry.getDropProbability(isUpload))));
				g2.drawRect(x, y, stepSize, stepSize);
				g2.setColor(new Color(0, 0, 0, 100));
				g2.drawString(
						"L: " + entry.getLatency(isUpload) / Time.MILLISECOND,
						x + 10, y + 10);
				g2.drawString("D: "
						+ (int) (entry.getDropProbability(isUpload) * 100),
						x + 10, y + 25);
			}
		}
		
	}

}