-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.c
323 lines (278 loc) · 7.87 KB
/
server.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <stdio.h>
#include <string.h> //strlen
#include <stdlib.h>
#include <errno.h>
#include <unistd.h> //close
#include <arpa/inet.h> //close
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h> //FD_SET, FD_ISSET, FD_ZERO macros
#define TRUE 1
#define FALSE 0
#define PORT 8888
int fpga_read_temp()
{
return 50;
}
// Acquisition if faked by decrementing a counter whenever acq status is checked.
int acq_countdown = 0;
void fpga_start_acquire()
{
printf("Start acq\n");
acq_countdown = 200;
return;
}
int fpga_check_acquire_state()
{
printf("Check acq %d\n", acq_countdown);
acq_countdown--;
if (acq_countdown == 0)
return 1;
else
return 0;
}
void fpga_stop_acquire()
{
acq_countdown = 0;
}
int main(int argc , char *argv[])
{
int opt = TRUE;
int master_socket, addrlen, new_socket, client_socket[3], max_clients = 3, activity, i, valread, sd;
// Store the socket of the client which started the acquisition
int acq_socket_no = -1;
int ret;
int max_sd;
// timeout of 0 for select()
struct timeval timeout = { .tv_sec = 0, .tv_usec = 0 };
struct sockaddr_in address;
int temp;
char buffer[1025]; //data buffer of 1K
//set of socket descriptors
fd_set readfds;
////a message
//char *message = "ECHO Daemon v1.0 \r\n";
//initialise all client_socket[] to 0 so not checked
for (i = 0; i < max_clients; i++)
{
client_socket[i] = 0;
}
//create a master socket
if( (master_socket = socket(AF_INET , SOCK_STREAM , 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
//set master socket to allow multiple connections ,
//this is just a good habit, it will work without this
if( setsockopt(master_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt,
sizeof(opt)) < 0 )
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
//type of socket created
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );
//bind the socket to localhost port 8888
if (bind(master_socket, (struct sockaddr *)&address, sizeof(address))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
printf("Listener on port %d \n", PORT);
//try to specify maximum of 3 pending connections for the master socket
if (listen(master_socket, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
//accept the incoming connection
addrlen = sizeof(address);
puts("Waiting for connections ...");
while(TRUE)
{
//clear the socket set
FD_ZERO(&readfds);
//add master socket to set
FD_SET(master_socket, &readfds);
max_sd = master_socket;
//add child sockets to set
for ( i = 0 ; i < max_clients ; i++)
{
//socket descriptor
sd = client_socket[i];
//if valid socket descriptor then add to read list
if(sd > 0)
FD_SET( sd , &readfds);
//highest file descriptor number, need it for the select function
if(sd > max_sd)
max_sd = sd;
}
// Check for any activity on any sockets. Timeout is set to 0, so select won't block
// For a blocking select() set timeout parameter to NULL
activity = select( max_sd + 1 , &readfds , NULL , NULL , &timeout);
if ((activity < 0) && (errno!=EINTR))
{
printf("select error");
}
//If something happened on the master socket ,
//then its an incoming connection
if (FD_ISSET(master_socket, &readfds))
{
printf("Activity on master socket\n");
if ((new_socket = accept(master_socket,
(struct sockaddr *)&address, (socklen_t*)&addrlen))<0)
{
perror("accept");
exit(EXIT_FAILURE);
}
//inform user of socket number - used in send and receive commands
printf("New connection , socket fd is %d , ip is : %s , port : %d\n" , new_socket , inet_ntoa(address.sin_addr) , ntohs
(address.sin_port));
/* //send new connection greeting message
if( send(new_socket, message, strlen(message), 0) != strlen(message) )
{
perror("send");
}
puts("Welcome message sent successfully"); */
//add new socket to array of sockets
for (i = 0; i < max_clients; i++)
{
//if position is empty
if( client_socket[i] == 0 )
{
client_socket[i] = new_socket;
printf("Adding to list of sockets as %d\n" , i);
break;
}
}
if (i==max_clients)
{
// No free sockets
printf("No free sockets left, closing connection\n");
close(new_socket);
}
}
//else its some IO operation on some other socket
for (i = 0; i < max_clients; i++)
{
sd = client_socket[i];
if (FD_ISSET( sd , &readfds))
{
printf("Activity on socket no %d: %d\n", i, sd);
//Check if it was for closing , and also read the
//incoming message
if ((valread = read( sd , buffer, 1024)) == 0)
{
printf("No data recevied\n");
//Somebody disconnected , get his details and print
getpeername(sd , (struct sockaddr*)&address , (socklen_t*)&addrlen);
printf("Host disconnected , ip %s , port %d \n" ,
inet_ntoa(address.sin_addr) , ntohs(address.sin_port));
// If acquisition client has closed the connection, stop acquisition
if (acq_socket_no == i)
{
acq_socket_no = -1;
fpga_stop_acquire();
}
//Close the socket and mark as 0 in list for reuse
close( sd );
client_socket[i] = 0;
}
// Handle message input
else
{
printf("Data received\n");
// Command parsing
switch(buffer[0])
{
case 'a' :
// Start acquire
// Check if acquire has been started by another client already
if (acq_socket_no == -1)
{
fpga_start_acquire();
acq_socket_no = i;
}
else
{
// If acquisition has been started by another client, just close connection
// a message like "Acquisition already started" would be nice
close(sd);
client_socket[i] = 0;
}
break;
case 't' :
// Send temperature and close connection afterwards
temp = fpga_read_temp();
sprintf(buffer, "FPGA Temperature: %d °C\n", temp);
send(sd, buffer, strlen(buffer), 0);
close(sd);
client_socket[i] = 0;
break;
case 's' :
// Stop acquire
// First check if an acquisition is running
if (acq_socket_no > -1)
{
// Stop it and close connection to the acq client (stop blocking)
fpga_stop_acquire();
close(client_socket[acq_socket_no]);
client_socket[acq_socket_no] = 0;
acq_socket_no = -1;
}
// Close connection to the 's' sending client
close(sd);
client_socket[i] = 0;
break;
case 'x' :
// Check status and close connection
if (acq_socket_no > -1)
sprintf(buffer, "Acquisition running\n");
else
sprintf(buffer, "No acquisition running\n");
send(sd, buffer, strlen(buffer), 0);
close(sd);
client_socket[i] = 0;
break;
default:
// Invalid command
// A message like "invalid command" would be nice
if (i == acq_socket_no)
{
fpga_stop_acquire();
acq_socket_no = -1;
}
close(sd);
client_socket[i] = 0;
break;
}
//set the string terminating NULL byte on the end
//of the data read
//buffer[valread] = '\0';
//send(sd , buffer , strlen(buffer) , 0 );
}
}
}
// Check for acquisition status on every loop
if (acq_socket_no > -1)
{
ret = fpga_check_acquire_state();
if (ret > 0)
{
// If acquisition done send return value to client and close connection
send(client_socket[acq_socket_no], &ret, sizeof(ret), 0);
close(client_socket[acq_socket_no]);
client_socket[acq_socket_no] = 0;
acq_socket_no = -1;
}
}
// should be done nicer
// Just wait a little bit
usleep(100*1000);
}
return 0;
}