diff options
Diffstat (limited to 'instr-daemon.c')
-rw-r--r-- | instr-daemon.c | 80 |
1 files changed, 17 insertions, 63 deletions
diff --git a/instr-daemon.c b/instr-daemon.c index 01d83c3..24315f7 100644 --- a/instr-daemon.c +++ b/instr-daemon.c @@ -23,74 +23,28 @@ GAsyncQueue** stdinQueue=NULL; GThread **peers; //actual connected peers -//GIOChannel callback for stdin -static gboolean -stdinAvailable (GIOChannel *source, GIOCondition condition, gpointer data) +void send_message(gchar* message) { - static char* buffer=NULL; - static gint64 allocated=0; - - //temporary buffer to read from stdin - char tmp[STDIN_BUF_SIZE]; - memset(tmp,0,STDIN_BUF_SIZE); - - int size = read(0, tmp, STDIN_BUF_SIZE); - if(size<=0) { - return TRUE; + if(NULL == message) { + return; + } + int count=0; + gssize size = strlen(message); + if(!size) { + return; } - //fix for \n or \r sending - if(size ==1) - if(tmp[0] == '\r' || tmp[0] == '\n') { - return TRUE; - } - - //the termination characters - if(!strncmp(tmp, "..", size-1)) { - //final reallocation of the buffer to accomodate the whole string - buffer=realloc(buffer,allocated+1); - if(buffer==NULL) { - g_printerr("realloc FAILED! Exiting!"); - exit(-1); - } - buffer[allocated] = '\0'; - - g_print_debug("Got the buffer: \"%s\", tid: %p\n", buffer, g_thread_self()); - - if (tmp[0] == '.' && tmp[1] == '.') { //fix for single dot sending - int count=0; - - //send the final buffer to the queue - for(count=0; count<maxConn; count++) { - //allocate on the heap and deallocate in response.c, cbClientOutput() - char *toSend = realloc(NULL, allocated+1); - memcpy(toSend, buffer, allocated+1); - - //make sure we send to only valid peers - if(peers[count] != 0) { - g_async_queue_push(stdinQueue[count], toSend); - } - } + //send the final buffer to the queue + for(count=0; count<maxConn; count++) { + //allocate on the heap and deallocate in response.c, cbClientOutput() + char *toSend = realloc(NULL, size+1); + memcpy(toSend, message, size+1); - //free the buffer - free(buffer); - buffer=NULL; - allocated=0; - return TRUE; + //make sure we send to only valid peers + if(peers[count] != 0) { + g_async_queue_push(stdinQueue[count], toSend); } } - - //allocate on the heap. buffer is NULL the first time - buffer=realloc(buffer, allocated+size); - if(buffer==NULL) { - g_printerr("realloc FAILED! Exiting!"); - exit(-1); - } - memcpy(buffer+allocated, tmp, size); - allocated += size; //keep track of allocation - - - return TRUE; } /** pulls an integer position from the vector which contains the @@ -297,7 +251,7 @@ int main(int argc, char **argv) g_printerr("No io channel\n"); exit(-1); } - g_io_add_watch(stdinChannel, G_IO_IN, stdinAvailable, NULL); + int idx=0; //allocate a maxConn queue on the heap |