Add ability to modify routes
authorJohn Abraham <john.abraham@gatech.edu>
Wed, 30 Nov 2011 20:37:59 -0500
changeset 65 5be2119b8a2d
parent 64 06594e46ff83
child 66 6df66b371ef7
Add ability to modify routes
Animator/NetView.cc
Routing/routingtab.cpp
Routing/routingtab.h
Routing/staticroute.cpp
Routing/staticroute.h
Simulation/private/simulation.cpp
--- a/Animator/NetView.cc	Wed Nov 23 09:34:06 2011 -0500
+++ b/Animator/NetView.cc	Wed Nov 30 20:37:59 2011 -0500
@@ -1453,7 +1453,7 @@
         ffwd->setChecked(false);
         timer->stop();
         mainInfo->GetSimulation()->StopAnimationInterface();
-
+        StopAnimationInterfaceButton->setVisible(false);
     }
     else
     {
--- a/Routing/routingtab.cpp	Wed Nov 23 09:34:06 2011 -0500
+++ b/Routing/routingtab.cpp	Wed Nov 30 20:37:59 2011 -0500
@@ -23,6 +23,7 @@
 #include "ns3/internet-module.h"
 #include "routingtab.h"
 #include "Animator/NetView.h"
+#include "staticroute.h"
 
 using namespace ns3;
 
@@ -38,7 +39,8 @@
     logtoFile(false),
     simtimepolling(true),
     simtimepolling_timer(ns3::Timer::CANCEL_ON_DESTROY),
-    filtering(false)
+    filtering(false),
+    modifying(false)
 {
     timer = new QTimer();
 }
@@ -139,10 +141,14 @@
     connect(filter_cb, SIGNAL (stateChanged (int)), this, SLOT (filter_cb_slot(int)));
 
 
+    modifyRoute_cb = new QCheckBox("Modify Route");
+    connect(modifyRoute_cb, SIGNAL (stateChanged (int)), this, SLOT (modifyRoute_cb_slot(int)));
+
     optionsHbox->addWidget(routingtable_cb);
     optionsHbox->addWidget(ipv4l3protocol_cb);
     optionsHbox->addWidget(interfaces_cb);
     optionsHbox->addWidget(filter_cb);
+    optionsHbox->addWidget(modifyRoute_cb);
 
     gridbox2 = new QGridLayout;
     AddRoutingCombos();
@@ -161,6 +167,7 @@
 
     centralWidget->setLayout(vbox);
     filter_cb_slot(0);
+    modifyRoute_cb_slot(0);
 
 
     return centralWidget;
@@ -192,6 +199,33 @@
 
 
 }
+void RoutingTab::modifyRoute_cb_slot(int state)
+{
+    if (modifyRoute_cb->isChecked())
+    {
+        modifying = true;
+        for (int i = 0 ; i< mRoutingCombo.size(); i++)
+        {
+           QPointer <RoutingCombo> p = mRoutingCombo[i];
+           p->ShowModifyRoute(true);
+
+        }
+
+    }
+    else
+    {
+        modifying = false;
+        for (int i = 0 ; i< mRoutingCombo.size(); i++)
+        {
+           QPointer <RoutingCombo> p = mRoutingCombo[i];
+           p->ShowModifyRoute(false);
+
+        }
+    }
+
+
+
+}
 
 
 void RoutingTab::wallclockrb_slot(bool state)
@@ -514,7 +548,52 @@
     InitInterfaceStats();
 
 
+    modifyRoute_vbox = new QVBoxLayout;
+    modifyRoute_gb = new QGroupBox;
+    modifyRoute_gb->setTitle("Modify Routes");
+    modifyRoute_gb->setLayout(modifyRoute_vbox);
+    randomEcmp_cb = new QCheckBox;
+    randomEcmp_cb->setText("RandomECMP");
+    connect(randomEcmp_cb, SIGNAL (stateChanged (int)), this, SLOT (randomEcmp_cb_slot(int)));
+
+
+    addHostRoute_button = new QPushButton("Add Static Host Route");
+    connect(addHostRoute_button, SIGNAL (clicked ()), this , SLOT (addHostRoute_button_slot()));
+    addHostRoute_button->setMaximumWidth(250);
+
+
+    addNetworkRoute_button = new QPushButton("Add Static Network Route");
+    connect(addNetworkRoute_button, SIGNAL (clicked ()), this , SLOT (addNetworkRoute_button_slot()));
+    addNetworkRoute_button->setMaximumWidth(250);
+
+
+    addDefaultRoute_button = new QPushButton("Set Default Route");
+    connect(addDefaultRoute_button, SIGNAL (clicked ()), this , SLOT (addDefaultRoute_button_slot()));
+    addDefaultRoute_button->setMaximumWidth(250);
+
+
+    removeRoute_le = new QLineEdit("");
+    removeRoute_le->setMaximumWidth(50);
+    removeRoute_label = new QLabel ("Route Index");
+    removeRoute_button = new QPushButton("Remove Route");
+    connect(removeRoute_button, SIGNAL (clicked ()), this, SLOT (removeRoute_button_slot()));
+
+
+    modifyRoute_vbox->addWidget(randomEcmp_cb);
+    modifyRoute_vbox->addWidget(addHostRoute_button);
+    modifyRoute_vbox->addWidget(addNetworkRoute_button);
+    modifyRoute_vbox->addWidget(addDefaultRoute_button);
+
+    QHBoxLayout * removeRouteHbox = new QHBoxLayout;
+    removeRouteHbox->addWidget(removeRoute_label);
+    removeRouteHbox->addWidget(removeRoute_le);
+    removeRouteHbox->addWidget(removeRoute_button);
+    modifyRoute_vbox->addLayout(removeRouteHbox);
+
+    InitModifyRoute();
+
     vbox->addWidget(label);
+    vbox->addWidget(modifyRoute_gb);
     vbox->addWidget(te);
     vbox->addWidget(ipv4l3protocol_gb);
     vbox->addWidget(interfaces_gb);
@@ -535,6 +614,51 @@
 
 }
 
+void RoutingCombo::randomEcmp_cb_slot(int state)
+{
+    if (randomEcmp_cb->isChecked())
+        GetGlobalRouting()->SetAttributeFailSafe("RandomEcmpRouting", BooleanValue(true));
+    else
+        GetGlobalRouting()->SetAttributeFailSafe("RandomEcmpRouting", BooleanValue(false));
+
+
+}
+
+
+void RoutingCombo::addHostRoute_button_slot()
+{
+    StaticRoute * sr = new StaticRoute (0,NodeList::GetNode(m_nodeId),"Add Host Route");
+   sr->getCentralWidget()->show();
+
+
+}
+
+void RoutingCombo::addNetworkRoute_button_slot()
+{
+    StaticRoute * sr = new StaticRoute (1,NodeList::GetNode(m_nodeId), "Add Network Route");
+   sr->getCentralWidget()->show();
+
+
+}
+
+void RoutingCombo::addDefaultRoute_button_slot()
+{
+    StaticRoute * sr = new StaticRoute (2,NodeList::GetNode(m_nodeId), "Set Default Route");
+   sr->getCentralWidget()->show();
+
+
+}
+void RoutingCombo::removeRoute_button_slot()
+{
+    if (removeRoute_le->text() == "")
+        return;
+    ns3::Ptr <ns3::Ipv4> ipv4 = NodeList::GetNode(m_nodeId)->GetObject <ns3::Ipv4> ();
+    ns3::Ipv4StaticRoutingHelper srhelper;
+    srhelper.GetStaticRouting(ipv4)->RemoveRoute(removeRoute_le->text().toUInt());
+
+}
+
+
 void RoutingCombo::SetLogtoFile(bool logit)
 {
     logtoFile = logit;
@@ -552,6 +676,15 @@
         te->setVisible(false);
 }
 
+void RoutingCombo::ShowModifyRoute(bool show)
+{
+    if(show)
+        modifyRoute_gb->setVisible(true);
+    else
+        modifyRoute_gb->setVisible(false);
+}
+
+
 QWidget* RoutingCombo::getCentralWidget()
 {
     return centralWidget;
@@ -581,6 +714,26 @@
 
 }
 
+Ptr <ns3::Ipv4GlobalRouting> RoutingCombo::GetGlobalRouting ()
+{
+    Ptr <ns3::Node> n = NodeList::GetNode(m_nodeId);
+    Ptr <ns3::GlobalRouter> gr = n->GetObject<ns3::GlobalRouter> ();
+    return gr->GetRoutingProtocol();
+
+
+}
+void RoutingCombo::InitModifyRoute()
+{
+    BooleanValue b;
+    GetGlobalRouting()->GetAttributeFailSafe ("RandomEcmpRouting", b);
+    if(b.Get())
+        randomEcmp_cb->setChecked(true);
+    else
+        randomEcmp_cb->setChecked(false);
+
+}
+
+
 void RoutingCombo::InitInterfaceStats()
 {
     this->interfaces_ndevices_stat = new QLabel;
--- a/Routing/routingtab.h	Wed Nov 23 09:34:06 2011 -0500
+++ b/Routing/routingtab.h	Wed Nov 30 20:37:59 2011 -0500
@@ -47,6 +47,7 @@
         void PrintInterface();
 
         void ShowRoutingTable (bool show);
+        void ShowModifyRoute (bool show);
         void ShowIpv4l3 (bool show);
         void ShowInterfaces (bool show);
         QWidget * getCentralWidget();
@@ -88,6 +89,24 @@
         std::vector <QPointer <QLabel> > vMacIPv4;
         QPointer <QGroupBox> interfaces_gb;
 
+
+
+        QPointer <QVBoxLayout> modifyRoute_vbox;
+        void InitModifyRoute ();
+
+        ns3::Ptr <ns3::Ipv4GlobalRouting> GetGlobalRouting ();
+
+        QPointer <QGroupBox> modifyRoute_gb;
+        QPointer <QCheckBox> randomEcmp_cb;
+        QPointer <QPushButton> addHostRoute_button;
+        QPointer <QPushButton> addNetworkRoute_button;
+        QPointer <QPushButton> addDefaultRoute_button;
+
+        QPointer <QLabel> removeRoute_label;
+        QPointer <QLineEdit> removeRoute_le;
+        QPointer <QPushButton> removeRoute_button;
+
+
         RoutingTab * m_rt;
         uint32_t m_nodeId;
         bool active;
@@ -99,6 +118,11 @@
 
     public slots:
         void onbuttonToggled ();
+        void randomEcmp_cb_slot(int);
+        void addHostRoute_button_slot ();
+        void addNetworkRoute_button_slot ();
+        void addDefaultRoute_button_slot ();
+        void removeRoute_button_slot ();
 
 };
 
@@ -173,14 +197,17 @@
 
     // Filter ;
     bool filtering;
+    bool modifying;
     QPointer <PacketFilter> pktfilter;
     QPointer <QCheckBox> filter_cb;
+    QPointer <QCheckBox> modifyRoute_cb;
 
 
 
 private slots:
     void force_update_slot();
     void filter_cb_slot(int state);
+    void modifyRoute_cb_slot(int state);
     void wallclockrb_slot(bool state);
     void simtimerb_slot(bool state);
     void apply();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Routing/staticroute.cpp	Wed Nov 30 20:37:59 2011 -0500
@@ -0,0 +1,200 @@
+#include "staticroute.h"
+#include "QtNetwork/QHostAddress"
+#include "ns3/internet-module.h"
+
+StaticRoute::StaticRoute(int mode, ns3::Ptr <ns3::Node> n,QString title, QWidget *parent) :
+    QWidget(parent), m_mode(mode), valid (false), m_n (n)
+{
+
+   // mode 0 == Host route
+   // mode 1 == Network route
+   // mode 2 == Default route
+    hbox = new QHBoxLayout;
+    dst_le = new QLineEdit ("");
+    mask_le = new QLineEdit ("");
+    nextHop_le = new QLineEdit("");
+    interface_le = new QLineEdit("");
+    metric_le = new QLineEdit("");
+    applyButton = new QPushButton("Apply");
+    connect(applyButton,SIGNAL (clicked()),this,SLOT(applyButton_slot()));
+
+    dst_label = new QLabel ("Destination");
+    mask_label = new QLabel ("Mask");
+    nextHop_label = new QLabel ("Next Hop");
+    interface_label = new QLabel ("Interface Index");
+    metric_label = new QLabel ("Metric");
+
+
+    if (mode ==0 || mode ==1)
+    {
+      hbox->addWidget(dst_label);
+      hbox->addWidget(dst_le);
+    }
+    if (mode ==1)
+    {
+        hbox->addWidget(mask_label);
+        hbox->addWidget(mask_le);
+    }
+    hbox->addWidget(nextHop_label);
+    hbox->addWidget(nextHop_le);
+    hbox->addWidget(interface_label);
+    hbox->addWidget(interface_le);
+    hbox->addWidget(metric_label);
+    hbox->addWidget(metric_le);
+
+
+
+    hbox->addWidget(applyButton);
+    centralWidget = new QWidget;
+    centralWidget->setLayout(hbox);
+    centralWidget->setWindowTitle(title);
+
+}
+
+
+QWidget* StaticRoute::getCentralWidget()
+{
+    return centralWidget;
+
+}
+
+bool IsIpValid(QString ip)
+{
+    if (ip == "ANY")
+        return true;
+    QHostAddress addr ;
+    if (addr.setAddress(ip))
+        return true;
+    else
+    {
+        QMessageBox msg;
+        msg.setText("Invalid IP:"+ip);
+        msg.exec();
+        return false;
+    }
+}
+
+
+void StaticRoute::applyButton_slot()
+{
+
+    if (!IsIpValid(dst_le->text()))
+        goto end;
+    dst_string = dst_le->text();
+
+
+    if (m_mode == 1)
+    {
+      if (!IsIpValid(mask_le->text()))
+        goto end;
+      mask_string = mask_le->text();
+    }
+
+    if (nextHop_le->text() != "")
+    {
+      if (!IsIpValid(nextHop_le->text()))
+          goto end;
+      nextHop_string = nextHop_le->text();
+    }
+    else
+    {
+        nextHop_string = "";
+
+    }
+    if (m_mode ==3)
+    {
+        if (nextHop_string == "")
+        {
+            QMessageBox msg;
+            msg.setText("Invalid nextHop:"+nextHop_string);
+            msg.exec();
+            goto end;
+        }
+
+    }
+    interface_string = interface_le->text();
+    if (interface_string == "")
+    {
+        QMessageBox msg;
+        msg.setText("Invalid Interface Index:"+interface_string);
+        msg.exec();
+        goto end;
+    }
+    metric_string = metric_le->text();
+
+    if (metric_string == "")
+    {
+        metric_string = "0";
+    }
+
+    valid = true;
+    if(valid)
+    {
+        ns3::Ptr <ns3::Ipv4> ipv4 = m_n->GetObject <ns3::Ipv4> ();
+        ns3::Ipv4StaticRoutingHelper srhelper;
+        if (m_mode == 0)
+        {
+            if (nextHop_string != "")
+
+                srhelper.GetStaticRouting(ipv4)->AddHostRouteTo
+                (
+                    ns3::Ipv4Address(dst_string.toAscii().data()),
+                    ns3::Ipv4Address(nextHop_string.toAscii().data()),
+                    interface_string.toUInt(),
+                    metric_string.toUInt()
+
+                 );
+            else
+                srhelper.GetStaticRouting(ipv4)->AddHostRouteTo
+                (
+                    ns3::Ipv4Address(dst_string.toAscii().data()),
+                    interface_string.toUInt(),
+                    metric_string.toUInt()
+
+                 );
+        }
+        else if (m_mode == 1)
+        {
+            if (nextHop_string != "")
+
+                srhelper.GetStaticRouting(ipv4)->AddNetworkRouteTo
+                (
+                    ns3::Ipv4Address(dst_string.toAscii().data()),
+                    ns3::Ipv4Mask(mask_string.toAscii().data()),
+                    ns3::Ipv4Address(nextHop_string.toAscii().data()),
+                    interface_string.toUInt(),
+                    metric_string.toUInt()
+
+                 );
+            else
+                srhelper.GetStaticRouting(ipv4)->AddNetworkRouteTo
+                (
+                    ns3::Ipv4Address(dst_string.toAscii().data()),
+                    ns3::Ipv4Mask(mask_string.toAscii().data()),
+                    interface_string.toUInt(),
+                    metric_string.toUInt()
+
+                 );
+        }
+        else if (m_mode ==2)
+        {
+
+            srhelper.GetStaticRouting(ipv4)->SetDefaultRoute
+            (
+                ns3::Ipv4Address(nextHop_string.toAscii().data()),
+                interface_string.toUInt(),
+                metric_string.toUInt()
+
+             );
+
+
+
+        }
+
+
+    }
+    end:
+    centralWidget->hide();
+
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Routing/staticroute.h	Wed Nov 30 20:37:59 2011 -0500
@@ -0,0 +1,58 @@
+#ifndef STATICROUTE_H
+#define STATICROUTE_H
+
+#include <QWidget>
+#include <QtGui>
+#include "ns3/network-module.h"
+
+
+class StaticRoute : public QWidget
+{
+    Q_OBJECT
+public:
+    explicit StaticRoute(int mode,ns3::Ptr< ns3::Node>,QString,QWidget *parent = 0);
+    QWidget * getCentralWidget ();
+
+signals:
+
+public slots:
+    void applyButton_slot();
+private:
+    QWidget * centralWidget;
+
+private:
+    QPointer <QHBoxLayout> hbox;
+    QPointer <QLineEdit> dst_le;
+    QPointer <QLineEdit> nextHop_le;
+    QPointer <QLineEdit> mask_le;
+    QPointer <QLineEdit> interface_le;
+    QPointer <QLineEdit> metric_le;
+
+
+
+
+    QPointer <QPushButton> applyButton;
+    QPointer <QLabel> dst_label;
+    QPointer <QLabel> nextHop_label;
+    QPointer <QLabel> mask_label;
+
+    QPointer <QLabel> interface_label;
+    QPointer <QLabel> metric_label;
+
+
+    int m_mode;
+    ns3::Ptr <ns3::Node> m_n;
+
+public:
+    QString dst_string;
+    QString mask_string;
+    QString nextHop_string;
+    QString interface_string;
+    QString metric_string;
+    bool valid;
+
+
+
+};
+
+#endif // STATICROUTE_H
--- a/Simulation/private/simulation.cpp	Wed Nov 23 09:34:06 2011 -0500
+++ b/Simulation/private/simulation.cpp	Wed Nov 30 20:37:59 2011 -0500
@@ -76,6 +76,16 @@
     ns3::Ptr <ns3::AnimSimulatorImpl> pImpl = CreateObject <AnimSimulatorImpl> (mi);
     Simulator::SetImplementation(pImpl);
     ConfigureSimulation(mainInfo->getArgc(),mainInfo->getArgv());
+    if (AnimationInterface::IsInitialized())
+    {
+        QString t = "AnimationInterface object already initialized\n \
+                     Please remove any AnimationInterface statements\n \
+                     from Simulation::ConfigureSimulation";
+        std::cout << t.toAscii().data() <<std::endl;
+
+        exit(-1);
+
+    }
     configureSimulationComplete = true;
     InitializeNodeStatistics();
     pAnim = new ns3::AnimationInterface();