forest-net
an overlay networks for large-scale virtual worlds
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
PacketLog.h
Go to the documentation of this file.
1 
9 #ifndef PACKETLOG_H
10 #define PACKETLOG_H
11 
12 #include "Forest.h"
13 #include "Util.h"
14 #include "PacketStore.h"
15 #include "Packet.h"
16 #include "CtlPkt.h"
17 #include "PacketFilter.h"
18 #include "ListPair.h"
19 
20 namespace forest {
21 
22 typedef int fltx; // filter index
23 
24 class PacketLog {
25 public:
27  ~PacketLog();
28 
29  fltx firstFilter() const;
30  fltx nextFilter(fltx) const;
31 
32  bool validFilter(fltx) const;
33 
34  bool match(fltx, pktx, int, bool) const;
35  void enable(fltx);
36  void disable(fltx);
37  fltx addFilter();
38  void dropFilter(fltx);
39  PacketFilter& getFilter(fltx);
40  void turnOnLogging(bool);
41  void enableLocalLog(bool);
42 
43  int size() const;
44  void log(int,int,bool,uint64_t);
45  int extract(int, string&);
46  void write(ostream&);
47  void purge();
48 
49 private:
50  static const int MAX_EVENTS=10000;
51  static const int MAX_FILTERS=100;
52 
53  bool logOn;
54  bool logLocal;
55  uint64_t dumpTime;
56  int numOut;
57  int numDataOut;
58 
59  static const int OUT_LIMIT = 50000;
60  static const int DATA_OUT_LIMIT = 10000;
61 
62  struct EventStruct {
63  pktx px;
64  int sendFlag;
65  int link;
66  uint64_t time;
67  };
68  EventStruct *evec;
69 
70  int eventCount;
71  int firstEvent;
72  int lastEvent;
73 
75  ListPair *filters;
76 
77  PacketStore *ps;
78 };
79 
80 inline int PacketLog::size() const { return eventCount; }
81 
82 inline int PacketLog::firstFilter() const { return filters->firstIn(); }
83 inline int PacketLog::nextFilter(int f) const { return filters->nextIn(f); }
84 
85 inline bool PacketLog::validFilter(int f) const { return filters->isIn(f); }
86 
87 inline void PacketLog::enable(fltx f) { fvec[f].on = true; }
88 inline void PacketLog::disable(fltx f) { fvec[f].on = false; }
89 
90 inline bool PacketLog::match(fltx f, pktx px, int lnk, bool sendFlag) const {
91  Packet& p = ps->getPacket(px);
92  if (fvec[f].on &&
93  (fvec[f].lnk == 0 || fvec[f].lnk == lnk) &&
94  (fvec[f].comt == 0 || fvec[f].comt == p.comtree) &&
95  (fvec[f].srcAdr == 0 || fvec[f].srcAdr == p.srcAdr) &&
96  (fvec[f].dstAdr == 0 || fvec[f].dstAdr == p.dstAdr) &&
97  (fvec[f].type == Forest::UNDEF_PKT || fvec[f].type == p.type) &&
98  ((fvec[f].in && !sendFlag) || (fvec[f].out && sendFlag))) {
99  if (p.type != Forest::CLIENT_SIG && p.type != Forest::NET_SIG)
100  return true;
101  CtlPkt::CpType cpt = (CtlPkt::CpType) ntohl(p.payload()[0]);
102  return (fvec[f].cpType == CtlPkt::UNDEF_CPTYPE ||
103  fvec[f].cpType == cpt);
104  }
105  return false;
106 }
107 
112 inline PacketFilter& PacketLog::getFilter(fltx f) { return fvec[f]; }
113 
117 inline void PacketLog::turnOnLogging(bool on) {
118  logOn = on; if (logOn) purge();
119 }
120 
125 inline void PacketLog::enableLocalLog(bool on) {
126  logLocal = on; if (logLocal) { numOut = numDataOut = 0; }
127 }
128 
129 } // ends namespace
130 
131 
132 #endif