-
Notifications
You must be signed in to change notification settings - Fork 0
/
Observer.cpp
82 lines (66 loc) · 1.76 KB
/
Observer.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
#include "Observer.h"
Observer::Observer() {
nodeNet = new Broadcasts();
}
Observer::~Observer() {
delete nodeNet;
}
int Observer::initObserver() {
int localFD = nodeNet->getNodeDescriptor();
this->obsQueue = kqueue();
EV_SET(&this->nodeEventSets, localFD,
EVFILT_READ, EV_ADD, 0, 0, NULL);
if (kevent(this->obsQueue, &this->nodeEventSets, 1, NULL, 0, NULL) < 0) {
// error
return FAILURE;
}
return SUCCESS;
}
int Observer::getEventSet() {
return this->nodeEventSets;
}
// fix later..
void Observer::runMonitoringEvents(int kq) {
struct kevent evSet;
struct kevent evList[32];
int nEvents, i;
struct sockaddr_storage addr;
socklen_t socklen = sizeof(addr);
int fd;
int sockFD = nodeNet->getNodeDescriptor();
while(ON_CONNECT) {
nEvents = kevent(kq, NULL, 0, evList, 32, NULL);
if (nEvents < 1) {
}
for (i = 0; i < nEvents; i++) {
if (evList[i].flags & EV_EOF) {
//printf("disconnect\n");
fd = evList[i].ident;
EV_SET(&evSet, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
if (kevent(kq, &evSet, 1, NULL, 0, NULL) == -1) {
}
nodeNet->deleteConnection(fd);
}
else if (evList[i].ident == sockFD) {
//fd = accept(evList[i].ident, (struct sockaddr *)&addr, &socklen);
fd = nodeNet->acceptRemoteRequest(evList[i].ident, (struct sockaddr *)&addr, &socklen);
if (fd == -1) {
//err(1, "accept");
}
if (nodeNet->addConnection(fd) == 0) {
EV_SET(&evSet, fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
if (kevent(kq, &evSet, 1, NULL, 0, NULL) == -1) {
err(1, "kevent");
}
//send_msg(fd, "welcome!\n");
} else {
//printf("connection refused\n");
close(fd);
}
}
else if (evList[i].flags & EVFILT_READ) {
//recv_msg(evList[i].ident);
}
}
}
}