-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.cpp
70 lines (62 loc) · 1.91 KB
/
message.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
#include "message.h"
packet* createSubRequest(char* topic, int sf, char id[10]) {
packet *p = new packet;
memset(p, 0, sizeof(packet));
p->m.tip_pachet = sf == 1 ? SUBSCRIBEREQ : SUBSCRIBEREQNOSF;
p->length = TCPCLIENTLEN;
memcpy(p->m.topic, topic, strlen(topic));
memcpy(p->id, id, strlen(id));
return p;
}
packet* createUnSubRequest(char* topic, char id[10]) {
packet *p = new packet;
memset(p, 0, sizeof(packet));
p->m.tip_pachet = UNSUBSCRIBEREQ;
p->length = TCPCLIENTLEN;
memcpy(p->m.topic, topic, strlen(topic));
memcpy(p->id, id, strlen(id));
return p;
}
int DIE(bool val, const char* s, bool shouldExit) {
if (val) {
perror(s);
if (shouldExit)
exit(0);
}
return val * FAIL + !val * SUCCESS;
}
float toFloat(myFloat f) {
float unsignVal = (float)(ntohl(f.parteIntreaga) / (double)pow(10, (int)f.putereNeg10));
if (f.semn == 1)
return - unsignVal;
else return unsignVal;
}
int toInteger(myInteger i) {
if (!i.semn)
return ntohl(i.value);
else return - ntohl(i.value);
}
void printPacket(packet* m) {
cout << htonl(m->length) << " ";
printMessage(m);
}
void printMessage(packet* p) {
message* m = &p->m;
cout << inet_ntoa(p->src.sin_addr) << ":" << ntohs(p->src.sin_port) << " - " << m->topic << " - ";
switch (m->tip_pachet) {
case INT:
cout << "INT - " << toInteger(m->value.intreg) << "\n";
break;
case SHORT_REAL:
printf("SHORT_REAL - %.2f\n", ntohs(m->value.real_scurt) / 100.0f);
break;
case FLOAT:
printf("FLOAT - %f\n", toFloat(m->value.real));
break;
case STRING:
cout << "STRING - " << m->value.str << "\n";
break;
default:
cout << (int)m->tip_pachet << "\n";
}
}