pygnetic / pygnetic / discovery / client.py

from weakref import proxy
from .. import __init__ as net
import messages


class Handler(net.Handler):
    def net_register_ack(self, message, **kwargs):
        self.parent.sid = message.sid

    def net_servers_list(self, message, **kwargs):
        pass

    def net_error(self, message, **kwargs):
        if message.type == messages.Errors.TIMEOUT:
            self._make_connection()
            self.parent.net_register(self.parent.info)

    def net_ping(self, message, **kwargs):
        self._make_connection()
        self.parent.net_ping(message.sid)


class Client(object):
    def __init__(self, host, port=5000, n_adapter=net.network.selected_adapter,
                 s_adapter=net.serialization.selected_adapter):
        messages.message_factory.s_adapter = s_adapter
        self.client = n_adapter.Client()
        self.connection = None
        self.handler = Handler()
        self.handler.parent = proxy(self)
        self.address = host, port
        self.info = ('', '', 0)
        self.sid = 0

    def _make_connection(self):
        if self.connection is None or not self.connection.connected:
            self.connection = self.client.connect(*self.address)
            self.connection.add_handler(self.handler)

    def register(self, name, host, port):
        self.info = name, host, port
        self._make_connection()
        self.connection.net_register(name, host, port)

    def ping(self):
        if self.sid == 0:
            return False
        self._make_connection()
        self.connection.net_ping(self.sid)
        return True
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.