-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfone_protocol.c
215 lines (182 loc) · 5.13 KB
/
fone_protocol.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
#include"shared_pipes.h"
#include"fone_serial.c"
static int create_data_pipes(char *name_reader, char *name_writer, char *name);
static void *process_subscriber(void *arg);
static void *process_client(void *arg);
static void *ctl_thread(void *arg);
static int create_pipe(const char *pipe);
static int create_data_pipes(char *name_reader, char *name_writer, char *name) {
/* Create wronly pipe */
snprintf(name_writer, PIPE_NAME_LENGTH, "/tmp/fs2a_");
strncat(name_writer, name, 32);
// can we access though?
if(create_pipe(name_writer) != 0) {
fprintf(stderr, "[Fatal] Can't create data pipe (%s)\n", name_writer);
return 1;
}
/* Create rdonly pipe */
snprintf(name_reader, PIPE_NAME_LENGTH, "/tmp/fa2s_");
strncat(name_reader, name, 32);
// can we access though
if(create_pipe(name_reader) != 0) {
fprintf(stderr, "[Fatal] Can't create data pipe. Exiting ctl thread (%s)\n", name_reader);
return 1;
}
return 0;
}
static void *process_subscriber(void *arg) {
char *name = (char *) arg;
char *name_reader = calloc(PIPE_NAME_LENGTH, sizeof(char));
char *name_writer = calloc(PIPE_NAME_LENGTH, sizeof(char));
char *buf = calloc(PIPE_BUF, sizeof(char));
int r_fd, w_fd;
if(create_data_pipes(name_reader, name_writer, name) != 0) {
return NULL; //TODO
}
w_fd = open(name_writer, O_WRONLY);
if(w_fd < 0) {
fprintf(stderr, "[Fatal] Can't open pipe (%s) fd to process client write: error %d\n", name_writer, w_fd);
return NULL; // TODO
}
r_fd = open(name_reader, O_RDONLY);
if(r_fd < 0) {
fprintf(stderr, "[Fatal] Can't open pipe (%s) fd to process client read: error %d\n", name, r_fd);
return NULL; // TODO
}
while(1) {
memset(buf, 0, PIPE_BUF);
if(read(r_fd, buf, PIPE_BUF) == 0) {
continue;
}
if(strncmp(buf, "finish", 6) == 0) {
subscription_remove_by_fd(w_fd);
close(w_fd);
close(r_fd);
unlink(name_reader);
unlink(name_writer);
break;
} else {
sq_push(buf, w_fd, r_fd);
}
sleep(1);
}
return NULL;
}
static void *process_client(void *arg) {
char *name = (char *) arg;
char *name_reader = calloc(PIPE_NAME_LENGTH, sizeof(char));
char *name_writer = calloc(PIPE_NAME_LENGTH, sizeof(char));
char *buf = calloc(PIPE_BUF, sizeof(char));
int r_fd, w_fd;
if(create_data_pipes(name_reader, name_writer, name) != 0) {
return NULL; //TODO
}
w_fd = open(name_writer, O_WRONLY);
if(w_fd < 0) {
fprintf(stderr, "[Fatal] Can't open pipe (%s) fd to process client write: error %d\n", name_writer, w_fd);
return NULL; // TODO
}
r_fd = open(name_reader, O_RDONLY);
if(r_fd < 0) {
fprintf(stderr, "[Fatal] Can't open pipe (%s) fd to process client read: error %d\n", name, r_fd);
return NULL; // TODO
}
while(1) {
memset(buf, 0, PIPE_BUF);
if(read(r_fd, buf, PIPE_BUF) == 0) {
continue;
}
if(strncmp(buf, "finish", 6) != 0) {
mq_push(buf, w_fd, r_fd);
} else {
mq.state = FINISHED;
mq_cleanup();
close(w_fd);
close(r_fd);
unlink(name_reader);
unlink(name_writer);
break;
}
sleep(1);
}
return NULL;
}
/*
* This is the control loop
*/
static void *ctl_thread(void *arg) {
char *buf = calloc(PIPE_BUF, sizeof(char));
char *random32 = calloc(32, sizeof(char));
int fd, i;
pthread_t client_processor, subscriber_processor;
while(1) {
memset(buf, PIPE_BUF, sizeof(char));
fd = open(fa2s_ctl, O_RDONLY);
if(fd < 0) {
fprintf(stderr, "[Fatal] Can't open fa2s_ctl pipe fd: error %d\n", fd);
break; // TODO
}
read(fd, buf, PIPE_BUF);
close(fd);
/* If not initiated properly, discard */
if(strncmp(buf, "hello", 5) != 0) {
if(strncmp(buf, "subscribe", 9) != 0) {
continue;
}
}
/* Random name for a named pipe */
memset(random32, 32, sizeof(char));
srand(time(NULL));
for(i=0; i<32; i++) {
random32[i] = (char) (26 * (rand() / (RAND_MAX + 1.0))) + 97;
}
if(strncmp(buf, "hello", 5) == 0) {
if(pthread_create(&client_processor, NULL, process_client, (void*)random32)) {
fprintf(stderr, "[Error] creating thread \"%s\"\n", random32);
break; // TODO
}
mq.state = STARTED;
} else if(strncmp(buf, "subscribe", 9) == 0) {
if(pthread_create(&subscriber_processor, NULL, process_subscriber, (void*)random32)) {
fprintf(stderr, "[Error] creating thread \"%s\"\n", random32);
break; // TODO
}
}
fd = open(fs2a_ctl, O_WRONLY);
if(fd < 0) {
fprintf(stderr, "[Fatal] Can't open fs2a_ctl pipe fd: error %d\n", fd);
break; // TODO
}
// TODO: What if client crashes and doesn't read?
write(fd, random32, PIPE_BUF);
close(fd);
/* We do not want to block a subscriber thread */
if(strncmp(buf, "hello", 5) == 0) {
pthread_join(client_processor, NULL);
}
}
return NULL;
}
static int create_pipe(const char *pipe) {
if(access(pipe, F_OK) == -1) {
if(mkfifo(pipe, 0666) != 0) {
fprintf(stderr, "[Error] can't access or create pipe \"%s\"\n", pipe);
return 1;
}
}
return 0;
}
static int create_ctl_pipes() {
pthread_t ctl_thread_t;
if(create_pipe(fa2s_ctl) != 0) {
return 1;
}
if(create_pipe(fs2a_ctl) != 0) {
return 1;
}
if(pthread_create(&ctl_thread_t, NULL, ctl_thread, NULL)) {
fprintf(stderr, "[Error] creating thread for fa2s_ctl\n");
return 1;
}
return 0;
}