Update of /cvsroot/freenet/freenet/src/freenet/node/rt
In directory sc8-pr-cvs1:/tmp/cvs-serv28391/src/freenet/node/rt

Modified Files:
        StandardNodeEstimator.java ResponseTimeEstimator.java 
        TimeEstimator.java NGRoutingTable.java NodeEstimator.java 
Log Message:
Move the HTML generation support code out from NodeEstimator into an inner class in 
NodeEstimator.
Updated the HTML generation support code in TimeEstimator somewhat.

Index: StandardNodeEstimator.java
===================================================================
RCS file: /cvsroot/freenet/freenet/src/freenet/node/rt/StandardNodeEstimator.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -w -r1.13 -r1.14
--- StandardNodeEstimator.java  30 Oct 2003 01:34:06 -0000      1.13
+++ StandardNodeEstimator.java  30 Oct 2003 03:18:52 -0000      1.14
@@ -5,13 +5,17 @@
 import java.io.DataOutputStream;
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.util.Hashtable;
 
 import freenet.Core;
 import freenet.Key;
+
 import freenet.node.NodeReference;
 import freenet.support.DataObjectPending;
 import freenet.support.Logger;
 import freenet.support.SimpleStringMap;
+import freenet.support.graph.Bitmap;
+import freenet.support.graph.Color;
 
 class StandardNodeEstimator extends NodeEstimator {
        // Apologies for the notation, but read the estimator function!
@@ -340,6 +344,109 @@
                return createdTime;
        }
        
+       public void writeTo(DataOutputStream out) throws IOException {
+               out.writeInt(1); // version number
+               rpConnectFailed.writeTo(out);
+               rpTransferFailed.writeTo(out);
+               rpSearchFailed.writeTo(out);
+               rpDNF.writeTo(out);
+               rtConnectFailed.writeTo(out);
+               rtConnected.writeTo(out);
+               rtTransferFailed.writeTo(out);
+               rtSearchFailed.writeTo(out);
+               etSuccessSearch.writeTo(out);
+               etTransferRate.writeTo(out);
+               epDNFGivenConnectionAndNotRejectedOrSearchFailed.writeTo(out);
+               etDNF.writeTo(out);
+               out.writeInt(successes);
+               out.writeLong(lastSuccessTime);
+               out.writeLong(lastAccessedTime);
+               out.writeLong(createdTime);
+       }
+       
+       private final static String[] REF_PROPERTIES =
+       { "Address", "Last Estimate", "Connection Probability", 
+         "Consecutive Failures", "Connection Attempts", "Successful Connections", 
+         "Last Attempt", "Successful Transfers", "Connection Fail Time",
+         "Connection Success Time", "NodeReference", "Node Version", 
+         "Search died probability", "Transfer died probability", 
+         "Search died time", 
+         "Open Outbound Connections", "Open Inbound Connections" };
+       
+       static class MyComparableStringMap 
+           extends SimpleStringMap implements ComparableStringMap {
+               public MyComparableStringMap(Object[] values) {
+                       super(REF_PROPERTIES, values);
+               }
+               
+               public int compareTo(Object o) {
+                       MyComparableStringMap sm = (MyComparableStringMap)o;
+                       // Connected nodes go at the top
+                       if(openConnections() == 0 && sm.openConnections() > 0)
+                               return 1;
+                       if(openConnections() > 0 && sm.openConnections() == 0)
+                               return -1;
+                       // Next sort by successes
+                       if(successes() < sm.successes())
+                               return 1;
+                       if(successes() > sm.successes())
+                               return -1;
+                       return 0;
+               }
+               
+               protected int openConnections() {
+                       Integer outboundConnections = (Integer)objs[15];
+                       Integer inboundConnections = (Integer)objs[16];
+                       return outboundConnections.intValue() +
+                               inboundConnections.intValue();
+               }
+               
+               protected int successes() {
+                       Integer successes = (Integer)objs[7];
+                       return successes.intValue();
+               }
+               
+               public boolean equals(Object o) {
+                       if(o instanceof MyComparableStringMap) 
+                               return compareTo(o) == 0;
+                       else return false;
+               }
+       }
+       
+       public ComparableStringMap getDiagProperties() {
+               Object[] values = new Object[REF_PROPERTIES.length];
+               values[0] = ref.firstPhysicalToString();
+               values[1] = new Long(lastEstimate);
+               values[2] = new Float(1.0 - rpConnectFailed.currentValue());
+               values[3] = new Long(consecutiveFailedConnects);
+               values[4] = new Long(connectTries);
+               values[5] = new Long(connectSuccesses);
+               long now = System.currentTimeMillis();
+               long secsSinceLastAttempt = -1;
+               if(connectTries > 0) {
+                       secsSinceLastAttempt = (now - lastConnectTryTime) / 1000;
+               }
+               values[6] = new Long(secsSinceLastAttempt);
+               values[7] = new Integer(successes);
+               values[8] = new Integer((int)rtConnectFailed.currentValue());
+               values[9] = new Integer((int)rtConnected.currentValue());
+               values[10] = ref;
+               values[11] = ref.getVersion();
+               values[12] = new Float(rpSearchFailed.currentValue());
+               values[13] = new Float(rpTransferFailed.currentValue());
+               values[14] = new Float(rtSearchFailed.currentValue());
+               values[15] = 
+                       new Integer(ngrt.countOutboundConnections(ref.getIdentity()));
+               values[16] = 
+                       new Integer(ngrt.countInboundConnections(ref.getIdentity()));
+               return new MyComparableStringMap(values);
+       }
+       
+       
+       
+       protected class StandardHTMLReportTool implements NodeEstimator.HTMLReportTool
+       {
+
        public void toHtml(PrintWriter pw, String imagePrefix) 
                throws IOException {
                String nodename = ref.firstPhysicalToString();
@@ -410,6 +517,46 @@
                pw.flush();
        }
        
+               public int graphType(String graphName) {
+                       if(graphName.equals("tSuccessSearch"))
+                               return T_SUCCESS_SEARCH;
+                       else if(graphName.equals("tTransferRate"))
+                               return T_TRANSFER_RATE;
+                       else if(graphName.equals("pDNF"))
+                               return P_DNF;
+                       else if(graphName.equals("tDNF"))
+                               return T_DNF;
+                       else return -1;
+               }
+
+               public String graphName(int g) {
+                       switch(g) {
+                       case T_SUCCESS_SEARCH:
+                               return "tSuccessSearch";
+                       case T_TRANSFER_RATE:
+                               return "tTransferRate";
+                       case P_DNF:
+                               return "pDNF";
+                       case T_DNF:
+                               return "tDNF";
+                       default:
+                               return null;
+                       }
+               }
+
+               public int estimatorType(int estimator) {
+                       switch(estimator) {
+                       case T_SUCCESS_SEARCH:
+                       case T_DNF:
+                               return TimeEstimator.TIME;
+                       case P_DNF:
+                               return TimeEstimator.PROBABILITY;
+                       case T_TRANSFER_RATE:
+                               return TimeEstimator.TRANSFER_RATE;
+                       }
+                       throw new IllegalArgumentException();
+               }
+
        protected String ageString(long age) {
                // FIXME: we must have a global utility to do this...
                String ageString = "";
@@ -438,44 +585,33 @@
                return ageString;
        }
        
-       public int estimatorType(int estimator) {
-               switch(estimator) {
-               case T_SUCCESS_SEARCH:
-               case T_DNF:
-                       return TimeEstimator.TIME;
-               case P_DNF:
-                       return TimeEstimator.PROBABILITY;
-               case T_TRANSFER_RATE:
-                       return TimeEstimator.TRANSFER_RATE;
-               }
-               throw new IllegalArgumentException();
+               /* (non-Javadoc)
+                * @see 
freenet.node.rt.NodeEstimator.HTMLReportTool#drapGraphOnImage(boolean, 
freenet.support.graph.Bitmap)
+                */
+               public void drapGraphOnImage(boolean dontClipPoints, Bitmap 
bmp,Hashtable lineColors) {
+                       Color c = (Color)lineColors.get(graphName(T_SUCCESS_SEARCH));
+                       if(c != null)
+                               
getEstimator(T_SUCCESS_SEARCH).getHTMLReportingTool().drapGraphOnImage(false, bmp, c, 
null);
+                       c = (Color)lineColors.get(graphName(T_TRANSFER_RATE));
+                       if(c != null)
+                               
getEstimator(T_TRANSFER_RATE).getHTMLReportingTool().drapGraphOnImage(false, bmp, c, 
null);
+                       c = (Color)lineColors.get(graphName(P_DNF));
+                       if(c != null)
+                               
getEstimator(P_DNF).getHTMLReportingTool().drapGraphOnImage(false, bmp, c, null);
+                       c = (Color)lineColors.get(graphName(T_DNF));
+                       if(c != null)
+                               
getEstimator(T_DNF).getHTMLReportingTool().drapGraphOnImage(false, bmp, c, null);
        }
        
-       public String graphName(int g) {
-               switch(g) {
-               case T_SUCCESS_SEARCH:
-                       return "tSuccessSearch";
-               case T_TRANSFER_RATE:
-                       return "tTransferRate";
-               case P_DNF:
-                       return "pDNF";
-               case T_DNF:
-                       return "tDNF";
-               default:
-                       return null;
-               }
-       }
        
-       public int graphType(String graphName) {
-               if(graphName.equals("tSuccessSearch"))
-                       return T_SUCCESS_SEARCH;
-               else if(graphName.equals("tTransferRate"))
-                       return T_TRANSFER_RATE;
-               else if(graphName.equals("pDNF"))
-                       return P_DNF;
-               else if(graphName.equals("tDNF"))
-                       return T_DNF;
-               else return -1;
+
+               /* (non-Javadoc)
+                * @see 
freenet.node.rt.NodeEstimator.HTMLReportTool#getEstimator(java.lang.String)
+                */
+               public TimeEstimator getEstimator(String type) {
+                       int typeNum = graphType(type);
+                       if(typeNum < 0) return null;
+                       return getEstimator(typeNum);
        }
        
        public TimeEstimator getEstimator(int type) {
@@ -493,101 +629,12 @@
                }
        }
        
-       public void writeTo(DataOutputStream out) throws IOException {
-               out.writeInt(1); // version number
-               rpConnectFailed.writeTo(out);
-               rpTransferFailed.writeTo(out);
-               rpSearchFailed.writeTo(out);
-               rpDNF.writeTo(out);
-               rtConnectFailed.writeTo(out);
-               rtConnected.writeTo(out);
-               rtTransferFailed.writeTo(out);
-               rtSearchFailed.writeTo(out);
-               etSuccessSearch.writeTo(out);
-               etTransferRate.writeTo(out);
-               epDNFGivenConnectionAndNotRejectedOrSearchFailed.writeTo(out);
-               etDNF.writeTo(out);
-               out.writeInt(successes);
-               out.writeLong(lastSuccessTime);
-               out.writeLong(lastAccessedTime);
-               out.writeLong(createdTime);
        }
        
-       private final static String[] REF_PROPERTIES =
-       { "Address", "Last Estimate", "Connection Probability", 
-         "Consecutive Failures", "Connection Attempts", "Successful Connections", 
-         "Last Attempt", "Successful Transfers", "Connection Fail Time",
-         "Connection Success Time", "NodeReference", "Node Version", 
-         "Search died probability", "Transfer died probability", 
-         "Search died time", 
-         "Open Outbound Connections", "Open Inbound Connections" };
-       
-       static class MyComparableStringMap 
-           extends SimpleStringMap implements ComparableStringMap {
-               public MyComparableStringMap(Object[] values) {
-                       super(REF_PROPERTIES, values);
-               }
-               
-               public int compareTo(Object o) {
-                       MyComparableStringMap sm = (MyComparableStringMap)o;
-                       // Connected nodes go at the top
-                       if(openConnections() == 0 && sm.openConnections() > 0)
-                               return 1;
-                       if(openConnections() > 0 && sm.openConnections() == 0)
-                               return -1;
-                       // Next sort by successes
-                       if(successes() < sm.successes())
-                               return 1;
-                       if(successes() > sm.successes())
-                               return -1;
-                       return 0;
-               }
-               
-               protected int openConnections() {
-                       Integer outboundConnections = (Integer)objs[15];
-                       Integer inboundConnections = (Integer)objs[16];
-                       return outboundConnections.intValue() +
-                               inboundConnections.intValue();
-               }
-               
-               protected int successes() {
-                       Integer successes = (Integer)objs[7];
-                       return successes.intValue();
-               }
-               
-               public boolean equals(Object o) {
-                       if(o instanceof MyComparableStringMap) 
-                               return compareTo(o) == 0;
-                       else return false;
-               }
-       }
-       
-       public ComparableStringMap getDiagProperties() {
-               Object[] values = new Object[REF_PROPERTIES.length];
-               values[0] = ref.firstPhysicalToString();
-               values[1] = new Long(lastEstimate);
-               values[2] = new Float(1.0 - rpConnectFailed.currentValue());
-               values[3] = new Long(consecutiveFailedConnects);
-               values[4] = new Long(connectTries);
-               values[5] = new Long(connectSuccesses);
-               long now = System.currentTimeMillis();
-               long secsSinceLastAttempt = -1;
-               if(connectTries > 0) {
-                       secsSinceLastAttempt = (now - lastConnectTryTime) / 1000;
-               }
-               values[6] = new Long(secsSinceLastAttempt);
-               values[7] = new Integer(successes);
-               values[8] = new Integer((int)rtConnectFailed.currentValue());
-               values[9] = new Integer((int)rtConnected.currentValue());
-               values[10] = ref;
-               values[11] = ref.getVersion();
-               values[12] = new Float(rpSearchFailed.currentValue());
-               values[13] = new Float(rpTransferFailed.currentValue());
-               values[14] = new Float(rtSearchFailed.currentValue());
-               values[15] = 
-                       new Integer(ngrt.countOutboundConnections(ref.getIdentity()));
-               values[16] = 
-                       new Integer(ngrt.countInboundConnections(ref.getIdentity()));
-               return new MyComparableStringMap(values);
+       /* (non-Javadoc)
+        * @see freenet.node.rt.NodeEstimator#getHTMLReportingTool()
+        */
+       public HTMLReportTool getHTMLReportingTool() {
+               return new StandardHTMLReportTool();
        }
 }

Index: ResponseTimeEstimator.java
===================================================================
RCS file: /cvsroot/freenet/freenet/src/freenet/node/rt/ResponseTimeEstimator.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -w -r1.17 -r1.18
--- ResponseTimeEstimator.java  30 Oct 2003 01:34:06 -0000      1.17
+++ ResponseTimeEstimator.java  30 Oct 2003 03:18:52 -0000      1.18
@@ -954,20 +954,16 @@
                                return Double.toString(((double)x) / (16*1000)) + 
"bytes/second";
                        throw new IllegalArgumentException("unknown type");
                }
+               public void drapGraphOnImage(boolean dontClipPoints, Bitmap bmp, Color 
lineCol, Color crossCol) {
 
-               public void drawGraphBMP(int width, int height, boolean dontClipPoints,
-                                            OutputStream os) 
-                       throws IOException {
-                       int[] val = new int[width];
+                       int[] val = new int[bmp.getWidth()];
                        BigInteger a = keyspace.subtract(BigInteger.ONE);
-                       BigInteger b = a.divide(BigInteger.valueOf(width));
+                       BigInteger b = a.divide(BigInteger.valueOf(bmp.getWidth()));
                        BigInteger at = BigInteger.ZERO;
                        int lowest = Integer.MAX_VALUE;
                        int highest = 0;
-                       for(int i=0;i<width;i++) {
+                       for (int i = 0; i < bmp.getWidth(); i++) {
                            val[i] = guess(at);
-               //          Core.logger.log(this, "Guess for "+at+": "+val[i],
-               //                          Logger.DEBUG);
                            lowest = Math.min(lowest,val[i]);
                                highest = Math.max(highest,val[i]);
                            at = at.add(b);
@@ -979,28 +975,35 @@
                        // lowest ... highest
                        // lowest mapped to 0
                        // highest mapped to height
-                       double multiplier = ((double)height-1) / ((double)(highest - 
lowest));
-                       Bitmap bmp = new Bitmap(width, height);
-                       bmp.setPenColor(new Color(0,0,0));
-                       for(int i=0;i<width;i++) {
+                       double multiplier = ((double) bmp.getHeight() - 1) / ((double) 
(highest - lowest));
+                       if (lineCol != null) {
+                               bmp.setPenColor(lineCol);
+                               for (int i = 0; i < bmp.getWidth(); i++) {
                            int position = (int)((val[i] - lowest) * multiplier);
-                           position = height - (position + 1);
+                                       position = bmp.getHeight() - (position + 1);
                            bmp.setPixel(i, position);
                        }
+                       }
+
                        // Now plot the recent points
-                       bmp.setPenColor(new Color(255,0,0));
+                       if (crossCol != null) {
+                               bmp.setPenColor(crossCol);
                        Enumeration e = recent.enumeration();
-                       while(e.hasMoreElements())
-                       {
+                               while (e.hasMoreElements()) {
                                RecentReports.KeyTimePair kt = 
(RecentReports.KeyTimePair)e.nextElement();
                                int w = kt.key.divide(b).intValue();
                                int h = (int)((kt.time - lowest) * multiplier);
                                h = Math.max(h,0);
-                               h = Math.min(height-1,h);
-                               h = height - (h+1);
+                                       h = Math.min(bmp.getHeight() - 1, h);
+                                       h = bmp.getHeight() - (h + 1);
                                bmp.drawPlus(w,h);
                        }
+                       }
+               }
 
+               public void drawGraphBMP(int width, int height, boolean 
dontClipPoints, OutputStream os) throws IOException {
+                       Bitmap bmp = new Bitmap(width, height);
+                       drapGraphOnImage(dontClipPoints, bmp, new Color(0, 0, 0), new 
Color(255, 0, 0));
                        BitmapEncoder enc = new DibEncoder();
                        enc.setBitmap(bmp);
                        enc.encode(os);

Index: TimeEstimator.java
===================================================================
RCS file: /cvsroot/freenet/freenet/src/freenet/node/rt/TimeEstimator.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -w -r1.3 -r1.4
--- TimeEstimator.java  29 Oct 2003 21:53:23 -0000      1.3
+++ TimeEstimator.java  30 Oct 2003 03:18:52 -0000      1.4
@@ -2,6 +2,8 @@
 
 import freenet.Key;
 import freenet.support.DataObject;
+import freenet.support.graph.Bitmap;
+import freenet.support.graph.Color;
 
 public interface TimeEstimator extends DataObject {
     public long guessRaw(Key k);
@@ -23,7 +25,7 @@
 
        public long lowestRaw();
        public long highestRaw();
-       /** An interface which represents a tool that gives th user of the
+       /** An interface which represents a tool that gives the user of the
         * TimeEstimator some help with rendering the estimator to HTML */
     interface HTMLReportTool
     {
@@ -31,6 +33,9 @@
                public String highestString(int type);
                public double convertFromRaw(long x, int type);
                public String formatFromRaw(long x, int type);
+               /**     Draws the estimation graph or the recent reports crosses or 
both of them on the supplied
+           bitmap. if lineCol or crossCol is null then their respective items will 
not be drawn **/
+               public void drapGraphOnImage(boolean dontClipPoints, Bitmap bmp, Color 
lineCol, Color crossCol);
                public void drawGraphBMP(int width, int height, boolean dontClipPoints,
                                     java.io.OutputStream os)
                throws java.io.IOException;

Index: NGRoutingTable.java
===================================================================
RCS file: /cvsroot/freenet/freenet/src/freenet/node/rt/NGRoutingTable.java,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -w -r1.21 -r1.22
--- NGRoutingTable.java 30 Oct 2003 01:34:06 -0000      1.21
+++ NGRoutingTable.java 30 Oct 2003 03:18:52 -0000      1.22
@@ -379,18 +379,20 @@
        public void toHtml(Identity i, PrintWriter pw, String imagePrefix) 
                throws IOException {
                NodeEstimator e = (NodeEstimator)(estimators.get(i));
-               if(e != null) {
-                       e.toHtml(pw, imagePrefix);
-               }
+               if(e != null)
+                       e.getHTMLReportingTool().toHtml(pw, imagePrefix);
        }
        
        public TimeEstimator getEstimator(Identity id, String type) {
-               NodeEstimator e = (NodeEstimator)(estimators.get(id));
-               if(e != null) {
-                       int typeNum = e.graphType(type);
-                       if(typeNum < 0) return null;
-                       return e.getEstimator(typeNum);
-               } else return null;
+               NodeEstimator e = (NodeEstimator)estimators.get(id);
+               if(e != null)
+                       return e.getHTMLReportingTool().getEstimator(type);
+               else
+                       return null;
+       }
+       public NodeEstimator getEstimator(Identity id)
+       {
+               return (NodeEstimator)estimators.get(id);
        }
        
        public void reportConnectionSuccess(Identity id, long time) {

Index: NodeEstimator.java
===================================================================
RCS file: /cvsroot/freenet/freenet/src/freenet/node/rt/NodeEstimator.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -w -r1.6 -r1.7
--- NodeEstimator.java  30 Oct 2003 01:34:06 -0000      1.6
+++ NodeEstimator.java  30 Oct 2003 03:18:52 -0000      1.7
@@ -3,12 +3,15 @@
 
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.util.Hashtable;
 
 import freenet.Key;
 import freenet.node.NodeReference;
 import freenet.support.DataObject;
+import freenet.support.graph.Bitmap;
 
-abstract class NodeEstimator implements DataObject {
+
+abstract public class NodeEstimator implements DataObject {
     NodeReference ref;
     RoutingMemory mem;
     public NodeReference reference() {
@@ -70,14 +73,22 @@
        
        abstract public ComparableStringMap getDiagProperties();
        
+       /** Returns a tool which can be used for generating HTML reports of this 
estimator. */ 
+       abstract public NodeEstimator.HTMLReportTool getHTMLReportingTool();
+       /** An interface which represents a tool that gives the user of the
+       * NodeEstimator some help with rendering the estimator to HTML */
+       public interface HTMLReportTool
+       {
        abstract public void toHtml(PrintWriter pw, String imageLinkPrefix) 
                throws IOException;
-       
        abstract public String graphName(int id);
-       
        abstract public int graphType(String type);
-       
        abstract public int estimatorType(int estimator);
-       
        abstract public TimeEstimator getEstimator(int type);
+               abstract public TimeEstimator getEstimator(String type);
+               /**     Draws all estimator graphs on the supplied
+               bitmap.  lineColors is expected to map from estimator names to Colors
+               If a color for any type of estimator is missing then the graph for 
that estimator won't be drawn to the bmp**/
+               public void drapGraphOnImage(boolean dontClipPoints, Bitmap 
bmp,Hashtable lineColors);
+       }
 }

_______________________________________________
cvs mailing list
[EMAIL PROTECTED]
http://dodo.freenetproject.org/cgi-bin/mailman/listinfo/cvs

Reply via email to