This repository has been archived by the owner on Jul 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
discovery.py
67 lines (53 loc) · 1.71 KB
/
discovery.py
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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Transpyre
# Network File Transfer Program
from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor
from twisted.application.internet import MulticastServer
from twisted.internet import task
import socket
from pydispatch import dispatcher
class DiscoveryProtocol(DatagramProtocol):
def __init__(self, api):
self.address = api.multicastAddress
self.port = api.multicastPort
self.notify = api.onClientFound
def startProtocol(self):
print 'Host discovery: listening'
self.transport.joinGroup(self.address)
def ping(self):
message = socket.gethostname()
self.transport.write(message, (self.address, self.port))
def datagramReceived(self, datagram, address):
# Check if we received a dgram from another Transpyre instance
if datagram != socket.gethostname():
self.notify(datagram, address[0])
class DiscoveryAPI(object):
"""
Can emit following signals:
- client-found
"""
def __init__(self):
self.discovery = None
self.refresh = None
self.loopDelay = 10
self.multicastAddress = None
self.multicastPort = None
def setMulticastAddress(self, address):
self.multicastAddress = address
def setMulticastPort(self, port):
self.multicastPort = port
def start(self):
self.stop()
self.discovery = DiscoveryProtocol(self)
reactor.listenMulticast(self.multicastPort, self.discovery)
self.refresh = task.LoopingCall(self.discovery.ping)
self.refresh.start(self.loopDelay)
def stop(self):
self.discovery = None
if self.refresh != None:
self.refresh.stop()
self.refresh = None
def onClientFound(self, name, address):
dispatcher.send( signal='client-found', sender=self, address=address, name=name )