Edouard@647: #!/usr/bin/env python Edouard@647: # -*- coding: utf-8 -*- Edouard@647: Edouard@647: #This file is part of Beremiz, a Integrated Development Environment for Edouard@647: #programming IEC 61131-3 automates supporting plcopen standard and CanFestival. Edouard@647: # Edouard@647: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD Edouard@647: # Edouard@647: #See COPYING file for copyrights details. Edouard@647: # Edouard@647: #This library is free software; you can redistribute it and/or Edouard@647: #modify it under the terms of the GNU General Public Edouard@647: #License as published by the Free Software Foundation; either Edouard@647: #version 2.1 of the License, or (at your option) any later version. Edouard@647: # Edouard@647: #This library is distributed in the hope that it will be useful, Edouard@647: #but WITHOUT ANY WARRANTY; without even the implied warranty of Edouard@647: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Edouard@647: #General Public License for more details. Edouard@647: # Edouard@647: #You should have received a copy of the GNU General Public Edouard@647: #License along with this library; if not, write to the Free Software Edouard@647: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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())