Revised OpenFlow Library  v0.6.0dev
 All Classes Files Functions Variables Friends Groups Pages
crofqueue.h
1 /*
2  * crofqueue.h
3  *
4  * Created on: 23.11.2014
5  * Author: andreas
6  */
7 
8 #ifndef CROFQUEUE_H_
9 #define CROFQUEUE_H_
10 
11 #include <list>
12 #include <ostream>
13 
14 #include "rofl/common/thread_helper.h"
15 #include "rofl/common/openflow/messages/cofmsg.h"
16 #include "rofl/common/logging.h"
17 
18 namespace rofl {
19 
20 class crofqueue {
21 public:
22 
26  crofqueue()
27  {};
28 
32  ~crofqueue() {
33  clear();
34  };
35 
36 public:
37 
41  bool
42  empty() {
43  RwLock rwlock(queuelock, RwLock::RWLOCK_READ);
44  return queue.empty();
45  };
46 
50  void
51  clear() {
52  RwLock rwlock(queuelock, RwLock::RWLOCK_WRITE);
53  while (not queue.empty()) {
54  delete queue.front();
55  queue.pop_front();
56  }
57  };
58 
62  size_t
63  store(rofl::openflow::cofmsg* msg) {
64  RwLock rwlock(queuelock, RwLock::RWLOCK_WRITE);
65  rofl::logging::trace << "[rofl-common][crofqueue][store] msg: " << std::endl << *msg;
66  queue.push_back(msg);
67  return queue.size();
68  };
69 
74  retrieve() {
76  RwLock rwlock(queuelock, RwLock::RWLOCK_WRITE);
77  if (queue.empty()) {
78  return msg;
79  }
80  msg = queue.front(); queue.pop_front();
81  rofl::logging::trace << "[rofl-common][crofqueue][retrieve] msg: " << std::endl << *msg;
82  return msg;
83  };
84 
89  front() {
91  RwLock rwlock(queuelock, RwLock::RWLOCK_READ);
92  if (queue.empty()) {
93  return msg;
94  }
95  msg = queue.front();
96  rofl::logging::trace << "[rofl-common][crofqueue][front] msg: " << std::endl << *msg;
97  return msg;
98  };
99 
103  void
104  pop() {
105  RwLock rwlock(queuelock, RwLock::RWLOCK_WRITE);
106  if (queue.empty()) {
107  return;
108  }
109  queue.pop_front();
110  rofl::logging::trace << "[rofl-common][crofqueue][pop] " << std::endl;
111  };
112 
113 public:
114 
115  friend std::ostream&
116  operator<< (std::ostream& os, const crofqueue& queue) {
117  RwLock rwlock(queue.queuelock, RwLock::RWLOCK_READ);
118  os << rofl::indent(0) << "<crofqueue size #" << queue.queue.size() << " >" << std::endl;
119  rofl::indent i(2);
120  for (std::list<rofl::openflow::cofmsg*>::const_iterator
121  it = queue.queue.begin(); it != queue.queue.end(); ++it) {
122  os << *(*it);
123  }
124  return os;
125  };
126 
127 private:
128 
129  std::list<rofl::openflow::cofmsg*> queue;
130  mutable PthreadRwLock queuelock;
131 };
132 
133 }; // end of namespace rofl
134 
135 #endif /* CROFQUEUE_H_ */
Definition: thread_helper.h:25
Definition: thread_helper.h:88
Definition: crofqueue.h:20
Definition: logging.h:76
Definition: cofmsg.h:62