Here is an algorithm for matching written in Python from the the MQTT list:

Paul

On 4 December 2014 at 05:33, Akalanka Pagoda Arachchi <[email protected]>
wrote:

> Hi Hasitha,
>
> Yes that looks good in performance. Will try to integrate this into
> Moquette, maybe we can share this with Moquette team so they can include
> this in their next release.
>
> Thanks,
> Akalanka
>
> On Thu, Dec 4, 2014 at 10:31 AM, Hasitha Amal De Silva <[email protected]>
> wrote:
>
>> Hi Akalanka,
>>
>> I went on the same path and used the moquette logic to create a simple
>> string hiearachy processor :
>>
>>
>> http://devnutshell.tumblr.com/post/96378477451/hierarchical-token-matcher-for-strings
>>
>> It's almost the same logic as moquette (but the tree only contains
>> strings, not subscription objects, so manipulation would be faster..).
>> Sharing in case it could help :)
>>
>>
>> On Thu, Dec 4, 2014 at 10:22 AM, Akalanka Pagoda Arachchi <
>> [email protected]> wrote:
>>
>>> Hi Pamod/Chamila,
>>>
>>> As Pamod mentioned the implementation is already there to resolve
>>> matching topics in Moquette. Since Moquette was integrated inside MB I was
>>> able to use the same implementation to resolve matching topics.
>>>
>>> Chamila, the Moquette implementation is using if else with String
>>> manipulation techniques. This looks simpler to understand than a Regex
>>> since I was having a hard time creating a Regex that matches the MQTT
>>> specifications.
>>>
>>> Thanks,
>>> Akalanka
>>>
>>> On Thu, Dec 4, 2014 at 7:44 AM, Pamod Sylvester <[email protected]> wrote:
>>>
>>>> Hi Akalanka,
>>>>
>>>> Could we also consider the approach used by the ProtocolProcessor of
>>>> Moquette ?
>>>>
>>>> Where all the subscriptions are kept in a Tree based on their hierarchy
>>>>  and when a message is published, the published topic is split into tokens,
>>>> and then the tree will be traversed for a match. When a match is found, the
>>>> message would be sent to the ancestral node and its descendants.
>>>>
>>>> It might also be good if we could find a way to point to the
>>>> functionality Moquette has introduced for cluster wide subscription
>>>> identification as well. This way if the protocol introduces a different set
>>>> of wild cards, the change will be reflected from the library itself. WDYT ?
>>>>
>>>> Thanks,
>>>> Pamod
>>>>
>>>> On Thu, Dec 4, 2014 at 12:34 AM, Chamila De Alwis <[email protected]>
>>>> wrote:
>>>>
>>>>> Hi Akalanka,
>>>>>
>>>>> Even though for the multi-level wildcard using if else might be
>>>>> simpler, since the single-level wildcard can appear in several places of
>>>>> the topic name wouldn't it be better to follow the regex approach?
>>>>>
>>>>>
>>>>> Regards,
>>>>> Chamila de Alwis
>>>>> Software Engineer | WSO2 | +94772207163
>>>>> Blog: code.chamiladealwis.com
>>>>>
>>>>>
>>>>>
>>>>> On Wed, Dec 3, 2014 at 2:49 PM, Akalanka Pagoda Arachchi <
>>>>> [email protected]> wrote:
>>>>>
>>>>>> Hi all,
>>>>>>
>>>>>> I am implementing wildcard support for MQTT in WSO2 MB.
>>>>>>
>>>>>> The MQTT 3.1 Specification [1] provides details about how the
>>>>>> wildcards should behave.
>>>>>>
>>>>>> According to this, to implement this match making I can either use
>>>>>> java.lang.String functions and if else conditions or use a regular
>>>>>> expression.
>>>>>>
>>>>>> I believe a regular expression will be a bit more complex to
>>>>>> understand and hence prefer to use if else conditions. What will be best
>>>>>> way to approach this?
>>>>>>
>>>>>>   [1] -
>>>>>> http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718107
>>>>>>
>>>>>> Thanks,
>>>>>> Akalanka
>>>>>> --
>>>>>> *Akalanka Pagoda Arachchi,*
>>>>>> *Software Engineer*
>>>>>> *078-4721791 <078-4721791>*
>>>>>>
>>>>>> _______________________________________________
>>>>>> Dev mailing list
>>>>>> [email protected]
>>>>>> http://wso2.org/cgi-bin/mailman/listinfo/dev
>>>>>>
>>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> Dev mailing list
>>>>> [email protected]
>>>>> http://wso2.org/cgi-bin/mailman/listinfo/dev
>>>>>
>>>>>
>>>>
>>>>
>>>> --
>>>> *Pamod Sylvester *
>>>>  *Senior Software Engineer *
>>>> Integration Technologies Team, WSO2 Inc.; http://wso2.com
>>>> email: [email protected] cell: +94 77 7779495
>>>>
>>>
>>>
>>>
>>> --
>>> *Darshana Akalanka Pagoda Arachchi,*
>>> *Software Engineer*
>>> *078-4721791*
>>>
>>> _______________________________________________
>>> Dev mailing list
>>> [email protected]
>>> http://wso2.org/cgi-bin/mailman/listinfo/dev
>>>
>>>
>>
>>
>> --
>> Cheers,
>>
>> Hasitha Amal De Silva
>>  Software Engineer
>> Mobile : 0772037426
>> Blog    : http://devnutshell.tumblr.com/
>> WSO2 Inc.: http://wso2.com ( lean.enterprise.middleware. )
>>
>
>
>
> --
> *Darshana Akalanka Pagoda Arachchi,*
> *Software Engineer*
> *078-4721791*
>
> _______________________________________________
> Dev mailing list
> [email protected]
> http://wso2.org/cgi-bin/mailman/listinfo/dev
>
>


-- 
Paul Fremantle
CTO and Co-Founder, WSO2
OASIS WS-RX TC Co-chair, Apache Member

UK: +44 207 096 0336

blog: http://pzf.fremantle.org
twitter.com/pzfreo
[email protected]

wso2.com Lean Enterprise Middleware

Disclaimer: This communication may contain privileged or other confidential
information and is intended exclusively for the addressee/s. If you are not
the intended recipient/s, or believe that you may have received this
communication in error, please reply to the sender indicating that fact and
delete the copy you received and in addition, you should not print, copy,
retransmit, disseminate, or otherwise use the information contained in this
communication. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys

def _check_for_subtree(tpl):
    if tpl[-1] == u'#':
        del tpl[-1]  # trim '#'
        return True
    else:
        return False

def acl_check(ap, sp):  # ap: acl pattern; sp: subscription pattern
    apl = ap.split(u'/')  # make a list
    ap_stf = _check_for_subtree(apl)  # set the subtree flag and trim if True
    apn = len(apl)
    
    spl = sp.split(u'/')  # make a list
    sp_stf = _check_for_subtree(spl)  # set the subtree flag and trim if True
    spn = len(spl)
    
    if sp_stf and not ap_stf:
        return False  # subscription may have subtree only if acl does also
    
    if apn > spn:  # subscription must have at least as many levels as acl
        return False
    elif spn > apn:
        if ap_stf:
            apl += [u'+'] * (spn - apn)  # pad the apl with '+' for matching
        else:  # if no acl subtree, subscription levels cannot exceed acl's
            return False
    else:  # apn == spn
        pass
        
    for i in range(spn):  # check each level - all levels must match
        if apl[i] == u'+':  # any value in sub matches; '+' also matches
            pass
        elif apl[i] == spl[i]: # an explicit match
            pass
        else:  # an explict failure to match at this level
            return False
            
    return True

if __name__ == u"__main__":
    test_data_tuple_list = [
        # (acl pattern, subscription pattern, expected result)
        (u'foo/+/bar', u'foo/#', False),
        (u'foo/+/ba℞/#', u'foo/baz/ba℞', True),
        (u'foo/+/ba℞/#', u'foo/baz/ba℞/+', True),
        (u'foo/+/ba℞/#', u'foo/baz/ba℞/#', True),
        (u'foo/+/ba℞/#', u'foo/baz/+/#', False),
        (u'/+//#', u'/foo///#', True),
        (u'#', u'#', True),
        (u'#', u'+', True),
        (u'+', u'#', False),
        (u'+', u'+', True),
        
    ]
    
    for tdt in test_data_tuple_list:
        result = acl_check(*tdt[:2])
        test_result = u'passed' if result == tdt[2] else u'failed'
        
        print(
            (
                u"ap: {}; sp: {}; Expectation: {}; "
                u"Result: {}; Test Result: {}"
            ).format(tdt[0], tdt[1], tdt[2], result, test_result)
        )
        
    sys.exit()
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to