-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcentral_server.c
More file actions
319 lines (268 loc) · 6.25 KB
/
central_server.c
File metadata and controls
319 lines (268 loc) · 6.25 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
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
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <signal.h>
#include <semaphore.h>
sem_t * sem_id;
/// DEFS ///
#define BUFLEN 512
#define PORT 9930
#define SHMSZ 1024
/// END OF DEFS ///
/// UTIL ///
/**
* @Name str_split
* @Param char* a_str , const char a_delim
* @Return char** result
*/
char** str_split(char* a_str, const char a_delim)
{
char** result = 0;
size_t count = 0;
char* tmp = a_str;
char* last_comma = 0;
char delim[2];
delim[0] = a_delim;
delim[1] = 0;
/* Count how many elements will be extracted. */
while (*tmp)
{
if (a_delim == *tmp)
{
count++;
last_comma = tmp;
}
tmp++;
}
/* Add space for trailing token. */
count += last_comma < (a_str + strlen(a_str) - 1);
/* Add space for terminating null string so caller
knows where the list of returned strings ends. */
count++;
result = malloc(sizeof(char*) * count);
if (result)
{
size_t idx = 0;
char* token = strtok(a_str, delim);
while (token)
{
assert(idx < count);
*(result + idx++) = strdup(token);
token = strtok(0, delim);
}
assert(idx == count - 1);
*(result + idx) = 0;
}
return result;
}
void signal_callback_handler(int signum)
{
/**
* Semaphore unlink: Remove a named semaphore from the system.
*/
if ( shm_unlink("/mysem") < 0 )
{
perror("shm_unlink");
}
/**
* Semaphore Close: Close a named semaphore
*/
if ( sem_close(sem_id) < 0 )
{
perror("sem_close");
}
/**
* Semaphore unlink: Remove a named semaphore from the system.
*/
if ( sem_unlink("/mysem") < 0 )
{
perror("sem_unlink");
}
// Terminate program
exit(signum);
}
/**
* @Name diep
* @Param char* s
*/
void diep(char *s)
{
perror(s);
exit(1);
}
/// END OF UTIL ///
/// STRUCT ////
struct Sensor
{
char* ip;
char* port;
char* label;
char* actions[3];
};
/**
* @Name newSensor
* @Return Sensor: mySensor
*/
struct Sensor *newSensor()
{
struct Sensor *mySensor = malloc(sizeof(struct Sensor));
assert(mySensor != NULL);
return mySensor;
}
/**
* @Name createSensor
* @Param char* msg
* @Return Sensor : mySensor
*/
struct Sensor *createSensor(char * msg)
{
struct Sensor *mySensor = malloc(sizeof(struct Sensor));
assert(mySensor != NULL);
char** tokens;
tokens = str_split(msg, '#');
if (tokens)
{
mySensor->label = *(tokens +0);
mySensor->actions[0] = *(tokens +1);
mySensor->actions[1] = *(tokens +2);
mySensor->actions[2] = *(tokens +3);
mySensor->ip = *(tokens +4);
mySensor->port = *(tokens +5);
free(tokens);
}
return mySensor;
}
/// END OF STRUCT ///
/// BEGIN OF MAIN ///
int main()
{
/// VARS FOR SOCKET ///
struct sockaddr_in si_me, si_other;
int sock, i, slen=sizeof(si_other);
char buf[BUFLEN];
char sen[20];
/// END VARS FOR SOCKET ///
/// VARS FOR SHARED MEMORY ///
int shmid;
key_t key;
char *shm, *s;
key = 5678;
/// END OF VARS FOR SHARED MEMORY ///
/// RES VARS FOR SHARED MEMORY ///
int shmid2;
key_t key2;
char *shm2, *s2;
key2 = 1234;
/// RES ŖEND OF VARS FOR SHARED MEMORY ///
signal(SIGINT, signal_callback_handler);
/**
* Semaphore open
*/
sem_id=sem_open("/mysem", O_CREAT, S_IRUSR | S_IWUSR, 1);
/// SOCKET PREP ///
if ((sock=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
diep("socket");
memset((char *) &si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(PORT);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sock,(struct sockaddr *) &si_me, sizeof(si_me))==-1)
diep("bind");
/// END SOCKET PREP ///
/// SHARED MEMORY PREP ///
/*
* Create the segment.
*/
if ((shmid = shmget(key, 27, IPC_CREAT | 0666)) < 0)
{
perror("shmget");
exit(1);
}
/*
* Now we attach the segment to our data space.
*/
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
{
perror("shmat");
exit(1);
}
/*
* Now put some things into the memory for the
* other process to read.
*/
s = shm;
/// END OF SHARED MEMORY PREP ///
/// (2) SHARED MEMORY PREP ///
if ((shmid2 = shmget(key2, 27, IPC_CREAT | 0666)) < 0)
{
perror("shmget");
exit(1);
}
if ((shm2 = shmat(shmid2, NULL, 0)) == (char *) -1)
{
perror("shmat");
exit(1);
}
s2 = shm2;
///(2) END OF SHARED MEMORY PREP ///
/// MAIN LOOP ///
while(1)
{
listen(sock,5);
/// RECIVE A MESSAGE FROM SENSORS ///
if (recvfrom(sock, buf, BUFLEN, 0,(struct sockaddr *) &si_other, &slen)==-1)
{
printf("error");
diep("recvfrom()");
}
else
{
/// add port to data ///
if(isdigit(buf[0]))
{ sleep(2);
printf("Waiting \n");
sem_wait(sem_id);
printf("Locked, About to sleep \n");
strcpy( s2, buf);
sem_post(sem_id);
printf("posting \n");
printf("RES %s \n",s2);
}else{
sleep(2);
printf("Waiting \n");
sem_wait(sem_id);
printf("Locked, About to sleep \n");
strcpy( s, buf);
sem_post(sem_id);
printf("posting \n");
printf("FINAL %s \n",s);
}
sleep(1);
//scanf( "%s" , sen );
if (sendto(sock, "1", BUFLEN, 0,(struct sockaddr *) &si_other, slen)==-1)
{
diep("sendto()");
}
else
{
printf("msg sent \n");
}
/// End add port to data ///
}
}
return 0;
}