added readme for websocket plugins
authorKevron Rees <tripzero.kev@gmail.com>
Tue, 4 Feb 2014 05:00:20 +0000 (21:00 -0800)
committerKevron Rees <tripzero.kev@gmail.com>
Tue, 4 Feb 2014 05:00:20 +0000 (21:00 -0800)
examples/websocketsink2
examples/websocketsource2
plugins/websocket/README [new file with mode: 0644]
plugins/websocket/common.cpp
plugins/websocket/common.h
plugins/websocket/websocketsink.cpp
plugins/websocket/websocketsink.h
plugins/websocket/websocketsinkmanager.cpp
plugins/websocket/websocketsource.cpp

index 7d2685a..f087fae 100644 (file)
@@ -13,7 +13,8 @@
                        "interface" : "eth1",
                        "ssl" : "false",
                        "port" : "23000",
-                       "binaryProtocol" : "true"
+                       "binaryProtocol" : "true",
+                       "useExtensions" : "true"
                } 
        ]
 }
index 6fb8631..97bec0b 100644 (file)
@@ -6,7 +6,8 @@
                        "port" : "23000",
                        "ssl" : "false",
                        "ip" : "192.168.1.40",
-                       "binaryProtocol" : "true"
+                       "binaryProtocol" : "true",
+                       "useExtensions" : "true"
                }
        ],
        "sinks": [
diff --git a/plugins/websocket/README b/plugins/websocket/README
new file mode 100644 (file)
index 0000000..e0cd6db
--- /dev/null
@@ -0,0 +1,71 @@
+websocket plugins
+
+The websocket sink and source plugins are designed to relay data between devices and 
+potentially to web pages.
+
+To enable the websocket plugins, run cmake and enable the websocket_plugin option:
+
+cmake -Dwebsocket_plugin=On ..
+
+
+To use the websocket sink plugin, add the following to the "sinks" array in /etc/ambd/config:
+
+{
+       "name" : "websocket sink",
+       "path" : "/usr/lib/automotive-message-broker/websocketsink.so",
+       "interface" : "lo",
+       "useExtensions" : "true",
+       "binaryProtocol" : "false",
+       "ssl" : "false".
+       "cert" : "libwebsockets-test-server.pem",
+       "key" : "libwebsockets-test-server.key.pem"
+}
+
+To use the websocket source plugin, add the following to the "sources" array in /etc/ambd/config:
+
+{
+       "name" : "WebsocketSource",
+       "path" : "/usr/lib/automotive-message-broker/websocketsource.so",
+       "port" : "23000",
+       "ssl" : "false",
+       "ip" : "192.168.1.40",
+       "binaryProtocol" : "true",
+       "useExtensions" : "true"
+}
+
+Configuration Key Definitions:
+
+"name"
+name of plugin.  This key is not used by the plugin at this moment.
+
+"path"
+path to plugin on the filesystem.
+
+"interface" (sink only)
+interface device.  Usually "lo" for loopback or "eth0".
+
+default: lo
+
+"useExtensions"
+Use built in libwebsocket extensions like zlib compression of frames
+
+default: false
+
+"binaryProtocol"
+Use fast and efficient binary protocol instead of JSON.
+
+default: false
+
+"ssl"
+Use ssl secure socket
+
+default: false
+
+"cert" (sink only)
+
+path to ssl certificate
+
+"key" (sink only)
+
+path to ssl key
+
index cc17f0d..d8f1ce1 100644 (file)
@@ -1,3 +1,70 @@
 #include "common.h"
 
+bool doBinary = false;
 
+int lwsWrite(libwebsocket *lws, QByteArray d, int len)
+{
+
+       int retval = -1;
+
+       QByteArray temp = d;
+
+       int numframes = 1;
+       int framesize = 122;
+
+       if(d.length() > framesize)
+       {
+               numframes = qCeil((double)d.length() / 122.0);
+               QVariantMap multiFrameMessage;
+               multiFrameMessage["type"] = "multiframe";
+               multiFrameMessage["frames"] = numframes;
+
+               QByteArray msg;
+
+               if(doBinary)
+                       msg = QJsonDocument::fromVariant(multiFrameMessage).toBinaryData();
+               else
+               {
+                       msg = QJsonDocument::fromVariant(multiFrameMessage).toJson();
+                       cleanJson(msg);
+               }
+
+               lwsWrite(lws, msg, msg.length());
+       }
+
+       while(numframes--)
+       {
+               int range = 0;
+               if(temp.length() > framesize)
+                       range = framesize;
+               else range = temp.length();
+
+               QByteArray toWrite = temp.mid(0,range);
+               const char* strToWrite = toWrite.data();
+
+               temp = temp.mid(range);
+
+               if(doBinary)
+               {
+                       retval = libwebsocket_write(lws, (unsigned char*)strToWrite, toWrite.length(), LWS_WRITE_BINARY);
+               }
+               else
+               {
+                       std::unique_ptr<char[]> buffer(new char[LWS_SEND_BUFFER_PRE_PADDING + len + LWS_SEND_BUFFER_POST_PADDING]);
+                       char *buf = buffer.get() + LWS_SEND_BUFFER_PRE_PADDING;
+                       memcpy(buf, strToWrite, toWrite.length());
+
+                       retval = libwebsocket_write(lws, (unsigned char*)strToWrite, toWrite.length(), LWS_WRITE_TEXT);
+               }
+       }
+       return retval;
+
+}
+
+
+void cleanJson(QByteArray &json)
+{
+       json.replace(" ", "");
+       json.replace("\n", "");
+       json.replace("\t", "");
+}
index e42836d..f4bc00d 100644 (file)
@@ -8,71 +8,11 @@
 #include <QJsonDocument>
 #include <memory>
 
-static bool doBinary = false;
+extern bool doBinary;
 
-static void cleanJson(QByteArray &json)
-{
-       json.replace(" ", "");
-       json.replace("\n", "");
-       json.replace("\t", "");
-}
+void cleanJson(QByteArray &json);
 
 // libwebsocket_write helper function
-static int lwsWrite(struct libwebsocket *lws, QByteArray d, int len)
-{
-       int retval = -1;
-
-       QByteArray temp = d;
-
-       int numframes = 1;
-       int framesize = 122;
-
-       if(d.length() > framesize)
-       {
-               numframes = qCeil((double)d.length() / 122.0);
-               QVariantMap multiFrameMessage;
-               multiFrameMessage["type"] = "multiframe";
-               multiFrameMessage["frames"] = numframes;
-
-               QByteArray msg;
-
-               if(doBinary)
-                       msg = QJsonDocument::fromVariant(multiFrameMessage).toBinaryData();
-               else
-               {
-                       msg = QJsonDocument::fromVariant(multiFrameMessage).toJson();
-                       cleanJson(msg);
-               }
-
-               lwsWrite(lws, msg, msg.length());
-       }
-
-       while(numframes--)
-       {       
-               int range = 0;
-               if(temp.length() > framesize)
-                       range = framesize;
-               else range = temp.length();
-
-               QByteArray toWrite = temp.mid(0,range);
-               const char* strToWrite = toWrite.data();
-
-               temp = temp.mid(range);
-
-               if(doBinary)
-               {
-                       retval = libwebsocket_write(lws, (unsigned char*)strToWrite, toWrite.length(), LWS_WRITE_BINARY);
-               }
-               else
-               {
-                       std::unique_ptr<char[]> buffer(new char[LWS_SEND_BUFFER_PRE_PADDING + len + LWS_SEND_BUFFER_POST_PADDING]);
-                       char *buf = buffer.get() + LWS_SEND_BUFFER_PRE_PADDING;
-                       memcpy(buf, strToWrite, toWrite.length());
-
-                       retval = libwebsocket_write(lws, (unsigned char*)strToWrite, toWrite.length(), LWS_WRITE_TEXT);
-               }
-       }
-               return retval;
-}
+int lwsWrite(struct libwebsocket *lws, QByteArray d, int len);
 
 #endif
index 197918e..a88db06 100644 (file)
@@ -34,9 +34,8 @@
 #include <QJsonDocument>
 #include <QVariantMap>
 
-WebSocketSink::WebSocketSink(AbstractRoutingEngine* re, libwebsocket *wsi, string uuid, VehicleProperty::Property property, std::string ambdproperty, bool dB) : AbstractSink(re,map<string, string> ())
+WebSocketSink::WebSocketSink(AbstractRoutingEngine* re, libwebsocket *wsi, string uuid, VehicleProperty::Property property, std::string ambdproperty) : AbstractSink(re,map<string, string> ())
 {
-       doBinary = dB;
        m_amdbproperty = ambdproperty;
        m_uuid = uuid;
        m_wsi = wsi;
index 68439a2..7867a5f 100644 (file)
@@ -29,7 +29,7 @@ class WebSocketSink : public AbstractSink
 {
 
 public:
-       WebSocketSink(AbstractRoutingEngine* re,libwebsocket *wsi,string uuid,VehicleProperty::Property property,std::string ambdproperty, bool dB);
+       WebSocketSink(AbstractRoutingEngine* re,libwebsocket *wsi,string uuid,VehicleProperty::Property property,std::string ambdproperty);
        ~WebSocketSink();
        const string uuid() ;
        void propertyChanged(AbstractPropertyType *value);
index 3809739..629000d 100644 (file)
@@ -85,6 +85,8 @@ void WebSocketSinkManager::setConfiguration(map<string, string> config)
        std::string ssl_key_path;
        int options = 0;
        bool ssl = false;
+       info.extensions = nullptr;
+
        //Try to load config
        for (map<string,string>::iterator i=configuration.begin();i!=configuration.end();i++)
        {
@@ -117,11 +119,19 @@ void WebSocketSinkManager::setConfiguration(map<string, string> config)
                                ssl = false;
                        }
                }
+               if ((*i).first == "useExtensions")
+               {
+                       {
+                               if((*i).second == "true")
+                               {
+                                       info.extensions = libwebsocket_get_internal_extensions();
+                               }
+                               else info.extensions = nullptr;
+                       }
+               }
        }
        info.iface = interface.c_str();
        info.protocols = protocollist;
-       info.extensions = nullptr;
-       //info.extensions = libwebsocket_get_internal_extensions();
        info.gid = -1;
        info.uid = -1;
        info.options = options;
@@ -342,7 +352,7 @@ void WebSocketSinkManager::addSink(libwebsocket* socket, VehicleProperty::Proper
 
        lwsWrite(socket, replystr, replystr.length());
 
-       WebSocketSink *sink = new WebSocketSink(m_engine,socket,uuid,property,property, doBinary);
+       WebSocketSink *sink = new WebSocketSink(m_engine,socket,uuid,property,property);
        m_sinkMap[property].push_back(sink);
 }
 extern "C" AbstractSinkManager * create(AbstractRoutingEngine* routingengine, map<string, string> config)
index 5300445..2b83b24 100644 (file)
@@ -487,16 +487,18 @@ WebSocketSource::WebSocketSource(AbstractRoutingEngine *re, map<string, string>
        memset(&info, 0, sizeof info);
        info.protocols = protocols;
        info.extensions = nullptr;
-       //info.extensions = libwebsocket_get_internal_extensions();
+
+       if(config.find("useExtensions") != config.end() && config["useExtensions"] == "true")
+       {
+               info.extensions = libwebsocket_get_internal_extensions();
+       }
+
        info.gid = -1;
        info.uid = -1;
        info.port = CONTEXT_PORT_NO_LISTEN;
        info.user = this;
-       //std::string ssl_key_path = "/home/michael/.ssh/id_rsa";
-       //info.ssl_ca_filepath = ssl_key_path.c_str();
-               
+
        context = libwebsocket_create_context(&info);
-       //context = libwebsocket_create_context(CONTEXT_PORT_NO_LISTEN, NULL,protocols, libwebsocket_internal_extensions,NULL, NULL, -1, -1, 0);
 
        setConfiguration(config);
        re->setSupported(supported(), this);