1
0
Fork 0

endpoints for messages

This commit is contained in:
Kevin Matz 2023-04-23 11:34:35 -04:00
parent 877ab3ed4c
commit 1cce721179
3 changed files with 240 additions and 0 deletions

View File

@ -9,12 +9,14 @@ target_sources(${PROJECT_NAME}
bundle.h
client.h
message.h
method.h
packet.h
PRIVATE
argument.cpp
bundle.cpp
client.cpp
message.cpp
method.cpp
packet.cpp
)

178
protocol/osc/method.cpp Normal file
View File

@ -0,0 +1,178 @@
/*
osc/method.cpp
Copyright (c) 2023 Kevin Matz (kevin.matz@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "method.h"
#include <regex>
namespace OSC {
/**
* @brief Method::Method
* @param parent
*/
Method::Method(Method *parent)
: parent_(parent)
, name_("")
{
if (parent)
parent->addChild(this);
}
Method::~Method()
{
for(auto &child: children_)
delete child;
}
/**
* @brief Method::addChild
* @param child
*/
void Method::addChild(Method *child)
{
children_.push_back(child);
}
/**
* @brief Method::address
* @return
*/
std::string Method::address() const
{
if (!parent_)
return name_; // root container
return parent_->address() + "/" + name_;
}
/**
* @brief Method::name
* @return
*/
std::string Method::name() const
{
return name_;
}
/**
* @brief Method::setName
* @param name
* @return
*/
bool Method::setName(std::string name)
{
std::regex bad_chars("[ #*,/\?\[]\{}]"); // ' ' # * , / ? [ ] { }
if (std::regex_search(name, bad_chars))
return false;
name_ = name;
return true;
}
/**
* @brief Method::matchesName
* @param name
* @return
*/
bool Method::matchesName(std::string &name) const
{
// try to optimize matches before resorting to expensive pattern matching
if (name == name_)
return true; // exact match
if (name.empty())
return false; // reject non-exact nulls
if (name == "*")
return true; // wildcard match
/// \todo pattern matching
return false;
}
/**
* @brief Method::isContainer
* @return
*/
bool Method::isContainer() const
{
return !children_.empty();
}
/**
* @brief Method::dispatch
* @param address
* @param msg
* @return
*/
bool Method::dispatch(std::list<std::string> patterns, const std::shared_ptr<Message> msg) const
{
if (patterns.empty())
return false; // NO pattern to match
auto name = patterns.front();
patterns.pop_front(); // take the first pattern
if (patterns.empty()) // LEAF pattern
{
if (isContainer()) // containters cannot be leafs
return false;
if (matchesName(name)) // accept the message
{
/// \todo do somthing with the message
return true;
}
}
else // BRANCH pattern
{
if (!isContainer()) // no children to match to
return false;
if (matchesName(name)) // accept pattern
{
for (const auto &child: children_) // offer the remaining patterns to children
child->dispatch(std::list<std::string>(patterns), msg);
return true;
}
else
{
if (name.empty()) // empty patterns, ie "//", apply to all (depth search)
{
if (!dispatch(std::list<std::string>(patterns), msg)) // try again with the remaining patterns
{
patterns.push_front(name); // return the empty pattern to the list
for (const auto &child: children_) // offer full search depth to children
child->dispatch(std::list<std::string>(patterns), msg);
return true;
}
}
}
}
return false; // reject the message
}
} // namespace OSC

60
protocol/osc/method.h Normal file
View File

@ -0,0 +1,60 @@
/*
osc/method.h
Copyright (c) 2023 Kevin Matz (kevin.matz@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include "message.h"
#include <list>
#include <string>
namespace OSC {
/**
* @brief The Method class
*
* \cite Spec10 OSC Methods are the potential destinations of OSC messages received by the OSC
* server and correspond to each of the points of control that the application makes available.
*/
class Method
{
public:
explicit Method(Method *parent = nullptr);
virtual ~Method();
virtual void addChild(Method *child);
std::string address() const;
std::string name() const;
bool setName(std::string name);
bool matchesName(std::string &name) const;
bool isContainer() const;
bool dispatch(std::list<std::string> patterns, const std::shared_ptr<Message> msg) const;
private:
Method *parent_;
std::vector<Method*> children_;
std::string name_;
};
} // namespace OSC