Exemple #1
0
    def __init__(self, addr, port, interface='', observers=None,
                 data_callback=None, shared_socket=None):
        """ Constructor for the UDPListener class.

        @param addr: address to listen on
        @param port: port to listen on
        @param interface: interface to listen on
        @param observers: list of initial subscribers
        @param data_callback: callback to get data forwarded to
        @param shared_socket: socket to be reused by this network listener

        @type addr: string
        @type port: integer
        @type interface: string
        @type observers: list of INetworkObserver
        @type data_callback: callable
        @type shared_socket: socket.socket
        """
        NetworkListener.__init__(self, observers, data_callback)
        self.addr = addr
        self.port = port
        self.interface = interface

        # win32 does not like interface='' (MSEARCH replies are not propagated) - TODO: check this is correct
        if os.name == 'nt':
            ifaces = get_active_ifaces()
            if ifaces:
                self.interface = get_ip_address(ifaces[0])
        
        self.socket = None
        self.fd_id = None
        self._create_socket(shared_socket)
    def _generate_random_address(self):
        # Default listen url: localhost:port where port is on the dynamic
        # range (non registered ports)
        ifaces = get_active_ifaces()
        host = None
        port = None
        if ifaces:
            host = get_ip_address(ifaces[0])
        else:
            host = 'localhost'

        while not port:
            port = random.randint(49152, 65535)
            try:
                check_port(host, port)
            except IOError:
                port = 0

        self.host = host
        self.port = port
Exemple #3
0
    def _generate_random_address(self):
        # Default listen url: localhost:port where port is on the dynamic
        # range (non registered ports)
        ifaces = get_active_ifaces()
        host = None
        port = None
        if ifaces:
            host = get_ip_address(ifaces[0])
        else:
            host = 'localhost'

        while not port:
            port = random.randint(49152, 65535)
            try:
                check_port(host, port)
            except IOError:
                port = 0

        self.host = host
        self.port = port
Exemple #4
0
""" Provides a simple API and observer model for listening over UDP.
"""

import socket
from struct import pack

from brisa import __enable_offline_mode__
from brisa.core import log, reactor
from brisa.core.network import get_active_ifaces
from brisa.core.ireactor import EVENT_TYPE_READ
from brisa.core.threaded_call import run_async_function


if not __enable_offline_mode__:
    if not get_active_ifaces():
        raise RuntimeError('Network is down.')


class CannotListenError(Exception):
    """ Exception denoting an error when binding interfaces for listening.
    """

    def __init__(self, interface, port, addr='', reason=''):
        """ Constructor for the CannotListenError class

        @param interface: interface where the error occured
        @param port: port at the error ocurred when binding
        @param addr: address at the error ocurred when binding
        @param reason: reason why the error happened
# http://opensource.org/licenses/mit-license.php or see LICENSE file.
# Copyright 2007-2008 Brisa Team <*****@*****.**>
""" Provides a simple API and observer model for listening over UDP.
"""

import socket
from struct import pack

from brisa import __enable_offline_mode__
from brisa.core import log, reactor
from brisa.core.network import get_active_ifaces
from brisa.core.ireactor import EVENT_TYPE_READ
from brisa.core.threaded_call import run_async_function

if not __enable_offline_mode__:
    if not get_active_ifaces():
        raise RuntimeError('Network is down.')


class CannotListenError(Exception):
    """ Exception denoting an error when binding interfaces for listening.
    """
    def __init__(self, interface, port, addr='', reason=''):
        """ Constructor for the CannotListenError class

        @param interface: interface where the error occured
        @param port: port at the error ocurred when binding
        @param addr: address at the error ocurred when binding
        @param reason: reason why the error happened

        @type interface: string