Cleaned up the code a little
All checks were successful
Locusworks Team/lsproject/pipeline/head This commit looks good
All checks were successful
Locusworks Team/lsproject/pipeline/head This commit looks good
This commit is contained in:
21
pom.xml
21
pom.xml
@ -22,6 +22,27 @@
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<finalName>LSProject</finalName>
|
||||
<shadedArtifactAttached>true</shadedArtifactAttached>
|
||||
<transformers>
|
||||
<transformer
|
||||
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<mainClass>net.locusworks.lsproject.driver.Program</mainClass>
|
||||
</transformer>
|
||||
</transformers>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
@ -28,17 +28,6 @@ public class Program {
|
||||
* and System.err are sent instead to a designated console window.
|
||||
*/
|
||||
public static void setupStreams() {
|
||||
PrintStream errorStream = new PrintStream(new OutputStream() {
|
||||
@Override
|
||||
public void write(int c) throws IOException {
|
||||
write(new byte[] { Byte.parseByte(Integer.toString(c)) });
|
||||
}
|
||||
|
||||
public void write(byte[] b) {
|
||||
console.writeError(new String(b));
|
||||
}
|
||||
});
|
||||
|
||||
PrintStream outputStream = new PrintStream(new OutputStream() {
|
||||
@Override
|
||||
public void write(int c) throws IOException {
|
||||
@ -50,7 +39,6 @@ public class Program {
|
||||
}
|
||||
});
|
||||
|
||||
//System.setErr(errorStream);
|
||||
System.setOut(outputStream);
|
||||
}
|
||||
}
|
||||
|
@ -2,8 +2,6 @@ package net.locusworks.lsproject.gui;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.GridLayout;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
@ -12,106 +10,100 @@ import javax.swing.event.TableModelEvent;
|
||||
import javax.swing.event.TableModelListener;
|
||||
import javax.swing.table.TableModel;
|
||||
|
||||
import net.locusworks.lsproject.driver.Program;
|
||||
import net.locusworks.lsproject.object.Network;
|
||||
import net.locusworks.lsproject.object.Router;
|
||||
|
||||
|
||||
public class CostViewerWindow extends JPanel implements TableModelListener{
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 8571350255455457432L;
|
||||
private boolean DEBUG = false;
|
||||
private Router router = null;
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 8571350255455457432L;
|
||||
private Router router = null;
|
||||
|
||||
|
||||
public CostViewerWindow(Router _router){
|
||||
super(new BorderLayout());
|
||||
public CostViewerWindow(Router _router){
|
||||
super(new BorderLayout());
|
||||
|
||||
router = _router;
|
||||
router = _router;
|
||||
|
||||
String[] columnNames = {"Connection",
|
||||
"Cost"};
|
||||
String[] columnNames = {"Connection", "Cost"};
|
||||
|
||||
int numberOfConnections = router.getConnections().size();
|
||||
int numberOfConnections = router.getConnections().size();
|
||||
|
||||
Object data[][]= new Object[numberOfConnections][2];
|
||||
Object data[][]= new Object[numberOfConnections][2];
|
||||
|
||||
for (int i = 0; i < numberOfConnections; i++){
|
||||
for (int i = 0; i < numberOfConnections; i++){
|
||||
|
||||
String tempFirstParticipant = router.getConnections()
|
||||
.get(i).getFirstParticipantAddress();
|
||||
String tempFirstParticipant = router.getConnections()
|
||||
.get(i).getFirstParticipantAddress();
|
||||
|
||||
String tempSecondParticipant = router.getConnections()
|
||||
.get(i).getSecondParticipantAddress();
|
||||
String tempSecondParticipant = router.getConnections()
|
||||
.get(i).getSecondParticipantAddress();
|
||||
|
||||
if(tempFirstParticipant.equals(router.getIpAddress())){
|
||||
data[i][0] = tempSecondParticipant;
|
||||
}
|
||||
else{
|
||||
data[i][0] = tempFirstParticipant;
|
||||
}
|
||||
data[i][1] = router.getConnections().get(i).getCost();
|
||||
}
|
||||
if(tempFirstParticipant.equals(router.getIpAddress())){
|
||||
data[i][0] = tempSecondParticipant;
|
||||
}
|
||||
else{
|
||||
data[i][0] = tempFirstParticipant;
|
||||
}
|
||||
data[i][1] = router.getConnections().get(i).getCost();
|
||||
}
|
||||
|
||||
final JTable table = new JTable(data, columnNames);
|
||||
table.setPreferredScrollableViewportSize(new Dimension(300, 70));
|
||||
table.setFillsViewportHeight(true);
|
||||
table.getModel().addTableModelListener(this);
|
||||
final JTable table = new JTable(data, columnNames);
|
||||
table.setPreferredScrollableViewportSize(new Dimension(300, 70));
|
||||
table.setFillsViewportHeight(true);
|
||||
table.getModel().addTableModelListener(this);
|
||||
|
||||
JScrollPane scrollPane = new JScrollPane(table);
|
||||
add(scrollPane);
|
||||
JScrollPane scrollPane = new JScrollPane(table);
|
||||
add(scrollPane);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void createGUI(){
|
||||
public void createGUI(){
|
||||
|
||||
JFrame frame = new JFrame("Cost Viewer For Router " + router.getIpAddress());
|
||||
CostViewerWindow newContentPane = new CostViewerWindow(router);
|
||||
newContentPane.setOpaque(true);
|
||||
frame.setContentPane(newContentPane);
|
||||
frame.pack();
|
||||
frame.setLocationRelativeTo(null);
|
||||
frame.setVisible(true);
|
||||
}
|
||||
JFrame frame = new JFrame("Cost Viewer For Router " + router.getIpAddress());
|
||||
CostViewerWindow newContentPane = new CostViewerWindow(router);
|
||||
newContentPane.setOpaque(true);
|
||||
frame.setContentPane(newContentPane);
|
||||
frame.pack();
|
||||
frame.setLocationRelativeTo(null);
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tableChanged(TableModelEvent e) {
|
||||
if (e.getColumn() == 0) {
|
||||
return;
|
||||
}
|
||||
@Override
|
||||
public void tableChanged(TableModelEvent e) {
|
||||
if (e.getColumn() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
int row = e.getFirstRow();
|
||||
int column = e.getColumn();
|
||||
TableModel model = (TableModel)e.getSource();
|
||||
Object data = model.getValueAt(row, column);
|
||||
int row = e.getFirstRow();
|
||||
int column = e.getColumn();
|
||||
TableModel model = (TableModel)e.getSource();
|
||||
Object data = model.getValueAt(row, column);
|
||||
|
||||
final String otherRouterIp = model.getValueAt(row, 0).toString();
|
||||
final String otherRouterIp = model.getValueAt(row, 0).toString();
|
||||
|
||||
final int oldCost = router.getConnectionByParticipant(router.getIpAddress(), otherRouterIp).getCost();
|
||||
int newCost = oldCost;
|
||||
try {
|
||||
newCost = Integer.parseInt(data.toString());
|
||||
final int oldCost = router.getConnectionByParticipant(router.getIpAddress(), otherRouterIp).getCost();
|
||||
int newCost = oldCost;
|
||||
try {
|
||||
newCost = Integer.parseInt(data.toString());
|
||||
|
||||
if (newCost < 1 || newCost > 10){
|
||||
System.out.println("Cost Has To Be Between 0 - 10");
|
||||
if (newCost < 1 || newCost > 10){
|
||||
System.out.println("Cost Has To Be Between 0 - 10");
|
||||
|
||||
newCost = oldCost;
|
||||
newCost = oldCost;
|
||||
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
router.getConnectionByParticipant(
|
||||
router.getIpAddress(), otherRouterIp).setCost(newCost);
|
||||
Router otherRouter = Network.getInstance().getRouterByIp(otherRouterIp);
|
||||
otherRouter.getConnectionByParticipant(
|
||||
router.getIpAddress(), otherRouterIp).setCost(newCost);
|
||||
}
|
||||
catch (NumberFormatException ex) {
|
||||
System.out.println("Invalid cost.");
|
||||
}
|
||||
}
|
||||
router.getConnectionByParticipant(router.getIpAddress(), otherRouterIp).setCost(newCost);
|
||||
Router otherRouter = Network.getInstance().getRouterByIp(otherRouterIp);
|
||||
otherRouter.getConnectionByParticipant(router.getIpAddress(), otherRouterIp).setCost(newCost);
|
||||
} catch (NumberFormatException ex) {
|
||||
System.out.println("Invalid cost.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -360,9 +360,7 @@ public class RouterViewerWindow extends JFrame implements MouseListener,
|
||||
@Override public void keyPressed(KeyEvent e) {
|
||||
keys[e.getKeyCode()] = true;
|
||||
|
||||
if (highlightedRouter != null &&
|
||||
!highlightedRouter.equals("") &&
|
||||
keys[KeyEvent.VK_DELETE]) {
|
||||
if (highlightedRouter != null && !highlightedRouter.equals("") && keys[KeyEvent.VK_DELETE]) {
|
||||
Network.getInstance().removeRouterByIp(highlightedRouter);
|
||||
|
||||
try {
|
||||
@ -375,7 +373,7 @@ public class RouterViewerWindow extends JFrame implements MouseListener,
|
||||
updateAllRouters();
|
||||
}
|
||||
|
||||
else if (keys[KeyEvent.VK_S] && ((e.getModifiers() & KeyEvent.VK_CONTROL) == 0)) {
|
||||
else if (keys[KeyEvent.VK_S] && ((e.getModifiersEx() & KeyEvent.VK_CONTROL) == 0)) {
|
||||
try {
|
||||
Utility.saveNetwork();
|
||||
|
||||
|
@ -8,7 +8,6 @@ import javax.swing.JScrollPane;
|
||||
import javax.swing.JTable;
|
||||
import javax.swing.table.AbstractTableModel;
|
||||
|
||||
import net.locusworks.lsproject.driver.Program;
|
||||
import net.locusworks.lsproject.object.Network;
|
||||
import net.locusworks.lsproject.object.Router;
|
||||
import net.locusworks.lsproject.object.helper.DijkstraCallback;
|
||||
@ -17,181 +16,172 @@ import net.locusworks.lsproject.object.helper.DijkstraCallback;
|
||||
*
|
||||
*/
|
||||
public class RoutingTableWindow extends JFrame implements WindowListener {
|
||||
private static final long serialVersionUID = 1768859322938299953L;
|
||||
private static final long serialVersionUID = 1768859322938299953L;
|
||||
|
||||
private Router router = null;
|
||||
private JTable table = null;
|
||||
private RoutingTableModel tableModel = new RoutingTableModel();
|
||||
private String set = "";
|
||||
private Router router = null;
|
||||
private JTable table = null;
|
||||
private RoutingTableModel tableModel = new RoutingTableModel();
|
||||
private String set = "";
|
||||
|
||||
/**
|
||||
* Display a new routing table window.
|
||||
* @param _router
|
||||
*/
|
||||
public RoutingTableWindow(Router _router) {
|
||||
super("Routing table - " + _router.getIpAddress());
|
||||
/**
|
||||
* Display a new routing table window.
|
||||
* @param _router
|
||||
*/
|
||||
public RoutingTableWindow(Router _router) {
|
||||
super("Routing table - " + _router.getIpAddress());
|
||||
|
||||
router = _router;
|
||||
router = _router;
|
||||
|
||||
initializeComponents();
|
||||
initializeComponents();
|
||||
|
||||
setVisible(true);
|
||||
}
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the GUI components used by this window.
|
||||
*/
|
||||
private void initializeComponents() {
|
||||
setLayout(new BorderLayout());
|
||||
/**
|
||||
* Initialize the GUI components used by this window.
|
||||
*/
|
||||
private void initializeComponents() {
|
||||
setLayout(new BorderLayout());
|
||||
|
||||
table = new JTable(tableModel);
|
||||
JScrollPane scrollPane = new JScrollPane(table);
|
||||
table = new JTable(tableModel);
|
||||
JScrollPane scrollPane = new JScrollPane(table);
|
||||
|
||||
add(table.getTableHeader(), BorderLayout.PAGE_START);
|
||||
add(scrollPane, BorderLayout.CENTER);
|
||||
add(table.getTableHeader(), BorderLayout.PAGE_START);
|
||||
add(scrollPane, BorderLayout.CENTER);
|
||||
|
||||
setSize(425, 200);
|
||||
setSize(425, 200);
|
||||
|
||||
router.getRoutingTable().registerCallback(tableModel);
|
||||
router.getRoutingTable().registerCallback(tableModel);
|
||||
|
||||
router.getRoutingTable().execute(router, null);
|
||||
}
|
||||
router.getRoutingTable().execute(router, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allow access to which router this window is displaying.
|
||||
* @return Router object.
|
||||
*/
|
||||
public Router getRouter() {
|
||||
return router;
|
||||
}
|
||||
/**
|
||||
* Allow access to which router this window is displaying.
|
||||
* @return Router object.
|
||||
*/
|
||||
public Router getRouter() {
|
||||
return router;
|
||||
}
|
||||
|
||||
/**
|
||||
* Model used for displaying the routing table.
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
private class RoutingTableModel extends AbstractTableModel implements DijkstraCallback {
|
||||
private ArrayList<String> columnNames = new ArrayList<String>();
|
||||
private Object[][] data;
|
||||
private int currentRow = 0;
|
||||
/**
|
||||
* Model used for displaying the routing table.
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
private class RoutingTableModel extends AbstractTableModel implements DijkstraCallback {
|
||||
private ArrayList<String> columnNames = new ArrayList<String>();
|
||||
private Object[][] data;
|
||||
private int currentRow = 0;
|
||||
|
||||
public RoutingTableModel() {
|
||||
}
|
||||
public RoutingTableModel() {
|
||||
}
|
||||
|
||||
/*
|
||||
* Handle callback.
|
||||
*/
|
||||
/*
|
||||
* Handle callback.
|
||||
*/
|
||||
|
||||
@Override public void reset() {
|
||||
set = router.getIpAddress();
|
||||
columnNames.clear();
|
||||
currentRow = 0;
|
||||
@Override public void reset() {
|
||||
set = router.getIpAddress();
|
||||
columnNames.clear();
|
||||
currentRow = 0;
|
||||
|
||||
columnNames.add("N'");
|
||||
columnNames.add(router.toString());
|
||||
for (Router r : Network.getInstance().getRouters()) {
|
||||
if (r != router) {
|
||||
columnNames.add(r.getIpAddress());
|
||||
}
|
||||
}
|
||||
columnNames.add("N'");
|
||||
columnNames.add(router.toString());
|
||||
for (Router r : Network.getInstance().getRouters()) {
|
||||
if (r != router) {
|
||||
columnNames.add(r.getIpAddress());
|
||||
}
|
||||
}
|
||||
|
||||
fireTableStructureChanged();
|
||||
fireTableStructureChanged();
|
||||
|
||||
data = new Object[Network.getInstance().getRouters().size()][columnNames.size()];
|
||||
data = new Object[Network.getInstance().getRouters().size()][columnNames.size()];
|
||||
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
for (int j =0; j < data[i].length; j++) {
|
||||
data[i][j] = "";
|
||||
fireTableCellUpdated(i, j);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
for (int j =0; j < data[i].length; j++) {
|
||||
data[i][j] = "";
|
||||
fireTableCellUpdated(i, j);
|
||||
}
|
||||
}
|
||||
|
||||
setValueAt(set, 0, 0);
|
||||
setValueAt(set, 0, 0);
|
||||
|
||||
fireTableDataChanged();
|
||||
fireTableDataChanged();
|
||||
|
||||
System.out.println("Routing table " + router + " reset.");
|
||||
}
|
||||
System.out.println("Routing table " + router + " reset.");
|
||||
}
|
||||
|
||||
@Override public void predecessorAdded(Router key, Router value) {
|
||||
if (key == router) return;
|
||||
final int columnIndex = getColumnByName(value.getIpAddress());
|
||||
@Override public void predecessorAdded(Router key, Router value) {
|
||||
if (key == router) return;
|
||||
final int columnIndex = getColumnByName(value.getIpAddress());
|
||||
|
||||
setValueAt(key.getIpAddress() + "," + getValueAt(currentRow, columnIndex), currentRow, columnIndex);
|
||||
}
|
||||
setValueAt(key.getIpAddress() + "," + getValueAt(currentRow, columnIndex), currentRow, columnIndex);
|
||||
}
|
||||
|
||||
@Override public void settledRouterAdded(Router r) {
|
||||
if (set.contains(r.getIpAddress())) return;
|
||||
@Override public void settledRouterAdded(Router r) {
|
||||
if (set.contains(r.getIpAddress())) return;
|
||||
|
||||
currentRow++;
|
||||
currentRow++;
|
||||
|
||||
set += (set.length() > 0 ? "," : "") + r.getIpAddress();
|
||||
set += (set.length() > 0 ? "," : "") + r.getIpAddress();
|
||||
|
||||
final int rowIndex = set.split("\\,").length - 1;
|
||||
final int rowIndex = set.split("\\,").length - 1;
|
||||
|
||||
setValueAt(set, rowIndex, 0);
|
||||
}
|
||||
setValueAt(set, rowIndex, 0);
|
||||
}
|
||||
|
||||
@Override public void shortestDistanceUpdated(Router router, int cost) {
|
||||
final int column = getColumnByName(router.getIpAddress());
|
||||
@Override public void shortestDistanceUpdated(Router router, int cost) {
|
||||
final int column = getColumnByName(router.getIpAddress());
|
||||
|
||||
setValueAt(cost, currentRow, column);
|
||||
}
|
||||
setValueAt(cost, currentRow, column);
|
||||
}
|
||||
|
||||
/*
|
||||
* Override AbstractTableModel functions.
|
||||
*/
|
||||
/*
|
||||
* Override AbstractTableModel functions.
|
||||
*/
|
||||
|
||||
@Override public String getColumnName(int columnNumber) {
|
||||
return columnNames.get(columnNumber);
|
||||
}
|
||||
@Override public String getColumnName(int columnNumber) {
|
||||
return columnNames.get(columnNumber);
|
||||
}
|
||||
|
||||
public int getColumnByName(String columnName) {
|
||||
for (int i = 0; i < columnNames.size(); i++) {
|
||||
if (columnNames.get(i).equals(columnName)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
public int getColumnByName(String columnName) {
|
||||
for (int i = 0; i < columnNames.size(); i++) {
|
||||
if (columnNames.get(i).equals(columnName)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public int getRowByFirstCellName(String cellName) {
|
||||
for (int i = 1; i < getRowCount(); i++) {
|
||||
if (columnNames.get(i).endsWith(cellName)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
@Override public int getColumnCount() {
|
||||
return columnNames.size();
|
||||
}
|
||||
|
||||
@Override public int getColumnCount() {
|
||||
return columnNames.size();
|
||||
}
|
||||
@Override public int getRowCount() {
|
||||
return data.length;
|
||||
}
|
||||
|
||||
@Override public int getRowCount() {
|
||||
return data.length;
|
||||
}
|
||||
@Override public Object getValueAt(int rowIndex, int columnIndex) {
|
||||
return data[rowIndex][columnIndex];
|
||||
}
|
||||
|
||||
@Override public Object getValueAt(int rowIndex, int columnIndex) {
|
||||
return data[rowIndex][columnIndex];
|
||||
}
|
||||
@Override public void setValueAt(Object value, int rowIndex, int columnIndex) {
|
||||
data[rowIndex][columnIndex] = value;
|
||||
|
||||
@Override public void setValueAt(Object value, int rowIndex, int columnIndex) {
|
||||
data[rowIndex][columnIndex] = value;
|
||||
super.fireTableCellUpdated(rowIndex, columnIndex);
|
||||
}
|
||||
}
|
||||
|
||||
super.fireTableCellUpdated(rowIndex, columnIndex);
|
||||
}
|
||||
}
|
||||
@Override public void windowClosing(WindowEvent e) {
|
||||
router.getRoutingTable().unregisterCallback(tableModel);
|
||||
}
|
||||
|
||||
@Override public void windowClosing(WindowEvent e) {
|
||||
router.getRoutingTable().unregisterCallback(tableModel);
|
||||
}
|
||||
|
||||
@Override public void windowActivated(WindowEvent arg0) {}
|
||||
@Override public void windowClosed(WindowEvent arg0) {}
|
||||
@Override public void windowDeactivated(WindowEvent arg0) {}
|
||||
@Override public void windowDeiconified(WindowEvent arg0) {}
|
||||
@Override public void windowIconified(WindowEvent arg0) {}
|
||||
@Override public void windowOpened(WindowEvent arg0) {}
|
||||
@Override public void windowActivated(WindowEvent arg0) {}
|
||||
@Override public void windowClosed(WindowEvent arg0) {}
|
||||
@Override public void windowDeactivated(WindowEvent arg0) {}
|
||||
@Override public void windowDeiconified(WindowEvent arg0) {}
|
||||
@Override public void windowIconified(WindowEvent arg0) {}
|
||||
@Override public void windowOpened(WindowEvent arg0) {}
|
||||
}
|
||||
|
@ -2,16 +2,11 @@ package net.locusworks.lsproject.gui;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.filechooser.FileFilter;
|
||||
|
||||
import net.locusworks.lsproject.driver.Program;
|
||||
import net.locusworks.lsproject.gui.helper.ImageHelper;
|
||||
import net.locusworks.lsproject.object.Network;
|
||||
import net.locusworks.lsproject.util.Utility;
|
||||
|
@ -7,7 +7,6 @@ import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.locusworks.lsproject.driver.Program;
|
||||
import net.locusworks.lsproject.object.Connection;
|
||||
import net.locusworks.lsproject.object.DijkstraEngine;
|
||||
import net.locusworks.lsproject.object.Network;
|
||||
|
Reference in New Issue
Block a user