runtime/ServicePublisher.py
author Edouard Tisserant
Thu, 22 Sep 2011 17:33:34 +0200
changeset 614 eed1dcf311a1
parent 606 780bd150ebba
child 643 1013a69ed1ea
permissions -rwxr-xr-x
added return type to suspendDebug
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     1
#!/usr/bin/env python
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     3
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     4
#This file is part of Beremiz, a Integrated Development Environment for
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     5
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival. 
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     6
#
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     8
#
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     9
#See COPYING file for copyrights details.
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    10
#
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    12
#modify it under the terms of the GNU General Public
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    15
#
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    19
#General Public License for more details.
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    20
#
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    21
#You should have received a copy of the GNU General Public
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    24
217
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    25
import Zeroconf, socket
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    26
262
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    27
class ServicePublisher():
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    28
    def __init__(self):
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    29
        # type: fully qualified service type name
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    30
        self.service_type = '_PYRO._tcp.local.'
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    31
        # properties: dictionary of properties (or a string holding the bytes for the text field)
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    32
        self.serviceproperties = {'description':'Beremiz remote PLC'}
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    33
        
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    34
        self.name = None
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    35
        self.ip_32b = None
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    36
        self.port = None
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    37
        self.server = None
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    38
        self.service_name = None
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    39
        
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    40
    def RegisterService(self, name, ip, port):
217
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    41
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    42
        # name: fully qualified service name
262
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    43
        self.service_name = 'Beremiz_%s.%s'%(name,self.service_type)
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    44
        self.name = name
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    45
        self.port = port
217
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    46
        # No ip params -> get host ip
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    47
        if ip == "":
262
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    48
            ip = self.gethostaddr()
217
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    49
262
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    50
        print "My IP is :"+ip
217
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    51
262
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    52
        self.server = Zeroconf.Zeroconf(ip)
217
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    53
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    54
        # address: IP address as unsigned short, network byte order
262
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    55
        self.ip_32b = socket.inet_aton(ip)
217
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    56
262
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    57
        self.server.registerService(
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    58
             Zeroconf.ServiceInfo(self.service_type,
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    59
                                  self.service_name,
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    60
                                  self.ip_32b,
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    61
                                  self.port,
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    62
                                  properties = self.serviceproperties))
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    63
    
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    64
    def UnRegisterService(self):
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    65
        self.server.unregisterService(
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    66
                                      Zeroconf.ServiceInfo(self.service_type, 
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    67
                                                           self.service_name, 
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    68
                                                           self.ip_32b, 
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    69
                                                           self.port, 
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    70
                                                           properties = self.serviceproperties))
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    71
        self.server.close()
606
780bd150ebba Fixed runtime with twisted but without Wx (-x 0 -t 1), made non-Zeroconf a default option, i.e when -n not specified
Edouard Tisserant
parents: 262
diff changeset
    72
        self.server = None
262
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    73
    
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    74
    def gethostaddr(self, dst = '224.0.1.41'):
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    75
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    76
        try:
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    77
            s.connect((dst, 7))
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    78
            (host, port) = s.getsockname()
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    79
            s.close()
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    80
            if host != '0.0.0.0':
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    81
                return host
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    82
        except error:
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    83
            pass
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 217
diff changeset
    84
        return socket.gethostbyname(socket.gethostname())