runtime/ServicePublisher.py
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Fri, 13 Jan 2017 15:08:40 +0300
changeset 1629 80eee3c5a057
parent 1571 486f94a8032c
child 1667 cefc9219bb48
permissions -rwxr-xr-x
attach checkedBlockDict variable to the body object
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
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 763
diff changeset
     4
# This file is part of Beremiz, a Integrated Development Environment for
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 763
diff changeset
     5
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
     6
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 763
diff changeset
     7
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
     8
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 763
diff changeset
     9
# See COPYING file for copyrights details.
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    10
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 763
diff changeset
    11
# This program is free software; you can redistribute it and/or
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 763
diff changeset
    12
# modify it under the terms of the GNU General Public License
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 763
diff changeset
    13
# as published by the Free Software Foundation; either version 2
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 763
diff changeset
    14
# of the License, or (at your option) any later version.
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    15
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 763
diff changeset
    16
# This program is distributed in the hope that it will be useful,
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 763
diff changeset
    17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 763
diff changeset
    18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 763
diff changeset
    19
# GNU General Public License for more details.
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    20
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 763
diff changeset
    21
# You should have received a copy of the GNU General Public License
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 763
diff changeset
    22
# along with this program; if not, write to the Free Software
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 763
diff changeset
    23
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    24
726
ae63ccc29444 refactoring
Edouard Tisserant
parents: 648
diff changeset
    25
import socket, threading
ae63ccc29444 refactoring
Edouard Tisserant
parents: 648
diff changeset
    26
from util import Zeroconf
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    27
763
c1104099c151 Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents: 726
diff changeset
    28
service_type = '_PYRO._tcp.local.'
c1104099c151 Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents: 726
diff changeset
    29
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    30
class ServicePublisher():
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    31
    def __init__(self):
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    32
        # type: fully qualified service type name
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    33
        self.serviceproperties = {'description':'Beremiz remote PLC'}
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    34
        
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    35
        self.name = None
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    36
        self.ip_32b = None
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    37
        self.port = None
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    38
        self.server = None
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    39
        self.service_name = None
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    40
        self.retrytimer = None
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    41
        
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    42
    def RegisterService(self, name, ip, port):
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    43
        try:
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    44
            self._RegisterService(name, ip, port)
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    45
        except Exception,e:
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    46
            self.retrytimer = threading.Timer(2,self.RegisterService,[name, ip, port])
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    47
            self.retrytimer.start() 
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    48
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    49
    def _RegisterService(self, name, ip, port):
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    50
        # name: fully qualified service name
763
c1104099c151 Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents: 726
diff changeset
    51
        self.service_name = 'Beremiz_%s.%s'%(name,service_type)
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    52
        self.name = name
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    53
        self.port = port
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    54
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    55
        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
    56
        print "MDNS brodcasting on :"+ip
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    57
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
    58
        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
    59
            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
    60
        print "MDNS brodcasted service address :"+ip
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    61
        self.ip_32b = socket.inet_aton(ip)
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    62
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    63
        self.server.registerService(
763
c1104099c151 Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents: 726
diff changeset
    64
             Zeroconf.ServiceInfo(service_type,
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    65
                                  self.service_name,
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    66
                                  self.ip_32b,
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    67
                                  self.port,
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    68
                                  properties = self.serviceproperties))
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    69
        self.retrytimer=None
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    70
    
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    71
    def UnRegisterService(self):
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    72
        if self.retrytimer is not None:
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    73
            self.retrytimer.cancel()
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    74
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    75
        self.server.unregisterService(
763
c1104099c151 Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents: 726
diff changeset
    76
                                      Zeroconf.ServiceInfo(service_type, 
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    77
                                                           self.service_name, 
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    78
                                                           self.ip_32b, 
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    79
                                                           self.port, 
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    80
                                                           properties = self.serviceproperties))
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    81
        self.server.close()
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    82
        self.server = None
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    83
    
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    84
    def gethostaddr(self, dst = '224.0.1.41'):
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    85
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    86
        try:
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    87
            s.connect((dst, 7))
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    88
            (host, port) = s.getsockname()
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    89
            s.close()
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    90
            if host != '0.0.0.0':
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    91
                return host
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    92
        except Exception,e:
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    93
            pass
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    94
        return socket.gethostbyname(socket.gethostname())