Skip to content

Commit

Permalink
Add message handler
Browse files Browse the repository at this point in the history
  • Loading branch information
mikee47 committed Mar 7, 2021
1 parent 5785f06 commit fb30f5a
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 19 deletions.
16 changes: 9 additions & 7 deletions Sming/Components/mdns/src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,19 @@ void Server::onReceive(pbuf* buf, IpAddress remoteIP, uint16_t remotePort)
}
}

if(!messageCallback) {
if(handlers.isEmpty()) {
return;
}

Message message(remoteIP, remotePort, buf->payload, buf->len);
if(message.parse()) {
/*
* TODO: Establish callback chain/queue to support multiple clients.
* If a client returns false then don't pass it any further.
*/
messageCallback(message);
if(!message.parse()) {
return;
}

for(auto& handler : handlers) {
if(!handler.onMessage(message)) {
break;
}
}
}

Expand Down
33 changes: 33 additions & 0 deletions Sming/Components/mdns/src/include/Network/Mdns/Handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/****
* Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
* Created 2015 by Skurydin Alexey
* http://github.com/SmingHub/Sming
* All files of the Sming Core are provided under the LGPL v3 license.
*
* Message.h
*
****/

#pragma once

#include <Data/LinkedObjectList.h>
#include "Message.h"

namespace mDNS
{
/**
* @brief Virtual base class used for chaining message handlers
*/
class Handler : public LinkedObjectTemplate<Handler>
{
public:
using List = LinkedObjectListTemplate<Handler>;

/**
* @brief Callback to be invoked for each received message
* @retval bool Return true to pass message to other handlers, false to stop
*/
virtual bool onMessage(Message& message) = 0;
};

} // namespace mDNS
20 changes: 15 additions & 5 deletions Sming/Components/mdns/src/include/Network/Mdns/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <Delegate.h>
#include <Network/UdpConnection.h>
#include "Request.h"
#include "Handler.h"

namespace mDNS
{
Expand All @@ -30,13 +31,13 @@ class Server : protected UdpConnection
public:
/**
* @brief Callback to be invoked for each received message
* @retval bool Depends on operation
* @retval bool See `onSend()`
*/
using MessageDelegate = Delegate<bool(Message& message)>;

/**
* @brief Callback to be invoked with raw data (debugging, etc.)
* @retval bool Depends on operation
* @retval bool See `onPacket()`
*/
using PacketDelegate = Delegate<bool(IpAddress remoteIP, uint16_t remotePort, const uint8_t* data, size_t length)>;

Expand All @@ -56,9 +57,18 @@ class Server : protected UdpConnection
* @brief Set callback to be invoked for each received message
* @param callback Return false from callback to prevent message being passed to other clients
*/
void onMessage(MessageDelegate callback)
void addHandler(Handler& handler)
{
messageCallback = callback;
handlers.add(&handler);
}

/**
* @brief Remove a message handler
* @note If there are no more handlers then consider setting a timeout and then shutting the server down.
*/
void removeHandler(Handler& handler)
{
handlers.remove(&handler);
}

/**
Expand Down Expand Up @@ -106,7 +116,7 @@ class Server : protected UdpConnection
void onReceive(pbuf* buf, IpAddress remoteIP, uint16_t remotePort) override;
};

MessageDelegate messageCallback;
Handler::List handlers;
MessageDelegate sendCallback;
PacketDelegate packetCallback;
UdpOut out;
Expand Down
36 changes: 29 additions & 7 deletions samples/Basic_Mdns/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,25 @@
#define WIFI_PWD "PleaseEnterPass"
#endif

DEFINE_FSTR_LOCAL(fstrSearchService, "_googlecast._tcp.local")
class MessageHandler : public mDNS::Handler
{
public:
/*
* Set the DNS record name we're interested in.
* Used as a filter in our `onMessage` method.
*/
void setSearchName(const String& name)
{
searchName = name;
}

bool onMessage(mDNS::Message& message) override;

private:
String searchName;
};

static MessageHandler myMessageHandler;

#ifdef ARCH_HOST
bool savePacket(IpAddress remoteIP, uint16_t remotePort, const uint8_t* data, size_t length)
Expand All @@ -31,20 +49,22 @@ bool savePacket(IpAddress remoteIP, uint16_t remotePort, const uint8_t* data, si
}
#endif

bool handleMessage(mDNS::Message& message)
bool MessageHandler::onMessage(mDNS::Message& message)
{
mDNS::printMessage(Serial, message);

// Check if we're interested in this reponse
// Check if we're interested in this message
if(!message.isReply()) {
debug_i("Ignoring query");
return false;
}
auto answer = message[mDNS::ResourceType::PTR];
if(answer == nullptr) {
debug_i("Ignoring message: no PTR record");
return false;
}
if(answer->getName() != fstrSearchService) {
debug_i("Ignoring message");
if(answer->getName() != searchName) {
debug_i("Ignoring message: Name doesn't match");
return false;
}

Expand Down Expand Up @@ -80,13 +100,15 @@ bool handleMessage(mDNS::Message& message)

void sendSearch()
{
String name(fstrSearchService);
String name = F("_googlecast._tcp.local");

// To discover all DNS-SD registered services, use:
// name = F("_services._dns-sd._udp.local");

bool ok = mDNS::server.search(name);
debug_i("search('%s'): %s", name.c_str(), ok ? "OK" : "FAIL");
// Tell our handler what we're looking for
myMessageHandler.setSearchName(name);
}

void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway)
Expand All @@ -98,7 +120,7 @@ void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway)
printMessage(Serial, msg);
return true;
});
mDNS::server.onMessage(handleMessage);
mDNS::server.addHandler(myMessageHandler);
#ifdef ARCH_HOST
mDNS::server.onPacket(savePacket);
#endif
Expand Down

0 comments on commit fb30f5a

Please sign in to comment.