Edouard@647: #!/usr/bin/env python
Edouard@647: # -*- coding: utf-8 -*-
Edouard@647: 
andrej@1571: # This file is part of Beremiz, a Integrated Development Environment for
andrej@1571: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
Edouard@647: #
andrej@1571: # Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
Edouard@647: #
andrej@1571: # See COPYING file for copyrights details.
Edouard@647: #
andrej@1571: # This program is free software; you can redistribute it and/or
andrej@1571: # modify it under the terms of the GNU General Public License
andrej@1571: # as published by the Free Software Foundation; either version 2
andrej@1571: # of the License, or (at your option) any later version.
Edouard@647: #
andrej@1571: # This program is distributed in the hope that it will be useful,
andrej@1571: # but WITHOUT ANY WARRANTY; without even the implied warranty of
andrej@1571: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
andrej@1571: # GNU General Public License for more details.
Edouard@647: #
andrej@1571: # You should have received a copy of the GNU General Public License
andrej@1571: # along with this program; if not, write to the Free Software
andrej@1571: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Edouard@647: 
Edouard@726: import socket, threading
Edouard@726: from util import Zeroconf
Edouard@647: 
Edouard@763: service_type = '_PYRO._tcp.local.'
Edouard@763: 
Edouard@647: class ServicePublisher():
Edouard@647:     def __init__(self):
Edouard@647:         # type: fully qualified service type name
Edouard@647:         self.serviceproperties = {'description':'Beremiz remote PLC'}
Edouard@647:         
Edouard@647:         self.name = None
Edouard@647:         self.ip_32b = None
Edouard@647:         self.port = None
Edouard@647:         self.server = None
Edouard@647:         self.service_name = None
Edouard@647:         self.retrytimer = None
Edouard@647:         
Edouard@647:     def RegisterService(self, name, ip, port):
Edouard@647:         try:
Edouard@647:             self._RegisterService(name, ip, port)
Edouard@647:         except Exception,e:
Edouard@647:             self.retrytimer = threading.Timer(2,self.RegisterService,[name, ip, port])
Edouard@647:             self.retrytimer.start() 
Edouard@647: 
Edouard@647:     def _RegisterService(self, name, ip, port):
Edouard@647:         # name: fully qualified service name
Edouard@763:         self.service_name = 'Beremiz_%s.%s'%(name,service_type)
Edouard@647:         self.name = name
Edouard@647:         self.port = port
Edouard@647: 
Edouard@647:         self.server = Zeroconf.Zeroconf(ip)
Edouard@648:         print "MDNS brodcasting on :"+ip
Edouard@647: 
Edouard@648:         if ip == "0.0.0.0":
Edouard@648:             ip = self.gethostaddr()
Edouard@648:         print "MDNS brodcasted service address :"+ip
Edouard@647:         self.ip_32b = socket.inet_aton(ip)
Edouard@647: 
Edouard@647:         self.server.registerService(
Edouard@763:              Zeroconf.ServiceInfo(service_type,
Edouard@647:                                   self.service_name,
Edouard@647:                                   self.ip_32b,
Edouard@647:                                   self.port,
Edouard@647:                                   properties = self.serviceproperties))
Edouard@647:         self.retrytimer=None
Edouard@647:     
Edouard@647:     def UnRegisterService(self):
Edouard@647:         if self.retrytimer is not None:
Edouard@647:             self.retrytimer.cancel()
Edouard@647: 
Edouard@647:         self.server.unregisterService(
Edouard@763:                                       Zeroconf.ServiceInfo(service_type, 
Edouard@647:                                                            self.service_name, 
Edouard@647:                                                            self.ip_32b, 
Edouard@647:                                                            self.port, 
Edouard@647:                                                            properties = self.serviceproperties))
Edouard@647:         self.server.close()
Edouard@647:         self.server = None
Edouard@647:     
Edouard@647:     def gethostaddr(self, dst = '224.0.1.41'):
Edouard@647:         s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
Edouard@647:         try:
Edouard@647:             s.connect((dst, 7))
Edouard@647:             (host, port) = s.getsockname()
Edouard@647:             s.close()
Edouard@647:             if host != '0.0.0.0':
Edouard@647:                 return host
Edouard@647:         except Exception,e:
Edouard@647:             pass
Edouard@647:         return socket.gethostbyname(socket.gethostname())