Skip to content

Commit

Permalink
Solves some bugs. Bump to version 0.7.3.
Browse files Browse the repository at this point in the history
  • Loading branch information
phlegx committed Jun 16, 2015
1 parent b133f07 commit 1750711
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 37 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 5 additions & 5 deletions websocket-rails-client/channel.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}


Expand All @@ -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<jsonxx::String>("token"));
this->flush_queue();
} else {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion websocket-rails-client/channel.hpp
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion websocket-rails-client/event.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion websocket-rails-client/event.hpp
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion websocket-rails-client/websocket.hpp
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion websocket-rails-client/websocket_connection.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion websocket-rails-client/websocket_connection.hpp
Original file line number Diff line number Diff line change
@@ -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
Expand Down
55 changes: 31 additions & 24 deletions websocket-rails-client/websocket_rails.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()) {
Expand All @@ -56,20 +56,22 @@ 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");
}


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) {
Expand Down Expand Up @@ -193,15 +195,15 @@ 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);
}


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);
}
Expand All @@ -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);
}
}
Expand All @@ -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);
}


Expand All @@ -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);
}


Expand Down Expand Up @@ -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<jsonxx::String>("connection_id"));
Expand Down Expand Up @@ -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);
}
Expand All @@ -365,14 +371,15 @@ bool WebsocketRails::connectionStale() {
std::vector<Channel> WebsocketRails::reconnectChannels() {
std::vector<Channel> results;
websocket_rails_lock guard(channel_queue_mutex);
for(auto& x: this->channel_queue) {
std::tr1::unordered_map<std::string, Channel> 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);
}
Expand Down
4 changes: 3 additions & 1 deletion websocket-rails-client/websocket_rails.hpp
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 1750711

Please sign in to comment.