Commit 4444c71c authored by Björn Richerzhagen's avatar Björn Richerzhagen
Browse files

Merge branch 'master' into 'releases/v2-3'

Bugfix: DAO Session handling

Fixes a session handling bug in the DAO, leading to not-terminating simulation runs.

See merge request !14
parents 7ac772c9 beebe6b5
/*
* 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.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.util.Collection;
import java.util.LinkedList;
import javax.swing.JComponent;
import de.tud.kom.p2psim.api.common.SimHost;
import de.tud.kom.p2psim.impl.topology.PositionVector;
import de.tud.kom.p2psim.impl.topology.views.VisualizationTopologyView.VisualizationInjector;
import de.tudarmstadt.maki.simonstrator.api.Event;
import de.tudarmstadt.maki.simonstrator.api.EventHandler;
import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.Oracle;
import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.component.ComponentNotAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.HostComponent;
import de.tudarmstadt.maki.simonstrator.api.component.overlay.NodeInformation;
/**
* Generic component that visualizes information from nodes implementing the
* {@link NodeInformation} interface.
*
* @author Bjoern Richerzhagen
* @version 1.0, Jul 9, 2015
*/
public class NodeInfoComponentVis extends JComponent {
protected Collection<NodeVis> nodes = new LinkedList<>();
public <T extends HostComponent> NodeInfoComponentVis(
final Class<T> componentClass) {
setBounds(0, 0, VisualizationInjector.WORLD_X,
VisualizationInjector.WORLD_Y);
setOpaque(true);
setVisible(true);
Event.scheduleWithDelay(1 * Time.MICROSECOND, new EventHandler() {
@Override
public void eventOccurred(Object content, int type) {
for (Host host : Oracle.getAllHosts()) {
try {
HostComponent c = host.getComponent(componentClass);
if (c instanceof NodeInformation) {
nodes.add(new NodeVis(host, (NodeInformation) c));
}
} catch (ComponentNotAvailableException e) {
// don't care
}
}
}
}, null, 0);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
boolean first = true;
for (NodeVis vis : nodes) {
if (first) {
vis.drawLegend(g2);
first = false;
}
vis.draw(g2);
}
}
/**
* Visualization-fragments for Node-centric visualiation-information.
*
* @author Bjoern Richerzhagen
* @version 1.0, Sep 22, 2013
*/
private class NodeVis {
private final NodeInformation nodeInfo;
private final SimHost host;
private final PositionVector loc;
private final Color[] colors = { Color.ORANGE, Color.BLUE, Color.RED,
Color.MAGENTA };
public NodeVis(Host host, NodeInformation nodeInfo) {
this.nodeInfo = nodeInfo;
this.host = (SimHost) host;
this.loc = this.host.getTopologyComponent().getRealPosition();
}
/**
* Called on one of the nodes to draw global objects such as a legend.
* Called before draw.
*
* @param g2
*/
public void drawLegend(Graphics2D g2) {
String[] dimensions = nodeInfo.getNodeColorDimensionDescriptions();
int radius = 4;
for (int dim = 0; dim < dimensions.length; dim++) {
radius += 2;
g2.setColor(Color.DARK_GRAY);
g2.drawOval(10, 20 * (dim + 1) - 10, radius * 2, radius * 2);
g2.drawString(dimensions[dim], 30, 20 * (dim + 1));
String[] colorDescs = nodeInfo.getNodeColorDescriptions(dim);
for (int i = 0; i < colorDescs.length; i++) {
g2.setColor(colors[i]);
g2.fillRect(30 + (i + 1) * 90, 20 * (dim + 1) - 10, 8, 8);
g2.setColor(Color.DARK_GRAY);
g2.drawString(colorDescs[i], 40 + (i + 1) * 90,
20 * (dim + 1));
}
}
}
public void draw(Graphics2D g2) {
Point center = loc.asPoint();
int numColors = nodeInfo.getNodeColorDimensions();
int radius = 4;
for (int color = 0; color < numColors; color++) {
int value = nodeInfo.getNodeColor(color);
radius += 2;
if (value < 0) {
continue;
}
g2.setColor(colors[value]);
g2.drawOval(center.x - radius, center.y - radius, radius * 2,
radius * 2);
}
String nodeDesc = nodeInfo.getNodeDescription();
g2.drawString(nodeDesc, center.x + 4, center.y + 4);
}
}
}
......@@ -106,6 +106,8 @@ public class DAO {
private static SessionFactory sessionFactory;
private static final ThreadLocal<Session> session = new ThreadLocal<Session>();
private static ServiceRegistry serviceRegistry;
/**
* Hibernate-only implementation to support annotated classes without active
......@@ -154,7 +156,7 @@ public class DAO {
for (Class<?> clazz : daoClasses) {
cfg.addAnnotatedClass(clazz);
}
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties()).build();
sessionFactory = cfg.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
......@@ -163,6 +165,7 @@ public class DAO {
}
if (currSession == null) {
currSession = sessionFactory.openSession();
Monitor.log(DAO.class, Level.DEBUG, "Opening hibernate session.");
DAO.session.set(currSession);
}
return currSession;
......@@ -269,8 +272,10 @@ public class DAO {
public static void close() {
// getEntityManager().close();
// DAO.entityManager.set(null);
getSession().close();
DAO.session.set(null);
Session s = getSession();
s.close();
Monitor.log(DAO.class, Level.DEBUG, "Closing hibernate session.");
DAO.session.remove();
}
/**
......@@ -363,6 +368,10 @@ public class DAO {
}
threads.poll();
}
if (sessionFactory != null) {
sessionFactory.close();
}
StandardServiceRegistryBuilder.destroy(serviceRegistry);
Monitor.log(DAO.class, Level.INFO,
"commit threads finished. \n stored " + objectCount
+ " objects in " + commitCount
......
......@@ -68,7 +68,7 @@ public class HostMetricDAO extends DAO {
// TODO: block commit and avoid multiple threads for commit of first
// object definition
commitQueue();
finishCommits();
// finishCommits();
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment