| 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 | // check arguments |
|---|
| 162 | if( argc == 0 ) |
|---|
| 163 | { |
|---|
| 164 | printf("\n[chat error] cannot get process argument / pid %x\n", getpid() ); |
|---|
| 165 | exit( 1 ); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | // get process index 0/1 |
|---|
| 169 | is_0 = (strcmp( "0" , argv[0] ) == 0); |
|---|
| 170 | |
|---|
| 171 | // get start cycle |
|---|
| 172 | get_cycle( &start_cycle ); |
|---|
| 173 | |
|---|
| 174 | ////////////////////////// |
|---|
| 175 | if( is_0 ) // P0 process |
|---|
| 176 | { |
|---|
| 177 | printf("\n[chat P0] starts at cycle %d\n", (unsigned int)start_cycle ); |
|---|
| 178 | |
|---|
| 179 | // 1. P0 creates the P0P1 fifo |
|---|
| 180 | error = mkfifo( P0P1_FIFO_PATH, 0 ); |
|---|
| 181 | |
|---|
| 182 | if( error ) |
|---|
| 183 | { |
|---|
| 184 | printf("\n[chat P0 error] cannot create P0P1 fifo\n" ); |
|---|
| 185 | exit( 1 ); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | printf("\n[chat P0] P0P1 fifo created\n"); |
|---|
| 189 | |
|---|
| 190 | // 2. P0 open the read_fdid file descriptor |
|---|
| 191 | write_fdid = open( P0P1_FIFO_PATH, |
|---|
| 192 | O_WRONLY, 0 ); |
|---|
| 193 | |
|---|
| 194 | if( write_fdid < 0 ) |
|---|
| 195 | { |
|---|
| 196 | printf("\n[chat P0 error] cannot get write_fdid for P0P1 fifo\n" ); |
|---|
| 197 | exit( 1 ); |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | printf("\n[chat P0] got write_fdid = %d\n", write_fdid ); |
|---|
| 201 | |
|---|
| 202 | // 3. P0 try to open the read_fdid file descriptor |
|---|
| 203 | while( 1 ) |
|---|
| 204 | { |
|---|
| 205 | read_fdid = open( P1P0_FIFO_PATH, |
|---|
| 206 | O_RDONLY, 0 ); |
|---|
| 207 | |
|---|
| 208 | if( read_fdid > 0 ) break; |
|---|
| 209 | |
|---|
| 210 | pthread_yield(); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | printf("\n[chat P0] got read_fdid = %d\n", read_fdid ); |
|---|
| 214 | |
|---|
| 215 | #if DEBUG_MAIN |
|---|
| 216 | display_fd_array( getpid() ); |
|---|
| 217 | #endif |
|---|
| 218 | // 4. P0 enter the chat loop |
|---|
| 219 | while( 1 ) |
|---|
| 220 | { |
|---|
| 221 | // handle received remote message |
|---|
| 222 | if( chat_send( is_0, write_fdid ) == 0 ) break; |
|---|
| 223 | |
|---|
| 224 | // handle local send message |
|---|
| 225 | if( chat_receive( is_0, read_fdid ) == 0 ) break; |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | // 5. P0 closes file descriptors |
|---|
| 229 | close( write_fdid ); |
|---|
| 230 | close( read_fdid ); |
|---|
| 231 | |
|---|
| 232 | printf("\n[chat P0] closed write_fdid & read_fdid\n"); |
|---|
| 233 | |
|---|
| 234 | // 6. P0 deletes fifo |
|---|
| 235 | unlink( P0P1_FIFO_PATH ); |
|---|
| 236 | |
|---|
| 237 | printf("\n[chat P0] deleted P0P1 fifo\n"); |
|---|
| 238 | } |
|---|
| 239 | ///////////////////////// |
|---|
| 240 | else // P1 process |
|---|
| 241 | { |
|---|
| 242 | printf("\n[chat P1] starts at cycle %d\n", (unsigned int)start_cycle ); |
|---|
| 243 | |
|---|
| 244 | // 1. P1 creates the P1P0 fifo |
|---|
| 245 | error = mkfifo( P1P0_FIFO_PATH, 0 ); |
|---|
| 246 | |
|---|
| 247 | if( error ) |
|---|
| 248 | { |
|---|
| 249 | printf("\n[chat P1 error] cannot create P1P0 fifo\n"); |
|---|
| 250 | exit( 1 ); |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | printf("\n[chat P1] P1P0 fifo created\n"); |
|---|
| 254 | |
|---|
| 255 | // 2. P1 open the read_fdid file descriptor |
|---|
| 256 | write_fdid = open( P1P0_FIFO_PATH, |
|---|
| 257 | O_WRONLY, 0 ); |
|---|
| 258 | |
|---|
| 259 | if( write_fdid < 0 ) |
|---|
| 260 | { |
|---|
| 261 | printf("\n[chat P1 error] cannot get write_fdid for P1P0 fifo\n" ); |
|---|
| 262 | exit( 1 ); |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | printf("\n[chat P1] got write_fdid = %d\n", write_fdid ); |
|---|
| 266 | |
|---|
| 267 | // 3. P1 try to open the read_fdid file descriptor |
|---|
| 268 | while( 1 ) |
|---|
| 269 | { |
|---|
| 270 | read_fdid = open( P0P1_FIFO_PATH, |
|---|
| 271 | O_RDONLY, 0 ); |
|---|
| 272 | |
|---|
| 273 | if( read_fdid > 0 ) break; |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | printf("\n[chat P1] got read_fdid = %d\n", read_fdid ); |
|---|
| 277 | |
|---|
| 278 | #if DEBUG_MAIN |
|---|
| 279 | display_fd_array( getpid() ); |
|---|
| 280 | #endif |
|---|
| 281 | |
|---|
| 282 | // 4. P1 enter the chat loop |
|---|
| 283 | while( 1 ) |
|---|
| 284 | { |
|---|
| 285 | // handle received remote message |
|---|
| 286 | if( chat_receive( is_0, read_fdid ) == 0 ) break; |
|---|
| 287 | |
|---|
| 288 | // handle local send message |
|---|
| 289 | if( chat_send( is_0, write_fdid ) == 0 ) break; |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | // 5. P1 closes file descriptors |
|---|
| 293 | close( write_fdid ); |
|---|
| 294 | close( read_fdid ); |
|---|
| 295 | |
|---|
| 296 | printf("\n[chat P1] closed write_fdid & read_fdid\n"); |
|---|
| 297 | |
|---|
| 298 | // 6. P1 deletes fifo |
|---|
| 299 | unlink( P1P0_FIFO_PATH ); |
|---|
| 300 | |
|---|
| 301 | printf("\n[chat P1] deleted P1P0 fifo\n"); |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | exit( 0 ); |
|---|
| 305 | |
|---|
| 306 | return 0; |
|---|
| 307 | |
|---|
| 308 | } // end main() |
|---|