-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReceiver.cpp
59 lines (53 loc) · 1.47 KB
/
Receiver.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
/*
* Receiver.cpp
*
* Created on: 1 gru 2014
* Author: edek437
*/
#include "Receiver.h"
#include "Poco/Thread.h"
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <boost/regex.hpp>
Receiver::Receiver(Poco::Net::StreamSocket& sock, Poco::Mutex *mut, bool *up,
std::string *old, std::vector<Person> *db) :
socket(sock), mutex(mut), update(up), old_name(old), message_db(db) {
}
void Receiver::run() {
boost::regex exp("(.*):(.*)");
for (;;) {
//sleep(10);
mutex->lock();
char message[1024];
std::string request = "new " + *old_name;
socket.sendBytes((char*) request.c_str(), request.size());
int rec = socket.receiveBytes((char *) message, sizeof(message) - 1);
message[rec] = '\0';
std::string answer(message);
if (answer == "No messages") {
mutex->unlock();
} else {
*update = true;
std::string name=boost::regex_replace(answer,exp,"\\1");
std::vector<Person>::iterator vit;
for(vit=message_db->begin();vit!=message_db->end();vit++){
if(vit->name==name){ //thank you eclipse. Wasted 1,5h to repair this "mistake"
break;
}
}
if (vit==message_db->end()){ //never coresponding with name before
message_db->push_back(Person(name,answer)); //thank you eclipse. Wasted 1,5h to repair this "mistake"
}
else{
vit->chatbox.push_back(answer); //another "mistake"
vit->is_new=true;
}
mutex->unlock();
}
}
}
Receiver::~Receiver() {
// TODO Auto-generated destructor stub
}