diff options
-rw-r--r-- | instr-client.c.orig | 143 | ||||
-rw-r--r-- | instr-daemon.c.orig | 273 | ||||
-rw-r--r-- | response.c.orig | 109 | ||||
-rw-r--r-- | response.h.orig | 18 | ||||
-rw-r--r-- | signalobject.c.orig | 38 | ||||
-rw-r--r-- | signalobject.h.orig | 44 |
6 files changed, 0 insertions, 625 deletions
diff --git a/instr-client.c.orig b/instr-client.c.orig deleted file mode 100644 index d88f654..0000000 --- a/instr-client.c.orig +++ /dev/null @@ -1,143 +0,0 @@ -#include <stdio.h> -#include <gio/gio.h> -#include <string.h> -#include "response.h" -#include <stdlib.h> -#include <strings.h> - -static int port = 3333; -#define BUFSIZE 1024 - -GMainContext *context=NULL; -GMainLoop *mainLoop=NULL; -GSocketConnection * connection = NULL; -GSocketClient * client = NULL; - -char exitTokens[5][7] = {"quit", "exit", "logout", "logoff", "local"}; - -//helper function to nicely terminate the client -static void terminate(char *message, int exitCode) -{ - g_object_unref(client); - g_object_unref(connection); - if (message) { - g_print("%s\n", message); - } - exit(exitCode); -} - -//a sort of strncmp but with \n or \r exception -static gboolean Matches(char *src, char* dst, gssize size) -{ - for(; *src != '\0', (size--)>0; src++) { - if(g_ascii_toupper(*src) != g_ascii_toupper(*dst)) { - return FALSE; - } - dst++; - } - for(; *dst != '\0'; dst++) - if (*dst != '\r' && *dst != '\n') { - return FALSE; - } - - return TRUE; -} - - -//GIOChannel callback -static gboolean -stdinAvailable (GIOChannel *source, GIOCondition condition, gpointer data) -{ - char tmp[BUFSIZE]; - memset(tmp,0,BUFSIZE); - int size = read(0, tmp, BUFSIZE); - if(size<=0) { - return TRUE; - } - - //test if we have an exit word - int idx=0; - for(idx=0; idx<sizeof(exitTokens)/sizeof(exitTokens[0]); idx++) { - if(Matches(exitTokens[idx], tmp, strlen(exitTokens[idx]))) { - g_socket_close(g_socket_connection_get_socket(connection), NULL); - terminate("Goodbye", 0); - } - } - - //write the message - gssize written=0; - GPollableOutputStream* out = (GPollableOutputStream*)data; - written = g_pollable_output_stream_write_nonblocking(out, tmp, size, NULL, NULL); - if (written <=0 || written == G_IO_ERROR_WOULD_BLOCK) { - return FALSE; - } - - return TRUE; -} - -//callback for server input -static gboolean cbServerInput(gpointer data, gpointer additional) -{ - char buffer[BUFSIZE]; - gssize size=0; - memset(buffer,0,BUFSIZE); - - GPollableInputStream* inStream = (GPollableInputStream*)data; - - size=g_pollable_input_stream_read_nonblocking(inStream, buffer, BUFSIZE, NULL, NULL); - if(size <=0 || size == G_IO_ERROR_WOULD_BLOCK) { - terminate("Connection to server lost", -1); - } - g_print("\n%s", buffer); - g_print("> "); - return TRUE; -} - -int main(int argc, char** argv) -{ - /* create a new connection */ - g_type_init (); - - GOutputStream *out; - GInputStream *in; - GError* error = NULL; - GIOChannel* stdinChannel = NULL; - client = g_socket_client_new(); - GSource *source = NULL; - - /* connect to the host */ - connection = g_socket_client_connect_to_host (client, - (gchar*)"localhost", - port, - NULL, - &error); - - if (error != NULL) { - g_print("Could not connect to server on port %d\n", port); - g_object_unref(client); - return -1; - } - g_print("> "); - - - //register Pollable sources - out = g_io_stream_get_output_stream (G_IO_STREAM (connection)); - in = g_io_stream_get_input_stream (G_IO_STREAM (connection)); - GSource *outSource = NULL; - GSource *inSource = g_pollable_input_stream_create_source((struct GPollableInputStream*)in, NULL); - outSource = g_pollable_output_stream_create_source((struct GPollableOutputStream*)out, NULL); - - //register stdin channel - stdinChannel = g_io_channel_unix_new(0); - if(stdinChannel == NULL) { - terminate("Could not open STDIN channel", -2); - } - - //attach the sources - g_io_add_watch(stdinChannel, G_IO_IN, stdinAvailable, (struct GPollableOutputStream*)out); - g_source_set_callback(inSource, cbServerInput, (GPollableOutputStream*)in, NULL); - - g_source_attach(inSource, NULL); - g_main_loop_run (g_main_loop_new (NULL, FALSE)); - return 0; -}
\ No newline at end of file diff --git a/instr-daemon.c.orig b/instr-daemon.c.orig deleted file mode 100644 index 0f07d16..0000000 --- a/instr-daemon.c.orig +++ /dev/null @@ -1,273 +0,0 @@ -#include "socket-common.h" -#include "response.h" -#include <stdlib.h> -#include <ctype.h> - -#define STDIN_BUF_SIZE 1024 - -int port=3333; //port to listen -int connections=0; -int maxConn=16; //max connections - 16 - -guint signalMyCb; //signal to register which is used in cbClientInput(), step 10 from requirements -GAsyncQueue** stdinQueue=NULL; -GThread **peers; //actual connected peers - - -//GIOChannel callback for stdin -static gboolean -stdinAvailable (GIOChannel *source, GIOCondition condition, gpointer data) -{ - static char* buffer=NULL; - static gint64 allocated=0; - GAsyncQueue* queue=NULL; - - //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; - - //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); - buffer[allocated] = '\0'; - int idx=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); - } - - //free the buffer - free(buffer); - buffer=NULL; - allocated=0; - return TRUE; - } - } - - //allocate on the heap. buffer is NULL the first time - buffer=realloc(buffer, allocated+size); - memcpy(buffer+allocated, tmp, size); - allocated += size; //keep track of allocation - - - return TRUE; -} - -/** pulls an integer position from the vector which contains the - position to use for the stdinQueue vector - @param id - the thread id -*/ -static int pullIndex(GThread* id) -{ - int i=0,ret=-1; - static GStaticMutex mutex = G_STATIC_MUTEX_INIT; - g_static_mutex_lock (&mutex); - for(i=0; i<maxConn; i++) - if(peers[i] == id) - { - peers[i] = (GThread*)0; - ret=i; - break; - } - g_static_mutex_unlock (&mutex); - return ret; -} - -/** - * pushed in the peers vector the thread id for future reference - * @param id - */ -static int pushIndex(GThread* id) -{ - int i=0,ret=-1; - static GStaticMutex mutex = G_STATIC_MUTEX_INIT; - g_static_mutex_lock (&mutex); - for(i=0; i<maxConn; i++) - if(peers[i] == 0) - { - peers[i] = id; - ret=i; - break; - } - - g_static_mutex_unlock (&mutex); - return ret; -} - -//handler for incoming connection, closes the connection if maxCon connections are already handled -static gboolean -incomingConnection (GSocketService *service, - GSocketConnection *connection, - GSocketListener *listener, - gpointer user_data) -{ - if(connections +1 > maxConn) - { - g_print_debug("Connection closed. Max reached\n"); - return TRUE; - } - connections++; - g_print_debug("Incoming connection\n"); - return FALSE; -} - - -//thread connection handler -static gboolean -handler (GThreadedSocketService *service, - GSocketConnection *connection, - GSocketListener *listener, - gpointer user_data) -{ - - - GOutputStream *out; - GInputStream *in; - gssize size; - - - out = g_io_stream_get_output_stream (G_IO_STREAM (connection)); - in = g_io_stream_get_input_stream (G_IO_STREAM (connection)); - - g_print_debug("Handling, connections: %d\n", connections); - - //register ourselves in the peers vector, use the index obtained in the stdinQueue - //should not get -1 - GThread* self = g_thread_self(); - int index=pushIndex(self); - - GSource *outSource = NULL; - GSource *inSource = g_pollable_input_stream_create_source((struct GPollableInputStream*)in, NULL); - - //get a reference to the async queue - GAsyncQueue *queue = g_async_queue_ref(stdinQueue[index]); - - //input from client - g_source_set_callback(inSource, cbClientInput, (GPollableOutputStream*)out, NULL); - g_source_attach(inSource, NULL); - - //keep thread alive, every 1000 microseconds - while(g_source_is_destroyed(inSource)==FALSE) - { - g_usleep(1000); - gint elems; - - //verify our queue length and activate/deactivate the "out" Source for this connection - //if we don't do this, the out Source will be scheduled frequently and will busy loop - if((elems=g_async_queue_length(queue))>0) - { - if(outSource == NULL) - { - - outSource = g_pollable_output_stream_create_source((struct GPollableOutputStream*)out, NULL); - g_source_set_callback(outSource, cbClientOutput, queue, NULL); - g_source_attach(outSource, NULL); - } - else - { - g_source_destroy(outSource); - g_source_unref(outSource); - outSource = NULL; - } - - } - else - { - if(outSource!= NULL && !g_source_is_destroyed(outSource)) - { - g_print_debug("Destroy source\n"); - g_source_destroy(outSource); - g_source_unref(outSource); - outSource = NULL; //added - } - } - //end of activate/deactivate - } - - //reached the end of the thread - if (g_output_stream_close(out, NULL, NULL) == FALSE) - g_print_debug("out not closed\n"); - if (g_input_stream_close(in, NULL, NULL) == FALSE) - g_print_debug("in not closed\n"); - - g_print_debug("Thread end\n"); - connections--; //keep track of connections - g_async_queue_unref(queue); //unreference the queue - pullIndex(g_thread_self()); //unregister from the peers vector - return TRUE; -} - -int main(int argc, char **argv) -{ - GSocketService *service = NULL; - GError *error = NULL; - GIOChannel* stdinChannel = NULL; - - g_type_init (); - - //register stdin channel - stdinChannel = g_io_channel_unix_new(0); - if(stdinChannel == NULL) { 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 - stdinQueue = malloc(sizeof(struct GAsyncQueue*) * maxConn); - //allocate peers vector on the heap - peers = malloc(sizeof(GThread*) * maxConn); - for(idx=0;idx<maxConn;idx++) - { - peers[idx] = 0; - stdinQueue[idx] = g_async_queue_new(); - } - - //create a threaded service - service = g_threaded_socket_service_new (maxConn+1); - if (!g_socket_listener_add_inet_port (G_SOCKET_LISTENER (service), - port, - NULL, - &error)) - { - g_printerr ("%s: %s\n", argv[0], error->message); - free(stdinQueue); - free(peers); - return 1; - } - - //init the signal signalMyCb - initSignals(&signalMyCb); - - g_print_debug("Server listening on port %d\n", port); - g_signal_connect (service, "run", G_CALLBACK (handler), NULL); - g_signal_connect (service, "incoming", G_CALLBACK(incomingConnection), NULL); - - g_main_loop_run (g_main_loop_new (NULL, FALSE)); - free(stdinQueue); - free(peers); - - return 0; - -} diff --git a/response.c.orig b/response.c.orig deleted file mode 100644 index 93d38b3..0000000 --- a/response.c.orig +++ /dev/null @@ -1,109 +0,0 @@ -#include "response.h" -#include "socket-common.h" -#include <string.h> - -//RESPONSE related functions from the server - -extern guint signalMyCb; //signal id -extern GAsyncQueue* stdinQueue; //our queue -SignalObject* signalObject; //the signal object which registered the signal id - -//write output to client -static gssize writeOutput(GPollableOutputStream* stream, gchar* data, gssize size); - -//callback for client output, this is where the queue messages are sent to the client -gboolean cbClientOutput(gpointer data, gpointer additional) -{ - GPollableOutputStream* outStream = (GPollableOutputStream*)data; - GAsyncQueue* queue = (GAsyncQueue*)additional; - if(g_async_queue_length(queue) <= 0) return FALSE; - - gpointer elem; - g_print_debug("Try pop\n"); - //try to pop an element from the queue - elem=g_async_queue_try_pop(queue); - g_print_debug("after pop\n"); - if(elem) - { - char* buf = (char*)elem; - g_print_debug("Extracted %s\n", buf); - gssize size = strlen(buf); - gssize written = 0; - - written=g_pollable_output_stream_write_nonblocking(outStream, buf, size+1, NULL, NULL); - if(written==-1) g_printerr("Could not write to client\n"); - - //free the alloc'ed memory - free(buf); - } - - return FALSE; -} - -//the client input callback -gboolean cbClientInput(gpointer data, gpointer additional) -{ - - char buffer[1024]; - gssize size=0; - memset(buffer,0,1024); - - GPollableInputStream* inStream = (GPollableInputStream*)data; - GPollableOutputStream* outStream = (GPollableOutputStream*)additional; - - size=g_pollable_input_stream_read_nonblocking(inStream, buffer, 1024, NULL, NULL); - if(size <=0 || size == G_IO_ERROR_WOULD_BLOCK) { g_print_debug("Got: %d\n", size); return FALSE; } - - //emit a signal which calls the echoCb function - SignalObjectClass myStruct; - myStruct.instance = NULL; - myStruct.cb = writeOutput; - myStruct.data = buffer; - myStruct.size = size; - myStruct.inStream = inStream; - myStruct.outStream = outStream; - g_signal_emit(signalObject, signalMyCb, 0, &myStruct, NULL); - - return TRUE; -} - -//write output to client -static gssize writeOutput(GPollableOutputStream* stream, gchar* data, gssize size) -{ - gssize what=0; - what=g_pollable_output_stream_write_nonblocking(stream, data, size, NULL, NULL); - if(what < 0 || what == G_IO_ERROR_WOULD_BLOCK) { return -1; } - return what; -} - -//initialize the signals -void initSignals(guint *signal) -{ - *signal = g_signal_new("runEchoService", SIGNAL_OBJECT_TYPE, G_SIGNAL_RUN_FIRST, 0, NULL, NULL, - g_cclosure_marshal_VOID__POINTER , G_TYPE_NONE, 1, G_TYPE_POINTER); - - signalObject = g_object_new(SIGNAL_OBJECT_TYPE, NULL); - g_signal_connect (signalObject, "runEchoService", G_CALLBACK (echoCb), NULL); -} - -//echo 3 times and send to client one time -void echoCb(gpointer instance, GObject *arg1, gpointer user_data) -{ - static GStaticMutex mutex = G_STATIC_MUTEX_INIT; - g_static_mutex_lock (&mutex); - SignalObjectClass *data = (SignalObjectClass*)arg1; - GPollableOutputStream* stream = (GPollableOutputStream*)data->outStream; - gchar* str = data->data; - gssize len = data->size; - gssize written = 0; - - gchar* upper = g_ascii_strup(str, len); - int i; - for(i=0; i<3; i++) g_print("%s\n", upper); - //send response back to client - - written = data->cb(stream, upper, len); - - g_free(upper); - g_static_mutex_unlock (&mutex); -} diff --git a/response.h.orig b/response.h.orig deleted file mode 100644 index 7b6301a..0000000 --- a/response.h.orig +++ /dev/null @@ -1,18 +0,0 @@ -#include <gio/gio.h> -#include <glibconfig.h> -#include <glib.h> -#include "signalobject.h" - - -typedef struct _streamStruct -{ - GPollableInputStream* in; - GPollableOutputStream* out; -} streamStruct; - -void echoCb(gpointer instance, GObject *arg1, gpointer user_data); - -gboolean cbClientInput(gpointer data, gpointer additional); -gboolean cbClientOutput(gpointer data, gpointer additional); -void initSignals(guint *signal); -void echoCb(gpointer instance, GObject *arg1, gpointer user_data);
\ No newline at end of file diff --git a/signalobject.c.orig b/signalobject.c.orig deleted file mode 100644 index 68003fe..0000000 --- a/signalobject.c.orig +++ /dev/null @@ -1,38 +0,0 @@ -#include "signalobject.h" - -G_DEFINE_TYPE (SignalObject, signal_object, G_TYPE_OBJECT); - -static GObject * -signal_object_constructor (GType gtype, - guint n_properties, - GObjectConstructParam *properties) -{ - GObject *obj; - - { - /* Always chain up to the parent constructor */ - obj = G_OBJECT_CLASS (signal_object_parent_class)->constructor (gtype, n_properties, properties); - } - - /* update the object state depending on constructor properties */ - - return obj; -} - - -static void -signal_object_class_init (SignalObjectClass *klass) -{ - GObjectClass *gobject_class = G_OBJECT_CLASS (klass); - - gobject_class->constructor = signal_object_constructor; -} - - -static void -signal_object_init (SignalObject *self) -{ - /* initialize the object */ -} - - diff --git a/signalobject.h.orig b/signalobject.h.orig deleted file mode 100644 index 8863086..0000000 --- a/signalobject.h.orig +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef SIGNAL_OBJECT_H -#define SIGNAL_OBJECT_H - -#include <gio/gio.h> -#include <glibconfig.h> -#include <glib.h> - - -#define SIGNAL_OBJECT_TYPE (signal_object_get_type ()) -/* -#define MAMAN_BAR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MAMAN_TYPE_BAR, MamanBar)) -#define MAMAN_IS_BAR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MAMAN_TYPE_BAR)) -#define MAMAN_BAR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MAMAN_TYPE_BAR, MamanBarClass)) -#define MAMAN_IS_BAR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MAMAN_TYPE_BAR)) -#define MAMAN_BAR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MAMAN_TYPE_BAR, MamanBarClass)) -*/ - -typedef struct _SignalObject SignalObject; -typedef struct _SignalObjectClass SignalObjectClass; - - -struct _SignalObject -{ - GObject parentInstance; - gpointer instance; - gssize (*cb)(GPollableOutputStream* stream, gchar* data, gssize size); - GPollableInputStream* inStream; - GPollableOutputStream* outStream; - gchar* data; - gssize size; -}; - -struct _SignalObjectClass -{ - GObjectClass parent_class; - gpointer instance; - gssize (*cb)(GPollableOutputStream* stream, gchar* data, gssize size); - GPollableInputStream* inStream; - GPollableOutputStream* outStream; - gchar* data; - gssize size; -}; - -#endif |