Skip to content

Commit

Permalink
Check handler_data in event_handler_exists
Browse files Browse the repository at this point in the history
  • Loading branch information
monkbroc committed Aug 18, 2015
1 parent ec614e3 commit 54fcd90
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
8 changes: 5 additions & 3 deletions communication/src/spark_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,12 +674,14 @@ void SparkProtocol::remove_event_handlers(const char* event_name)
}

bool SparkProtocol::event_handler_exists(const char *event_name, EventHandler handler,
SubscriptionScope::Enum scope, const char* id)
void *handler_data, SubscriptionScope::Enum scope, const char* id)
{
const int NUM_HANDLERS = sizeof(event_handlers) / sizeof(FilteringEventHandler);
for (int i = 0; i < NUM_HANDLERS; i++)
{
if (event_handlers[i].handler==handler && event_handlers[i].scope==scope) {
if (event_handlers[i].handler==handler &&
event_handlers[i].handler_data==handler_data &&
event_handlers[i].scope==scope) {
const size_t MAX_FILTER_LEN = sizeof(event_handlers[i].filter);
const size_t FILTER_LEN = strnlen(event_name, MAX_FILTER_LEN);
if (!strncmp(event_handlers[i].filter, event_name, FILTER_LEN)) {
Expand All @@ -698,7 +700,7 @@ bool SparkProtocol::event_handler_exists(const char *event_name, EventHandler ha
bool SparkProtocol::add_event_handler(const char *event_name, EventHandler handler,
void *handler_data, SubscriptionScope::Enum scope, const char* id)
{
if (event_handler_exists(event_name, handler, scope, id))
if (event_handler_exists(event_name, handler, handler_data, scope, id))
return true;

const int NUM_HANDLERS = sizeof(event_handlers) / sizeof(FilteringEventHandler);
Expand Down
2 changes: 1 addition & 1 deletion communication/src/spark_protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class SparkProtocol
void *handler_data, SubscriptionScope::Enum scope,
const char* device_id);
bool event_handler_exists(const char *event_name, EventHandler handler,
SubscriptionScope::Enum scope, const char* id);
void *handler_data, SubscriptionScope::Enum scope, const char* id);
void remove_event_handlers(const char* event_name);
void send_subscriptions();
bool send_subscription(const char *event_name, const char *device_id);
Expand Down
9 changes: 7 additions & 2 deletions user/tests/wiring/cloud/cloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,17 @@ test(Spark_Second_Event_Handler_Not_Matched) {
class Subscriber {
public:
void subscribe() {
Particle.subscribe("test/event3", &Subscriber::handler, this);
assertTrue(Particle.subscribe("test/event3", &Subscriber::handler, this));
// To make sure calling subscribe with a different handler is not a no op
assertTrue(Particle.subscribe("test/event3", &Subscriber::handler2, this));
receivedCount = 0;
}
void handler(const char *eventName, const char *data) {
receivedCount++;
}
void handler2(const char *eventName, const char *data) {
receivedCount++;
}
int receivedCount;
} subscriber;

Expand All @@ -197,5 +202,5 @@ test(Subscribe_With_Object) {
while ((millis()-start)<30000 && !subscriber.receivedCount)
idle();

assertEqual(subscriber.receivedCount, 1);
assertEqual(subscriber.receivedCount, 2);
}

0 comments on commit 54fcd90

Please sign in to comment.