-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServiceObj.cpp
136 lines (113 loc) · 2.35 KB
/
ServiceObj.cpp
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "ServiceObj.h"
void MessageQueue::Push(Message& msg)
{
lock_free_.push(msg);
}
Message MessageQueue::Pop()
{
Message result;
lock_free_.pop(result);
return result;
}
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
void ServiceObj::Init(int handle)
{
m_handle = handle;
m_con = new DDCondition(m_mutex);
}
void ServiceObj::Free()
{}
void ServiceObj::Push(Message& msg)
{
m_mq.Push(msg);
m_con->signal();
}
Message ServiceObj::Pop()
{
return m_mq.Pop();
}
void ServiceObj::Wait()
{
m_con->wait();
}
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
ServiceMgr::ServiceMgr()
{}
ServiceMgr::~ServiceMgr()
{}
void ServiceMgr::Init(int pid, std::vector<Node>& nodes)
{
for (std::vector<Node>::iterator iter = nodes.begin(); iter != nodes.end(); ++iter)
{
for (int i = 0;i<10;++i)
{
char sp[32] = { 0, };
sprintf(sp, "%d", iter->unis[i].port);
m_services[iter->unis[i].name] = iter->IP;
m_services[iter->unis[i].name] += "&&";
m_services[iter->unis[i].name] += sp;
}
}
}
void ServiceMgr::Register(int handle, ServiceObj* sobj)
{
if (handle < 255)
{
m_objs[handle] = sobj;
m_handles.push_back(handle);
}
}
void* serviceThread(void *arg)
{
ServiceObj* obj = (ServiceObj*)arg;
while (true)
{
if (obj->Empty())
{
obj->Wait();
}
else
{
Message pmsg = obj->Pop();
obj->Dispatch(pmsg);
pmsg.releaseBuf();
}
}
return NULL;
}
void ServiceMgr::DoService()
{
for (std::vector<int>::iterator iter = m_handles.begin(); iter != m_handles.end(); ++iter)
{
if (m_objs[*iter] != NULL)
{
m_objs[*iter]->Init(*iter);
pthread_t serpd;
pthread_create(&serpd, NULL, serviceThread, (void*)(m_objs[*iter]));
m_ptsp.push_back(serpd);
}
}
}
void ServiceMgr::WaitService()
{
for (std::vector<pthread_t>::iterator iter = m_ptsp.begin(); iter != m_ptsp.end(); ++iter)
{
pthread_join(*iter, NULL);
}
}
ServiceMgr* ServiceMgr::instance(void)
{
static ServiceMgr* g_service = NULL;
static DDMutex _lock;
if (g_service == NULL)
{
GUARD_RETURN(DDMutex, obj, _lock, NULL);
if (g_service == NULL)
{
g_service = new ServiceMgr;
}
}
return g_service;
}