Update of /cvsroot/freenet/freenet/src/freenet/node/rt/testapplication
In directory sc8-pr-cvs1:/tmp/cvs-serv31921/src/freenet/node/rt/testapplication
Added Files:
NGRTestApplication.java
Log Message:
Enabled the NGRtest application to load an existing routing table.
--- NEW FILE: NGRTestApplication.java ---
/*
* Created on Nov 24, 2003
*
*/
package freenet.node.rt.testapplication;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.AbstractListModel;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import freenet.Address;
import freenet.BadAddressException;
import freenet.DSAAuthentity;
import freenet.DSAIdentity;
import freenet.Identity;
import freenet.Key;
import freenet.Version;
import freenet.crypt.Global;
import freenet.crypt.RandomSource;
import freenet.crypt.Yarrow;
import freenet.node.NodeReference;
import freenet.node.rt.DataObjectRoutingStore;
import freenet.node.rt.DecayingRunningAverage;
import freenet.node.rt.NGRoutingTable;
import freenet.node.rt.NodeEstimator;
import freenet.node.rt.NodeEstimatorFactory;
import freenet.node.rt.ResponseTimeEstimator;
import freenet.node.rt.RunningAverageFactory;
import freenet.node.rt.StandardNodeEstimatorFactory;
import freenet.node.rt.TimeEstimatorFactory;
import freenet.support.SimpleDataObjectStore;
import freenet.support.StringMap;
import freenet.support.graph.Bitmap;
import freenet.transport.TCP;
/**
* @author Iakin
*
*/
public class NGRTestApplication extends JFrame{
public class NodeRefListModel extends AbstractListModel {
Hashtable idToNodeRefMap = new Hashtable();
LinkedList nodeRefList = new LinkedList();
public int getSize() {
return nodeRefList.size();
}
public Object getElementAt(int index) {
return
((NodeReference)nodeRefList.get(index)).firstPhysicalToString();
}
public void add(NodeReference ref){
idToNodeRefMap.put(ref.getIdentity().toString(),ref);
nodeRefList.addLast(ref);
this.fireIntervalAdded(this,nodeRefList.size()-1,nodeRefList.size()-1);
}
}
HashSet selectedNodeRefs = new HashSet();
NGRoutingTable ngrt;
OverviewBitmapLabel overviewImagePanel;
DetailBitmapLabel detailImagePanel;
LabelPairPanel selectedValuePanel;
LabelPairPanel selectedKeyPanel;
JList nodeRefsList;
NodeRefListModel nodeRefsListModel= new NodeRefListModel();
RandomSource randomsource = new Yarrow();
File routingDir;
boolean bAppInitialized = false;
public NGRTestApplication() {
super("NGRTest");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
boolean bKnowsWhatToDo = false;
while(!bKnowsWhatToDo)
{
MessageBox m = new MessageBox();
m.setTitle("Open existing routing table?");
m.setCloseWindowCommand("No");
m.setFrame(this);
String result= null;
try {
result = m.askYesNoModal("Do you want to open an
existing routing table?");
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(result.equalsIgnoreCase("Yes"))
{
File dir;
try {
dir = askRTDir();
if(dir != null){
bKnowsWhatToDo = true;
routingDir = dir;
}
} catch (InterruptedException e2) {
e2.printStackTrace();
}
}else{
bKnowsWhatToDo = true;
routingDir = new File("dfasdfdfsdfsdsdf232130");
//Something that doesn't exist.... (for now at least)
}
}
try{
createUI();
setBounds(0,0,500,400);
setVisible(true);
createNGRT();
}catch(Throwable e){
System.err.println(e);
}
bAppInitialized = true;
detailImagePanel.regenerate();
overviewImagePanel.regenerate();
}
private File askRTDir() throws InterruptedException {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setDialogTitle("Select location of existing routing table
files");
chooser.setApproveButtonText("Select");
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File dir = chooser.getSelectedFile();
if (!dir.isDirectory()){
MessageBox m = new MessageBox();
m.setTitle("Error");
m.setFrame(this);
m.askOkayModal("Selected 'directory' does not appear
to be a directory");
return null;
}
if (!dir.exists()){
MessageBox m = new MessageBox();
m.setTitle("Error");
m.setFrame(this);
m.askOkayModal("Selected 'directory' does not appear
to exist");
return null;
}
File rtFile = new File(dir,"ngrt_global_a");
if(!rtFile.exists()){
MessageBox m = new MessageBox();
m.setTitle("Error");
m.setFrame(this);
m.askOkayModal("Selected directory does not appear to
contain a routingtable ('ngrt_global_a' is missing)");
return null;
}
rtFile = new File(dir,"rtprops_a");
if(!rtFile.exists()){
MessageBox m = new MessageBox();
m.setTitle("Error");
m.setFrame(this);
m.askOkayModal("Selected directory does not appear to
contain a routingtable ('rtprops_a' is missing)");
return null;
}
rtFile = new File(dir,"rtnodes_a");
if(!rtFile.exists()){
MessageBox m = new MessageBox();
m.setTitle("Error");
m.setFrame(this);
m.askOkayModal("Selected directory does not appear to
contain a routingtable ('rtnodes_a' is missing)");
return null;
}
return dir;
}
return null;
}
private void createUI() {
getContentPane().setLayout(new
BoxLayout(getContentPane(),BoxLayout.Y_AXIS));
overviewImagePanel = new OverviewBitmapLabel();
overviewImagePanel.setVisible(true);
overviewImagePanel.setMinimumSize(new Dimension(100,50));
overviewImagePanel.setPreferredSize(new Dimension(100,50));
getContentPane().add(overviewImagePanel);
JPanel detailPanel = new JPanel();
detailPanel.setLayout(new BoxLayout(detailPanel,BoxLayout.X_AXIS));
detailPanel.setMinimumSize(new Dimension(100,50));
detailPanel.setPreferredSize(new Dimension(100,50));
getContentPane().add(detailPanel);
detailPanel.setVisible(true);
detailImagePanel = new DetailBitmapLabel();
detailImagePanel.setLayout(new BorderLayout());
detailImagePanel.setMinimumSize(new Dimension(100,50));
detailImagePanel.setPreferredSize(new Dimension(100,50));
detailPanel.add(detailImagePanel,BorderLayout.WEST);
detailImagePanel.setVisible(true);
JPanel detailToolPanel = new JPanel();
detailToolPanel.setLayout(new
BoxLayout(detailToolPanel,BoxLayout.PAGE_AXIS));
detailToolPanel.setVisible(true);
detailPanel.add(detailToolPanel);
selectedValuePanel = new LabelPairPanel("Selected value: ",new
Long(0));
detailToolPanel.add(selectedValuePanel);
selectedKeyPanel = new LabelPairPanel("Selected key: ",new
Key(BigInteger.ZERO));
detailToolPanel.add(selectedKeyPanel);
ZoomToolPanel zoomPanel = new ZoomToolPanel();
zoomPanel.setVisible(true);
detailToolPanel.add(zoomPanel);
RTNodeRefPanel idPanel = new RTNodeRefPanel();
idPanel.setVisible(true);
getContentPane().add(idPanel);
}
static class BitmapPanel extends JPanel {
Bitmap img;
public BitmapPanel() {
super();
setBackground(Color.white);
}
public void paint(Graphics g) {
super.paint(g);
if(img == null)
return;
img.copyTo(g);
}
}
private static class Coordinate
{
int x=0;
int y=0;
}
abstract class SelectableKeyspaceGraphPanel extends BitmapPanel implements
MouseListener
{
NodeEstimator.lowestHighestPair MaxMin=new
NodeEstimator.lowestHighestPair();
private Coordinate cSelectedCoord = new Coordinate(); //The last
location where the used clicked on this panel
Key selectedKey = new Key(BigInteger.ZERO);
long selectedValue =0;
SelectableKeyspaceGraphPanel()
{
addMouseListener(this);
}
public void resize(int arg0, int arg1) {
super.resize(arg0, arg1);
regenerate();
}
private BigInteger getKeyspaceStepLength(){
return Key.keyspace.divide(BigInteger.valueOf(getWidth()));
}
protected void setSelection(Coordinate c)
{
cSelectedCoord = c;
selectedKey = new
Key(getKeyspaceStepLength().multiply(BigInteger.valueOf(cSelectedCoord.x)));
selectedValue =
Math.round((((double)MaxMin.highest)/getHeight())*cSelectedCoord.y);
}
protected abstract void regenerate();
protected void drawCross(Graphics g, Coordinate coord,Color c){
Color cOld = g.getColor();
g.setColor(c);
g.drawLine(coord.x-10,coord.y,coord.x+10,coord.y);
g.drawLine(coord.x,coord.y-10,coord.x,coord.y+10);
g.setColor(cOld);
}
public void paint(Graphics g) {
super.paint(g);
drawCross(g,cSelectedCoord,new Color(0,0,255));
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
Coordinate c = new Coordinate();
c.x = e.getX();
c.y = e.getY();
setSelection(c);
repaint();
}
public void reshape(int arg0, int arg1, int arg2, int arg3) {
super.reshape(arg0, arg1, arg2, arg3);
regenerate();
}
}
class OverviewBitmapLabel extends SelectableKeyspaceGraphPanel {
protected void regenerate() {
if (getWidth() > 0 && getHeight() > 0) {
if(bAppInitialized)
{
img = new Bitmap(getWidth(), getHeight());
MaxMin = ngrt.drapGraphOnImage(img, new
freenet.support.graph.Color(200, 200, 200));
Iterator i = selectedNodeRefs.iterator();
while (i.hasNext()) {
Identity id = ((NodeReference)
i.next()).getIdentity();
NodeEstimator est = (NodeEstimator)
ngrt.getEstimator(id);
est.getHTMLReportingTool().drawCombinedGraph(img, 0, new
freenet.support.graph.Color(0, 255, 0), MaxMin);
}
repaint();
}
}
}
}
class DetailBitmapLabel extends SelectableKeyspaceGraphPanel {
private JPopupMenu rightClickMenu = new JPopupMenu();
DetailBitmapLabel(){
super();
JMenuItem j = new JMenuItem("Report success on displayed nodes
here");
j.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
reportSuccessOnSelectedNodes(selectedKey,selectedValue);
}
});
rightClickMenu.add(j);
j = new JMenuItem("Report failure on displayed nodes here");
j.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
reportFailureOnSelectedNodes(selectedKey,selectedValue);
}
});
rightClickMenu.add(j);
j = new JMenuItem("Add node with this key as initial key");
j.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
addNode(selectedKey);
}
});
rightClickMenu.add(j);
}
protected void regenerate() {
freenet.support.graph.Color col = new
freenet.support.graph.Color(0, 255, 0);
NodeEstimator.lowestHighestPair newMaxMin = new
NodeEstimator.lowestHighestPair();
img = new Bitmap(getWidth(), getHeight());
Iterator i = selectedNodeRefs.iterator();
while (i.hasNext()) {
Identity id = ((NodeReference) i.next()).getIdentity();
NodeEstimator est = (NodeEstimator)
ngrt.getEstimator(id);
newMaxMin.merge(est.getHTMLReportingTool().drawCombinedGraph(img, 0, null, null));
}
this.MaxMin = newMaxMin;
i = selectedNodeRefs.iterator();
while (i.hasNext()) {
Identity id = ((NodeReference) i.next()).getIdentity();
NodeEstimator est = (NodeEstimator)
ngrt.getEstimator(id);
est.getHTMLReportingTool().drawCombinedGraph(img, 0,
col, MaxMin);
}
repaint();
}
protected void setSelection(Coordinate c)
{
super.setSelection(c);
selectedKeyPanel.setRight(selectedKey);
selectedValuePanel.setRight(new Long(selectedValue));
}
public void mouseReleased(MouseEvent e) {
super.mouseReleased(e); //TODO: this will cause an unnecessary
repaint
if(e.isPopupTrigger())
rightClickMenu.show(e.getComponent(), e.getX(),
e.getY());
//repaint();
}
}
class LabelPairPanel extends JPanel
{
Object oLeft;
Object oRight;
private JLabel left;
private JLabel right;
LabelPairPanel(Object oLeft,Object oRight)
{
setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
left = new JLabel(oLeft.toString());
add(left);
right = new JLabel(oRight.toString());
add(right);
this.oLeft = oLeft;
this.oRight = oRight;
}
void setRight(Object o){
right.setText(o.toString());
oRight = o;
}
Object getRight(){
return oRight;
}
}
class ZoomToolPanel extends JPanel {
ZoomToolPanel() {
setMinimumSize(new Dimension(200, 100));
setPreferredSize(new Dimension(200, 100));
setMaximumSize(new Dimension(200, 100));
setLayout(new BorderLayout());
JButton bZoomPlus = new JButton("+");
bZoomPlus.setVisible(true);
add(bZoomPlus, BorderLayout.WEST);
bZoomPlus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
zoomIn();
}
});
JButton bZoomMinus = new JButton("-");
bZoomMinus.setVisible(true);
add(bZoomMinus, BorderLayout.EAST);
bZoomMinus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
zoomOut();
}
});
JButton bpanUp = new JButton("Pan up");
bpanUp.setVisible(true);
add(bpanUp, BorderLayout.NORTH);
bpanUp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
panUp();
}
});
JButton bpanDown = new JButton("Pan down");
bpanDown.setVisible(true);
add(bpanDown, BorderLayout.SOUTH);
bpanDown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
panDown();
}
});
JButton bzoomAll = new JButton("Zoom extents");
bzoomAll.setVisible(true);
add(bzoomAll, BorderLayout.CENTER);
bzoomAll.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
zoomExtents();
}
});
}
}
class RTNodeRefPanel extends JPanel {
RTNodeRefPanel() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
nodeRefsList = new JList(nodeRefsListModel);
nodeRefsList.addSelectionInterval(0,nodeRefsListModel.getSize()-1); //Select all nodes
that is in the model by default
nodeRefsList.setVisible(true);
add(nodeRefsList, BorderLayout.NORTH);
nodeRefsList.addListSelectionListener(new
ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
//if (!e.getValueIsAdjusting()) {
for (int i = e.getFirstIndex(); i <=
e.getLastIndex(); i++) {
Object o =
nodeRefsListModel.nodeRefList.get(i);
if
(nodeRefsList.getSelectionModel().isSelectedIndex(i)) {
if
(!selectedNodeRefs.contains(o))
selectedNodeRefs.add(o);
} else if
(selectedNodeRefs.contains(o))
selectedNodeRefs.remove(o);
}
detailImagePanel.regenerate();
overviewImagePanel.regenerate();
//}
}
});
JPanel buttonPanel = new JPanel();
add(buttonPanel);
buttonPanel.setVisible(true);
buttonPanel.setLayout(new BoxLayout(buttonPanel,
BoxLayout.X_AXIS));
JButton bAddUnKeyedNode = new JButton("Add another node (no
key)");
bAddUnKeyedNode.setVisible(true);
buttonPanel.add(bAddUnKeyedNode);
bAddUnKeyedNode.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
addUnKeyedNode();
}
});
JButton bAddKeyedNode = new JButton("Add another node (with
selected key)");
bAddKeyedNode.setVisible(true);
buttonPanel.add(bAddKeyedNode);
bAddKeyedNode.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
addKeyedNode();
}
});
}
}
private void createNGRT(){
RunningAverageFactory raf = DecayingRunningAverage.factory();
TimeEstimatorFactory tef = ResponseTimeEstimator.factory();
NodeEstimatorFactory nef = new StandardNodeEstimatorFactory(raf, tef,
randomsource);
double initFastestTransfer = 40000;
File[] nodeFiles = new File[1];
nodeFiles[0] = new File(routingDir,"rtnodes_a");
File[] propsFiles = new File[1];
propsFiles[0] = new File(routingDir,"rtprops_a");
SimpleDataObjectStore rtNodes = null;
SimpleDataObjectStore rtProps = null;
try {
rtNodes = new SimpleDataObjectStore(nodeFiles, 0);
rtProps = new SimpleDataObjectStore(propsFiles, 0);
} catch (IOException e) {
System.err.print("Should not happen!");
e.printStackTrace();
}
DataObjectRoutingStore routingStore =new
DataObjectRoutingStore(rtNodes, rtProps);
try {
ngrt = new NGRoutingTable(routingStore, 100, nef, tef,
initFastestTransfer,routingDir);
StringMap[] refs = ngrt.getSnapshot().refData();
for (int i = 0; i < refs.length; i++) {
NodeReference ref =
(NodeReference)refs[i].values()[11];
nodeRefsListModel.add(ref);
selectedNodeRefs.add(ref);
nodeRefsList.addSelectionInterval(nodeRefsListModel.getSize()-1,nodeRefsListModel.getSize()-1);
}
overviewImagePanel.regenerate();
detailImagePanel.regenerate();
} catch (IOException e1) {
System.err.print("Should not happen!");
e1.printStackTrace();
}
}
private void addUnKeyedNode(){
addNode(null);
}
private void addKeyedNode()
{
addNode((Key)selectedKeyPanel.getRight());
}
private void addNode(Key InitialKey){
DSAAuthentity auth = new DSAAuthentity(Global.DSAgroupC,randomsource);
DSAIdentity id = new DSAIdentity(Global.DSAgroupC,auth);
Address[] a= new Address[1];
try {
a[0] = (new TCP(100, false)).getAddress("<dummy>:0");
} catch (BadAddressException e) {
System.err.print("Should not happen!");
e.printStackTrace();
}
NodeReference ref = new NodeReference(id, a, new long[0], new
long[0],Version.getVersionString(),null);
ref.addSignature(auth);
nodeRefsListModel.add(ref);
ngrt.reference(InitialKey,ref,null);
selectedNodeRefs.add(ref);
nodeRefsList.addSelectionInterval(nodeRefsListModel.getSize()-1,nodeRefsListModel.getSize()-1);
overviewImagePanel.regenerate();
detailImagePanel.regenerate();
}
private void zoomIn()
{
//TODO: Implement
}
private void zoomOut()
{
//TODO: Implement
}
private void zoomExtents()
{
//TODO: Implement
}
private void panUp()
{
//TODO: Implement
}
private void panDown()
{
//TODO: Implement
}
private void reportSuccessOnSelectedNodes(Key k,long timeValue)
{
//TODO: Implement
detailImagePanel.regenerate();
overviewImagePanel.regenerate();
}
private void reportFailureOnSelectedNodes(Key k,long timeValue)
{
//TODO: Implement
detailImagePanel.regenerate();
overviewImagePanel.regenerate();
}
public static void main(String[] args) {
new NGRTestApplication();
}
}
_______________________________________________
cvs mailing list
[EMAIL PROTECTED]
http://dodo.freenetproject.org/cgi-bin/mailman/listinfo/cvs