File: CTrafStatsList.cc

package info (click to toggle)
trafstats 0.4.20-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 296 kB
  • ctags: 110
  • sloc: cpp: 1,036; sh: 475; perl: 173; makefile: 98
file content (87 lines) | stat: -rw-r--r-- 2,395 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/** 
  * CTrafStatsList.cc: Function definitions for the list stuff
  * Thankfully, STL templates did most of the complicated work 
  * for me so I just had to link the right bits into the right
  * hooks.

  * Extra functions: 
  * Constructor from single Entry;
  * Copy constructor
  * overloaded insertion (using insertion sort makes sense when you 
    know you always start with an empty list)
  * overloaded clear operator
  * storeToDatabase: takes a PgConnection pointer to a PostgreSQL database
    and stores all entries into said database.
  
*/
#include "trafstats.h"
#include "CTrafStatsList.h"
#include <iostream.h>

extern int verbosity;

CTrafStatsList::CTrafStatsList(const CTrafStatsEntry& entry) {
        push_front(entry);
}


CTrafStatsList::CTrafStatsList(CTrafStatsList& copy) {
        iterator i;
        for (i=copy.begin(); i!=copy.end(); ++i) {
                push_back(*i);
        }
	iCount = copy.count();
}

CTrafStatsList::CTrafStatsList& CTrafStatsList::insert(const CTrafStatsEntry& entry) {
	iterator i;
	iCount++;
	if(verbosity >= LOG_DEBUG+6) {
		syslog(LOG_DEBUG,"[sniffer] Seeking insert point.");
	}
	// Find the proper insert point.
	for(i=begin(); *i < entry && i!=end(); ++i);
	if(verbosity >= LOG_DEBUG+6) {
		syslog(LOG_DEBUG,"[sniffer] Insert point found.");
	}
	// Now, the current i is either a match for, or 'larger than'
	// the entry. 

	if(i->matches(entry)) { 
		// We already had this entry, so just add
		*i += entry;
		DLOG("Matched " << entry.asString()
			<< " with " << i->asString() << endl);
		return *this;
	} // else
	DLOG("New entry: " << entry.asString());	
	// New entry, call insert from parent class.
	list<CTrafStatsEntry>::insert(i,entry);
	
	return *this;
}

void CTrafStatsList::clear() {
	// The parent class already implements clear(), just need to zero 
	// the count
	list<CTrafStatsEntry>::clear();
	iCount=0;
}

bool CTrafStatsList::storeToDatabase(PgDatabase* conn) {
	// Store all entries into database.
        iterator i;
        for (i=begin(); i!=end(); ++i) {
		if(verbosity >= LOG_DEBUG) {
			syslog(LOG_DEBUG,"%s",i->asQuery().c_str());
		}
		int output=conn->ExecCommandOk(i->asQuery().c_str());
		if(!output && verbosity >= LOG_ERR) {
			syslog(LOG_ERR,"Insert query failed: %s",
				conn->ErrorMessage());
			syslog(LOG_ERR,"Query was: <%s>",
				i->asQuery().c_str());
		}
        }
	return true;
};