runtime/ServicePublisher.py
author laurent
Wed, 15 Feb 2012 00:32:01 +0100
changeset 683 57aa9da845d5
parent 648 73295e742da2
child 726 ae63ccc29444
permissions -rwxr-xr-x
Fix wrong panel size making strange background in topology plugin element
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
     1
#!/usr/bin/env python
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
     2
# -*- coding: utf-8 -*-
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
     3
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
     4
#This file is part of Beremiz, a Integrated Development Environment for
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
     5
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival. 
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
     6
#
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
     8
#
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
     9
#See COPYING file for copyrights details.
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    10
#
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    11
#This library is free software; you can redistribute it and/or
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    12
#modify it under the terms of the GNU General Public
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    13
#License as published by the Free Software Foundation; either
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    15
#
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    16
#This library is distributed in the hope that it will be useful,
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    19
#General Public License for more details.
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    20
#
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    21
#You should have received a copy of the GNU General Public
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    22
#License along with this library; if not, write to the Free Software
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    24
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    25
import Zeroconf, socket, threading
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    26
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    27
class ServicePublisher():
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    28
    def __init__(self):
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    29
        # type: fully qualified service type name
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    30
        self.service_type = '_PYRO._tcp.local.'
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    31
        # properties: dictionary of properties (or a string holding the bytes for the text field)
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    32
        self.serviceproperties = {'description':'Beremiz remote PLC'}
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    33
        
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    34
        self.name = None
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    35
        self.ip_32b = None
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    36
        self.port = None
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    37
        self.server = None
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    38
        self.service_name = None
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    39
        self.retrytimer = None
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    40
        
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    41
    def RegisterService(self, name, ip, port):
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    42
        try:
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    43
            self._RegisterService(name, ip, port)
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    44
        except Exception,e:
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    45
            self.retrytimer = threading.Timer(2,self.RegisterService,[name, ip, port])
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    46
            self.retrytimer.start() 
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    47
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    48
    def _RegisterService(self, name, ip, port):
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    49
        # name: fully qualified service name
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    50
        self.service_name = 'Beremiz_%s.%s'%(name,self.service_type)
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    51
        self.name = name
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    52
        self.port = port
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    53
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    54
        self.server = Zeroconf.Zeroconf(ip)
648
73295e742da2 Avoid starting Zeroconf if ip unspecified or set to localhost. Pick one interface address when given IP is 0.0.0.0
Edouard Tisserant
parents: 647
diff changeset
    55
        print "MDNS brodcasting on :"+ip
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    56
648
73295e742da2 Avoid starting Zeroconf if ip unspecified or set to localhost. Pick one interface address when given IP is 0.0.0.0
Edouard Tisserant
parents: 647
diff changeset
    57
        if ip == "0.0.0.0":
73295e742da2 Avoid starting Zeroconf if ip unspecified or set to localhost. Pick one interface address when given IP is 0.0.0.0
Edouard Tisserant
parents: 647
diff changeset
    58
            ip = self.gethostaddr()
73295e742da2 Avoid starting Zeroconf if ip unspecified or set to localhost. Pick one interface address when given IP is 0.0.0.0
Edouard Tisserant
parents: 647
diff changeset
    59
        print "MDNS brodcasted service address :"+ip
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    60
        self.ip_32b = socket.inet_aton(ip)
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    61
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    62
        self.server.registerService(
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    63
             Zeroconf.ServiceInfo(self.service_type,
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    64
                                  self.service_name,
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    65
                                  self.ip_32b,
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    66
                                  self.port,
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    67
                                  properties = self.serviceproperties))
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    68
        self.retrytimer=None
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    69
    
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    70
    def UnRegisterService(self):
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    71
        if self.retrytimer is not None:
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    72
            self.retrytimer.cancel()
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    73
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    74
        self.server.unregisterService(
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    75
                                      Zeroconf.ServiceInfo(self.service_type, 
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    76
                                                           self.service_name, 
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    77
                                                           self.ip_32b, 
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    78
                                                           self.port, 
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    79
                                                           properties = self.serviceproperties))
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    80
        self.server.close()
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    81
        self.server = None
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    82
    
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    83
    def gethostaddr(self, dst = '224.0.1.41'):
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    84
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    85
        try:
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    86
            s.connect((dst, 7))
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    87
            (host, port) = s.getsockname()
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    88
            s.close()
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    89
            if host != '0.0.0.0':
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    90
                return host
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    91
        except Exception,e:
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    92
            pass
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    93
        return socket.gethostbyname(socket.gethostname())