Skip to content

Commit 2639981

Browse files
committed
Big bug in channel and addressing. Changed from copy to address pointer.
1 parent 0b41b50 commit 2639981

File tree

10 files changed

+60
-90
lines changed

10 files changed

+60
-90
lines changed

README.md

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# WebsocketRailsClient++ (v0.7.3)
1+
# WebsocketRailsClient++ (v0.7.4)
22

33
WebsocketRailsClient++ is a C++ library that uses the implementation of RFC6455 (The WebSocket Protocol)
44
implemented in the WebSocket++ library, the Json++ light-weight JSON parser and the Boost library. It allows
@@ -35,9 +35,9 @@ connections to the Ruby Websocket-Rails server and supports the Websocket-Rails
3535

3636
#### Connection Callbacks
3737

38-
* ```on_open(boost::bind cb)``` : callback on open connection.
39-
* ```on_close(boost::bind cb)``` : callback on close connection.
40-
* ```on_fail(boost::bind cb)``` : callback on fail connection.
38+
* ```onOpen(boost::bind cb)``` : callback on open connection.
39+
* ```onClose(boost::bind cb)``` : callback on close connection.
40+
* ```onFail(boost::bind cb)``` : callback on fail connection.
4141

4242
#### Trigger an Event on Server
4343

@@ -56,6 +56,7 @@ connections to the Ruby Websocket-Rails server and supports the Websocket-Rails
5656

5757
#### Channel Management
5858

59+
* ```getChannel(std::string channel_name)``` : Get a channel after subscribed to it.
5960
* ```subscribe(std::string channel_name)``` : Subscribe to a channel.
6061
* ```subscribePrivate(std::string channel_name, boost::bind cb_succ, boost::bind cb_fail)``` : Subscribe to a private channel with callbacks.
6162
* ```unsubscribe(std::string channel_name)``` : Unsubscribe a channel.
@@ -64,7 +65,6 @@ connections to the Ruby Websocket-Rails server and supports the Websocket-Rails
6465
#### Trigger a Channel-Event on Server
6566

6667
* ```trigger(std::string event_name, jsonxx::Object event_data)``` : trigger channel event with data without callback.
67-
* ```trigger(std::string event_name, jsonxx::Object event_data, boost::bind cb_succ, boost::bind cb_fail)``` : trigger channel event with data and callbacks.
6868

6969
#### Bind to an Incoming Channel-Event
7070

@@ -147,36 +147,36 @@ dispatcher.connect();
147147

148148
```cpp
149149
/* Event bind callback definition */
150-
dispatcher.bind("users.pool", boost::bind(callback, _1));
150+
dispatcher.bind("users_pool", boost::bind(callback, _1));
151151

152152
/* Event bind callback definition with member function callback */
153153
Foo my_foo;
154-
Event event3 = dispatcher.bind("users.pool", boost::bind(&Foo::success_func, my_foo, _1));
154+
dispatcher.bind("users_pool", boost::bind(&Foo::success_func, my_foo, _1));
155155

156156
/* Event unbind callback definition */
157-
dispatcher.unbindAll("users.pool");
157+
dispatcher.unbindAll("users_pool");
158158
```
159159

160160
* Trigger events
161161

162162
```cpp
163163
/* Trigger an event */
164-
Event event1 = dispatcher.trigger("users.create", jsonxx::Object("name", "Hans Mustermann"));
165-
Event event2 = dispatcher.trigger("users.create", jsonxx::Object("name", "Frau Mustermann"), boost::bind(success_func, _1), boost::bind(failure_func, _1));
164+
dispatcher.trigger("users_create", jsonxx::Object("name", "Hans Mustermann"));
165+
dispatcher.trigger("users_create", jsonxx::Object("name", "Frau Mustermann"), boost::bind(success_func, _1), boost::bind(failure_func, _1));
166166

167167
/* Trigger an event with member function callback */
168168
Bar my_bar;
169-
Event event3 = dispatcher.trigger("users.pool", boost::bind(&Foo::success_func, my_bar, _1), boost::bind(&Foo::failure_func, my_bar, _1));
169+
dispatcher.trigger("users_pool", boost::bind(&Foo::success_func, my_bar, _1), boost::bind(&Foo::failure_func, my_bar, _1));
170170
```
171171

172172
* Use channels
173173

174174
```cpp
175175
/* Subscribe to channels */
176-
Channel channel1 = dispatcher.subscribe("Authors");
177-
Channel channel2 = dispatcher.subscribe("Users", boost::bind(success_func, _1), boost::bind(failure_func, _1));
178-
Channel private_channel1 = dispatcher.subscribePrivate("Administrators");
179-
Channel private_channel2 = dispatcher.subscribePrivate("Moderators", boost::bind(success_func, _1), boost::bind(failure_func, _1));
176+
dispatcher.subscribe("Authors");
177+
dispatcher.subscribe("Users", boost::bind(success_func, _1), boost::bind(failure_func, _1));
178+
dispatcher.subscribePrivate("Administrators");
179+
dispatcher.subscribePrivate("Moderators", boost::bind(success_func, _1), boost::bind(failure_func, _1));
180180

181181
/* Unsubscribe channels */
182182
dispatcher.unsubscribe("Authors");
@@ -187,14 +187,13 @@ dispatcher.unsubscribe("Users", boost::bind(success_func, _1), boost::bind(failu
187187

188188
```cpp
189189
/* Trigger an event on channel */
190-
Event event = channel1.trigger("users.create", jsonxx::Object("name", "Hans Mustermann"));
191-
Event event = channel2.trigger("users.create", jsonxx::Object("name", "Frau Mustermann"), boost::bind(success_func, _1), boost::bind(failure_func, _1));
190+
dispatcher.getChannel("Authors").trigger("users_create", jsonxx::Object("name", "Hans Mustermann"));
192191

193192
/* Channel event bind callback definition */
194-
channel1.bind("users.pool", boost::bind(callback, _1));
193+
dispatcher.getChannel("Authors").bind("users_pool", boost::bind(callback, _1));
195194

196195
/* Event unbind callback definition */
197-
channel1.unbindAll("users.pool");
196+
dispatcher.getChannel("Authors").unbindAll("users_pool");
198197
```
199198

200199
* Callback additional parameters

websocket-rails-client/channel.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
*
33
* Name : channel.cpp
4-
* Version : v0.7.3
4+
* Version : v0.7.4
55
* Description : Channel Class in C++, Ansi-style
66
* Author : Egon Zemmer
77
* Company : Phlegx Systems
@@ -69,7 +69,7 @@ void Channel::bind(std::string event_name, cb_func callback) {
6969
vec_cb_func v;
7070
this->callbacks[event_name] = v;
7171
}
72-
return this->callbacks[event_name].push_back(callback);
72+
this->callbacks[event_name].push_back(callback);
7373
}
7474

7575

@@ -93,19 +93,6 @@ void Channel::trigger(std::string event_name, jsonxx::Object event_data) {
9393
}
9494
}
9595

96-
void Channel::trigger(std::string event_name, jsonxx::Object event_data, cb_func success_callback, cb_func failure_callback) {
97-
jsonxx::Array data = this->initEventData(event_name);
98-
data.get<jsonxx::Object>(1).import("channel", this->name);
99-
data.get<jsonxx::Object>(1).import("data", event_data);
100-
data.get<jsonxx::Object>(1).import("token", this->token);
101-
Event event(data, success_callback, failure_callback);
102-
if(this->token.empty()) {
103-
this->event_queue.push(event);
104-
} else {
105-
this->dispatcher->triggerEvent(event);
106-
}
107-
}
108-
10996

11097
std::string Channel::getName() {
11198
return this->name;

websocket-rails-client/channel.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
*
33
* Name : channel.hpp
4-
* Version : v0.7.3
4+
* Version : v0.7.4
55
* Description : Channel Header Class in C++, Ansi-style
66
* Author : Egon Zemmer
77
* Company : Phlegx Systems
@@ -47,7 +47,6 @@ class Channel {
4747
void bind(std::string event_name, cb_func callback);
4848
void unbindAll(std::string event_name);
4949
void trigger(std::string event_name, jsonxx::Object event_data);
50-
void trigger(std::string event_name, jsonxx::Object event_data, cb_func success_callback, cb_func failure_callback);
5150
std::string getName();
5251
map_vec_cb_func getCallbacks();
5352
void setCallbacks(map_vec_cb_func callbacks);

websocket-rails-client/event.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
*
33
* Name : event.cpp
4-
* Version : v0.7.3
4+
* Version : v0.7.4
55
* Description : Event Class in C++, Ansi-style
66
* Author : Egon Zemmer
77
* Company : Phlegx Systems

websocket-rails-client/event.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
*
33
* Name : event.hpp
4-
* Version : v0.7.3
4+
* Version : v0.7.4
55
* Description : Event Header Class in C++, Ansi-style
66
* Author : Egon Zemmer
77
* Company : Phlegx Systems

websocket-rails-client/websocket.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
*
33
* Name : websocket.hpp
4-
* Version : v0.7.3
4+
* Version : v0.7.4
55
* Description : Websocket Header File in C++, Ansi-style
66
* Author : Egon Zemmer
77
* Company : Phlegx Systems
@@ -44,7 +44,7 @@
4444
#include <websocketpp/client.hpp>
4545
#include <websocketpp/common/thread.hpp>
4646

47-
#define TIMEOUT_CONN 30
47+
#define TIMEOUT_CONN 5
4848

4949
typedef boost::function<void(jsonxx::Object)> cb_func;
5050
typedef std::vector<boost::function<void(jsonxx::Object)> > vec_cb_func;

websocket-rails-client/websocket_connection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
*
33
* Name : websocket.cpp
4-
* Version : v0.7.3
4+
* Version : v0.7.4
55
* Description : WebsocketConnection Class in C++, Ansi-style
66
* Author : Egon Zemmer
77
* Company : Phlegx Systems

websocket-rails-client/websocket_connection.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
*
33
* Name : websocket.hpp
4-
* Version : v0.7.3
4+
* Version : v0.7.4
55
* Description : WebsocketConnection Header Class in C++, Ansi-style
66
* Author : Egon Zemmer
77
* Company : Phlegx Systems

websocket-rails-client/websocket_rails.cpp

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
*
33
* Name : websocket_rails.cpp
4-
* Version : v0.7.3
4+
* Version : v0.7.4
55
* Description : WebsocketRails Class in C++, Ansi-style
66
* Author : Egon Zemmer
77
* Company : Phlegx Systems
@@ -69,8 +69,7 @@ std::string WebsocketRails::disconnect() {
6969
}
7070

7171

72-
WebsocketRails::connection WebsocketRails::reconnect() {
73-
connection conn_struct;
72+
void WebsocketRails::reconnect() {
7473
std::string oldconnection_id = this->getConn() != NULL ? this->getConn()->getConnectionId() : "";
7574
this->disconnect();
7675
if(this->connect() == "connected") {
@@ -80,10 +79,8 @@ WebsocketRails::connection WebsocketRails::reconnect() {
8079
this->triggerEvent(event);
8180
}
8281
}
83-
conn_struct.channels = this->reconnectChannels();
82+
this->reconnectChannels();
8483
}
85-
conn_struct.state = this->state;
86-
return conn_struct;
8784
}
8885

8986

@@ -215,47 +212,45 @@ void WebsocketRails::triggerEvent(Event event) {
215212
* Channel functions *
216213
************************************/
217214

218-
Channel WebsocketRails::subscribe(std::string channel_name) {
215+
216+
Channel * WebsocketRails::getChannel(std::string channel_name) {
217+
return &this->channel_queue[channel_name];
218+
}
219+
220+
221+
Channel * WebsocketRails::subscribe(std::string channel_name) {
219222
if(this->channel_queue.find(channel_name) == this->channel_queue.end()) {
220223
Channel channel(channel_name, *this, false);
221224
this->channel_queue[channel_name] = channel;
222-
return channel;
223-
} else {
224-
return this->channel_queue[channel_name];
225225
}
226+
return &this->channel_queue[channel_name];
226227
}
227228

228229

229-
Channel WebsocketRails::subscribe(std::string channel_name, cb_func success_callback, cb_func failure_callback) {
230+
Channel * WebsocketRails::subscribe(std::string channel_name, cb_func success_callback, cb_func failure_callback) {
230231
if(this->channel_queue.find(channel_name) == this->channel_queue.end()) {
231232
Channel channel(channel_name, *this, false, success_callback, failure_callback);
232233
this->channel_queue[channel_name] = channel;
233-
return channel;
234-
} else {
235-
return this->channel_queue[channel_name];
236234
}
235+
return &this->channel_queue[channel_name];
237236
}
238237

239238

240-
Channel WebsocketRails::subscribePrivate(std::string channel_name) {
239+
Channel * WebsocketRails::subscribePrivate(std::string channel_name) {
241240
if(this->channel_queue.find(channel_name) == this->channel_queue.end()) {
242241
Channel channel(channel_name, *this, true);
243242
this->channel_queue[channel_name] = channel;
244-
return channel;
245-
} else {
246-
return this->channel_queue[channel_name];
247243
}
244+
return &this->channel_queue[channel_name];
248245
}
249246

250247

251-
Channel WebsocketRails::subscribePrivate(std::string channel_name, cb_func success_callback, cb_func failure_callback) {
248+
Channel * WebsocketRails::subscribePrivate(std::string channel_name, cb_func success_callback, cb_func failure_callback) {
252249
if(this->channel_queue.find(channel_name) == this->channel_queue.end()) {
253250
Channel channel(channel_name, *this, true, success_callback, failure_callback);
254251
this->channel_queue[channel_name] = channel;
255-
return channel;
256-
} else {
257-
return this->channel_queue[channel_name];
258252
}
253+
return &this->channel_queue[channel_name];
259254
}
260255

261256

@@ -334,19 +329,16 @@ bool WebsocketRails::connectionStale() {
334329
}
335330

336331

337-
std::vector<Channel> WebsocketRails::reconnectChannels() {
338-
std::vector<Channel> results;
332+
void WebsocketRails::reconnectChannels() {
339333
std::tr1::unordered_map<std::string, Channel> channel_queue_old = this->channel_queue;
340334
for(auto& x: channel_queue_old) {
341-
Channel channel = x.second;
342-
map_vec_cb_func callbacks = channel.getCallbacks();
335+
Channel * channel = &x.second;
336+
map_vec_cb_func callbacks = channel->getCallbacks();
343337
cb_func success_callback, failure_callback;
344-
channel.destroy(success_callback, failure_callback);
345-
std::string channel_name = channel.getName();
338+
channel->destroy(success_callback, failure_callback);
339+
std::string channel_name = channel->getName();
346340
this->channel_queue.erase(channel_name);
347-
channel = channel.isPrivate() ? this->subscribePrivate(channel_name) : this->subscribe(channel_name);
348-
channel.setCallbacks(callbacks);
349-
results.push_back(channel);
341+
channel = channel->isPrivate() ? this->subscribePrivate(channel_name) : this->subscribe(channel_name);
342+
channel->setCallbacks(callbacks);
350343
}
351-
return results;
352344
}

websocket-rails-client/websocket_rails.hpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
*
33
* Name : websocket_rails.hpp
4-
* Version : v0.7.3
4+
* Version : v0.7.4
55
* Description : WesocketRails Header Class in C++, Ansi-style
66
* Author : Egon Zemmer
77
* Company : Phlegx Systems
@@ -33,14 +33,6 @@
3333
class WebsocketRails {
3434
public:
3535

36-
/**
37-
* Connection Return Type
38-
**/
39-
typedef struct {
40-
std::string state;
41-
std::vector<Channel> channels;
42-
} connection;
43-
4436
/**
4537
* Constructors
4638
**/
@@ -52,7 +44,7 @@ class WebsocketRails {
5244
**/
5345
std::string connect();
5446
std::string disconnect();
55-
connection reconnect();
47+
void reconnect();
5648
std::string getState();
5749
std::string setState(std::string state);
5850
WebsocketConnection * getConn();
@@ -81,10 +73,11 @@ class WebsocketRails {
8173
/**
8274
* Channel functions
8375
**/
84-
Channel subscribe(std::string channel_name);
85-
Channel subscribe(std::string channel_name, cb_func success_callback, cb_func failure_callback);
86-
Channel subscribePrivate(std::string channel_name);
87-
Channel subscribePrivate(std::string channel_name, cb_func success_callback, cb_func failure_callback);
76+
Channel * getChannel(std::string channel_name);
77+
Channel * subscribe(std::string channel_name);
78+
Channel * subscribe(std::string channel_name, cb_func success_callback, cb_func failure_callback);
79+
Channel * subscribePrivate(std::string channel_name);
80+
Channel * subscribePrivate(std::string channel_name, cb_func success_callback, cb_func failure_callback);
8881
void unsubscribe(std::string channel_name);
8982
void unsubscribe(std::string channel_name, cb_func success_callback, cb_func failure_callback);
9083

@@ -107,14 +100,14 @@ class WebsocketRails {
107100
/**
108101
* Functions
109102
**/
110-
Channel processSubscribe(std::string channel_name, bool is_private);
103+
Channel * processSubscribe(std::string channel_name, bool is_private);
111104
void setConn(WebsocketConnection * conn);
112105
void connectionEstablished(jsonxx::Object data);
113106
void dispatch(Event event);
114107
void dispatchChannel(Event event);
115108
void pong();
116109
bool connectionStale();
117-
std::vector<Channel> reconnectChannels();
110+
void reconnectChannels();
118111

119112
};
120113

0 commit comments

Comments
 (0)