runtime/ServicePublisher.py
changeset 1784 64beb9e9c749
parent 1744 69dfdb26f600
child 1826 91796f408540
equal deleted inserted replaced
1729:31e63e25b4cc 1784:64beb9e9c749
    19 
    19 
    20 # You should have received a copy of the GNU Lesser General Public
    20 # You should have received a copy of the GNU Lesser General Public
    21 # License along with this library; if not, write to the Free Software
    21 # License along with this library; if not, write to the Free Software
    22 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
    22 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
    23 
    23 
    24 import socket, threading
    24 import socket
       
    25 import threading
    25 from util import Zeroconf
    26 from util import Zeroconf
    26 
    27 
    27 service_type = '_PYRO._tcp.local.'
    28 service_type = '_PYRO._tcp.local.'
    28 
    29 
       
    30 
    29 class ServicePublisher():
    31 class ServicePublisher():
    30     def __init__(self):
    32     def __init__(self):
    31         # type: fully qualified service type name
    33         # type: fully qualified service type name
    32         self.serviceproperties = {'description':'Beremiz remote PLC'}
    34         self.serviceproperties = {'description': 'Beremiz remote PLC'}
    33         
    35 
    34         self.name = None
    36         self.name = None
    35         self.ip_32b = None
    37         self.ip_32b = None
    36         self.port = None
    38         self.port = None
    37         self.server = None
    39         self.server = None
    38         self.service_name = None
    40         self.service_name = None
    39         self.retrytimer = None
    41         self.retrytimer = None
    40         
    42 
    41     def RegisterService(self, name, ip, port):
    43     def RegisterService(self, name, ip, port):
    42         try:
    44         try:
    43             self._RegisterService(name, ip, port)
    45             self._RegisterService(name, ip, port)
    44         except Exception,e:
    46         except Exception, e:
    45             self.retrytimer = threading.Timer(2,self.RegisterService,[name, ip, port])
    47             self.retrytimer = threading.Timer(2, self.RegisterService, [name, ip, port])
    46             self.retrytimer.start() 
    48             self.retrytimer.start()
    47 
    49 
    48     def _RegisterService(self, name, ip, port):
    50     def _RegisterService(self, name, ip, port):
    49         # name: fully qualified service name
    51         # name: fully qualified service name
    50         self.service_name = 'Beremiz_%s.%s'%(name,service_type)
    52         self.service_name = 'Beremiz_%s.%s' % (name, service_type)
    51         self.name = name
    53         self.name = name
    52         self.port = port
    54         self.port = port
    53 
    55 
    54         self.server = Zeroconf.Zeroconf(ip)
    56         self.server = Zeroconf.Zeroconf(ip)
    55         print "MDNS brodcasting on :"+ip
    57         print "MDNS brodcasting on :"+ip
    62         self.server.registerService(
    64         self.server.registerService(
    63              Zeroconf.ServiceInfo(service_type,
    65              Zeroconf.ServiceInfo(service_type,
    64                                   self.service_name,
    66                                   self.service_name,
    65                                   self.ip_32b,
    67                                   self.ip_32b,
    66                                   self.port,
    68                                   self.port,
    67                                   properties = self.serviceproperties))
    69                                   properties=self.serviceproperties))
    68         self.retrytimer=None
    70         self.retrytimer = None
    69     
    71 
    70     def UnRegisterService(self):
    72     def UnRegisterService(self):
    71         if self.retrytimer is not None:
    73         if self.retrytimer is not None:
    72             self.retrytimer.cancel()
    74             self.retrytimer.cancel()
    73 
    75 
    74         self.server.unregisterService(
    76         self.server.unregisterService(
    75                                       Zeroconf.ServiceInfo(service_type, 
    77                                       Zeroconf.ServiceInfo(service_type,
    76                                                            self.service_name, 
    78                                                            self.service_name,
    77                                                            self.ip_32b, 
    79                                                            self.ip_32b,
    78                                                            self.port, 
    80                                                            self.port,
    79                                                            properties = self.serviceproperties))
    81                                                            properties=self.serviceproperties))
    80         self.server.close()
    82         self.server.close()
    81         self.server = None
    83         self.server = None
    82     
    84 
    83     def gethostaddr(self, dst = '224.0.1.41'):
    85     def gethostaddr(self, dst='224.0.1.41'):
    84         s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    86         s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    85         try:
    87         try:
    86             s.connect((dst, 7))
    88             s.connect((dst, 7))
    87             (host, port) = s.getsockname()
    89             (host, port) = s.getsockname()
    88             s.close()
    90             s.close()
    89             if host != '0.0.0.0':
    91             if host != '0.0.0.0':
    90                 return host
    92                 return host
    91         except Exception,e:
    93         except Exception, e:
    92             pass
    94             pass
    93         return socket.gethostbyname(socket.gethostname())
    95         return socket.gethostbyname(socket.gethostname())