1 #include "datathread.h"
3 DataThread::DataThread()
13 xmlParser = XML_ParserCreate(NULL);
16 printfd(__FILE__, "DataThread::DataThread() Failed to create parser\n");
19 XML_SetElementHandler(xmlParser, DTXMLStart, DTXMLEnd);
22 DataThread::~DataThread()
24 XML_ParserFree(xmlParser);
27 bool DataThread::Handle(int s)
31 printfd(__FILE__, "DataThread::Handle() Users not set\n");
36 printfd(__FILE__, "DataThread::Handle() Storage not set\n");
42 if (pthread_create(&tid, NULL, Run, this))
44 printfd(__FILE__, "DataThread::Handle() Failed to create thread\n");
47 if (pthread_detach(tid))
49 printfd(__FILE__, "DataThread::Handle() Cannot detach the thread\n");
54 void * DataThread::Run(void * self)
56 DataThread * dt = reinterpret_cast<DataThread *>(self);
60 if (dt->ReadRequest())
62 if (dt->DecodeRequest())
64 if (dt->ParseRequest())
68 printfd(__FILE__, "DataThread::Run() All done\n");
72 printfd(__FILE__, "DataThread::Run() Failed to answer the request\n");
77 printfd(__FILE__, "DataThread::Run() Cannot parse the request\n");
82 printfd(__FILE__, "DataThread::Run() Cannot decode the request\n");
87 printfd(__FILE__, "DataThread::Run() Cannot read the request\n");
95 bool DataThread::ReadRequest()
100 int res = read(sock, &size, sizeof(size));
101 if (res != sizeof(size))
103 printfd(__FILE__, "DataThread::ReadRequest() Reading login size failed! Wanted %d bytes, got %d bytes.\n", sizeof(size), res);
110 printfd(__FILE__, "DataThread::ReadRequest() Invalid login size.\n");
115 buf = new char[size];
117 res = read(sock, buf, size);
120 printfd(__FILE__, "DataThread::ReadRequest() Reading login failed! Wanted %d bytes, got %d bytes.\n", size, res);
126 login.assign(buf, size);
129 res = read(sock, &size, sizeof(size));
130 if (res != sizeof(size))
132 printfd(__FILE__, "DataThread::ReadRequest() Reading request size failed! Wanted %d bytes, got %d bytes.\n", sizeof(size), res);
139 printfd(__FILE__, "DataThread::ReadRequest() Invalid request size.\n");
144 data = new char[size + 1];
147 res = read(sock, data, size);
150 printfd(__FILE__, "DataThread::ReadRequest() Reading request failed! Wanted %d bytes, got %d bytes.\n", size, res);
159 bool DataThread::DecodeRequest()
161 if (users->FindByName(login, &(uit)))
163 printfd(__FILE__, "DataThread::DecodeRequest() User '%s' not found.\n", login.c_str());
168 std::string password = uit->property.password;
171 char * key = new char[password.length()];
172 strncpy(key, password.c_str(), password.length());
175 reinterpret_cast<unsigned char *>(key),
178 for (int i = 0; i < dataSize / 8; ++i)
182 a = n2l(reinterpret_cast<unsigned char *>(data + i * 8));
183 b = n2l(reinterpret_cast<unsigned char *>(data + i * 8 + 4));
184 Blowfish_Decrypt(&ctx,
187 l2n(a, reinterpret_cast<unsigned char *>(data + i * 8));
188 l2n(b, reinterpret_cast<unsigned char *>(data + i * 8 + 4));
196 bool DataThread::ParseRequest()
198 if (XML_Parse(xmlParser, data, dataSize, 1) != XML_STATUS_OK)
200 printfd(__FILE__, "DataThread::ParseRequest() Failed to parse the request\n");
201 request.isBad = true;
207 bool DataThread::MakeAnswer()
219 printfd(__FILE__, "DataThread::MakeAnswer() Failed to send answer");
225 printfd(__FILE__, "DataThread::MakeAnswer() Failed to make stat answer\n");
231 printfd(__FILE__, "DataThread::MakeAnswer() Failed to make conf answer\n");
238 void DataThread::Cleanup()
245 request.conf.erase(request.conf.begin(), request.conf.end());
246 request.stat.erase(request.stat.begin(), request.stat.end());
249 request.isBad = false;
252 void DataThread::ParseTag(const std::string & name, const char ** attr)
256 if (name == "request")
260 printfd(__FILE__, "DataThread::ParseTag() 'request' tag require an attribute\n");
261 request.isBad = true;
266 std::string attrName(*attr++);
267 std::string attrValue(*attr++);
270 printfd(__FILE__, "DataThread::ParseTag() Extra attributes on tag 'request'\n");
272 if (attrName == "login")
274 if (attrValue != login)
276 printfd(__FILE__, "DataThread::ParseTag() Logins doesn't match\n");
277 request.isBad = true;
283 printfd(__FILE__, "DataThread::ParseTag() Unexpected attribute '%s'\n", attrName.c_str());
284 request.isBad = true;
289 else if (name == "conf")
291 pvList = &(request.conf);
293 else if (name == "stat")
295 pvList = &(request.stat);
303 bool DataThread::MakeConf()
308 bool DataThread::MakeStat()
313 bool DataThread::SendAnswer()
318 void DTXMLStart(void * data, const char * name, const char ** attr)
320 DataThread * dt = reinterpret_cast<DataThread *>(data);
321 dt->ParseTag(name, attr);
324 void DTXMLEnd(void * data, const char * name)
326 //DataThread * dt = reinterpret_cast<DataThread *>(data);