|
1 /* |
|
2 * Copyright 2008 The MITRE Corporation |
|
3 * |
|
4 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 * you may not use this file except in compliance with the License. |
|
6 * You may obtain a copy of the License at |
|
7 * |
|
8 * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 * |
|
10 * Unless required by applicable law or agreed to in writing, software |
|
11 * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 * See the License for the specific language governing permissions and |
|
14 * limitations under the License. |
|
15 * |
|
16 * The US Government will not be charged any license fee and/or royalties |
|
17 * related to this software. Neither name of The MITRE Corporation; nor the |
|
18 * names of its contributors may be used to endorse or promote products |
|
19 * derived from this software without specific prior written permission. |
|
20 */ |
|
21 |
|
22 /* |
|
23 * This product includes software written and developed |
|
24 * by Brian Adamson and Joe Macker of the Naval Research |
|
25 * Laboratory (NRL). |
|
26 */ |
|
27 |
|
28 #ifndef _NORM_SESSION_MANAGER_H_ |
|
29 #define _NORM_SESSION_MANAGER_H_ |
|
30 |
|
31 #if defined(NORM_ENABLED) |
|
32 |
|
33 #include <list> |
|
34 |
|
35 #include <oasys/thread/Thread.h> |
|
36 #include <oasys/thread/MsgQueue.h> |
|
37 #include <oasys/util/Singleton.h> |
|
38 |
|
39 #include "NORMConvergenceLayer.h" |
|
40 #include "NORMReceiver.h" |
|
41 |
|
42 namespace dtn { |
|
43 |
|
44 /** |
|
45 * Class (and thread) to manage interactions with Norm instance. |
|
46 */ |
|
47 class NORMSessionManager : public oasys::Thread, |
|
48 public oasys::Logger, |
|
49 public oasys::Singleton<NORMSessionManager> { |
|
50 public: |
|
51 /** |
|
52 * Destructor. |
|
53 */ |
|
54 virtual ~NORMSessionManager(); |
|
55 |
|
56 /** |
|
57 * Initialize Norm engine. |
|
58 */ |
|
59 void init(); |
|
60 |
|
61 /** |
|
62 * Register a Norm session with the manager. |
|
63 */ |
|
64 void register_receiver(const NORMReceiver *receiver); |
|
65 |
|
66 /** |
|
67 * Unregister a Norm session. |
|
68 */ |
|
69 //void remove_receiver(const NORMReceiverRef &receiver); |
|
70 void remove_receiver(const NORMReceiver *receiver); |
|
71 |
|
72 /** |
|
73 * Norm instance handle accessor. |
|
74 */ |
|
75 NormInstanceHandle norm_instance() {return norm_instance_;} |
|
76 |
|
77 protected: |
|
78 /** |
|
79 * Struct to store info on registered Norm sessions. |
|
80 */ |
|
81 struct NORMNode { |
|
82 NORMNode(NormSessionHandle session, |
|
83 oasys::MsgQueue<NormEvent> *queue) |
|
84 : session_(session), queue_(queue) {} |
|
85 |
|
86 bool operator==(const NormSessionHandle &session) const |
|
87 { |
|
88 return (session_ == session); |
|
89 } |
|
90 |
|
91 NormSessionHandle session_; |
|
92 oasys::MsgQueue<NormEvent> *queue_; |
|
93 }; |
|
94 typedef std::list<NORMNode> receiver_list_t; |
|
95 |
|
96 /** |
|
97 * Type for an iterator. |
|
98 */ |
|
99 typedef receiver_list_t::iterator iterator; |
|
100 |
|
101 iterator begin() {return receivers_.begin();} |
|
102 |
|
103 iterator end() {return receivers_.end();} |
|
104 |
|
105 /** |
|
106 * Loop on NormGetNextEvent. |
|
107 */ |
|
108 virtual void run(); |
|
109 |
|
110 /** |
|
111 * Orderly shutdown of Norm instance. |
|
112 */ |
|
113 void destroy_instance(bool stop_first = true); |
|
114 |
|
115 NormInstanceHandle norm_instance_; ///< Norm istance handle |
|
116 mutable oasys::SpinLock lock_; ///< Lock to protect internal data structures. |
|
117 receiver_list_t receivers_; ///< registered Norm sessions |
|
118 |
|
119 private: |
|
120 friend class oasys::Singleton<NORMSessionManager, true>; |
|
121 NORMSessionManager(); |
|
122 }; |
|
123 |
|
124 } // namespace dtn |
|
125 #endif // NORM_ENABLED |
|
126 #endif // _NORM_SESSION_MANAGER_H_ |