From 1f2b9999b906452c42e9e68ed19e0817de8d2ea7 Mon Sep 17 00:00:00 2001 From: Tamas TEVESZ Date: Wed, 24 Mar 2010 01:51:07 +0100 Subject: [PATCH] Remove network support from WINGs - nobody used it Amend network stuff removal still more wings network removals [crmafra: squashed together three patches] --- WINGs/Documentation/README.connection | 51 -- WINGs/Examples/Makefile.am | 6 +- WINGs/Examples/connect.c | 172 ------ WINGs/Examples/server.c | 661 ----------------------- WINGs/Makefile.am | 2 - WINGs/WINGs/WUtil.h | 168 ------ WINGs/connection.c | 958 ---------------------------------- WINGs/host.c | 265 ---------- WINGs/po/Makefile.am | 2 - configure.ac | 28 +- 10 files changed, 5 insertions(+), 2308 deletions(-) delete mode 100644 WINGs/Documentation/README.connection delete mode 100644 WINGs/Examples/connect.c delete mode 100644 WINGs/Examples/server.c delete mode 100644 WINGs/connection.c delete mode 100644 WINGs/host.c diff --git a/WINGs/Documentation/README.connection b/WINGs/Documentation/README.connection deleted file mode 100644 index be4c200b..00000000 --- a/WINGs/Documentation/README.connection +++ /dev/null @@ -1,51 +0,0 @@ -Methods of handling WMConnectionDidDieNotification notification events -(same for WMConnectionDidTimeoutNotification) ----------------------------------------------------------------------- - -Once your program got this notification (you need to install an observer for -it), there are some ways to handle it: - -1. Make your observer enqueue a new notification in the ASAP queue, and the - observer for this new notification (it can be the same function if you - arrange to distinguish between the two cases), should remove the connection. - You can also close the connection before enqueuing the new notification to - the ASAP queue, but is not strictly necessarily, since it will be closed - when the observer for the new enqueued notification will be called and you - will call the close/remove function there. This is just to make sure your - connection will be silent, and won't generate new events until you reach - that point. - This is by far the best method, since it will assure you that if you - enqueue more than one notification to remove the same connection, they will - be coalesced, and called only once. - -2. In your observer, put the died/closed connection in an array or bag, and - destroy all the connections present in the array/bag, in your main loop, - after you call the WHandleEvents()/WMHandleEvent(). Also closing the - connection can be done before putting the connection in the array/bag, but - is optional as noted above. In this case you need to make sure you don't - put in the array/bag the same connection more than once, in case the - DieNotification is sent more that once to you. This is automagically solved - by method 1. - -3. In case it's your only connection, and you plan to exit if it was closed or - died, then you can safely close/remove it, and exit. As long as you no - longer access it, there is no problem. - -4. Make you observer remove the connection. Then make sure that after that - point your code no longer tries to access that connection (this usually - means until your code gets back to the main loop). This is almost always - very hard to achive and subject to hidden errors. I do not recommend this - way of handling the died notification. It is ugly and very complicated to - handle if the program is in a very deeply nested function when it finds out - that the connection died. If you use it and get plenty of SIGSEGVs then you - know why. This method was not presented here to be used, but to show what - should be avoided in dealing with the died notification, in case someone - gets the idea to try it this way. - - -Note: read/write operations means to use our read/write functions (like - WMGetMessage()/WMSendMessage()), not the C library ones read()/write(). -Note2: removing a connection is done by WMDestroyConnection(), while - WMCloseConnection() only closes the socket, and removed any pending - queues and timers on the connection. - diff --git a/WINGs/Examples/Makefile.am b/WINGs/Examples/Makefile.am index 2c91bb9f..bfd4b8dd 100644 --- a/WINGs/Examples/Makefile.am +++ b/WINGs/Examples/Makefile.am @@ -18,13 +18,11 @@ puzzle_DEPENDENCIES = $(top_builddir)/WINGs/libWINGs.la connect_DEPENDENCIES = $(top_builddir)/WINGs/libWUtil.la -connect_LDADD = $(top_builddir)/WINGs/libWUtil.la @LIBRARY_SEARCH_PATH@ \ - @NETLIBS@ @INTLIBS@ +connect_LDADD = $(top_builddir)/WINGs/libWUtil.la @LIBRARY_SEARCH_PATH@ @INTLIBS@ server_DEPENDENCIES = $(top_builddir)/WINGs/libWUtil.la -server_LDADD = $(top_builddir)/WINGs/libWUtil.la @LIBRARY_SEARCH_PATH@ \ - @NETLIBS@ @INTLIBS@ +server_LDADD = $(top_builddir)/WINGs/libWUtil.la @LIBRARY_SEARCH_PATH@ @INTLIBS@ INCLUDES = -I$(top_srcdir)/WINGs -I$(top_srcdir)/wrlib -I$(top_srcdir)/src \ diff --git a/WINGs/Examples/connect.c b/WINGs/Examples/connect.c deleted file mode 100644 index 9b4b93a4..00000000 --- a/WINGs/Examples/connect.c +++ /dev/null @@ -1,172 +0,0 @@ -/* - * WINGs connect.c: example how to create a network client using WMConnection - * - * Copyright (c) 1999-2003 Dan Pascu - * - */ - -#include -#include -#include -#include - -#include - -static int initialized = 0; - -static void didReceiveInput(ConnectionDelegate * self, WMConnection * cPtr); - -static void connectionDidDie(ConnectionDelegate * self, WMConnection * cPtr); - -static void didInitialize(ConnectionDelegate * self, WMConnection * cPtr); - -static ConnectionDelegate socketDelegate = { - NULL, /* data */ - NULL, /* canResumeSending */ - NULL, /* didCatchException */ - connectionDidDie, /* didDie */ - didInitialize, /* didInitialize */ - didReceiveInput, /* didReceiveInput */ - NULL /* didTimeout */ -}; - -void wAbort(Bool foo) -{ - exit(1); -} - -static char *getMessage(WMConnection * cPtr) -{ - char *buffer; - WMData *aData; - int length; - - aData = WMGetConnectionAvailableData(cPtr); - if (!aData) - return NULL; - if ((length = WMGetDataLength(aData)) == 0) { - WMReleaseData(aData); - return NULL; - } - - buffer = (char *)wmalloc(length + 1); - WMGetDataBytes(aData, buffer); - buffer[length] = '\0'; - WMReleaseData(aData); - - return buffer; -} - -static void inputHandler(int fd, int mask, void *clientData) -{ - WMConnection *cPtr = (WMConnection *) clientData; - WMData *aData; - char buf[4096]; - int n; - - if (!initialized) - return; - - n = read(fd, buf, 4096); - if (n > 0) { - aData = WMCreateDataWithBytes(buf, n); - WMSendConnectionData(cPtr, aData); - WMReleaseData(aData); - } -} - -static void didReceiveInput(ConnectionDelegate * self, WMConnection * cPtr) -{ - char *buffer; - - buffer = getMessage(cPtr); - if (!buffer) { - fprintf(stderr, "Connection closed by peer.\n"); - exit(0); - } - - printf("%s", buffer); - - wfree(buffer); -} - -static void connectionDidDie(ConnectionDelegate * self, WMConnection * cPtr) -{ - WMCloseConnection(cPtr); - - fprintf(stderr, "Connection closed by peer.\n"); - exit(0); -} - -static void didInitialize(ConnectionDelegate * self, WMConnection * cPtr) -{ - int state = WMGetConnectionState(cPtr); - WMHost *host; - - if (state == WCConnected) { - host = WMGetHostWithAddress(WMGetConnectionAddress(cPtr)); - fprintf(stderr, "connected to '%s:%s'\n", - host ? WMGetHostName(host) : WMGetConnectionAddress(cPtr), WMGetConnectionService(cPtr)); - initialized = 1; - if (host) - WMReleaseHost(host); - return; - } else { - wsyserrorwithcode(WCErrorCode, "Unable to connect"); - exit(1); - } -} - -int main(int argc, char **argv) -{ - char *ProgName, *host, *port; - int i; - WMConnection *sPtr; - - wsetabort(wAbort); - - WMInitializeApplication("connect", &argc, argv); - - ProgName = strrchr(argv[0], '/'); - if (!ProgName) - ProgName = argv[0]; - else - ProgName++; - - host = NULL; - port = "34567"; - - if (argc > 1) { - for (i = 1; i < argc; i++) { - if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) { - printf("usage: %s [host [port]]\n\n", ProgName); - exit(0); - } else { - if (!host) - host = argv[i]; - else - port = argv[i]; - } - } - } - - printf("Trying to make connection to '%s:%s'\n", host ? host : "localhost", port); - - sPtr = WMCreateConnectionToAddressAndNotify(host, port, NULL); - if (!sPtr) { - wfatal("could not create connection. exiting"); - exit(1); - } - - WMSetConnectionDelegate(sPtr, &socketDelegate); - - /* watch what user types and send it over the connection */ - WMAddInputHandler(0, WIReadMask, inputHandler, sPtr); - - while (1) { - WHandleEvents(); - } - - return 0; - -} diff --git a/WINGs/Examples/server.c b/WINGs/Examples/server.c deleted file mode 100644 index e747a1f5..00000000 --- a/WINGs/Examples/server.c +++ /dev/null @@ -1,661 +0,0 @@ -/* - * WINGs server.c: example how to create a network server using WMConnection - * - * Copyright (c) 2001-2003 Dan Pascu - * - */ - -#include -#include -#include -#include - -#include - -#define _(P) P -#define MAXCMD_SIZE 512 - -char *SEConnectionShouldBeRemovedNotification = "SEConnectionShouldBeRemovedNotification"; - -static void didReceiveInput(ConnectionDelegate * self, WMConnection * cPtr); - -static void connectionDidDie(ConnectionDelegate * self, WMConnection * cPtr); - -static void connectionDidTimeout(ConnectionDelegate * self, WMConnection * cPtr); - -extern char *SEConnectionShouldBeRemovedNotification; - -static WMUserDefaults *timeDB = NULL; -static char *ServerAddress = NULL; -static char *ServerPort = NULL; -static WMArray *allowedHostList = NULL; -static WMArray *clientConnections = NULL; -static WMConnection *serverPtr = NULL; - -static ConnectionDelegate socketDelegate = { - NULL, /* client data */ - NULL, /* canResumeSending */ - NULL, /* didCatchException */ - connectionDidDie, /* didDie */ - NULL, /* didInitialize */ - didReceiveInput, /* didReceiveInput */ - connectionDidTimeout /* didTimeout */ -}; - -void wAbort(Bool foo) -{ - exit(1); -} - -static void printHelp(char *progname) -{ - printf(_("usage: %s [options]\n\n"), progname); - puts(_(" --help print this message")); - puts(_(" --listen [address:]port only listen on the specified address/port")); - puts(_(" --allow host1[,host2...] only allow connections from listed hosts\n")); - puts(_(" By default server listens on all interfaces and port 34567, unless" - " something\nelse is specified with the --listen option. If address is" - " omitted or the keyword\n'Any' is used, it will listen on all interfaces else" - " only on the specified one.\n\nFor example --listen localhost: will" - " listen on the default port 34567, but only\non connections comming" - " in through the loopback interface.\n\n Also by default the server" - " listens to incoming connections from any host,\nunless a list of" - " hosts is given with the --allow option, in which case it will\nreject" - " connections not comming from those hosts.\nThe list of hosts is comma" - " separated and should NOT contain ANY spaces.")); -} - -static void enqueueConnectionForRemoval(WMConnection * cPtr) -{ - WMNotification *notif; - - /*don't release notif here. it will be released by queue after processing */ - notif = WMCreateNotification(SEConnectionShouldBeRemovedNotification, cPtr, NULL); - WMEnqueueNotification(WMGetDefaultNotificationQueue(), notif, WMPostASAP); -} - -static int sendMessage(WMConnection * cPtr, char *message) -{ - WMData *aData; - int res; - - if (WMGetConnectionState(cPtr) != WCConnected) - return -1; - - aData = WMCreateDataWithBytes(message, strlen(message)); - res = WMSendConnectionData(cPtr, aData); - WMReleaseData(aData); - - return res; -} - -static Bool enqueueMessage(WMConnection * cPtr, char *message) -{ - WMData *aData; - Bool res; - - if (WMGetConnectionState(cPtr) != WCConnected) - return False; - - aData = WMCreateDataWithBytes(message, strlen(message)); - res = WMEnqueueConnectionData(cPtr, aData); - WMReleaseData(aData); - - return res; -} - -static char *findDelimiter(char *data, const char *endPtr) -{ - wassertrv(data < endPtr, NULL); - - while (data < endPtr && *data != '\n' && *data != '\r' && *data != ';' && *data != '\0') - data++; - - if (data < endPtr) - return data; - - return NULL; -} - -static WMArray *getAvailableMessages(WMConnection * cPtr) -{ - char *ptr, *crtPos, *buffer; - const char *bytes, *endPtr; - WMData *aData, *receivedData, *holdData; - WMRange range; - WMArray *messages; - int length; - - receivedData = WMGetConnectionAvailableData(cPtr); - if (!receivedData) - return NULL; - if ((length = WMGetDataLength(receivedData)) == 0) { - WMReleaseData(receivedData); - return NULL; - } - - holdData = (WMData *) WMGetConnectionClientData(cPtr); - if (holdData) { - WMAppendData(holdData, receivedData); - WMReleaseData(receivedData); - WMSetConnectionClientData(cPtr, NULL); - aData = holdData; - } else { - aData = receivedData; - } - - length = WMGetDataLength(aData); - bytes = (char *)WMDataBytes(aData); - endPtr = bytes + length; - - messages = WMCreateArrayWithDestructor(1, wfree); - crtPos = (char *)bytes; - while (crtPos < endPtr && (ptr = findDelimiter(crtPos, endPtr)) != NULL) { - range.position = (crtPos - bytes); - range.count = (ptr - crtPos); - if (range.count > MAXCMD_SIZE) { - /* Hmmm... The message is too long. Possibly that someone is - * flooding us, or there is a dumb client which do not know - * who is talking to. */ - sendMessage(cPtr, "Command too long\n\r"); - WMFreeArray(messages); - WMReleaseData(aData); - WMCloseConnection(cPtr); - enqueueConnectionForRemoval(cPtr); - return NULL; - } - buffer = wmalloc(range.count + 1); - WMGetDataBytesWithRange(aData, buffer, range); - buffer[range.count] = '\0'; - WMAddToArray(messages, buffer); - crtPos = ptr; - while (crtPos < endPtr && (*crtPos == '\n' || *crtPos == '\r' || - *crtPos == '\t' || *crtPos == '\0' || - *crtPos == ';' || *crtPos == ' ')) { - crtPos++; - } - } - - if (crtPos < endPtr) { - range.position = (crtPos - bytes); - range.count = (endPtr - crtPos); - if (range.count > MAXCMD_SIZE) { - /* Flooooooding!!!! */ - sendMessage(cPtr, "Message too long\n\r"); - WMFreeArray(messages); - WMReleaseData(aData); - WMCloseConnection(cPtr); - enqueueConnectionForRemoval(cPtr); - return NULL; - } - holdData = WMGetSubdataWithRange(aData, range); - WMSetConnectionClientData(cPtr, holdData); - } - WMReleaseData(aData); - - if (WMGetArrayItemCount(messages) == 0) { - WMFreeArray(messages); - messages = NULL; - } - return messages; -} - -static void complainAboutBadArgs(WMConnection * cPtr, char *cmdName, char *badArgs) -{ - char *buf = wmalloc(strlen(cmdName) + strlen(badArgs) + 100); - - sprintf(buf, _("Invalid parameters '%s' for command %s. Use HELP for" - " a list of commands.\n"), badArgs, cmdName); - sendMessage(cPtr, buf); - wfree(buf); -} - -static void sendUpdateMessage(WMConnection * cPtr, char *id, int time) -{ - char *buf = wmalloc(strlen(id) + 100); - - sprintf(buf, "%s has %i minutes left\n", id, time); - sendMessage(cPtr, buf); - wfree(buf); -} - -static void showId(WMConnection * cPtr) -{ - sendMessage(cPtr, "Server example based on WMConnection\n"); -} - -static void showHelp(WMConnection * cPtr) -{ - char *buf = wmalloc(strlen(WMGetApplicationName()) + 16); - - sprintf(buf, _("%s commands:\n\n"), WMGetApplicationName()); - - enqueueMessage(cPtr, _("\n")); - enqueueMessage(cPtr, buf); - enqueueMessage(cPtr, _("GET \t- return time left (in minutes) " "for user with id \n")); - enqueueMessage(cPtr, _("SET