author | mickeyl <mickeyl> | 2003-04-09 21:37:05 (UTC) |
---|---|---|
committer | mickeyl <mickeyl> | 2003-04-09 21:37:05 (UTC) |
commit | b055856776807f0a459a86b1e1f62902d2d3a9c3 (patch) (unidiff) | |
tree | 46bee28833c363607d20f4db93eef0166a45852d /libopie2/opienet/opcap.cpp | |
parent | c8401f7ebb9e9314ed48517da38b949c24800c50 (diff) | |
download | opie-b055856776807f0a459a86b1e1f62902d2d3a9c3.zip opie-b055856776807f0a459a86b1e1f62902d2d3a9c3.tar.gz opie-b055856776807f0a459a86b1e1f62902d2d3a9c3.tar.bz2 |
implements a statistic interface for OPacketCapturer
-rw-r--r-- | libopie2/opienet/opcap.cpp | 40 |
1 files changed, 36 insertions, 4 deletions
diff --git a/libopie2/opienet/opcap.cpp b/libopie2/opienet/opcap.cpp index 5c464cf..6a3dc26 100644 --- a/libopie2/opienet/opcap.cpp +++ b/libopie2/opienet/opcap.cpp | |||
@@ -39,6 +39,7 @@ | |||
39 | 39 | ||
40 | #include <qapplication.h> // don't use oapplication here (will decrease reusability in other projects) | 40 | #include <qapplication.h> // don't use oapplication here (will decrease reusability in other projects) |
41 | #include <qsocketnotifier.h> | 41 | #include <qsocketnotifier.h> |
42 | #include <qobjectlist.h> | ||
42 | 43 | ||
43 | /*====================================================================================== | 44 | /*====================================================================================== |
44 | * OPacket | 45 | * OPacket |
@@ -87,6 +88,19 @@ int OPacket::caplen() const | |||
87 | } | 88 | } |
88 | 89 | ||
89 | 90 | ||
91 | void OPacket::updateStats( QMap<QString,int>& stats, QObjectList* l ) | ||
92 | { | ||
93 | if (!l) return; | ||
94 | QObject* o = l->first(); | ||
95 | while ( o ) | ||
96 | { | ||
97 | stats[o->name()]++; | ||
98 | updateStats( stats, const_cast<QObjectList*>( o->children() ) ); | ||
99 | o = l->next(); | ||
100 | } | ||
101 | } | ||
102 | |||
103 | |||
90 | QString OPacket::dump( int bpl ) const | 104 | QString OPacket::dump( int bpl ) const |
91 | { | 105 | { |
92 | static int index = 0; | 106 | static int index = 0; |
@@ -127,12 +141,12 @@ QString OPacket::dump( int bpl ) const | |||
127 | } | 141 | } |
128 | 142 | ||
129 | 143 | ||
130 | |||
131 | int OPacket::len() const | 144 | int OPacket::len() const |
132 | { | 145 | { |
133 | return _hdr.len; | 146 | return _hdr.len; |
134 | } | 147 | } |
135 | 148 | ||
149 | |||
136 | /*====================================================================================== | 150 | /*====================================================================================== |
137 | * OEthernetPacket | 151 | * OEthernetPacket |
138 | *======================================================================================*/ | 152 | *======================================================================================*/ |
@@ -772,6 +786,13 @@ void OPacketCapturer::close() | |||
772 | pcap_close( _pch ); | 786 | pcap_close( _pch ); |
773 | _open = false; | 787 | _open = false; |
774 | } | 788 | } |
789 | |||
790 | qDebug( "OPacketCapturer::close() --- dumping capturing statistics..." ); | ||
791 | qDebug( "--------------------------------------------------" ); | ||
792 | for( QMap<QString,int>::Iterator it = _stats.begin(); it != _stats.end(); ++it ) | ||
793 | qDebug( "%s : %d", (const char*) it.key(), it.data() ); | ||
794 | qDebug( "--------------------------------------------------" ); | ||
795 | |||
775 | } | 796 | } |
776 | 797 | ||
777 | 798 | ||
@@ -793,7 +814,6 @@ int OPacketCapturer::fileno() const | |||
793 | } | 814 | } |
794 | } | 815 | } |
795 | 816 | ||
796 | |||
797 | OPacket* OPacketCapturer::next() | 817 | OPacket* OPacketCapturer::next() |
798 | { | 818 | { |
799 | packetheaderstruct header; | 819 | packetheaderstruct header; |
@@ -803,11 +823,16 @@ OPacket* OPacketCapturer::next() | |||
803 | 823 | ||
804 | if ( header.len ) | 824 | if ( header.len ) |
805 | { | 825 | { |
806 | return new OPacket( dataLink(), header, pdata, 0 ); | 826 | OPacket* p = new OPacket( dataLink(), header, pdata, 0 ); |
807 | // packets shouldn't be inserted in the QObject child-parent hierarchy, | 827 | // packets shouldn't be inserted in the QObject child-parent hierarchy, |
808 | // because due to memory constraints they will be deleted as soon | 828 | // because due to memory constraints they will be deleted as soon |
809 | // as possible - that is right after they have been processed | 829 | // as possible - that is right after they have been processed |
810 | // by emit() [ see below ] | 830 | // by emit() [ see below ] |
831 | |||
832 | //TODO: make gathering statistics optional, because it takes time | ||
833 | p->updateStats( _stats, const_cast<QObjectList*>( p->children() ) ); | ||
834 | |||
835 | return p; | ||
811 | } | 836 | } |
812 | else | 837 | else |
813 | { | 838 | { |
@@ -839,6 +864,7 @@ bool OPacketCapturer::open( const QString& name ) | |||
839 | qDebug( "OPacketCapturer::open(): libpcap opened successfully." ); | 864 | qDebug( "OPacketCapturer::open(): libpcap opened successfully." ); |
840 | _pch = handle; | 865 | _pch = handle; |
841 | _open = true; | 866 | _open = true; |
867 | _stats.clear(); | ||
842 | 868 | ||
843 | // in case we have an application object, create a socket notifier | 869 | // in case we have an application object, create a socket notifier |
844 | if ( qApp ) | 870 | if ( qApp ) |
@@ -866,10 +892,16 @@ bool OPacketCapturer::isOpen() const | |||
866 | 892 | ||
867 | void OPacketCapturer::readyToReceive() | 893 | void OPacketCapturer::readyToReceive() |
868 | { | 894 | { |
869 | qDebug( "OPacketCapturer::readyToReceive(): about to emit 'receivePacket(...)'" ); | 895 | qDebug( "OPacketCapturer::readyToReceive(): about to emit 'receivePacket(p)'" ); |
870 | OPacket* p = next(); | 896 | OPacket* p = next(); |
871 | emit receivedPacket( p ); | 897 | emit receivedPacket( p ); |
872 | // emit is synchronous - packet has been dealt with, now it's safe to delete | 898 | // emit is synchronous - packet has been dealt with, now it's safe to delete |
873 | delete p; | 899 | delete p; |
874 | } | 900 | } |
875 | 901 | ||
902 | |||
903 | const QMap<QString,int>& OPacketCapturer::statistics() const | ||
904 | { | ||
905 | return _stats; | ||
906 | } | ||
907 | |||