-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatchmanager.cc
116 lines (98 loc) · 2.72 KB
/
dispatchmanager.cc
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright: (C) 2016 ESIMTEK ALL RIGHTS RESERVED
// author: Yonghua Zheng
#include <map>
#include "dispatchmanager.h"
const int DispatchManager::main_id = 0;
const int DispatchManager::modemmanager_id = 1;
DispatchManager* DispatchManager::singleton = NULL;
DispatchManager::DispatchManager()
{
container = new MessageDispatchContainer();
loop = new MessageDispatch();
registerr(DispatchManager::main_id, loop);
}
DispatchManager::~DispatchManager()
{
container->clear();
delete loop;
delete container;
}
void DispatchManager::Instantiate()
{
if (!DispatchManager::singleton)
DispatchManager::singleton = new DispatchManager();
//return DispatchManager::singleton;
}
void DispatchManager::Dispose()
{
if (DispatchManager::singleton) {
delete DispatchManager::singleton;
DispatchManager::singleton = NULL;
}
}
bool DispatchManager::registerr(int id, MessageDispatch* dispatch)
{
MessageDispatchContainer::iterator it = container->find(id);
if (it == container->end()) {
container->insert(MessageDispatchContainer::value_type(id, dispatch));
return true;
}
return false;
}
bool DispatchManager::unregister(int id)
{
MessageDispatchContainer::iterator it = container->find(id);
if (it != container->end()) {
container->erase(it);
return true;
}
return false;
}
bool DispatchManager::post(int id, const char* message)
{
MessageDispatchContainer::iterator it = container->find(id);
if (it != container->end()) {
MessageDispatch* dispatch = it->second;
if (dispatch) {
dispatch->post(message);
return true;
}
}
return false;
}
bool DispatchManager::get(int id, char* message, int length)
{
MessageDispatchContainer::iterator it = container->find(id);
if (it != container->end()) {
MessageDispatch* dispatch = it->second;
if (dispatch) {
dispatch->read(message, length);
return true;
}
}
return false;
}
bool DispatchManager::Register(int id, MessageDispatch* dispatch)
{
if (DispatchManager::singleton)
return DispatchManager::singleton->registerr(id, dispatch);
return false;
}
bool DispatchManager::Unregister(int id)
{
if (DispatchManager::singleton)
return DispatchManager::singleton->unregister(id);
return false;
}
bool DispatchManager::Post(int id, const char *message)
{
if (DispatchManager::singleton)
return DispatchManager::singleton->post(id, message);
return false;
}
bool DispatchManager::Get(int id, char *message, int length)
{
if (DispatchManager::singleton)
return DispatchManager::singleton->get(id, message, length);
return false;
}