Initial commit of tcpsink, a websocket providing sink plugin.
authorMichael Carpenter <malcom2073@gmail.com>
Mon, 20 Aug 2012 00:58:37 +0000 (20:58 -0400)
committerMichael Carpenter <malcom2073@gmail.com>
Mon, 20 Aug 2012 00:58:37 +0000 (20:58 -0400)
plugins/CMakeLists.txt
plugins/CMakeLists.txt~
plugins/tcpsink/CMakeLists.txt [new file with mode: 0644]
plugins/tcpsink/tcpsinkmanager.cpp [new file with mode: 0644]
plugins/tcpsink/tcpsinkmanager.h [new file with mode: 0644]
plugins/tcpsink/tcpsinkplugin.cpp [new file with mode: 0644]
plugins/tcpsink/tcpsinkplugin.h [new file with mode: 0644]

index 5f21b68..edf4b7b 100644 (file)
@@ -17,3 +17,4 @@ set_target_properties(examplesinkplugin PROPERTIES PREFIX "")
 target_link_libraries(examplesinkplugin -lamb -L${CMAKE_CURRENT_BINARY_DIR}/lib ${link_libraries})
 
 add_subdirectory(wheel)
+add_subdirectory(tcpsink)
\ No newline at end of file
index ed50e32..5f21b68 100644 (file)
@@ -1,9 +1,19 @@
 
 include_directories(${CMAKE_SOURCE_DIR}/lib ${include_dirs})
 
-set(exampleplugin_headers exampleplugin.h)
-set(exampleplugin_sources exampleplugin.cpp)
-add_library(exampleplugin MODULE ${exampleplugin_sources})
-set_target_properties(exampleplugin PROPERTIES PREFIX "")
-target_link_libraries(exampleplugin -lamb -L${CMAKE_CURRENT_BINARY_DIR}/lib ${link_libraries})
+set(examplesourceplugin_headers exampleplugin.h)
+set(examplesourceplugin_sources exampleplugin.cpp)
 
+add_library(examplesourceplugin MODULE ${examplesourceplugin_sources})
+set_target_properties(examplesourceplugin PROPERTIES PREFIX "")
+target_link_libraries(examplesourceplugin -lamb -L${CMAKE_CURRENT_BINARY_DIR}/lib ${link_libraries})
+
+
+set(examplesinkplugin_headers examplesink.h)
+set(examplesinkplugin_sources examplesink.cpp)
+
+add_library(examplesinkplugin MODULE ${examplesinkplugin_sources})
+set_target_properties(examplesinkplugin PROPERTIES PREFIX "")
+target_link_libraries(examplesinkplugin -lamb -L${CMAKE_CURRENT_BINARY_DIR}/lib ${link_libraries})
+
+add_subdirectory(wheel)
diff --git a/plugins/tcpsink/CMakeLists.txt b/plugins/tcpsink/CMakeLists.txt
new file mode 100644 (file)
index 0000000..d26ef66
--- /dev/null
@@ -0,0 +1,8 @@
+
+include_directories(${CMAKE_SOURCE_DIR}/lib ${include_dirs})
+
+set(tcpsinkplugin_headers tcpsinkplugin.h)
+set(tcpsinkplugin_sources tcpsinkmanager.cpp tcpsinkplugin.cpp)
+add_library(tcpsinkplugin MODULE ${tcpsinkplugin_sources})
+set_target_properties(tcpsinkplugin PROPERTIES PREFIX "")
+target_link_libraries(tcpsinkplugin -lamb -lwebsockets  -L${CMAKE_CURRENT_BINARY_DIR}/lib ${link_libraries})
\ No newline at end of file
diff --git a/plugins/tcpsink/tcpsinkmanager.cpp b/plugins/tcpsink/tcpsinkmanager.cpp
new file mode 100644 (file)
index 0000000..879e4c5
--- /dev/null
@@ -0,0 +1,143 @@
+/*
+    <one line to give the library's name and an idea of what it does.>
+    Copyright (C) 2012  Michael Carpenter <email>
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+
+#include "tcpsinkmanager.h"
+
+//Global variables, these will be moved into the class
+struct pollfd pollfds[100];
+int count_pollfds = 0;
+libwebsocket_context *context;
+static int websocket_callback(struct libwebsocket_context *context,struct libwebsocket *wsi,enum libwebsocket_callback_reasons reason, void *user,void *in, size_t len);
+bool gioPollingFunc(GIOChannel *source,GIOCondition condition,gpointer data);
+
+
+TcpSinkManager::TcpSinkManager(AbstractRoutingEngine* engine):AbstractSinkManager(engine)
+{
+       //new TcpSinkPlugin(engine);
+       
+       //Protocol list for libwebsockets.
+       protocollist[0] = { "http-only", websocket_callback, 0 };
+       protocollist[1] = { NULL, NULL, 0 };
+
+       int port = 8080;
+       const char *interface = "lo";
+       const char *ssl_cert_path = NULL;
+       const char *ssl_key_path = NULL;
+       int options = 0;
+       
+       //Create a listening socket on port 8080 on localhost.
+       context = libwebsocket_create_context(port, interface, protocollist,libwebsocket_internal_extensions,ssl_cert_path, ssl_key_path, -1, -1, options);
+}
+extern "C" AbstractSinkManager * create(AbstractRoutingEngine* routingengine)
+{
+       return new TcpSinkManager(routingengine);
+}
+static int websocket_callback(struct libwebsocket_context *context,struct libwebsocket *wsi,enum libwebsocket_callback_reasons reason, void *user,void *in, size_t len)
+{
+       printf("Switch: %i\n",reason);
+       switch (reason)
+       {
+               case LWS_CALLBACK_CLIENT_WRITEABLE:
+               {
+                       //Connection has been established.
+                       printf("Connection established\n");
+                       break;
+               }
+               case LWS_CALLBACK_HTTP:
+               {
+                       //HTTP request
+                       char *requested_uri = (char *) in;
+                       printf("requested URI: %s\n", requested_uri);
+                 
+                       if (strcmp(requested_uri, "/") == 0)
+                       {
+                               const char *universal_response = "Hello, World!";
+                               // http://git.warmcat.com/cgi-bin/cgit/libwebsockets/tree/lib/libwebsockets.h#n597
+                               libwebsocket_write(wsi, (unsigned char*)universal_response, strlen(universal_response), LWS_WRITE_HTTP);
+                       }
+                       //We're done, close the connection
+                       libwebsocket_close_and_free_session(context, wsi,LWS_CLOSE_STATUS_NORMAL);
+                       break;
+               }
+               case LWS_CALLBACK_ADD_POLL_FD:
+               {
+                       //Add a FD to the poll list.
+                       printf("Adding poll\n");
+                       GIOChannel *chan = g_io_channel_unix_new((int)(long)user);
+                       g_io_add_watch(chan,G_IO_IN,(GIOFunc)gioPollingFunc,0);
+                       g_io_add_watch(chan,G_IO_PRI,(GIOFunc)gioPollingFunc,0);
+                       pollfds[count_pollfds].fd = (int)(long)user;
+                       pollfds[count_pollfds].events = (int)len;
+                       pollfds[count_pollfds++].revents = 0;
+                       break;
+               }
+               case LWS_CALLBACK_DEL_POLL_FD:
+               {
+                       //Remove FD from the poll list.
+                       for (int n = 0; n < count_pollfds; n++)
+                               if (pollfds[n].fd == (int)(long)user)
+                                       while (n < count_pollfds) {
+                                               pollfds[n] = pollfds[n + 1];
+                                               n++;
+                                       }
+                       count_pollfds--;
+                       break;
+               }
+               case LWS_CALLBACK_SET_MODE_POLL_FD:
+               {
+                       //Set the poll mode
+                       GIOChannel *chan = g_io_channel_unix_new((int)(long)user);
+                       g_io_add_watch(chan,(GIOCondition)(int)len,(GIOFunc)gioPollingFunc,0);
+                       break;
+               }
+               case LWS_CALLBACK_CLEAR_MODE_POLL_FD:
+               {
+                       //Don't handle this yet.
+                       break;
+               }
+               default:
+               {
+                       printf("Unhandled callback: %i\n",reason);
+                       break;
+               }
+       }
+       return 0; 
+}
+
+bool gioPollingFunc(GIOChannel *source,GIOCondition condition,gpointer data)
+{
+       //This is the polling function. If it return false, glib will stop polling this FD.
+       printf("Polling...%i\n",condition);
+       struct pollfd pollstruct;
+       int newfd = g_io_channel_unix_get_fd(source);
+       pollstruct.fd = newfd;
+       pollstruct.events = condition;
+       pollstruct.revents = condition;
+       libwebsocket_service_fd(context,&pollstruct);
+       if (condition == G_IO_HUP)
+       {
+         //Hang up. Returning false closes out the GIOChannel.
+         printf("Callback on G_IO_HUP\n");
+         return false;
+       }
+       
+       return true;
+}
+
diff --git a/plugins/tcpsink/tcpsinkmanager.h b/plugins/tcpsink/tcpsinkmanager.h
new file mode 100644 (file)
index 0000000..376bc3e
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+    <one line to give the library's name and an idea of what it does.>
+    Copyright (C) 2012  Michael Carpenter <email>
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+
+#ifndef TCPSINKMANAGER_H
+#define TCPSINKMANAGER_H
+
+#include <abstractroutingengine.h>
+#include <abstractsink.h>
+#include <gio/gio.h>
+#include <libwebsockets.h>
+
+class TcpSinkManager: public AbstractSinkManager
+{
+public:
+       TcpSinkManager(AbstractRoutingEngine* engine);
+private:
+       struct libwebsocket_protocols protocollist[2];
+};
+
+#endif // TCPSINKMANAGER_H
diff --git a/plugins/tcpsink/tcpsinkplugin.cpp b/plugins/tcpsink/tcpsinkplugin.cpp
new file mode 100644 (file)
index 0000000..14a9020
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+    <one line to give the library's name and an idea of what it does.>
+    Copyright (C) 2012  Michael Carpenter <email>
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+
+#include "tcpsinkplugin.h"
+#include <glib/gtypes.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+
+
+
+TcpSinkPlugin::TcpSinkPlugin(AbstractRoutingEngine* re) : AbstractSink(re)
+{
+
+}
+string TcpSinkPlugin::uuid()
+{
+       return "e43f6cad-60e3-4454-9638-01ffa9ab8c8f";
+}
+void TcpSinkPlugin::propertyChanged(VehicleProperty::Property property, boost::any value, string  uuid)
+{
+}
+void TcpSinkPlugin::supportedChanged(PropertyList supportedProperties)
+{
+}
+PropertyList TcpSinkPlugin::subscriptions()
+{
+       return PropertyList();
+} 
+
diff --git a/plugins/tcpsink/tcpsinkplugin.h b/plugins/tcpsink/tcpsinkplugin.h
new file mode 100644 (file)
index 0000000..d53a514
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+    <one line to give the library's name and an idea of what it does.>
+    Copyright (C) 2012  Michael Carpenter <email>
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+
+#ifndef TCPSINKPLUGIN_H
+#define TCPSINKPLUGIN_H
+#include <glib.h>
+#include <abstractroutingengine.h>
+#include "abstractsink.h"
+class TcpSinkPlugin : public AbstractSink
+{
+
+public:
+       TcpSinkPlugin(AbstractRoutingEngine* re);
+       string uuid() ;
+       void propertyChanged(VehicleProperty::Property property, boost::any value, string  uuid);
+       void supportedChanged(PropertyList supportedProperties);
+       PropertyList subscriptions();
+
+};
+
+#endif // TCPSINKPLUGIN_H