runtime/ServicePublisher.py
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Thu, 27 Jul 2017 20:34:38 +0000
branch1.1 Korean release
changeset 2563 18b6352e096a
parent 763 c1104099c151
child 1571 486f94a8032c
permissions -rwxr-xr-x
Close branch 1.1 Korean release.
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
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())