WebSocketSinkManager::WebSocketSinkManager(AbstractRoutingEngine* engine, map<string, string> config):AbstractSinkManager(engine, config)
{
m_engine = engine;
-
-
+
+
//Create a listening socket on port 23000 on localhost.
-
+
}
void WebSocketSinkManager::init()
{
// //Config has been passed, let's start stuff up.
configuration = config;
-
+
//Default values
int port = 23000;
std::string interface = "lo";
const char *ssl_cert_path = NULL;
const char *ssl_key_path = NULL;
int options = 0;
-
+
//Try to load config
for (map<string,string>::iterator i=configuration.begin();i!=configuration.end();i++)
{
port = boost::lexical_cast<int>((*i).second);
}
}
- context = libwebsocket_create_context(port, interface.c_str(), protocollist, ssl_cert_path, ssl_key_path, -1, -1, options);
+ context = libwebsocket_create_context(port, interface.c_str(), protocollist,libwebsocket_internal_extensions,ssl_cert_path, ssl_key_path, -1, -1, options);
}
void WebSocketSinkManager::addSingleShotSink(libwebsocket* socket, VehicleProperty::Property property,string id)
{
DebugOut(0)<<"websocketsink: Invalid property requested: "<<property;
return;
}
-
+
}
velocityRequest.completed = [socket,id,property](AsyncPropertyReply* reply)
{
printf("Got property:%s\n",reply->value->toString().c_str());
//uint16_t velocity = boost::any_cast<uint16_t>(reply->value);
stringstream s;
-
+
//TODO: Dirty hack hardcoded stuff, jsut to make it work.
string tmpstr = "";
tmpstr = property;
s << "{\"type\":\"methodReply\",\"name\":\"get\",\"data\":[{\"name\":\"" << tmpstr << "\",\"value\":\"" << reply->value->toString() << "\"}],\"transactionid\":\"" << id << "\"}";
-
+
string replystr = s.str();
//printf("Reply: %s\n",replystr.c_str());
DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "Reply:" << replystr << "\n";
new_response+=LWS_SEND_BUFFER_PRE_PADDING;
strcpy(new_response,replystr.c_str());
libwebsocket_write(socket, (unsigned char*)new_response, strlen(new_response), LWS_WRITE_TEXT);
-
+
//TODO: run valgrind on this. libwebsocket's documentation says NOTHING about this, yet malloc insists it's true.
//delete new_response; <- Unneeded. Apparently libwebsocket free's it.
delete (char*)(new_response-LWS_SEND_BUFFER_PRE_PADDING); //Needs to subtract pre-padding, to get back to the start of the pointer.
-
+
};
AsyncPropertyReply* reply = routingEngine->getPropertyAsync(velocityRequest);
stringstream s;
s << "{\"type\":\"methodReply\",\"name\":\"unsubscribe\",\"data\":[\"" << property << "\"],\"transactionid\":\"" << uuid << "\"}";
-
+
string replystr = s.str();
//printf("Reply: %s\n",replystr.c_str());
DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "Reply:" << replystr << "\n";
m_engine->setProperty(request);
DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "AbstractRoutingEngine::setProperty called with arguments:" << property << value << "\n";
delete type;
-
+
}
void WebSocketSinkManager::addSink(libwebsocket* socket, VehicleProperty::Property property,string uuid)
{
stringstream s;
-
+
//TODO: Dirty hack hardcoded stuff, jsut to make it work.
string tmpstr = "";
if (property == "running_status_speedometer")
//Invalid property requested.
return;
}
-
+
}
s << "{\"type\":\"methodReply\",\"name\":\"subscribe\",\"data\":[\"" << property << "\"],\"transactionid\":\"" << uuid << "\"}";
-
+
string replystr = s.str();
//printf("Reply: %s\n",replystr.c_str());
DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "Reply:" << replystr << "\n";
{
//printf("Switch: %i\n",reason);
-
+
switch (reason)
{
case LWS_CALLBACK_CLIENT_WRITEABLE:
//printf("Client writable\n");
break;
}
+ case LWS_CALLBACK_SERVER_WRITEABLE:
+ {
+ //printf("Server writable\n");
+ break;
+ }
+
case LWS_CALLBACK_RECEIVE:
{
//printf("Data Received: %s\n",(char*)in);
//TODO: Do we want exceptions, or just to return an invalid json reply? Probably an invalid json reply.
DebugOut() << __SMALLFILE__ << ":" << __LINE__ << " Requested: " << (char*)in << "\n";
GError* error = nullptr;
-
-
+
+
JsonParser* parser = json_parser_new();
if (!json_parser_load_from_data(parser,(char*)in,len,&error))
{
DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "Error loading JSON\n";
return 0;
}
-
+
JsonNode* node = json_parser_get_root(parser);
if(node == nullptr)
{
//throw std::runtime_error("Unable to get JSON root object");
return 0;
}
-
+
JsonReader* reader = json_reader_new(node);
if(reader == nullptr)
{
//throw std::runtime_error("Unable to create JSON reader");
return 0;
}
-
-
-
-
-
+
+
+
+
+
string type;
json_reader_read_member(reader,"type");
type = json_reader_get_string_value(reader);
json_reader_end_member(reader);
-
+
string name;
json_reader_read_member(reader,"name");
name = json_reader_get_string_value(reader);
//Raw string value
string path = json_reader_get_string_value(reader);
data.push_back(path);
-
+
}
else
{
}
}
json_reader_end_member(reader);
-
+
string id;
json_reader_read_member(reader,"transactionid");
if (strcmp("gchararray",g_type_name(json_node_get_value_type(json_reader_get_value(reader)))) == 0)
id = strstr.str();
}
json_reader_end_member(reader);
-
+
///TODO: this will probably explode:
//mlc: I agree with Kevron here, it does explode.
//if(error) g_error_free(error);
-
+
g_object_unref(reader);
g_object_unref(parser);
-
-
+
+
if (type == "method")
{
if (name == "get")
//(*d);
d++;
}
-
+
}
}
}
libwebsocket_service_fd(context,&pollstruct);
return true;
}
-