Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

networksensor.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2003 by Hans Karlsson                                   *
00003  *   karlsson.h@home.se                                                      *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  ***************************************************************************/
00010 #include "networksensor.h"
00011 
00012 NetworkSensor::NetworkSensor( QString dev, int interval ):Sensor( interval )
00013 {
00014    device = dev.lower();
00015    if( device == "" )
00016         device = "eth0";
00017 
00018      getInOutBytes(receivedBytes,transmittedBytes);
00019     netTimer.start();
00020 
00021 }
00022 NetworkSensor::~NetworkSensor()
00023 {
00024 }
00025 void NetworkSensor::getInOutBytes ( unsigned long &in,unsigned long &out) const
00026 {
00027     QFile file("/proc/net/dev");
00028     QString line;
00029     if ( file.open(IO_ReadOnly | IO_Translate) )
00030     {
00031         QTextStream t( &file );        // use a text stream
00032         line = t.readLine();
00033         while(line !=0 && !line.contains(device))
00034         {
00035             line = t.readLine();
00036         }
00037         if ( line.contains( device ) )
00038         {
00039             QRegExp rx( "\\W+"+device+":\\D*(\\d+)(?:\\D+\\d+){7}\\D+(\\d+)", false);
00040             rx.search(line);
00041             in = rx.cap(1).toULong();
00042             out = rx.cap(2).toULong();
00043        }
00044         else
00045         {
00046                 kdDebug("NetworkSensor") << "Network sensor: can not find " << device << endl;
00047             in = 0;
00048             out = 0;
00049         }
00050     file.close();
00051     }
00052 }
00053 
00054 void NetworkSensor::update()
00055 {
00056     SensorParams *sp;
00057     Meter *meter;
00058     QObjectListIt it( *objList );
00059     QString format;
00060     int decimals;
00061 
00062     unsigned long inB, outB;
00063     const double delay = (double) netTimer.elapsed(); // msec elapsed since last update
00064     getInOutBytes( inB, outB );
00065     netTimer.restart();
00066 
00067     while( it != 0 )
00068     {
00069         sp = (SensorParams*)(*it);
00070         meter = sp->getMeter();
00071         format = sp->getParam( "FORMAT" );
00072         decimals = ( sp->getParam( "DECIMALS" ) ).toInt();
00073         if (format.length() == 0 )
00074         {
00075             format = "%in";
00076         }
00077 
00078         format.replace( QRegExp("%in", false), QString::number( (inB - receivedBytes)/delay, 'f', decimals ) );
00079         format.replace( QRegExp("%out", false), QString::number( (outB - transmittedBytes)/delay, 'f', decimals ) );
00080 
00081         meter->setValue( format );
00082         ++it;
00083     }
00084     receivedBytes = inB;
00085     transmittedBytes = outB;
00086 }

Generated on Mon May 16 13:59:20 2005 for karamba by  doxygen 1.3.9.1