-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretrieve.c
187 lines (165 loc) · 5.19 KB
/
retrieve.c
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
#include "client.h"
#include "retrieve.h"
struct Info_for_Client
{
char ip_address[16]; // Assuming IPv4 address
int port;
int flag_for_backup;
};
void storage2(int port1, char *ip1, char **command2, int flag)
{
int storage_socket;
struct sockaddr_in nm_addr;
socklen_t nm_addr_size;
storage_socket = socket(AF_INET, SOCK_STREAM, 0);
if (storage_socket < 0)
{
perror("Error in storage server socket creation");
exit(1);
}
nm_addr.sin_family = AF_INET;
nm_addr.sin_port = htons(port1);
nm_addr.sin_addr.s_addr = inet_addr(ip1);
if (connect(storage_socket, (struct sockaddr *)&nm_addr, sizeof(nm_addr)) < 0)
{
perror("Error in connecting to the storage server");
close(storage_socket);
exit(1);
}
printf("Connected to the storage server\n");
char command3[1024];
strcpy(command3, command2[0]);
if (flag)
{
strcat(command3, " ");
strcat(command3, "./backup");
strcat(command3, command2[1] + 1);
}
else
{
strcat(command3, " ");
strcat(command3, command2[1]);
}
send(storage_socket, command3, strlen(command3), 0);
char buffer[1024]; // Define buffer size according to your needs
size_t totalSize = 0;
off_t fileSize;
mode_t filePermissions;
// Receive file size from the server
if (recv(storage_socket, &fileSize, sizeof(off_t), 0) == -1)
{
perror("Error receiving file size");
return;
}
// Receive file permissions from the server
if (recv(storage_socket, &filePermissions, sizeof(mode_t), 0) == -1)
{
perror("Error receiving file permissions");
return;
}
// Process the received information as needed
printf("Received File Size: %lld\n", (long long)fileSize);
printf("Received File Permissions: %o\n", filePermissions);
struct timeval timeout1;
timeout1.tv_sec = 1;
timeout1.tv_usec = 0;
if (setsockopt(storage_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout1, sizeof(timeout1)) < 0)
{
perror("setsockopt for receive timeout");
close(storage_socket);
}
// Example: Attempt to receive data with a timeout
char buffer1[1024];
ssize_t bytesRead1;
bytesRead1 = recv(storage_socket, buffer1, sizeof(buffer1), 0);
if (bytesRead1 == -1)
{
if (errno == EAGAIN || errno == EWOULDBLOCK)
{
printf("Receive operation timed out.\n");
}
else
{
perror("recv");
}
}
else
{
printf("Received acknowledgment from storage server operation successful: %s\n", buffer1);
}
close(storage_socket);
// Add a NULL pointer at the end to indicate the end of the array
}
void retrieves(char **command)
{
int storage_socket;
struct sockaddr_in nm_addr;
socklen_t nm_addr_size;
storage_socket = socket(AF_INET, SOCK_STREAM, 0);
if (storage_socket < 0)
{
perror("Error in storage server socket creation");
exit(1);
}
nm_addr.sin_family = AF_INET;
nm_addr.sin_port = htons(NM_PORT);
nm_addr.sin_addr.s_addr = inet_addr(NM_SERVER_IP);
if (connect(storage_socket, (struct sockaddr *)&nm_addr, sizeof(nm_addr)) < 0)
{
perror("Error in connecting to the naming server");
close(storage_socket);
exit(1);
}
printf("Connected to the naming server\n");
struct timeval timeout1;
timeout1.tv_sec = 1;
timeout1.tv_usec = 0;
if (setsockopt(storage_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout1, sizeof(timeout1)) < 0)
{
perror("setsockopt for receive timeout");
close(storage_socket);
}
// Example: Attempt to receive data with a timeout
char buffer1[1024];
ssize_t bytesRead1;
bytesRead1 = recv(storage_socket, buffer1, sizeof(buffer1), 0);
if (bytesRead1 == -1)
{
if (errno == EAGAIN || errno == EWOULDBLOCK)
{
printf("Receive operation timed out.\n");
}
else
{
perror("recv");
}
}
else
{
printf("Received acknowledgment from naming server connection successful: %s\n", buffer1);
}
char command3[1024];
strcpy(command3, command[0]);
strcat(command3, " ");
strcat(command3, command[1]);
printf("%s\n", command3);
send(storage_socket, command3, strlen(command3), 0);
struct Info_for_Client info_for_client;
while (1)
{
int bytes_received = recv(storage_socket, &info_for_client, sizeof(info_for_client), 0);
if (bytes_received > 0)
{
// Error or connection closed, break out of the loop
break;
}
}
// Send the Info_for_Client data to the naming server
int port = info_for_client.port;
char *ip = strdup(info_for_client.ip_address);
int flag = info_for_client.flag_for_backup;
storage2(port, ip, command, flag);
char ack[] = "ACK";
send(storage_socket, ack, strlen(ack), 0);
close(storage_socket);
}