1 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
2 | // file : chat.c |
---|
3 | // author : Alain Greiner |
---|
4 | // date : november 2020 |
---|
5 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
6 | // This file describes a single thread, fifo based, chat application. |
---|
7 | // |
---|
8 | // The chat[0] process is supposed to communicate with another chat[1] process through |
---|
9 | // two fifos, identified by the P0P1_FIFO_PATH & P1P0_fifo_PATH arguments. |
---|
10 | // |
---|
11 | // The behaviour depends on the process argument (argc/argv mechanism) : |
---|
12 | // - when this argument is 0 the process is P0 (write P0P1 fifo / read P1P0 fifo). |
---|
13 | // - when this argument is non-zero, the process is P1 (write P1P0 fifo / read P0P1 fifo). |
---|
14 | // |
---|
15 | // - For a P0 process : |
---|
16 | // 1) it creates the P0P1 fifo. |
---|
17 | // 2) it open a write-only file descriptor on this P0P1 fifo. |
---|
18 | // 3) in a waiting loop it open a read-only file descriptor on the P1P0 fifo. |
---|
19 | // 4) it enters the chat loop, starting by a send(). |
---|
20 | // |
---|
21 | // - For a P1 process : |
---|
22 | // 1) it creates the P1P0 fifo. |
---|
23 | // 2) it open a write-only file descriptor on this P1P0 fifo. |
---|
24 | // 3) in a waiting loop, it open a read-only file descriptor on the P0P1 fifo. |
---|
25 | // 4) it enters the chat loop, starting by a receive(). |
---|
26 | // |
---|
27 | // Both sides can stop the communication and exit by sending an "exit" string. |
---|
28 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
29 | |
---|
30 | #include <stdio.h> |
---|
31 | #include <stdlib.h> |
---|
32 | #include <string.h> |
---|
33 | #include <almosmkh.h> |
---|
34 | #include <unistd.h> |
---|
35 | #include <fcntl.h> |
---|
36 | #include <sys/stat.h> |
---|
37 | |
---|
38 | |
---|
39 | #define P0P1_FIFO_PATH "/misc/chat_p0p1_fifo" |
---|
40 | #define P1P0_FIFO_PATH "/misc/chat_p1p0_fifo" |
---|
41 | |
---|
42 | #define CHAT_BUF_SIZE 256 |
---|
43 | |
---|
44 | #define DEBUG_MAIN 0 |
---|
45 | |
---|
46 | /////////////////////////////////////////////////////////////////// |
---|
47 | // - This function send the string captured on STDIN to the fifo |
---|
48 | // and return the number of characters sent if success. |
---|
49 | // - It send an empty string (first character == 0), |
---|
50 | // and return 0 when the captured string is "exit". |
---|
51 | /////////////////////////////////////////////////////////////////// |
---|
52 | int chat_send( int is_0, |
---|
53 | int fdid ) |
---|
54 | { |
---|
55 | char buffer[CHAT_BUF_SIZE]; // string buffer for one message |
---|
56 | int size; // string length (including NUL) |
---|
57 | int nbytes; // number of characters actually sent |
---|
58 | |
---|
59 | // display local prompt |
---|
60 | printf("\n[local] " ); |
---|
61 | |
---|
62 | // build local message |
---|
63 | size = get_string( buffer , CHAT_BUF_SIZE ); |
---|
64 | |
---|
65 | // send local message |
---|
66 | nbytes = write( fdid , buffer , size ); |
---|
67 | |
---|
68 | if( nbytes != size ) |
---|
69 | { |
---|
70 | printf("\n[chat P%d error] cannot send\n", is_0 ); |
---|
71 | // return exit |
---|
72 | return 0; |
---|
73 | } |
---|
74 | |
---|
75 | // handle "exit" string |
---|
76 | if( strncmp( "exit" , buffer , 4 ) == 0 ) |
---|
77 | { |
---|
78 | printf("\n[chat P%d] local message is <exit> => quit\n", is_0 ); |
---|
79 | // return exit |
---|
80 | return 0; |
---|
81 | } |
---|
82 | |
---|
83 | // return success |
---|
84 | return nbytes; |
---|
85 | |
---|
86 | } // end chat_send() |
---|
87 | |
---|
88 | /////////////////////////////////////////////////////////////////////// |
---|
89 | // - This function copies the string received from the fifo to STDOUT, |
---|
90 | // and return the number of characters received if success. |
---|
91 | // - It returns 0 when the received string is empty, too long, |
---|
92 | // or when an EOF signals end of communication. |
---|
93 | /////////////////////////////////////////////////////////////////////// |
---|
94 | int chat_receive( int is_0, |
---|
95 | int fdid ) |
---|
96 | { |
---|
97 | char buffer[CHAT_BUF_SIZE]; // string buffer for one message |
---|
98 | int nbytes; // number of characters received in one read |
---|
99 | |
---|
100 | // initialize loop variables |
---|
101 | int space = CHAT_BUF_SIZE; // number of empty slots in buffer |
---|
102 | int received = 0; // total number of received bytes |
---|
103 | char * buf = &buffer[0]; |
---|
104 | |
---|
105 | // display remote prompt |
---|
106 | printf("\n[remote] "); |
---|
107 | |
---|
108 | // get remote message |
---|
109 | while( 1 ) |
---|
110 | { |
---|
111 | // try to read as many bytes as available space in buffer |
---|
112 | nbytes = read( fdid , buf , space ); |
---|
113 | |
---|
114 | // analyse number of bytes received |
---|
115 | if( (nbytes < 0) || ((nbytes + received) >= CHAT_BUF_SIZE) ) // error |
---|
116 | { |
---|
117 | printf("\n\n[chat P%d error] received %d bytes\n", is_0 , received + nbytes ); |
---|
118 | // return exit |
---|
119 | return 0; |
---|
120 | } |
---|
121 | else if( buffer[nbytes - 1] != 0 ) // uncompleted string => new read |
---|
122 | { |
---|
123 | buf = buf + nbytes; |
---|
124 | space = space + nbytes; |
---|
125 | received = received + nbytes; |
---|
126 | } |
---|
127 | else // NUL terminated string => exit loop |
---|
128 | { |
---|
129 | received = received + nbytes; |
---|
130 | break; |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | // display remote message |
---|
135 | printf("%s\n", buffer ); |
---|
136 | |
---|
137 | // handle "exit" string |
---|
138 | if( strncmp( "exit" , buffer , 4 ) == 0 ) // handle "exit" string |
---|
139 | { |
---|
140 | printf("\n[chat P%d] remote message is <exit> => quit \n", is_0 ); |
---|
141 | // return exit |
---|
142 | return 0; |
---|
143 | } |
---|
144 | |
---|
145 | // return success |
---|
146 | return received; |
---|
147 | |
---|
148 | } // end chat_receive() |
---|
149 | |
---|
150 | |
---|
151 | |
---|
152 | /////////////////////////////////// |
---|
153 | int main( int argc , char ** argv ) |
---|
154 | { |
---|
155 | int error; |
---|
156 | int is_0; // P0 process when zero / P1 process otherwise |
---|
157 | int read_fdid; // file descriptor index for read fifo |
---|
158 | int write_fdid; // file descriptor index for read fifo |
---|
159 | unsigned long long start_cycle; |
---|
160 | |
---|
161 | // get start cycle |
---|
162 | get_cycle( &start_cycle ); |
---|
163 | |
---|
164 | // check arguments |
---|
165 | if( argc != 1 || argv[0] == NULL ) |
---|
166 | { |
---|
167 | printf("\n[chat error] : pid %x / argc %d / argv %x / argv[0] %x / cycle %d\n", |
---|
168 | getpid() , argc , argv , argv[0] , (unsigned int)start_cycle ); |
---|
169 | |
---|
170 | exit( -1 ); |
---|
171 | } |
---|
172 | |
---|
173 | // get process index 0/1 |
---|
174 | is_0 = (atoi( argv[0] ) == 0); |
---|
175 | |
---|
176 | ////////////////////////// |
---|
177 | if( is_0 ) // P0 process |
---|
178 | { |
---|
179 | printf("\n[chat P0] starts at cycle %d\n", (unsigned int)start_cycle ); |
---|
180 | |
---|
181 | // 1. P0 creates the P0P1 fifo |
---|
182 | error = mkfifo( P0P1_FIFO_PATH, 0 ); |
---|
183 | |
---|
184 | if( error ) |
---|
185 | { |
---|
186 | printf("\n[chat P0 error] cannot create P0P1 fifo\n" ); |
---|
187 | exit( 1 ); |
---|
188 | } |
---|
189 | |
---|
190 | printf("\n[chat P0] P0P1 fifo created\n"); |
---|
191 | |
---|
192 | // 2. P0 open the read_fdid file descriptor |
---|
193 | write_fdid = open( P0P1_FIFO_PATH, |
---|
194 | O_WRONLY, 0 ); |
---|
195 | |
---|
196 | if( write_fdid < 0 ) |
---|
197 | { |
---|
198 | printf("\n[chat P0 error] cannot get write_fdid for P0P1 fifo\n" ); |
---|
199 | exit( 1 ); |
---|
200 | } |
---|
201 | |
---|
202 | printf("\n[chat P0] got write_fdid = %d\n", write_fdid ); |
---|
203 | |
---|
204 | // 3. P0 try to open the read_fdid file descriptor |
---|
205 | while( 1 ) |
---|
206 | { |
---|
207 | read_fdid = open( P1P0_FIFO_PATH, |
---|
208 | O_RDONLY, 0 ); |
---|
209 | |
---|
210 | if( read_fdid > 0 ) break; |
---|
211 | |
---|
212 | pthread_yield(); |
---|
213 | } |
---|
214 | |
---|
215 | printf("\n[chat P0] got read_fdid = %d\n", read_fdid ); |
---|
216 | |
---|
217 | #if DEBUG_MAIN |
---|
218 | display_fd_array( getpid() ); |
---|
219 | #endif |
---|
220 | // 4. P0 enter the chat loop |
---|
221 | while( 1 ) |
---|
222 | { |
---|
223 | // handle received remote message |
---|
224 | if( chat_send( is_0, write_fdid ) == 0 ) break; |
---|
225 | |
---|
226 | // handle local send message |
---|
227 | if( chat_receive( is_0, read_fdid ) == 0 ) break; |
---|
228 | } |
---|
229 | |
---|
230 | // 5. P0 closes file descriptors |
---|
231 | close( write_fdid ); |
---|
232 | close( read_fdid ); |
---|
233 | |
---|
234 | printf("\n[chat P0] closed write_fdid & read_fdid\n"); |
---|
235 | |
---|
236 | // 6. P0 deletes fifo |
---|
237 | unlink( P0P1_FIFO_PATH ); |
---|
238 | |
---|
239 | printf("\n[chat P0] deleted P0P1 fifo\n"); |
---|
240 | } |
---|
241 | ///////////////////////// |
---|
242 | else // P1 process |
---|
243 | { |
---|
244 | printf("\n[chat P1] starts at cycle %d\n", (unsigned int)start_cycle ); |
---|
245 | |
---|
246 | // 1. P1 creates the P1P0 fifo |
---|
247 | error = mkfifo( P1P0_FIFO_PATH, 0 ); |
---|
248 | |
---|
249 | if( error ) |
---|
250 | { |
---|
251 | printf("\n[chat P1 error] cannot create P1P0 fifo\n"); |
---|
252 | exit( 1 ); |
---|
253 | } |
---|
254 | |
---|
255 | printf("\n[chat P1] P1P0 fifo created\n"); |
---|
256 | |
---|
257 | // 2. P1 open the read_fdid file descriptor |
---|
258 | write_fdid = open( P1P0_FIFO_PATH, |
---|
259 | O_WRONLY, 0 ); |
---|
260 | |
---|
261 | if( write_fdid < 0 ) |
---|
262 | { |
---|
263 | printf("\n[chat P1 error] cannot get write_fdid for P1P0 fifo\n" ); |
---|
264 | exit( 1 ); |
---|
265 | } |
---|
266 | |
---|
267 | printf("\n[chat P1] got write_fdid = %d\n", write_fdid ); |
---|
268 | |
---|
269 | // 3. P1 try to open the read_fdid file descriptor |
---|
270 | while( 1 ) |
---|
271 | { |
---|
272 | read_fdid = open( P0P1_FIFO_PATH, |
---|
273 | O_RDONLY, 0 ); |
---|
274 | |
---|
275 | if( read_fdid > 0 ) break; |
---|
276 | } |
---|
277 | |
---|
278 | printf("\n[chat P1] got read_fdid = %d\n", read_fdid ); |
---|
279 | |
---|
280 | #if DEBUG_MAIN |
---|
281 | display_fd_array( getpid() ); |
---|
282 | #endif |
---|
283 | |
---|
284 | // 4. P1 enter the chat loop |
---|
285 | while( 1 ) |
---|
286 | { |
---|
287 | // handle received remote message |
---|
288 | if( chat_receive( is_0, read_fdid ) == 0 ) break; |
---|
289 | |
---|
290 | // handle local send message |
---|
291 | if( chat_send( is_0, write_fdid ) == 0 ) break; |
---|
292 | } |
---|
293 | |
---|
294 | // 5. P1 closes file descriptors |
---|
295 | close( write_fdid ); |
---|
296 | close( read_fdid ); |
---|
297 | |
---|
298 | printf("\n[chat P1] closed write_fdid & read_fdid\n"); |
---|
299 | |
---|
300 | // 6. P1 deletes fifo |
---|
301 | unlink( P1P0_FIFO_PATH ); |
---|
302 | |
---|
303 | printf("\n[chat P1] deleted P1P0 fifo\n"); |
---|
304 | } |
---|
305 | |
---|
306 | exit( 0 ); |
---|
307 | |
---|
308 | return 0; |
---|
309 | |
---|
310 | } // end main() |
---|