-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyflx.cpp
More file actions
260 lines (228 loc) · 7.07 KB
/
pyflx.cpp
File metadata and controls
260 lines (228 loc) · 7.07 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* (C) Copyright DevCartel. All rights reserved,
* Bangkok, Thailand
*/
#include "pyflx.h"
using namespace std;
using namespace boost::python;
struct connection_error : std::exception {
char const* what() const throw() { return "CONNECTION_ERROR"; }
};
/***************
* Symproxy Class
***************/
Symproxy::Symproxy() :
nCount(0),
tmInit({0,0}),
onLogonCallback(0),
onSubscribeCallback(0),
onUnsubscribeCallback(0)
{
}
Symproxy::~Symproxy() {}
bool Symproxy::initialize(int nBindPort) {
return CSymProxy::Initialize(nBindPort, false);
}
int Symproxy::getFd() {
FD_ZERO(&readF);
nFd = CSymProxy::GetFd();
return nFd;
}
int Symproxy::select() {
tm = tmInit;
FD_SET(nFd, &readF);
int nReturn = ::select(nFd + 1, &readF, NULL, NULL, &tm);
return nReturn;
}
bool Symproxy::checkSocket() {
return CSymProxy::CheckSocket(readF);
}
bool Symproxy::closeClient() {
return CSymProxy::CloseClient();
}
bool Symproxy::dispatch() {
return CSymProxy::Dispatch();
}
bool Symproxy::OnLogon(const char *szUserName, const char *szPassword) {
if(onLogonCallback) {
PyEval_InitThreads();
PyGILState_STATE state = PyGILState_Ensure();
boost::python::call<bool>(onLogonCallback, szUserName, szPassword);
PyGILState_Release(state);
}
return true;
}
void Symproxy::OnSubscribe(const char* szTopic) {
if(onSubscribeCallback) {
PyEval_InitThreads();
PyGILState_STATE state = PyGILState_Ensure();
boost::python::call<void>(onSubscribeCallback, szTopic);
PyGILState_Release(state);
}
return;
}
void Symproxy::OnUnsubscribe(const char* szTopic) {
if(onUnsubscribeCallback) {
PyEval_InitThreads();
PyGILState_STATE state = PyGILState_Ensure();
boost::python::call<void>(onUnsubscribeCallback, szTopic);
PyGILState_Release(state);
}
return;
}
void Symproxy::onLogon(PyObject *callable) {
onLogonCallback = callable;
}
void Symproxy::onSubscribe(PyObject *callable) {
onSubscribeCallback = callable;
}
void Symproxy::onUnsubscribe(PyObject *callable) {
onUnsubscribeCallback = callable;
}
void Symproxy::publish(const char* szTopic, boost::python::dict data) {
HTICK pTick = CreateTick(szTopic);
boost::python::list keys = (boost::python::list)data.keys();
for (int j = len(keys) - 1 ; j >= 0; j--) {
int key = extract<int>(keys[j]);
extract<int> isInt(data[key]);
if (isInt.check()) {
int value = extract<int>(data[key]);
if (SetTickField(pTick, key, value))
continue;
}
extract<double> isDouble(data[key]);
if (isDouble.check()) {
double value = extract<double>(data[key]);
if (SetTickField(pTick, key, value))
continue;
}
extract<string> isString(str(data[key]));
if (isString.check()) {
string value = extract<string>(str(data[key]));
if (SetTickField(pTick, key, value.c_str()))
continue;
}
extract<char> isChar(str(data[key]));
if (isChar.check()) {
char value = extract<char>(str(data[key]));
if (SetTickField(pTick, key, value))
continue;
}
}
Flush();
}
/******************
* Symlistener Class
******************/
Symlistener::Symlistener() :
pConn(0),
onDataCallback(0),
m_nVal(0),
m_dblVal(0),
m_sVal(),
m_cVal(0),
m_Dict()
{
pConn = new CSymConnection();
}
Symlistener::~Symlistener() {
pConn->UnsubscribeAll();
pConn->Disconnect();
delete pConn;
}
bool Symlistener::connect(const char *host, int port, const char *username=0, const char *password=0) {
bool bConnectSuccessful = pConn->Connect(host, port, username, password);
if (bConnectSuccessful) {
pConn->AddClient(this);
pConn->DispatchEvents();
return true;
} else {
return false;
}
}
void Symlistener::dispatchEvents() {
pConn->DispatchEvents();
}
bool Symlistener::subscribeAll() {
return pConn->SubscribeAll();
}
bool Symlistener::unsubscribeAll() {
return pConn->UnsubscribeAll();
}
bool Symlistener::subscribe(const char *szTopic) {
return pConn->Subscribe(szTopic);
}
bool Symlistener::unsubscribe(const char *szTopic) {
return pConn->Unsubscribe(szTopic);
}
void Symlistener::OnData(const char *szTopic, const CSymData *pData) {
m_Dict.clear();
SETFLIDS s = pData->m_setFlids;
SETFLIDS::iterator it;
for(it = s.begin(); it != s.end(); it++) {
if(GetDouble(pData, *it, m_dblVal)) {
m_Dict[*it] = m_dblVal;
} else if(GetInt(pData, *it, m_nVal)) {
m_Dict[*it] = m_nVal;
} else if(GetString(pData, *it, m_sVal)) {
m_Dict[*it] = m_sVal.c_str();
} else if(GetChar(pData, *it, m_cVal)) {
m_Dict[*it] = m_cVal;
}
}
if(onDataCallback) {
PyEval_InitThreads();
PyGILState_STATE state = PyGILState_Ensure();
boost::python::call<void>(onDataCallback, szTopic, m_Dict);
PyGILState_Release(state);
}
return;
}
void Symlistener::onData(PyObject *callable) {
onDataCallback = callable;
}
void Symlistener::loop() {
pConn->Loop();
throw connection_error();
}
void Symlistener::disconnect() {
pConn->Disconnect();
}
/****************************************************************
* Exception translator for Python. See py_error class in pyflx.h.
****************************************************************/
static void translateException(const py_error &err) {
PyErr_SetString(PyExc_Exception, err.what().c_str());
}
static void translate(const connection_error &err) {
PyErr_SetString(PyExc_Exception, err.what());
}
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(Symlistener_connect_overloads, Symlistener::connect, 2, 4)
BOOST_PYTHON_MODULE(pyflx)
{
register_exception_translator<py_error>(&translateException);
register_exception_translator<connection_error>(&translate);
class_<Symproxy>("Symproxy")
.def("initialize", &Symproxy::initialize)
.def("onLogon", &Symproxy::onLogon)
.def("onSubscribe", &Symproxy::onSubscribe)
.def("onUnsubscribe", &Symproxy::onUnsubscribe)
.def("getFd", &Symproxy::getFd)
.def("select", &Symproxy::select)
.def("checkSocket", &Symproxy::checkSocket)
.def("closeClient", &Symproxy::closeClient)
.def("dispatch", &Symproxy::dispatch)
.def("publish", &Symproxy::publish)
;
class_<Symlistener>("Symlistener")
.def("connect", &Symlistener::connect, Symlistener_connect_overloads(args("username", "password")))
.def("subscribeAll", &Symlistener::subscribeAll)
.def("unsubscribeAll", &Symlistener::unsubscribeAll)
.def("subscribe", &Symlistener::subscribe)
.def("unsubscribe", &Symlistener::unsubscribe)
.def("onData", &Symlistener::onData)
.def("dispatchEvents", &Symlistener::dispatchEvents)
.def("loop", &Symlistener::loop)
.def("disconnect", &Symlistener::disconnect)
;
}