package org.netfpga.router;
/*
* AbstractMainFrame.java
*
* Created on May 8, 2007, 11:05 PM
*/
import jargs.gnu.CmdLineParser;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.Timer;
import org.netfpga.eventcap.EventCaptureMainFrame;
import org.netfpga.mdi.MDIDesktopPane;
import org.netfpga.mdi.WindowMenu;
import org.netfpga.backend.NFDevice;
import org.netfpga.backend.NFDeviceConsts;
import org.netfpga.backend.NFDeviceTest;
/**
*
* @author jnaous
*/
@SuppressWarnings("serial")
public abstract class AbstractMainFrame extends javax.swing.JFrame {
private JScrollPane scrollPane = new JScrollPane();
private JInternalFrame quickstartFrame;
protected NFDevice nf2;
private static final String DEFAULT_NF_DEVICE = "nf2c0";
protected Timer updateTimer;
private int updateDelay = 1000;
/**
* Creates new form AbstractMainFrame
*/
public AbstractMainFrame() {
initComponents();
menuBar.add(new WindowMenu((MDIDesktopPane) desktopPane));
/* Add scrolling for the Frames */
scrollPane.getViewport().add(desktopPane);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(scrollPane,BorderLayout.CENTER);
/* instantiate the device object to interact with the hardware */
nf2 = new NFDevice(DEFAULT_NF_DEVICE);
if(nf2.checkIface()!=0){
System.err.println("Cannot find interface "+nf2.getIfaceName());
System.exit(1);
}
if(nf2.openDescriptor()!=0){
System.err.println("Cannot open interface "+nf2.getIfaceName());
System.exit(1);
}
/* enable DMA */
//nf2.writeReg(NFDeviceConsts.DMA_ENABLE_REG, 1);
ActionListener taskPerformer = new ActionListener() {
int count=0;
public void actionPerformed(ActionEvent evt) {
// System.out.println("Count is: "+count);
count++;
}
};
updateTimer = new Timer(updateDelay, taskPerformer);
updateTimer.start();
/* instantiate the quickstart frame */
quickstartFrame = this.getNewQuickStartFrame();
quickstartFrame.setVisible(true);
((MDIDesktopPane)this.desktopPane).add(quickstartFrame);
setContentPane(desktopPane);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents() {
desktopPane = new org.netfpga.mdi.MDIDesktopPane();
menuBar = new javax.swing.JMenuBar();
fileMenu = new javax.swing.JMenu();
devNameMenuItem = new javax.swing.JMenuItem();
updateTimeMenuItem = new javax.swing.JMenuItem();
exitMenuItem = new javax.swing.JMenuItem();
helpMenu = new javax.swing.JMenu();
aboutMenuItem = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Router Control Panel");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
});
fileMenu.setText("File");
devNameMenuItem.setText("Edit device name");
devNameMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
devNameMenuItemActionPerformed(evt);
}
});
fileMenu.add(devNameMenuItem);
updateTimeMenuItem.setText("Update rate");
updateTimeMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
updateTimeMenuItemActionPerformed(evt);
}
});
fileMenu.add(updateTimeMenuItem);
exitMenuItem.setText("Exit");
exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitMenuItemActionPerformed(evt);
}
});
fileMenu.add(exitMenuItem);
menuBar.add(fileMenu);
helpMenu.setText("Help");
aboutMenuItem.setText("About");
helpMenu.add(aboutMenuItem);
// menuBar.add(helpMenu);
setJMenuBar(menuBar);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 917, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 755, Short.MAX_VALUE)
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void updateTimeMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateTimeMenuItemActionPerformed
boolean pass=false;
String delayStr;
do {
delayStr = JOptionPane.showInputDialog("Enter the update interval in milliseconds (integer value): ");
if(delayStr==null){
pass = true;
} else {
try{
this.updateDelay = Integer.parseInt(delayStr);
pass = true;
} catch (NumberFormatException e){
pass = false;
}
}
} while (!pass);
updateTimer.setDelay(this.updateDelay);
}//GEN-LAST:event_updateTimeMenuItemActionPerformed
private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
nf2.closeDescriptor();
this.updateTimer.stop();
}//GEN-LAST:event_formWindowClosing
private void devNameMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_devNameMenuItemActionPerformed
String devName;
String oldDevName=nf2.getIfaceName();
boolean pass;
do{
pass = true;
devName = JOptionPane.showInputDialog("Enter the name of the interface (e.g. nf2c0):");
if(devName!=null){
try {
nf2.closeDescriptor();
} catch (Exception e) {
System.err.println("Caught Exception when closing descriptor");
e.printStackTrace();
}
nf2.setIfaceName(devName);
if(nf2.checkIface()!=0){
String error = "Cannot find interface "+nf2.getIfaceName();
JOptionPane.showMessageDialog(null, error, "Warning", JOptionPane.ERROR_MESSAGE);
System.err.println(error);
pass = false;
}
if(nf2.openDescriptor()!=0){
String error = "Cannot open interface "+nf2.getIfaceName();
JOptionPane.showMessageDialog(null, error, "Warning", JOptionPane.ERROR_MESSAGE);
System.err.println(error);
pass=false;
}
}
} while (devName!=null && !pass);
/* reset to old device name */
if(devName==null){
nf2.closeDescriptor();
nf2.setIfaceName(oldDevName);
if(nf2.checkIface()!=0){
String error = "Cannot find interface "+nf2.getIfaceName();
JOptionPane.showMessageDialog(null, error, "Warning", JOptionPane.ERROR_MESSAGE);
System.err.println(error);
System.exit(1);
}
if(nf2.openDescriptor()!=0){
String error = "Cannot open interface "+nf2.getIfaceName();
JOptionPane.showMessageDialog(null, error, "Warning", JOptionPane.ERROR_MESSAGE);
System.err.println(error);
System.exit(1);
}
}
}//GEN-LAST:event_devNameMenuItemActionPerformed
private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
System.exit(0);
}//GEN-LAST:event_exitMenuItemActionPerformed
public javax.swing.JDesktopPane getDesktopPane() {
return desktopPane;
}
protected static void printUsage() {
System.err.println(
"Usage: router [--use_bin <binFile>]\n" +
"<binFile> is the file to use as bin for the download.");
}
/**
* Create a new instance of the quickstart frame
*/
protected abstract JInternalFrame getNewQuickStartFrame();
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JMenuItem aboutMenuItem;
private javax.swing.JDesktopPane desktopPane;
private javax.swing.JMenuItem devNameMenuItem;
private javax.swing.JMenuItem exitMenuItem;
private javax.swing.JMenu fileMenu;
private javax.swing.JMenu helpMenu;
private javax.swing.JMenuBar menuBar;
private javax.swing.JMenuItem updateTimeMenuItem;
// End of variables declaration//GEN-END:variables
/**
* parses the command line options, downloads the bin file,
* and starts the software router
* @param args
* @param default_bin
*/
public static void runEnv(String[] args, String default_bin) {
CmdLineParser parser = new CmdLineParser();
CmdLineParser.Option name = parser.addStringOption("use_bin");
CmdLineParser.Option sr = parser.addBooleanOption("sr");
CmdLineParser.Option dl = parser.addBooleanOption("no_download");
try {
parser.parse(args);
}
catch ( CmdLineParser.OptionException e ) {
System.err.println(e.getMessage());
printUsage();
System.exit(2);
}
String binFile = (String)parser.getOptionValue(name, default_bin);
boolean no_sr = !(Boolean)parser.getOptionValue(sr, Boolean.FALSE);
boolean no_download = (Boolean)parser.getOptionValue(dl, Boolean.FALSE);
/* Download the bin file */
if(!no_download){
try {
Process p = Runtime.getRuntime().exec("nf_download "+binFile);
BufferedReader input =
new BufferedReader
(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
System.out.println(line);
}
p.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/* start the software router */
if(!no_sr){
try {
@SuppressWarnings("unused")
Process p = Runtime.getRuntime().exec("."+File.separator+"sr -r rtable cpuhw");
BufferedReader input =
new BufferedReader
(new InputStreamReader(p.getInputStream()));
String line;
int i=0;
while ((line = input.readLine()) != null && i<20) {
System.out.println(line);
i++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}