IBR-DTNSuite  0.10
EventSwitch.h
Go to the documentation of this file.
1 /*
2  * EventSwitch.h
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
22 #ifndef EVENTSWITCH_H_
23 #define EVENTSWITCH_H_
24 
25 #include "Component.h"
26 #include "core/Event.h"
27 #include <ibrcommon/Exceptions.h>
28 #include <ibrcommon/thread/Mutex.h>
30 
31 #include <list>
32 #include <map>
33 
34 namespace dtn
35 {
36  namespace core
37  {
38  class EventReceiver;
39 
41  {
42  private:
43  EventSwitch();
44  virtual ~EventSwitch();
45 
46  bool _running;
47  bool _shutdown;
48 
52  virtual const std::string getName() const;
53 
54  class Task
55  {
56  public:
57  Task(EventProcessor &proc, dtn::core::Event *evt);
58  ~Task();
59 
60  EventProcessor &processor;
61  dtn::core::Event *event;
62  };
63 
64  class Worker : public ibrcommon::JoinableThread
65  {
66  public:
67  Worker(EventSwitch &sw);
68  ~Worker();
69 
70  protected:
71  void run() throw ();
72  virtual void __cancellation() throw ();
73 
74  private:
75  EventSwitch &_switch;
76  bool _running;
77  };
78 
79  ibrcommon::Conditional _queue_cond;
80  std::list<Task*> _queue;
81  std::list<Task*> _prio_queue;
82  std::list<Task*> _low_queue;
83 
84  void process();
85 
86  protected:
87  virtual void componentUp() throw ();
88  virtual void componentDown() throw ();
89 
90  public:
91  static EventSwitch& getInstance();
92  void loop(size_t threads = 0);
93 
97  bool empty() const;
98 
103  void shutdown();
104 
105  //static void raiseEvent(Event *evt);
106  static void queue(EventProcessor &proc, Event *evt);
107 
108  friend class Worker;
109  };
110  }
111 }
112 
113 #endif /* EVENTSWITCH_H_ */