runtime/ServicePublisher.py
author lbessard
Wed, 10 Sep 2008 16:44:27 +0200
changeset 252 5c8167203f6e
parent 217 f3eb35df4d87
child 262 141a7145c099
permissions -rwxr-xr-x
Bug with FBDPanel fixed
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
217
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    27
# type: fully qualified service type name
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    28
service_type = '_PYRO._tcp.local.'
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    29
217
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    30
# properties: dictionary of properties (or a string holding the bytes for the text field)
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    31
serviceproperties = {'description':'Beremiz remote PLC'}
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    32
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    33
def gethostaddr(dst = '224.0.1.41'):
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    34
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    35
    try:
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    36
        s.connect((dst, 7))
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    37
        (host, port) = s.getsockname()
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    38
        s.close()
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    39
        if host != '0.0.0.0':
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    40
            return host
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    41
    except error:
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    42
        pass
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    43
    return socket.gethostbyname(socket.gethostname())
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    44
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    45
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    46
def ServicePublisher(name, ip, port):
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    47
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    48
        # name: fully qualified service name
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    49
        service_name = 'Beremiz_%s.%s'%(name,service_type)
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    50
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    51
        # 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
    52
        if ip == "":
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    53
            ip = gethostaddr()
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    54
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    55
        print "Mon IP est :"+ip
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    56
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    57
        server = Zeroconf.Zeroconf(ip)
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    58
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    59
        # address: IP address as unsigned short, network byte order
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    60
        ip_32b = socket.inet_aton(ip)
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    61
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    62
        server.registerService(
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    63
             Zeroconf.ServiceInfo(service_type,
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    64
                                  service_name,
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    65
                                  ip_32b,
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    66
                                  port,
217
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    67
                                  properties = serviceproperties))
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    68
        
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 203
diff changeset
    69
        return server