Mercurial > java-rrd-hg
changeset 39:ca4cf1d4a36e
* fix indentation
author | Peter Stamfest <peter@stamfest.at> |
---|---|
date | Sat, 02 Nov 2013 22:44:58 +0100 |
parents | 10d2d5fd0f6d |
children | c6c58f76aa7d |
files | src/main/java/net/stamfest/rrd/CommandResult.java src/main/java/net/stamfest/rrd/RRDp.java |
diffstat | 2 files changed, 226 insertions(+), 213 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/net/stamfest/rrd/CommandResult.java Sat Nov 02 22:32:58 2013 +0100 +++ b/src/main/java/net/stamfest/rrd/CommandResult.java Sat Nov 02 22:44:58 2013 +0100 @@ -44,39 +44,39 @@ // support for output from special commands public HashMap<String, String> info = null; // info, graphv - public byte image[] = null; // graphv + public byte image[] = null; // graphv public float getUser() { - return user; - } + return user; + } - public float getSystem() { - return system; - } + public float getSystem() { + return system; + } - public float getTotal() { - return total; - } + public float getTotal() { + return total; + } - public boolean isOk() { - return ok; - } + public boolean isOk() { + return ok; + } - public String getError() { - return error; - } + public String getError() { + return error; + } - public String getOutput() { - return output; - } + public String getOutput() { + return output; + } - public HashMap<String, String> getInfo() { - return info; - } + public HashMap<String, String> getInfo() { + return info; + } - public byte[] getImageBytes() { - return image; - } + public byte[] getImageBytes() { + return image; + } public String toString() { StringBuffer sb = new StringBuffer();
--- a/src/main/java/net/stamfest/rrd/RRDp.java Sat Nov 02 22:32:58 2013 +0100 +++ b/src/main/java/net/stamfest/rrd/RRDp.java Sat Nov 02 22:44:58 2013 +0100 @@ -1,4 +1,3 @@ - /* Copyright (c) 2010 by Peter Stamfest <peter@stamfest.at> @@ -28,9 +27,7 @@ dealings in this Software without prior written authorization from Peter Stamfest. -*/ - - + */ package net.stamfest.rrd; import java.io.IOException; @@ -46,231 +43,247 @@ import java.util.regex.Pattern; public class RRDp implements RRDCommand { - public static final int SOCKET_TIMEOUT = 10000; - private static final Logger logger = Logger.getLogger(RRDp.class.getName()); - Process rrdtool = null; - Socket socket = null; - - private OutputStream writer; - private InputStream reader; - - public RRDp(String basedir, String cachedAddress) throws IOException { - String cmd[] = new String[] { "rrdtool", "-", basedir }; + public static final int SOCKET_TIMEOUT = 10000; + private static final Logger logger = Logger.getLogger(RRDp.class.getName()); + Process rrdtool = null; + Socket socket = null; + private OutputStream writer; + private InputStream reader; - ProcessBuilder pb = new ProcessBuilder(cmd); - if (cachedAddress != null) { - pb.environment().put("RRDCACHED_ADDRESS", cachedAddress); - } + public RRDp(String basedir, String cachedAddress) throws IOException { + String cmd[] = new String[]{"rrdtool", "-", basedir}; - rrdtool = pb.start(); - - InputStream r = rrdtool.getInputStream(); - reader = r; - writer = rrdtool.getOutputStream(); + ProcessBuilder pb = new ProcessBuilder(cmd); + if (cachedAddress != null) { + pb.environment().put("RRDCACHED_ADDRESS", cachedAddress); } - public RRDp(String host, int port) throws IOException { - socket = new Socket(); - socket.connect(new InetSocketAddress(host, port), SOCKET_TIMEOUT); - socket.setSoTimeout(SOCKET_TIMEOUT); + rrdtool = pb.start(); + + InputStream r = rrdtool.getInputStream(); + reader = r; + writer = rrdtool.getOutputStream(); + } + + public RRDp(String host, int port) throws IOException { + socket = new Socket(); + socket.connect(new InetSocketAddress(host, port), SOCKET_TIMEOUT); + socket.setSoTimeout(SOCKET_TIMEOUT); - reader = socket.getInputStream(); - writer = socket.getOutputStream(); + reader = socket.getInputStream(); + writer = socket.getOutputStream(); + } + // OK u:0.00 s:0.00 r:17.20 + static private Pattern okpat = Pattern.compile("^OK u:([0-9.]+) s:([0-9.]+) r:([0-9.]+)"); + static private Pattern infoPat = Pattern.compile("^\\s*(.*?)\\s*=\\s*(.*?)\\s*$"); + + protected synchronized CommandResult sendCommand(String command[]) throws IOException, RRDException { + if (rrdtool == null && socket == null) { + throw new IOException("No subprocess available (already closed?)"); + } + + if (command == null || command.length == 0) { + throw new IllegalArgumentException(); + } + + if (command[0].equals("graph")) { + throw new RRDException("Use graphv instead"); } - // OK u:0.00 s:0.00 r:17.20 - static private Pattern okpat = Pattern.compile("^OK u:([0-9.]+) s:([0-9.]+) r:([0-9.]+)"); - static private Pattern infoPat = Pattern.compile("^\\s*(.*?)\\s*=\\s*(.*?)\\s*$"); - protected synchronized CommandResult sendCommand(String command[]) throws IOException, RRDException { - if (rrdtool == null && socket == null) { - throw new IOException("No subprocess available (already closed?)"); - } + StringBuilder sb = new StringBuilder(); + for (String c : command) { + if (sb.length() > 0) { + sb.append(' '); + } + sb.append(c); + } + sb.append('\n'); + + if (logger.isLoggable(Level.FINER)) { + logger.finer(sb.toString()); + } - if (command == null || command.length == 0) { - throw new IllegalArgumentException(); - } + try { + writer.write(sb.toString().getBytes()); + writer.flush(); + } catch (IOException io) { + finish(); + throw io; + } - if (command[0].equals("graph")) { - throw new RRDException("Use graphv instead"); - } + String line = null; + + CommandResult r = new CommandResult(); + sb.setLength(0); + HashMap<String, String> data = null; + Matcher m; + int replyLines = 0; - StringBuilder sb = new StringBuilder(); - for (String c: command) { - if (sb.length() > 0) sb.append(' '); - sb.append(c); + while (true) { + try { + line = readLine(); + if (line == null) { + break; } - sb.append('\n'); + } catch (IOException e) { + finish(); + throw e; + } + replyLines++; + if (line.startsWith("ERROR")) { + r.ok = false; + r.error = line; + break; + } + if (line.startsWith("OK ")) { + m = okpat.matcher(line); - if (logger.isLoggable(Level.FINER)) { - logger.finer(sb.toString()); + if (m.find()) { + r.user = Float.parseFloat(m.group(1)); + r.system = Float.parseFloat(m.group(2)); + r.total = Float.parseFloat(m.group(3)); + r.ok = true; + break; } + } - try { - writer.write(sb.toString().getBytes()); - writer.flush(); - } catch (IOException io) { - finish(); - throw io; + m = infoPat.matcher(line); + if (m.find()) { + if (data == null) { + data = new HashMap<String, String>(); } - String line = null; - - CommandResult r = new CommandResult(); - sb.setLength(0); - HashMap<String, String> data = null; - Matcher m; - int replyLines = 0; + String k = m.group(1); + String v = m.group(2); - while (true) { - try { - line = readLine(); - if (line == null) break; - } catch (IOException e) { - finish(); - throw e; - } - replyLines++; - if (line.startsWith("ERROR")) { - r.ok = false; - r.error = line; - break; - } - if (line.startsWith("OK ")) { - m = okpat.matcher(line); - - if (m.find()) { - r.user = Float.parseFloat(m.group(1)); - r.system = Float.parseFloat(m.group(2)); - r.total= Float.parseFloat(m.group(3)); - r.ok = true; - break; - } - } - - m = infoPat.matcher(line); - if (m.find()) { - if (data == null) data = new HashMap<String, String>(); + if (k.equals("image") && v.startsWith("BLOB_SIZE:")) { + int len = Integer.parseInt(v.substring(10)); - String k = m.group(1); - String v = m.group(2); - - if (k.equals("image") && v.startsWith("BLOB_SIZE:")) { - int len = Integer.parseInt(v.substring(10)); - - // System.err.println("BLOB " + len); - byte img[] = new byte[len]; - int pos = 0; - while (pos != len) { - int n = reader.read(img, pos, len - pos); - if (n <= 0) throw new RRDException("protocol error"); - pos += n; - } - r.image = img; - } - - data.put(k, v); - if (logger.isLoggable(Level.FINEST)) { - logger.finest(String.format("put info: %s=%s", k, v)); - } - continue; + // System.err.println("BLOB " + len); + byte img[] = new byte[len]; + int pos = 0; + while (pos != len) { + int n = reader.read(img, pos, len - pos); + if (n <= 0) { + throw new RRDException("protocol error"); } - - sb.append(line).append("\n"); - } - - if (line == null) { - finish(); - - if (replyLines == 0) { - // EOF without ANY response - throw new RRDException("Protocol error: No reply (EOF)"); - } - throw new RRDException("Protocol error: short reply (EOF)"); - } - - r.output = sb.toString(); - r.info = data; - - if (logger.isLoggable(Level.FINEST)) { - logger.finest(r.toString()); + pos += n; + } + r.image = img; } - if (logger.isLoggable(Level.FINER)) { - logger.log(Level.FINER, "{0}:{1}", new Object[]{r.ok, r.error}); + data.put(k, v); + if (logger.isLoggable(Level.FINEST)) { + logger.finest(String.format("put info: %s=%s", k, v)); } + continue; + } - return r; + sb.append(line).append("\n"); + } + + if (line == null) { + finish(); + + if (replyLines == 0) { + // EOF without ANY response + throw new RRDException("Protocol error: No reply (EOF)"); + } + throw new RRDException("Protocol error: short reply (EOF)"); } - private String readLine() throws IOException { - int c; - byte b[] = new byte[128]; - int pos = 0; + r.output = sb.toString(); + r.info = data; + + if (logger.isLoggable(Level.FINEST)) { + logger.finest(r.toString()); + } + + if (logger.isLoggable(Level.FINER)) { + logger.log(Level.FINER, "{0}:{1}", new Object[]{r.ok, r.error}); + } + + return r; + } - while (true) { - c = reader.read(); - if (c == -1 || c == '\n') break; - if (pos == b.length) { - b = Arrays.copyOf(b, b.length * 2); - } - b[pos++] = (byte) c; - } + private String readLine() throws IOException { + int c; + byte b[] = new byte[128]; + int pos = 0; - if (c == -1 && pos == 0) return null; - - return new String(b, 0, pos); + while (true) { + c = reader.read(); + if (c == -1 || c == '\n') { + break; + } + if (pos == b.length) { + b = Arrays.copyOf(b, b.length * 2); + } + b[pos++] = (byte) c; } - public void finish() { - if (rrdtool != null || socket != null) { - try { - if (socket != null) socket.close(); - writer.close(); - reader.close(); - } catch (IOException e) { - logger.log(Level.INFO, "ignoring exception", e); - } - rrdtool = null; - socket = null; - writer = null; - reader = null; - } + if (c == -1 && pos == 0) { + return null; } - @Override - protected void finalize() throws Throwable { - super.finalize(); - finish(); + return new String(b, 0, pos); + } + + public void finish() { + if (rrdtool != null || socket != null) { + try { + if (socket != null) { + socket.close(); + } + writer.close(); + reader.close(); + } catch (IOException e) { + logger.log(Level.INFO, "ignoring exception", e); + } + rrdtool = null; + socket = null; + writer = null; + reader = null; } + } - @Override - public CommandResult command(String cmd[]) throws IOException, RRDException { - try { - return sendCommand(cmd); - } catch (RRDException e) { - if (rrdtool == null) throw e; - logErrorStream(); - throw e; - } catch (IOException e) { - if (rrdtool == null) throw e; - logErrorStream(); - throw e; - } + @Override + protected void finalize() throws Throwable { + super.finalize(); + finish(); + } + + @Override + public CommandResult command(String cmd[]) throws IOException, RRDException { + try { + return sendCommand(cmd); + } catch (RRDException e) { + if (rrdtool == null) { + throw e; + } + logErrorStream(); + throw e; + } catch (IOException e) { + if (rrdtool == null) { + throw e; + } + logErrorStream(); + throw e; } + } protected void logErrorStream() throws IOException { InputStream error = rrdtool.getErrorStream(); if (logger.isLoggable(Level.INFO)) { - byte buf[] = new byte[1024]; - StringBuilder sb = new StringBuilder(); - while (error.available() > 0) { - int n = error.read(buf); - sb.append(new String(buf, 0, n)); - } + byte buf[] = new byte[1024]; + StringBuilder sb = new StringBuilder(); + while (error.available() > 0) { + int n = error.read(buf); + sb.append(new String(buf, 0, n)); + } - logger.info(sb.toString()); + logger.info(sb.toString()); } } - }