1 | #include "MessageProcessor.h"
|
---|
2 |
|
---|
3 | #include <iostream>
|
---|
4 |
|
---|
5 | #include "Common.h"
|
---|
6 |
|
---|
7 | MessageProcessor::MessageProcessor() {
|
---|
8 | lastUsedId = 0;
|
---|
9 | }
|
---|
10 |
|
---|
11 | MessageProcessor::~MessageProcessor() {
|
---|
12 | }
|
---|
13 |
|
---|
14 | int MessageProcessor::sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest) {
|
---|
15 | msg->id = ++lastUsedId;
|
---|
16 | MessageContainer message(*msg, *dest);
|
---|
17 | sentMessages[msg->id] = message;
|
---|
18 |
|
---|
19 | cout << "Sending message" << endl;
|
---|
20 | cout << "id: " << msg->id << endl;
|
---|
21 | cout << "type: " << msg->type << endl;
|
---|
22 | cout << "buffer: " << msg->buffer << endl;
|
---|
23 |
|
---|
24 | int ret = sendto(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)dest, sizeof(struct sockaddr_in));
|
---|
25 |
|
---|
26 | cout << "Send a message of type " << msg->type << endl;
|
---|
27 |
|
---|
28 | return ret;
|
---|
29 | }
|
---|
30 |
|
---|
31 | int MessageProcessor::receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *source) {
|
---|
32 | socklen_t socklen = sizeof(struct sockaddr_in);
|
---|
33 |
|
---|
34 | // assume we don't care about the value of socklen
|
---|
35 | int ret = recvfrom(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, &socklen);
|
---|
36 |
|
---|
37 | if (ret == -1)
|
---|
38 | return ret;
|
---|
39 |
|
---|
40 | // add id to the NETWORK_MSG struct
|
---|
41 | if (msg->type == MSG_TYPE_ACK) {
|
---|
42 | if (!sentMessages[msg->id].isAcked) {
|
---|
43 | cout << "Received new ack" << endl;
|
---|
44 | sentMessages[msg->id].isAcked = true;
|
---|
45 | sentMessages[msg->id].timeAcked = getCurrentMillis();
|
---|
46 | }
|
---|
47 |
|
---|
48 | return -1; // don't do any further processing
|
---|
49 | }else {
|
---|
50 | if (ret > -1) {
|
---|
51 | cout << "Received message" << endl;
|
---|
52 | cout << "id: " << msg->id << endl;
|
---|
53 | cout << "type: " << msg->type << endl;
|
---|
54 | cout << "buffer: " << msg->buffer << endl;
|
---|
55 | }
|
---|
56 |
|
---|
57 | NETWORK_MSG ack;
|
---|
58 | ack.id = msg->id;
|
---|
59 | ack.type = MSG_TYPE_ACK;
|
---|
60 |
|
---|
61 | sendto(sock, (char*)&ack, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, sizeof(struct sockaddr_in));
|
---|
62 | }
|
---|
63 |
|
---|
64 | return ret;
|
---|
65 | }
|
---|
66 |
|
---|
67 | void MessageProcessor::resendUnackedMessages(int sock) {
|
---|
68 | map<int, MessageContainer>::iterator it;
|
---|
69 |
|
---|
70 | for(it = sentMessages.begin(); it != sentMessages.end(); it++) {
|
---|
71 | sendto(sock, (char*)&it->second.msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)&it->second.clientAddr, sizeof(struct sockaddr_in));
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | void MessageProcessor::cleanAckedMessages() {
|
---|
76 | map<int, MessageContainer>::iterator it = sentMessages.begin();
|
---|
77 |
|
---|
78 | while (it != sentMessages.end()) {
|
---|
79 | if (it->second.isAcked) {
|
---|
80 | // cout << "Found acked message" << endl;
|
---|
81 | // cout << "time acked" << it->second.timeAcked << endl;
|
---|
82 | // cout << "cur time" << getCurrentMillis() << endl;
|
---|
83 | if ((getCurrentMillis() - it->second.timeAcked) > 1000) {
|
---|
84 | cout << "Message was acked. time to delete it" << endl;
|
---|
85 | cout << "old map size" << sentMessages.size() << endl;
|
---|
86 | sentMessages.erase(it++);
|
---|
87 | cout << "new map size" << sentMessages.size() << endl;
|
---|
88 | }else
|
---|
89 | it++;
|
---|
90 | }else
|
---|
91 | it++;
|
---|
92 | }
|
---|
93 | }
|
---|