-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServiceObj.h
138 lines (114 loc) · 1.99 KB
/
ServiceObj.h
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
137
138
#ifndef SERVICEOBJ_H
#define SERVICEOBJ_H
#include "boost/lockfree/queue.hpp"
#include "Stream.h"
#include "Thread.h"
#include <vector>
#include <map>
struct Message
{
Message()
{
memset(this, 0, sizeof(Message));
}
Message(int fd, int _t, Stream_Base* _s) :m_fd(fd), m_type(_t), m_stream(_s)
{}
int getFd(){ return m_fd; }
int getType(){ return m_type; }
Stream_Base* getMsg(){ return m_stream; }
void releaseBuf()
{
if (m_stream)
{
delete m_stream;
m_stream = NULL;
}
}
int m_fd;
int m_type;
Stream_Base* m_stream;
};
class MessageQueue
{
public:
MessageQueue(){}
~MessageQueue(){}
void Push(Message&);
Message Pop();
bool Empty(){ return lock_free_.empty(); }
private:
boost::lockfree::queue<Message> lock_free_;
};
class ServiceObj
{
public:
ServiceObj()
{
m_handle = 0;
m_con = NULL;
}
virtual ~ServiceObj(){}
virtual void Init(int handle);
virtual void Free();
virtual void Dispatch(Message& msg){}
virtual void OnConnect(int fd){}
void Push(Message& msg);
Message Pop();
void Wait();
bool Empty(){ return m_mq.Empty(); }
private:
int m_handle;
MessageQueue m_mq;
DDCondition* m_con;
DDMutex m_mutex;
};
struct Unit
{
Unit()
{
memset(this, 0, sizeof(Unit));
}
int id;
int port;
char name[32];
char path[256];
};
struct Node
{
Node()
{
memset(this, 0, sizeof(Node));
}
int PID;
char IP[32];
Unit unis[10];
};
class ServiceMgr
{
public:
ServiceMgr();
~ServiceMgr();
void Init(int pid, std::vector<Node>&);
void Register(int handle, ServiceObj* sobj);
inline ServiceObj* GetService(int handle)
{
if (handle < 255)
{
return m_objs[handle];
}
return NULL;
}
std::map<std::string, std::string>& GetServicesMap()
{
return m_services;
}
void DoService();
void WaitService();
static ServiceMgr* instance(void);
private:
ServiceObj* m_objs[255];
std::vector<int> m_handles;
std::vector<pthread_t> m_ptsp;
std::map<std::string, std::string> m_services;
};
#endif