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: etisserant@217: # type: fully qualified service type name etisserant@217: service_type = '_PYRO._tcp.local.' etisserant@203: etisserant@217: # properties: dictionary of properties (or a string holding the bytes for the text field) etisserant@217: serviceproperties = {'description':'Beremiz remote PLC'} etisserant@217: etisserant@217: def gethostaddr(dst = '224.0.1.41'): etisserant@217: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) etisserant@217: try: etisserant@217: s.connect((dst, 7)) etisserant@217: (host, port) = s.getsockname() etisserant@217: s.close() etisserant@217: if host != '0.0.0.0': etisserant@217: return host etisserant@217: except error: etisserant@217: pass etisserant@217: return socket.gethostbyname(socket.gethostname()) etisserant@217: etisserant@217: etisserant@217: def ServicePublisher(name, ip, port): etisserant@217: etisserant@217: # name: fully qualified service name etisserant@217: service_name = 'Beremiz_%s.%s'%(name,service_type) etisserant@217: etisserant@217: # No ip params -> get host ip etisserant@217: if ip == "": etisserant@217: ip = gethostaddr() etisserant@217: etisserant@217: print "Mon IP est :"+ip etisserant@217: etisserant@217: server = Zeroconf.Zeroconf(ip) etisserant@217: etisserant@217: # address: IP address as unsigned short, network byte order etisserant@217: ip_32b = socket.inet_aton(ip) etisserant@217: etisserant@217: server.registerService( etisserant@217: Zeroconf.ServiceInfo(service_type, etisserant@217: service_name, etisserant@217: ip_32b, etisserant@203: port, etisserant@217: properties = serviceproperties)) etisserant@217: etisserant@217: return server