Repository navigation
Expand file tree
/
Copy pathworker.cpp
More file actions
145 lines (122 loc) · 3.68 KB
/
Copy pathworker.cpp
File metadata and controls
145 lines (122 loc) · 3.68 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <unordered_set>
#include <map>
#include <arpa/inet.h>
#include <sys/epoll.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <chrono>
#include <vector>
#include <cassert>
#include <algorithm>
#include <cmath>
#include "task.h"
using namespace std;
double func(double x) {
return -3 * x * x + sin(x) * x - 14;
}
double calcOnInterval(double l, double r, double step) {
double result = 0;
while (l < r) {
double realR = min(r, l + step);
double segmentLength = realR - l;
result += func(l + segmentLength / 2) * segmentLength;
l += step;
}
return result;
}
const size_t BUFFER_SIZE = 1024;
const size_t UDP_PORT = 3456;
const size_t TCP_PORT = 3457;
string masterAddress;
int tcpSock;
int udpSock;
struct sockaddr_in tcpAddr;
struct sockaddr_in udpAddr;
void GetBroadcast(unsigned short broadcastPort) {
int sockFd;
struct sockaddr_in addr;
socklen_t addr_len = sizeof(addr);
char buffer[BUFFER_SIZE];
ssize_t numBytesReceived;
if ((sockFd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket");
exit(1);
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(broadcastPort);
if (bind(sockFd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
perror("bind");
close(sockFd);
exit(1);
}
cerr << "Listening for broadcast messages on port " << broadcastPort << "..." << endl;
while (true) {
numBytesReceived = recvfrom(sockFd, buffer, BUFFER_SIZE - 1, 0, (struct sockaddr*)&addr, &addr_len);
if (numBytesReceived < 0) {
perror("recvfrom");
continue;
}
buffer[numBytesReceived] = '\0';
string message(buffer);
char senderIp[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &addr.sin_addr, senderIp, INET_ADDRSTRLEN);
cout << senderIp << " broadcasted message: " << message << endl;
if (message == "Broadcast to calculate the integral") {
masterAddress = senderIp;
break;
}
}
close(sockFd);
}
void SetupTcp() {
tcpSock = socket(AF_INET, SOCK_STREAM, 0);
if (tcpSock < 0) {
perror("TCP socket creation failed");
exit(1);
}
tcpAddr.sin_family = AF_INET;
tcpAddr.sin_port = htons(TCP_PORT);
inet_pton(AF_INET, masterAddress.data(), &tcpAddr.sin_addr);
if (bind(tcpSock, (struct sockaddr*)&tcpAddr, sizeof(tcpAddr)) < 0) {
perror("bind");
close(tcpSock);
exit(1);
}
if (listen(tcpSock, 1) < 0) {
perror("listen");
close(tcpSock);
exit(1);
}
int flags = fcntl(tcpSock, F_GETFL, 0);
if (flags == -1) {
perror("fcntl");
exit(1);
}
fcntl(tcpSock, F_SETFL, flags | O_NONBLOCK);
}
void SetupUdp() {
udpSock = socket(AF_INET, SOCK_DGRAM, 0);
if (udpSock < 0) {
perror("socket");
exit(1);
}
udpAddr.sin_family = AF_INET;
udpAddr.sin_port = htons(UDP_PORT);
inet_pton(AF_INET, masterAddress.data(), &udpAddr.sin_addr);
}
int main() {
GetBroadcast(4567);
// сначала сделать SetupTcp и начать принимать соединения, чтобы не пропустить подключение.
SetupUdp();
while (true) {
// прочитать из tcp буфера, если что-то пришло то начать выполнять задачу с фьючой.
// если прошло кратное таймауту время, отправить heartbeat по UDP.
}
}