runtime/ServicePublisher.py
changeset 217 f3eb35df4d87
parent 203 cb9901076a21
child 262 141a7145c099
equal deleted inserted replaced
216:11124e129a28 217:f3eb35df4d87
    20 #
    20 #
    21 #You should have received a copy of the GNU General Public
    21 #You should have received a copy of the GNU General Public
    22 #License along with this library; if not, write to the Free Software
    22 #License along with this library; if not, write to the Free Software
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    24 
    24 
    25 import Zeroconf
    25 import Zeroconf, socket
    26 
    26 
    27 class PublishService():
    27 # type: fully qualified service type name
    28     def __init__(self):
    28 service_type = '_PYRO._tcp.local.'
    29         self.server = Zeroconf.Zeroconf()
       
    30 
    29 
    31     def ConfigureService(self, type, name, address, port, description):
    30 # properties: dictionary of properties (or a string holding the bytes for the text field)
    32         self.newservice = Zeroconf.ServiceInfo(type,
    31 serviceproperties = {'description':'Beremiz remote PLC'}
    33                                   name,
    32 
    34                                   address,
    33 def gethostaddr(dst = '224.0.1.41'):
       
    34     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
       
    35     try:
       
    36         s.connect((dst, 7))
       
    37         (host, port) = s.getsockname()
       
    38         s.close()
       
    39         if host != '0.0.0.0':
       
    40             return host
       
    41     except error:
       
    42         pass
       
    43     return socket.gethostbyname(socket.gethostname())
       
    44 
       
    45 
       
    46 def ServicePublisher(name, ip, port):
       
    47 
       
    48         # name: fully qualified service name
       
    49         service_name = 'Beremiz_%s.%s'%(name,service_type)
       
    50 
       
    51         # No ip params -> get host ip
       
    52         if ip == "":
       
    53             ip = gethostaddr()
       
    54 
       
    55         print "Mon IP est :"+ip
       
    56 
       
    57         server = Zeroconf.Zeroconf(ip)
       
    58 
       
    59         # address: IP address as unsigned short, network byte order
       
    60         ip_32b = socket.inet_aton(ip)
       
    61 
       
    62         server.registerService(
       
    63              Zeroconf.ServiceInfo(service_type,
       
    64                                   service_name,
       
    65                                   ip_32b,
    35                                   port,
    66                                   port,
    36                                   weight = 0, # weight: weight of the service
    67                                   properties = serviceproperties))
    37                                   priority= 0, # priority: priority of the service
    68         
    38                                   properties = description)
    69         return server
    39     
       
    40     def PublishService(self):
       
    41         self.server.registerService(self.newservice)