summaryrefslogtreecommitdiff
path: root/efl/ecore_con/efl.ecore_con_lookup.pxi
blob: e00291630e3a583e738adc967d44625fad2b0a1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Copyright (C) 2007-2015 various contributors (see AUTHORS)
#
# This file is part of Python-EFL.
#
# Python-EFL is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# Python-EFL is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this Python-EFL.  If not, see <http://www.gnu.org/licenses/>.


cdef void _con_dns_lookup_cb(const char *canonname, const char *ip, sockaddr *sockaddr, int addrlen, void *data) with gil:
    cdef Lookup self = <Lookup>data

    try:
        # TODO read sockaddr and replace the placeholder None with something
        #      more usefull from the sockaddr struct.
        self.done_cb(_ctouni(canonname), _ctouni(ip), None, *self.args, **self.kargs)
    except Exception:
        traceback.print_exc()

    Py_DECREF(self)


cdef class Lookup(object):
    def __init__(self, name, done_cb, *args, **kargs):
        """Lookup()

        A simple class to perform asynchronous DNS lookups.

        :param string name: The hostname to query
        :param callable done_cb: The function to call when done
        :param \*args: Any other arguments will be passed back in ``done_cb``
        :param \**kargs: Any other keywords arguments will be passed back in ``done_cb``

        .. versionadded:: 1.17

        Just create an instance and give a callback function to be called
        when the operation is complete.

        This class performs a DNS lookup on the hostname specified by
        `name`, then calls `done_cb` with the result and the data given as
        parameter. The result will be given to the done_cb as follows:

        **expected `done_cb` signature**::

            func(canonname, ip, sockaddr)

        where:
            * **canonname** (string) is the canonical domain name
            * **ip** (string) is the recolved ip address
            * **sockaddr** (None) is a placeholder for future expansion



        **Usage example**::

            import ecore_con

            def done_cb(canonname, ip, sockaddr):
                print(canonname)
                print(ip)

            ecore_con.Lookup('example.com', done_cb)

        
        """

        if not callable(done_cb):
            raise TypeError("Parameter 'done_cb' must be callable")

        self.done_cb = done_cb
        self.args = args
        self.kargs = kargs

        if isinstance(name, unicode): name = PyUnicode_AsUTF8String(name)
        ecore_con_lookup(<const char *>name if name is not None else NULL,
                         _con_dns_lookup_cb, <void*>self)

        Py_INCREF(self)