etisserant@203: #!/usr/bin/env python etisserant@203: # -*- coding: utf-8 -*- etisserant@203: etisserant@203: #This file is part of Beremiz, a Integrated Development Environment for etisserant@203: #programming IEC 61131-3 automates supporting plcopen standard and CanFestival. etisserant@203: # etisserant@203: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD etisserant@203: # etisserant@203: #See COPYING file for copyrights details. etisserant@203: # etisserant@203: #This library is free software; you can redistribute it and/or etisserant@203: #modify it under the terms of the GNU General Public etisserant@203: #License as published by the Free Software Foundation; either etisserant@203: #version 2.1 of the License, or (at your option) any later version. etisserant@203: # etisserant@203: #This library is distributed in the hope that it will be useful, etisserant@203: #but WITHOUT ANY WARRANTY; without even the implied warranty of etisserant@203: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU etisserant@203: #General Public License for more details. etisserant@203: # etisserant@203: #You should have received a copy of the GNU General Public etisserant@203: #License along with this library; if not, write to the Free Software etisserant@203: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA etisserant@203: etisserant@217: import Zeroconf, socket etisserant@203: greg@262: class ServicePublisher(): greg@262: def __init__(self): greg@262: # type: fully qualified service type name greg@262: self.service_type = '_PYRO._tcp.local.' greg@262: # properties: dictionary of properties (or a string holding the bytes for the text field) greg@262: self.serviceproperties = {'description':'Beremiz remote PLC'} greg@262: greg@262: self.name = None greg@262: self.ip_32b = None greg@262: self.port = None greg@262: self.server = None greg@262: self.service_name = None greg@262: greg@262: def RegisterService(self, name, ip, port): etisserant@217: etisserant@217: # name: fully qualified service name greg@262: self.service_name = 'Beremiz_%s.%s'%(name,self.service_type) greg@262: self.name = name greg@262: self.port = port etisserant@217: # No ip params -> get host ip etisserant@217: if ip == "": greg@262: ip = self.gethostaddr() etisserant@217: greg@262: print "My IP is :"+ip etisserant@217: greg@262: self.server = Zeroconf.Zeroconf(ip) etisserant@217: etisserant@217: # address: IP address as unsigned short, network byte order greg@262: self.ip_32b = socket.inet_aton(ip) etisserant@217: greg@262: self.server.registerService( greg@262: Zeroconf.ServiceInfo(self.service_type, greg@262: self.service_name, greg@262: self.ip_32b, greg@262: self.port, greg@262: properties = self.serviceproperties)) greg@262: greg@262: def UnRegisterService(self): greg@262: self.server.unregisterService( greg@262: Zeroconf.ServiceInfo(self.service_type, greg@262: self.service_name, greg@262: self.ip_32b, greg@262: self.port, greg@262: properties = self.serviceproperties)) greg@262: self.server.close() greg@262: del self.server greg@262: greg@262: def gethostaddr(self, dst = '224.0.1.41'): greg@262: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) greg@262: try: greg@262: s.connect((dst, 7)) greg@262: (host, port) = s.getsockname() greg@262: s.close() greg@262: if host != '0.0.0.0': greg@262: return host greg@262: except error: greg@262: pass greg@262: return socket.gethostbyname(socket.gethostname())