From e7ea3b3cc5ec302eab89c0e34e05ba668bf71b68 Mon Sep 17 00:00:00 2001 From: orbiter Date: Wed, 28 Jul 2010 12:45:53 +0000 Subject: [PATCH] added a buffer for network images to reduced load on yacy.net network image server git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@7007 6c8d7289-2bf4-0310-a012-ef5d649a1542 --- htroot/NetworkPicture.java | 22 ++++++++++++++--- .../anomic/http/server/HTTPDFileHandler.java | 15 ++++++++++++ .../de/anomic/yacy/graphics/EncodedImage.java | 24 +++++++++++++++++++ 3 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 source/de/anomic/yacy/graphics/EncodedImage.java diff --git a/htroot/NetworkPicture.java b/htroot/NetworkPicture.java index 6fd72b7b8..a06769809 100644 --- a/htroot/NetworkPicture.java +++ b/htroot/NetworkPicture.java @@ -24,21 +24,32 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -import net.yacy.visualization.RasterPlotter; +import java.util.TreeMap; + import de.anomic.http.server.RequestHeader; import de.anomic.search.Switchboard; import de.anomic.search.SwitchboardConstants; import de.anomic.server.serverObjects; import de.anomic.server.serverSwitch; +import de.anomic.yacy.graphics.EncodedImage; import de.anomic.yacy.graphics.NetworkGraph; /** draw a picture of the yacy network */ public class NetworkPicture { - public static RasterPlotter respond(final RequestHeader header, final serverObjects post, final serverSwitch env) { + private static final TreeMap buffer = new TreeMap(); + + public static EncodedImage respond(final RequestHeader header, final serverObjects post, final serverSwitch env) { final Switchboard sb = (Switchboard) env; final boolean authorized = sb.adminAuthenticated(header) >= 2; + long timeSeconds = System.currentTimeMillis() / 1000; + EncodedImage bufferedImage = null; + synchronized (buffer) { + bufferedImage = buffer.get(timeSeconds); + } + if (bufferedImage != null) return bufferedImage; + int width = 768; int height = 576; int passiveLimit = 720; // 12 hours @@ -73,7 +84,12 @@ public class NetworkPicture { if (passiveLimit > 1000000) passiveLimit = 1000000; if (potentialLimit > 1000000) potentialLimit = 1000000; if (maxCount > 10000) maxCount = 10000; - return NetworkGraph.getNetworkPicture(sb.peers, 10000, width, height, passiveLimit, potentialLimit, maxCount, coronaangle, communicationTimeout, env.getConfig(SwitchboardConstants.NETWORK_NAME, "unspecified"), env.getConfig("network.unit.description", "unspecified"), bgcolor); + bufferedImage = new EncodedImage(NetworkGraph.getNetworkPicture(sb.peers, 10000, width, height, passiveLimit, potentialLimit, maxCount, coronaangle, communicationTimeout, env.getConfig(SwitchboardConstants.NETWORK_NAME, "unspecified"), env.getConfig("network.unit.description", "unspecified"), bgcolor).getImage(), "png"); + synchronized (buffer) { + buffer.put(timeSeconds, bufferedImage); + while (buffer.size() > 8) buffer.remove(buffer.firstKey()); + } + return bufferedImage; } } diff --git a/source/de/anomic/http/server/HTTPDFileHandler.java b/source/de/anomic/http/server/HTTPDFileHandler.java index 8e06c86ef..9ec7f6bfa 100644 --- a/source/de/anomic/http/server/HTTPDFileHandler.java +++ b/source/de/anomic/http/server/HTTPDFileHandler.java @@ -101,6 +101,7 @@ import de.anomic.server.serverObjects; import de.anomic.server.serverSwitch; import de.anomic.server.servletProperties; import de.anomic.yacy.yacyBuildProperties; +import de.anomic.yacy.graphics.EncodedImage; public final class HTTPDFileHandler { @@ -548,6 +549,20 @@ public final class HTTPDFileHandler { result.writeTo(out); } } + if (img instanceof EncodedImage) { + final EncodedImage yp = (EncodedImage) img; + // send an image to client + targetDate = new Date(System.currentTimeMillis()); + nocache = true; + final String mimeType = MimeTable.ext2mime(targetExt, "text/html"); + final ByteBuffer result = yp.getImage(); + + // write the array to the client + HTTPDemon.sendRespondHeader(conProp, out, httpVersion, 200, null, mimeType, result.length(), targetDate, null, null, null, null, nocache); + if (!method.equals(HeaderFramework.METHOD_HEAD)) { + result.writeTo(out); + } + } if (img instanceof Image) { final Image i = (Image) img; // send an image to client diff --git a/source/de/anomic/yacy/graphics/EncodedImage.java b/source/de/anomic/yacy/graphics/EncodedImage.java new file mode 100644 index 000000000..7c4337f62 --- /dev/null +++ b/source/de/anomic/yacy/graphics/EncodedImage.java @@ -0,0 +1,24 @@ +package de.anomic.yacy.graphics; + +import java.awt.image.BufferedImage; + +import net.yacy.kelondro.util.ByteBuffer; +import net.yacy.visualization.RasterPlotter; + +public class EncodedImage { + private ByteBuffer image; + private String extension; + + public EncodedImage(final BufferedImage sourceImage, final String targetExt) { + this.image = RasterPlotter.exportImage(sourceImage, targetExt); + this.extension = targetExt; + } + + public ByteBuffer getImage() { + return this.image; + } + + public String getExtension() { + return this.extension; + } +}