-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather.cpp
More file actions
223 lines (175 loc) · 7.65 KB
/
weather.cpp
File metadata and controls
223 lines (175 loc) · 7.65 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <zlib.h>
#include <thread>
#include <chrono>
#include <sstream>
#include <random>
#include <unordered_map>
#pragma comment(lib, "Ws2_32.lib") // Link with Ws2_32.lib
#define SERVER_PORT 5000
#define BUFFER_SIZE 1024
using namespace std;
unordered_map<int, int> ack_count;
int last_acked_id = -1;
int cwnd = 1; // Congestion window (in packets)
int ssthresh = 32; // Slow start threshold
string compress_data(const string& data) {
int compression_level = Z_BEST_COMPRESSION; // Default to highest compression
if (data.size() < 100) {
// cout<<"NO compression"<<"\n";
compression_level = Z_NO_COMPRESSION; // No compression for small data
} else if (data.size() < 150) {
compression_level = Z_BEST_SPEED; // Prioritize speed for medium-sized data
}
string compressed(BUFFER_SIZE, '\0');
z_stream strm = {0};
strm.total_in = strm.avail_in = data.size();
strm.total_out = strm.avail_out = BUFFER_SIZE;
strm.next_in = (Bytef *)data.data();
strm.next_out = (Bytef *)compressed.data();
if (deflateInit(&strm, compression_level) != Z_OK) {
throw runtime_error("zlib: Failed to initialize compression.");
}
deflate(&strm, Z_FINISH);
deflateEnd(&strm);
compressed.resize(strm.total_out);
return compressed;
}
string generate_weather_data(int client_id) {
stringstream ss;
ss << "Client " << client_id << ": ";
ss << "Temperature: " << 275 + client_id % 10 << " K, "
<< "Humidity: " << 50 + (client_id % 20) << "%, "
<< "Air Pressure: " << 1000 + (client_id % 15) << " hPa";
return ss.str();
}
// TCP Reno congestion control
void tcp_reno_congestion_control(bool ack_received) {
if (ack_received) {
if (cwnd < ssthresh) {
// Slow start phase: exponential growth
cwnd *= 2;
} else {
// Congestion avoidance phase: linear growth
cwnd += 1;
}
} else {
// Congestion detected: reduce cwnd and enter congestion avoidance
ssthresh = max(cwnd / 2, 2);
cwnd = 1;
}
}
void send_weather_data(SOCKET server_socket, int client_id) {
int packet_id = 0; // Initialize the packet ID
while (true) {
bool flag=false;
for(int i=0;i<cwnd;i++){
// Generate and compress weather data
string weather_data = generate_weather_data(client_id);
string compressed_data = compress_data(weather_data);
// Add packet ID and cwnd to the compressed data
string packet_with_id_and_cwnd = to_string(packet_id) + "|" + to_string(cwnd) + "|"+to_string(ssthresh) +"|"+compressed_data;
// Simulate network transmission based on bandwidth and congestion window
size_t data_to_send = min(packet_with_id_and_cwnd.size(), static_cast<size_t>(cwnd * 1024)); // Simulate packet size using cwnd
if (send(server_socket, packet_with_id_and_cwnd.c_str(), data_to_send, 0) == SOCKET_ERROR) {
cerr << "Client " << client_id << " failed to send data." << endl;
} else {
fd_set read_fds;
FD_ZERO(&read_fds);
FD_SET(server_socket, &read_fds);
struct timeval timeout;
timeout.tv_sec = 1;
timeout.tv_usec = 0; // 0 microseconds
int select_result = select(server_socket + 1, &read_fds, NULL, NULL, &timeout);
if (select_result > 0 && FD_ISSET(server_socket, &read_fds)) {
// Server response received
char ack_buffer[10]; // Buffer for receiving ACK (with ID)
int ack_received = recv(server_socket, ack_buffer, sizeof(ack_buffer), 0);
if (ack_received > 0) {
int ack_id = stoi(string(ack_buffer, ack_received));
cout<<ack_id <<" "<<packet_id<<endl;
// Handle ACK reception
if (ack_id == packet_id) {
// Normal ACK received
ack_count[ack_id] = 0; // Reset the duplicate ACK count for this packet ID
last_acked_id = ack_id;
//cout << "ACK received for packet " << ack_id << ". Increasing cwnd.\n";
} else if (ack_id == last_acked_id) {
// Duplicate ACK received
ack_count[ack_id] += 1;
cout << "Duplicate ACK received for packet " << ack_id << ". Count: " << ack_count[ack_id] << "\n";
if (ack_count[ack_id] == 3) {
// Triple duplicate ACKs: trigger congestion control
cerr << "Triple duplicate ACKs for packet " << ack_id << ". Reducing cwnd.\n";
tcp_reno_congestion_control(false); // Trigger congestion control, reduce cwnd
packet_id=ack_id;
flag=true;
break;
}
}
} else {
// No ACK received, simulate congestion and reduce congestion window
cerr << "No ACK received. Reducing cwnd.\n";
}
} else if (select_result == 0) {
// Timeout occurred
cerr << "Timeout occurred ."+to_string(packet_id+1)<<endl;
} else {
// Select failed, treat as an error
cerr << "Error occurred during select().\n";
// tcp_reno_congestion_control(false); // Handle as congestion
}
}
// Move to the next packet
packet_id++;
size_t sleep_duration = static_cast<size_t>((data_to_send ) ); // Milliseconds
this_thread::sleep_for(chrono::milliseconds(sleep_duration > 0 ? sleep_duration : 1));
}
// cout<<" cwnd:" <<cwnd<<endl;
if(!flag)
{
cout<<"doubled cw"<<endl;
tcp_reno_congestion_control(true); // Trigger congestion control, reduce cwnd
}
cout<<" cwnd:" <<cwnd<<endl;
}
}
int main(int argc, char* argv[]) {
if (argc != 2) {
cerr << "Usage: " << argv[0] << " <client_id> " << endl;
return 1;
}
int client_id = stoi(argv[1]);
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
cerr << "WSAStartup failed with error: " << iResult << endl;
return 1;
}
SOCKET server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == INVALID_SOCKET) {
cerr << "Socket creation error.\n";
WSACleanup();
return 1;
}
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(SERVER_PORT);
inet_pton(AF_INET, "127.0.0.1", &server_address.sin_addr);
// Connect to the server
if (connect(server_socket, (struct sockaddr*)&server_address, sizeof(server_address)) == SOCKET_ERROR) {
cerr << "Connection failed.\n";
closesocket(server_socket);
WSACleanup();
return 1;
}
cout << "Client " << client_id << " connected to the server\n";
// Send weather data continuously with network adaptation and dynamic data size
send_weather_data(server_socket, client_id);
// Cleanup
closesocket(server_socket);
WSACleanup();
return 0;
}