void *Relay(void *inarg) { channels_t *ch = (channels_t *)inarg; int term = 0; int id = ch->id; char *item; lpel_stream_desc_t *in, *out; in = LpelStreamOpen(ch->in, 'r'); out = LpelStreamOpen(ch->out, 'w'); printf("Relay %d START\n", id); while (!term) { item = LpelStreamRead( in); assert( item != NULL ); //printf("Relay %d: %s", id, item ); if ( 0 == strcmp( item, "T\n")) { term = 1; } LpelStreamWrite( out, item); } // end while LpelStreamClose( in, 1); LpelStreamClose( out, 0); //free(ch); SCCFreePtr(ch); printf("Relay %d TERM\n", id); return NULL; }
/** * Non-blocking write to a stream * * @param sd stream descriptor * @param item data item (a pointer) to write * @pre current task is single writer * @pre item != NULL * @return 0 if the item could be written, -1 if the stream was full */ int LpelStreamTryWrite( lpel_stream_desc_t *sd, void *item) { if (!LpelBufferIsSpace(&sd->stream->buffer)) { return -1; } LpelStreamWrite( sd, item ); return 0; }
void *Relay(void *inarg) { void *item; char *msg; int i, dest; lpel_stream_desc_t *out[NUM_COLL]; lpel_stream_desc_t *in; int term = 0; printf("start Relay\n" ); /* open streams */ for (i=0; i<NUM_COLL; i++) { out[i] = LpelStreamOpen(scoll[i], 'w'); } in = LpelStreamOpen(sinp, 'r'); /* main task: relay to consumer via defined stream */ while( !term) { item = LpelStreamRead(in); assert( item != NULL ); msg = (char *)item; if ( 0 == strcmp( msg, "T\n" ) ) { term = 1; dest = 0; } else { dest = atoi(msg); } if ( 0<dest && dest<NUM_COLL) { LpelStreamWrite( out[dest], item); printf("Relay dest: %d\n", dest); } } /* terminate msg goes to 0 */ assert( dest==0 ); printf("Relay dest: %d\n", dest); /* relay to all, close streams */ for (i=0; i<NUM_COLL; i++) { LpelStreamWrite( out[i], item); LpelStreamClose( out[i], 0); } LpelStreamClose(in, 1); printf("exit Relay\n" ); return NULL; }
static void *Inputter(void *arg) { lpel_stream_desc_t *out = LpelStreamOpen((lpel_stream_t*)arg, 'w'); char *buf; do { buf = fgets( malloc( 120 * sizeof(char) ), 119, stdin ); LpelStreamWrite( out, buf); } while ( buf[0] != 'T') ; LpelStreamClose( out, 0); printf("exit Inputter\n" ); return NULL; }
static void *Inputter(void *arg) { lpel_stream_desc_t *out = LpelStreamOpen((lpel_stream_t*)arg, 'w'); char *buf; printf("\nInputter START\n\n"); do { //buf = fgets( malloc( 120 * sizeof(char) ), 119, stdin ); buf = fgets( SCCMallocPtr( 120 * sizeof(char) ), 119, stdin ); printf("\nLPEL STREAM WRITE\n\n"); LpelStreamWrite( out, buf); } while ( 0 != strcmp(buf, "T\n") ); LpelStreamClose( out, 0); printf("\nInputter TERM\n\n"); return NULL; }
void SNetStreamWrite(snet_stream_desc_t * sd, void *item) { LpelStreamWrite((lpel_stream_desc_t *) sd, item); }