Additions for creating new WebsockSink class instances per tcp connection
authorMichael Carpenter <malcom2073@gmail.com>
Wed, 22 Aug 2012 21:42:10 +0000 (17:42 -0400)
committerMichael Carpenter <malcom2073@gmail.com>
Wed, 22 Aug 2012 21:42:10 +0000 (17:42 -0400)
plugins/websocketsink/websocketsink.cpp
plugins/websocketsink/websocketsink.h
plugins/websocketsink/websocketsinkmanager.cpp
plugins/websocketsink/websocketsinkmanager.h

index 587392a..c080ee6 100644 (file)
 
 
 
-WebSocketSink::WebSocketSink(AbstractRoutingEngine* re) : AbstractSink(re)
+WebSocketSink::WebSocketSink(AbstractRoutingEngine* re,libwebsocket *wsi,string uuid) : AbstractSink(re)
 {
-
+       m_uuid = uuid;
 }
 string WebSocketSink::uuid()
 {
-       return "e43f6cad-60e3-4454-9638-01ffa9ab8c8f";
+       return m_uuid;
 }
 void WebSocketSink::propertyChanged(VehicleProperty::Property property, boost::any value, string  uuid)
 {
index 6fb2cdc..e4c2d5e 100644 (file)
 #include <glib.h>
 #include <abstractroutingengine.h>
 #include "abstractsink.h"
+#include <libwebsockets.h>
 class WebSocketSink : public AbstractSink
 {
 
 public:
-       WebSocketSink(AbstractRoutingEngine* re);
+       WebSocketSink(AbstractRoutingEngine* re,libwebsocket *wsi,string uuid);
        string uuid() ;
        void propertyChanged(VehicleProperty::Property property, boost::any value, string  uuid);
        void supportedChanged(PropertyList supportedProperties);
        PropertyList subscriptions();
-
+private:
+       string m_uuid;
 };
 
 #endif // WEBSOCKETSINK_H
index 0dc9912..2828010 100644 (file)
@@ -18,6 +18,7 @@
 
 
 #include "websocketsinkmanager.h"
+#include "websocketsink.h"
 #include <sstream>
 #include <json-glib/json-glib.h>
 
@@ -49,17 +50,17 @@ WebSocketSinkManager::WebSocketSinkManager(AbstractRoutingEngine* engine):Abstra
        //Create a listening socket on port 23000 on localhost.
        context = libwebsocket_create_context(port, interface, protocollist,libwebsocket_internal_extensions,ssl_cert_path, ssl_key_path, -1, -1, options);
 }
-void WebSocketSinkManager::addSink(libwebsocket* socket, VehicleProperty::Property property)
+void WebSocketSinkManager::addSingleShotSink(libwebsocket* socket, VehicleProperty::Property property,string id)
 {
        AsyncPropertyRequest velocityRequest;
        velocityRequest.property = VehicleProperty::VehicleSpeed;
-       velocityRequest.completed = [socket](AsyncPropertyReply* reply) {
+       velocityRequest.completed = [socket,id](AsyncPropertyReply* reply) {
                printf("Got property:%i\n",boost::any_cast<uint16_t>(reply->value));
                uint16_t velocity = boost::any_cast<uint16_t>(reply->value);
                stringstream s;
                
                //TODO: Dirty hack hardcoded stuff, jsut to make it work.
-               s << "{\"type\":\"reply\",\"name\":\"Velocity\",\"arguments\":\"[\"" << velocity << "\"],\"transactionid\":\"aeff0345defaa03c132\"}";
+               s << "{\"type\":\"reply\",\"name\":\"Velocity\",\"arguments\":\"[\"" << velocity << "\"],\"transactionid\":\"" << id << "\"}";
                
                string replystr = s.str();
                
@@ -76,6 +77,10 @@ void WebSocketSinkManager::addSink(libwebsocket* socket, VehicleProperty::Proper
 
        AsyncPropertyReply* reply = routingEngine->getPropertyAsync(velocityRequest);
 }
+void WebSocketSinkManager::addSink(libwebsocket* socket, VehicleProperty::Property property,string uuid)
+{
+       WebSocketSink *sink = new WebSocketSink(m_engine,socket,uuid);
+}
 
 extern "C" AbstractSinkManager * create(AbstractRoutingEngine* routingengine)
 {
@@ -109,8 +114,8 @@ static int websocket_callback(struct libwebsocket_context *context,struct libweb
                }
                case LWS_CALLBACK_HTTP:
                {
+                       //TODO: Verify that ALL requests get sent via LWS_CALLBACK_HTTP, so we can use that instead of LWS_CALLBACK_RECIEVE
                        printf("requested URI: %s\n", (char*)in);
-                       //This contains the JSON, but so does LWS_CALLBACK_RECEIVE...
                        
                        /*{
                          "type":"method",
@@ -124,6 +129,7 @@ static int websocket_callback(struct libwebsocket_context *context,struct libweb
                        if (!json_parser_load_from_data(parser,(char*)in,len,&error))
                        {
                          printf("Error loading JSON\n");
+                         return 0;
                        }
                        
                        JsonNode* node = json_parser_get_root(parser);
@@ -182,6 +188,7 @@ static int websocket_callback(struct libwebsocket_context *context,struct libweb
                        {
                          if (name == "GetProperty")
                          {
+                           //GetProperty is going to be a singleshot sink.
                            string arg = arguments.front();
                            if (arg == "Velocity")
                            {                      
@@ -189,11 +196,16 @@ static int websocket_callback(struct libwebsocket_context *context,struct libweb
                            //m_engine->subscribeToProperty(VehicleProperty::VehicleSpeed,this);
                              
                            
-                           sinkManager->addSink(wsi,VehicleProperty::Property::VehicleSpeed);
+                           sinkManager->addSingleShotSink(wsi,VehicleProperty::Property::VehicleSpeed,id);
                            //libwebsocket_write(wsi, (unsigned char*)new_response, strlen(new_response), LWS_WRITE_TEXT);
                            }
                            
                          }
+                         else if (name == "Subscribe")
+                         {
+                           //Subscribe is a permanent sink, until unsubscription.
+                           sinkManager->addSink(wsi,VehicleProperty::VehicleSpeed,id);
+                         }
                        }
                        
                        ///TODO: this will probably explode:
index ae5f9ef..a8e6f97 100644 (file)
@@ -28,7 +28,8 @@ class WebSocketSinkManager: public AbstractSinkManager
 {
 public:
        WebSocketSinkManager(AbstractRoutingEngine* engine);
-       void addSink(libwebsocket *socket,VehicleProperty::Property property);
+       void addSingleShotSink(libwebsocket* socket, VehicleProperty::Property property,string id);
+       void addSink(libwebsocket* socket, VehicleProperty::Property property,string uuid);
 private:
   AbstractRoutingEngine *m_engine;
        struct libwebsocket_protocols protocollist[2];