Monitor
mainwindow.cpp
00001 
00002 #include <QStandardItemModel>
00003 #include <QStringList>
00004 #include <QStringListModel>
00005 #include <QAbstractItemModel>
00006 #include <string>
00007 #include <cstdlib>
00008 #include <stdio.h>
00009 #include <string.h>
00010 #include <QTreeView>
00011 #include <QTimer>
00012 #include <arpa/inet.h>
00013 #include <netinet/in.h>
00014 #include "mainwindow.h"
00015 #include "ui_mainwindow.h"
00016 #include <vector>
00017 #include "settingsdialog.h"
00018 
00019 
00023 MainWindow::MainWindow(QWidget *parent) :
00024     QMainWindow(parent),
00025     ui(new Ui::MainWindow)
00026 {
00027     ui->setupUi(this);
00028 
00029     m_scene = new QGraphicsScene(this);
00030 
00031     comm = CommModel::init();
00032     sleep(2);
00033     widget = new GraphWidget(this, comm);
00034     widget->setSelectedIP(0);
00035     widget->setFrameStyle(-1);
00036     widget->setParent (ui->graphicsView);
00037     widget->setMainWindow(this);
00038 
00039     doPhysics = true;
00040 
00041     widget->setDoPhysics(true);
00042 
00043     //ocu = new node();
00044     //ocu->ip_addr = 1;
00045 
00046     selectedNode = NULL;
00047     //ocu->mac_addr = 1;
00048 
00049     ui->graphicsView->setScene(m_scene);
00050 
00051 
00052     refreshRate = 100;
00053     inactiveThreshold = 3000;
00054     doPhysics = false;
00055     showThroughput = true;
00056     showLatency = true;
00057     showSignalStrength = true;
00058     showInactivityTime = true;
00059     isPlaying = false;
00060     firstTime = true;
00061 
00062     timer = new QTimer(this);
00063     connect(timer, SIGNAL(timeout()), this, SLOT(updateList()));
00064     this->on_playButton_released();
00065 }
00066 
00070 MainWindow::~MainWindow()
00071 {
00072     delete this->comm;
00073     delete ui;
00074 }
00075 
00079 void MainWindow::togglePlaying()
00080 {
00081     if(isPlaying)
00082     {
00083        perror("Changing to paused!\n");
00084        isPlaying = false;
00085        ui->playButton->setIcon(QIcon(":/new/prefix1/images/Play.png"));
00086     }
00087     else
00088     {
00089         perror("Changing to playing!\n");
00090         isPlaying = true;
00091         ui->playButton->setIcon(QIcon(":/new/prefix1/images/Pause.png"));
00092     }
00093 }
00094 
00102 void MainWindow::setNodeInAllModel(int row, node *myNode, QStandardItemModel* model)
00103 {
00104     QStandardItem *item;
00105     //IP ADDRESS STUFF
00106     struct in_addr theAddr;
00107     theAddr.s_addr = ntohl(myNode->ip_addr);
00108     char *stringrepofIpAddress = inet_ntoa(theAddr);
00109 
00110     item = new QStandardItem( QString("%0").arg(stringrepofIpAddress));
00111     if (selectedNode == myNode)
00112     {
00113         QFont font ("Arial");
00114         font.setPixelSize(15);
00115         font.setWeight(QFont::Bold);
00116         item->setFont(font);
00117     }
00118     model->setItem(row, 0, item);
00119 }
00120 
00130 void MainWindow::setNodeInModel(int row, node* myNode, node* sourceNode, QStandardItemModel* model, CommModel* comm)
00131 {
00132    if (myNode == NULL)
00133       return;
00134 
00135    QFont font ("Tokyo");
00136    font.setPointSize(20);
00137    font.setWeight(QFont::Bold);
00138    font.setItalic(true);
00139 
00140    bool sn = false;
00141    if (selectedNode == myNode)
00142    {
00143        sn = true;
00144    }
00145    QStandardItem *item;
00146    //item = new QStandardItem( QString("Node %0").arg(row));
00147    char buffer[17];
00148    sprintf(buffer, "%02x:%02x:%02x:%02x:%02x:%02x", myNode->mac_addr.addr[0], myNode->mac_addr.addr[1], myNode->mac_addr.addr[2],
00149            myNode->mac_addr.addr[3], myNode->mac_addr.addr[4], myNode->mac_addr.addr[5]);
00150    item = new QStandardItem(QString(buffer));
00151    /*item = new QStandardItem( QString("%1:%2:%3:%4:%5:%6").arg(myNode->mac_addr.addr[0], 0, 16)
00152                              .arg(myNode->mac_addr.addr[1], 0, 16).arg(myNode->mac_addr.addr[2], 0, 16)
00153                              .arg(myNode->mac_addr.addr[3], 0, 16).arg(myNode->mac_addr.addr[4], 0, 16)
00154                              .arg(myNode->mac_addr.addr[5], 0, 16));*/
00155    model->setItem(row, 0, item);
00156 
00157    //IP ADDRESS STUFF
00158    struct in_addr theAddr;
00159    theAddr.s_addr = ntohl(myNode->ip_addr);
00160    char *stringrepofIpAddress = inet_ntoa(theAddr);
00161    int thisCol = 1;
00162 
00163    if(true)
00164    {
00165       item = new QStandardItem( QString("%0").arg(stringrepofIpAddress));
00166       model->setItem(row, thisCol++, item);
00167    }
00168    if(showSignalStrength)
00169    {
00170       item = new QStandardItem( QString("%3 dB").arg(comm->get_signal_strength(sourceNode, myNode)));
00171       model->setItem(row, thisCol++, item);
00172    }
00173    /*if(true)
00174    {
00175        item = new QStandardItem( QString("%3").arg(comm->get_weight(sourceNode, myNode)));
00176        model->setItem(row, thisCol++, item);
00177    }*/
00178    if(showThroughput)
00179    {
00180       item = new QStandardItem( QString("%3 kb").arg(comm->get_throughput(sourceNode, myNode)));
00181       model->setItem(row, thisCol++, item);
00182    }
00183    if(showInactivityTime)
00184    {
00185    item = new QStandardItem( QString("%3 ms").arg(comm->get_inactive_time(sourceNode, myNode)));
00186    model->setItem(row, thisCol++, item);
00187    }
00188 }
00192 void MainWindow::updateList()
00193 {
00194     QStandardItemModel* model = new QStandardItemModel();
00195     QStandardItemModel* model_all = new QStandardItemModel();
00196 
00197     std::vector<node *> allNodes = comm->get_all_nodes();
00198 
00199     //By default the selected node is the first one returned
00200     if(allNodes.size() > 0)
00201     {
00202         selectedNode = allNodes[0];
00203 
00204     }
00205     else {
00206         return;
00207     }
00208     //Iterate through and remove the selected node form the list of other nodes and set
00209     //it as selected
00210     for(std::vector<node *>::iterator it = allNodes.begin(); it != allNodes.end(); ++it) {
00211         if( (*it)->ip_addr == widget->getSelectedIP() )
00212         {
00213             selectedNode = *it;
00214             printf("Set selectedNode to what u clicked on");
00215         }
00216     }
00217     /*
00218     for(int r = 0; r < allNodes.size(); r++)
00219     {
00220         node* curNode = allNodes[r];
00221         if( curNode.getIpAddr() == widget->getSelectedIP() )
00222         {
00223             selectedNode = allNodes[r];
00224             allNodes.erase(allNodes.begin()+r);
00225             //POSSIBLE ERROR ABOVE THIS MIGHT NEED TO BNE begin() + (r-1)
00226         }
00227     }*/
00228 
00229     printf("Before get all nodes\n");
00230     widget->setSelectedIP(selectedNode->ip_addr);
00231 
00232 
00233     for(int r = 0; r < allNodes.size(); r++)
00234     {
00235          setNodeInAllModel(r, allNodes[r], model_all);
00236     }
00237 
00238 
00239     if(selectedNode)
00240     {
00241         int row = 0;
00242 
00243         struct in_addr theAddr;
00244         theAddr.s_addr = ntohl(selectedNode->ip_addr);
00245         char *stringrepofIpAddress = inet_ntoa(theAddr);
00246         char nbz[50];
00247         strcpy(nbz, "Selected Node: ");
00248         strcat(nbz, stringrepofIpAddress);
00249         ui->statusBar->showMessage(nbz);
00250 
00251         foreach(node* n, comm->get_neighbors(selectedNode))
00252         {
00253             setNodeInModel(row++, n, selectedNode, model, comm);
00254         }
00255     }
00256 
00257     printf("Before model\n");
00258 
00259     model_all->setHeaderData(0, Qt::Horizontal, "IP ADDR");
00260     ui->treeView_2->setModel(model_all);
00261 
00262     int headerRow = 0;
00263 
00264     model->setHeaderData(headerRow++, Qt::Horizontal, "MAC ADDR");
00265     model->setHeaderData(headerRow++, Qt::Horizontal, "IP ADDR");
00266     if(showSignalStrength)
00267        model->setHeaderData(headerRow++, Qt::Horizontal, "Signal Strength");
00268     //model->setHeaderData(headerRow++, Qt::Horizontal, "Weight");
00269     if(showThroughput)
00270        model->setHeaderData(headerRow++, Qt::Horizontal, "Throughput");
00271     if(showInactivityTime)
00272        model->setHeaderData(headerRow++, Qt::Horizontal, "Inactive Time");
00273     ui->treeView->setModel((model));
00274 
00275     widget->updateGraph(allNodes);
00276     widget->show();
00277 }
00278 
00283 void MainWindow::on_playButton_released()
00284 {
00285 
00286     if(firstTime)
00287     {
00288        firstTime = false;
00289        perror("First time being pressed...\n");
00290        perror("Trying to call init...\n");
00291        //    comm = CommModel::init();
00292        //sleep(2);
00293        perror("About to upadte List\n");
00294     }
00295 
00296 
00297 
00298     //Must manage a boolean isPlaying
00299     if(!isPlaying)
00300     {
00301        updateList();
00302        timer->start(refreshRate);
00303     }
00304     else
00305     {
00306         //Do pause stuff
00307         perror("Stopping timer.\n");
00308         timer->stop();
00309     }
00310     togglePlaying();
00311 
00312 }
00313 
00317 void MainWindow::on_settingsButton_released() {
00318     settingsDialog = new SettingsDialog();
00319     settingsDialog->setMainWindow(this);
00320     settingsDialog->updateSettings(refreshRate, inactiveThreshold, doPhysics, showLatency, showThroughput, showSignalStrength, showInactivityTime);
00321     settingsDialog->show();
00322 }
00323 
00324 void MainWindow::updateSettings(int _refreshRate, int _inactiveThreshold, bool _doPhysics, bool _showSignalStrength, bool _showThroughput,
00325                                 bool _showInactivityTime, bool _showLatency)
00326 {
00327     refreshRate = _refreshRate;
00328     inactiveThreshold = _inactiveThreshold;
00329     doPhysics = _doPhysics;
00330     showSignalStrength = _showSignalStrength;
00331     showThroughput = _showThroughput;
00332     showInactivityTime = _showInactivityTime;
00333     showLatency = _showLatency;
00334 
00335     widget->setDoPhysics(_doPhysics);
00336 
00337     if (!_doPhysics) {
00338         // circle plot
00339         fprintf(stderr, "===    plot in circle ===\n");
00340         widget->plotNodes();
00341     }
00342 }
00343 
00348 int MainWindow::getInactiveThreshold() {
00349     return inactiveThreshold;
00350 }
00351 
00356 CommModel * MainWindow::getComm() {
00357     return comm;
00358 }
 All Classes Functions