From 1750711630e5ba23e9022933d1dc8791912705c4 Mon Sep 17 00:00:00 2001 From: Egon Zemmer Date: Tue, 16 Jun 2015 11:43:24 +0200 Subject: [PATCH] Solves some bugs. Bump to version 0.7.3. --- README.md | 2 +- websocket-rails-client/channel.cpp | 10 ++-- websocket-rails-client/channel.hpp | 2 +- websocket-rails-client/event.cpp | 2 +- websocket-rails-client/event.hpp | 2 +- websocket-rails-client/websocket.hpp | 2 +- .../websocket_connection.cpp | 2 +- .../websocket_connection.hpp | 2 +- websocket-rails-client/websocket_rails.cpp | 55 +++++++++++-------- websocket-rails-client/websocket_rails.hpp | 4 +- 10 files changed, 46 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index caa10d9..c104f52 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# WebsocketRailsClient++ (v0.7.2) +# WebsocketRailsClient++ (v0.7.3) WebsocketRailsClient++ is a C++ library that uses the implementation of RFC6455 (The WebSocket Protocol) implemented in the WebSocket++ library, the Json++ light-weight JSON parser and the Boost library. It allows diff --git a/websocket-rails-client/channel.cpp b/websocket-rails-client/channel.cpp index 619ebeb..bd36766 100644 --- a/websocket-rails-client/channel.cpp +++ b/websocket-rails-client/channel.cpp @@ -1,7 +1,7 @@ /** * * Name : channel.cpp - * Version : v0.7.2 + * Version : v0.7.3 * Description : Channel Class in C++, Ansi-style * Author : Egon Zemmer * Company : Phlegx Systems @@ -54,7 +54,7 @@ Channel::Channel(std::string name, WebsocketRails & dispatcher, bool is_private, ************************************/ void Channel::destroy(cb_func success_callback, cb_func failure_callback) { - if(this->getConnectionId() == (this->dispatcher->getConn() != 0 ? this->dispatcher->getConn()->getConnectionId() : "")) { + if(this->getConnectionId() == (this->dispatcher->getConn() != NULL ? this->dispatcher->getConn()->getConnectionId() : "")) { std::string event_name = "websocket_rails.unsubscribe"; jsonxx::Array data = this->initEventData(event_name); Event event(data, success_callback, failure_callback); @@ -127,7 +127,7 @@ map_vec_cb_func Channel::getCallbacks() { void Channel::setCallbacks(map_vec_cb_func callbacks) { channel_lock guard(this->dispatcher->ch_callbacks_mutex); - this-> callbacks = callbacks; + this->callbacks = callbacks; } @@ -138,7 +138,7 @@ bool Channel::isPrivate() { void Channel::dispatch(std::string event_name, jsonxx::Object event_data) { if(event_name == "websocket_rails.channel_token") { - this->setConnectionId(this->dispatcher->getConn() != 0 ? this->dispatcher->getConn()->getConnectionId() : ""); + this->setConnectionId(this->dispatcher->getConn() != NULL ? this->dispatcher->getConn()->getConnectionId() : ""); this->setToken(event_data.get("token")); this->flush_queue(); } else { @@ -196,7 +196,7 @@ void Channel::initObject() { } else { event_name = "websocket_rails.subscribe"; } - this->setConnectionId(this->dispatcher->getConn() != 0 ? this->dispatcher->getConn()->getConnectionId() : ""); + this->setConnectionId(this->dispatcher->getConn() != NULL ? this->dispatcher->getConn()->getConnectionId() : ""); jsonxx::Array data = this->initEventData(event_name); Event event = Event(data, this->on_success, this->on_failure); this->dispatcher->triggerEvent(event); diff --git a/websocket-rails-client/channel.hpp b/websocket-rails-client/channel.hpp index 69f4f2f..8dec671 100644 --- a/websocket-rails-client/channel.hpp +++ b/websocket-rails-client/channel.hpp @@ -1,7 +1,7 @@ /** * * Name : channel.hpp - * Version : v0.7.2 + * Version : v0.7.3 * Description : Channel Header Class in C++, Ansi-style * Author : Egon Zemmer * Company : Phlegx Systems diff --git a/websocket-rails-client/event.cpp b/websocket-rails-client/event.cpp index f3da739..6a629d7 100644 --- a/websocket-rails-client/event.cpp +++ b/websocket-rails-client/event.cpp @@ -1,7 +1,7 @@ /** * * Name : event.cpp - * Version : v0.7.2 + * Version : v0.7.3 * Description : Event Class in C++, Ansi-style * Author : Egon Zemmer * Company : Phlegx Systems diff --git a/websocket-rails-client/event.hpp b/websocket-rails-client/event.hpp index 8503de2..0665454 100644 --- a/websocket-rails-client/event.hpp +++ b/websocket-rails-client/event.hpp @@ -1,7 +1,7 @@ /** * * Name : event.hpp - * Version : v0.7.2 + * Version : v0.7.3 * Description : Event Header Class in C++, Ansi-style * Author : Egon Zemmer * Company : Phlegx Systems diff --git a/websocket-rails-client/websocket.hpp b/websocket-rails-client/websocket.hpp index 824070a..e0188c8 100644 --- a/websocket-rails-client/websocket.hpp +++ b/websocket-rails-client/websocket.hpp @@ -1,7 +1,7 @@ /** * * Name : websocket.hpp - * Version : v0.7.2 + * Version : v0.7.3 * Description : Websocket Header File in C++, Ansi-style * Author : Egon Zemmer * Company : Phlegx Systems diff --git a/websocket-rails-client/websocket_connection.cpp b/websocket-rails-client/websocket_connection.cpp index 5d48136..087a476 100644 --- a/websocket-rails-client/websocket_connection.cpp +++ b/websocket-rails-client/websocket_connection.cpp @@ -1,7 +1,7 @@ /** * * Name : websocket.cpp - * Version : v0.7.2 + * Version : v0.7.3 * Description : WebsocketConnection Class in C++, Ansi-style * Author : Egon Zemmer * Company : Phlegx Systems diff --git a/websocket-rails-client/websocket_connection.hpp b/websocket-rails-client/websocket_connection.hpp index 27f26e4..36661c9 100644 --- a/websocket-rails-client/websocket_connection.hpp +++ b/websocket-rails-client/websocket_connection.hpp @@ -1,7 +1,7 @@ /** * * Name : websocket.hpp - * Version : v0.7.2 + * Version : v0.7.3 * Description : WebsocketConnection Header Class in C++, Ansi-style * Author : Egon Zemmer * Company : Phlegx Systems diff --git a/websocket-rails-client/websocket_rails.cpp b/websocket-rails-client/websocket_rails.cpp index a3c5a6f..f521cd9 100644 --- a/websocket-rails-client/websocket_rails.cpp +++ b/websocket-rails-client/websocket_rails.cpp @@ -1,7 +1,7 @@ /** * * Name : websocket_rails.cpp - * Version : v0.7.2 + * Version : v0.7.3 * Description : WebsocketRails Class in C++, Ansi-style * Author : Egon Zemmer * Company : Phlegx Systems @@ -40,7 +40,7 @@ WebsocketRails::WebsocketRails(std::string url) : url(url), conn() {} std::string WebsocketRails::connect() { this->setState("connecting"); - this->conn = new WebsocketConnection(this->url, *this); + this->setConn(new WebsocketConnection(this->url, *this)); this->websocket_connection_thread = boost::thread(&WebsocketConnection::run, this->getConn()); int count = 0; while(!this->isConnected()) { @@ -56,12 +56,14 @@ std::string WebsocketRails::connect() { std::string WebsocketRails::disconnect() { - if(this->getConn() != 0) { + if(this->getConn() != NULL) { if(this->isConnected()) { this->getConn()->close(); } + this->websocket_connection_thread.interrupt(); this->websocket_connection_thread.join(); delete this->getConn(); + this->setConn(NULL); } return this->setState("disconnected"); } @@ -69,7 +71,7 @@ std::string WebsocketRails::disconnect() { WebsocketRails::connection WebsocketRails::reconnect() { connection conn_struct; - std::string oldconnection_id = this->getConn() != 0 ? this->getConn()->getConnectionId() : ""; + std::string oldconnection_id = this->getConn() != NULL ? this->getConn()->getConnectionId() : ""; this->disconnect(); if(this->connect() == "connected") { for(auto& x: this->event_queue) { @@ -193,7 +195,7 @@ void WebsocketRails::unbindAll(std::string event_name) { void WebsocketRails::trigger(std::string event_name, jsonxx::Object event_data) { jsonxx::Array data; - data << event_name << event_data << (this->getConn() != 0 ? this->getConn()->getConnectionId() : ""); + data << event_name << event_data << (this->getConn() != NULL ? this->getConn()->getConnectionId() : ""); Event event(data); this->triggerEvent(event); } @@ -201,7 +203,7 @@ void WebsocketRails::trigger(std::string event_name, jsonxx::Object event_data) void WebsocketRails::trigger(std::string event_name, jsonxx::Object event_data, cb_func success_callback, cb_func failure_callback) { jsonxx::Array data; - data << event_name << event_data << (this->getConn() != 0 ? this->getConn()->getConnectionId() : ""); + data << event_name << event_data << (this->getConn() != NULL ? this->getConn()->getConnectionId() : ""); Event event(data, success_callback, failure_callback); this->triggerEvent(event); } @@ -214,7 +216,7 @@ void WebsocketRails::triggerEvent(Event event) { this->event_queue[event.getId()] = event; } } - if(this->getConn() != 0) { + if(this->getConn() != NULL) { this->getConn()->trigger(event); } } @@ -227,13 +229,7 @@ void WebsocketRails::triggerEvent(Event event) { Channel WebsocketRails::subscribe(std::string channel_name) { websocket_rails_lock guard(channel_queue_mutex); - if(this->channel_queue.find(channel_name) == this->channel_queue.end()) { - Channel channel(channel_name, *this, false); - this->channel_queue[channel_name] = channel; - return channel; - } else { - return this->channel_queue[channel_name]; - } + return this->processSubscribe(channel_name, false); } @@ -251,13 +247,7 @@ Channel WebsocketRails::subscribe(std::string channel_name, cb_func success_call Channel WebsocketRails::subscribePrivate(std::string channel_name) { websocket_rails_lock guard(channel_queue_mutex); - if(this->channel_queue.find(channel_name) == this->channel_queue.end()) { - Channel channel(channel_name, *this, true); - this->channel_queue[channel_name] = channel; - return channel; - } else { - return this->channel_queue[channel_name]; - } + return this->processSubscribe(channel_name, true); } @@ -310,6 +300,22 @@ void WebsocketRails::unsubscribe(std::string channel_name, cb_func success_callb ********************************************************/ +Channel WebsocketRails::processSubscribe(std::string channel_name, bool is_private) { + if(this->channel_queue.find(channel_name) == this->channel_queue.end()) { + Channel channel(channel_name, *this, is_private); + this->channel_queue[channel_name] = channel; + return channel; + } else { + return this->channel_queue[channel_name]; + } +} + + +void WebsocketRails::setConn(WebsocketConnection * conn) { + this->conn = conn; +} + + void WebsocketRails::connectionEstablished(jsonxx::Object event_data) { this->setState("connected"); this->getConn()->setConnectionId(event_data.get("connection_id")); @@ -351,7 +357,7 @@ void WebsocketRails::dispatchChannel(Event event) { void WebsocketRails::pong() { jsonxx::Array data; - data << "websocket_rails.pong" << jsonxx::Object() << (this->getConn() != 0 ? this->getConn()->getConnectionId() : ""); + data << "websocket_rails.pong" << jsonxx::Object() << (this->getConn() != NULL ? this->getConn()->getConnectionId() : ""); Event pong(data); this->getConn()->trigger(pong); } @@ -365,14 +371,15 @@ bool WebsocketRails::connectionStale() { std::vector WebsocketRails::reconnectChannels() { std::vector results; websocket_rails_lock guard(channel_queue_mutex); - for(auto& x: this->channel_queue) { + std::tr1::unordered_map channel_queue_old = this->channel_queue; + for(auto& x: channel_queue_old) { Channel channel = x.second; map_vec_cb_func callbacks = channel.getCallbacks(); cb_func success_callback, failure_callback; channel.destroy(success_callback, failure_callback); std::string channel_name = channel.getName(); this->channel_queue.erase(channel_name); - channel = channel.isPrivate() ? this->subscribePrivate(channel_name) : this->subscribe(channel_name); + channel = this->processSubscribe(channel_name, channel.isPrivate()); channel.setCallbacks(callbacks); results.push_back(channel); } diff --git a/websocket-rails-client/websocket_rails.hpp b/websocket-rails-client/websocket_rails.hpp index 2f91e23..4bf0d1c 100644 --- a/websocket-rails-client/websocket_rails.hpp +++ b/websocket-rails-client/websocket_rails.hpp @@ -1,7 +1,7 @@ /** * * Name : websocket_rails.hpp - * Version : v0.7.2 + * Version : v0.7.3 * Description : WesocketRails Header Class in C++, Ansi-style * Author : Egon Zemmer * Company : Phlegx Systems @@ -121,6 +121,8 @@ class WebsocketRails { /** * Functions **/ + Channel processSubscribe(std::string channel_name, bool is_private); + void setConn(WebsocketConnection * conn); void connectionEstablished(jsonxx::Object data); void dispatch(Event event); void dispatchChannel(Event event);