author | Andrey Skvortsov <andrej.skvortzov@gmail.com> |
Fri, 28 Jul 2017 15:34:11 +0300 | |
changeset 1719 | 704c7036db85 |
parent 1667 | cefc9219bb48 |
child 1730 | 64d8f52bc8c8 |
permissions | -rwxr-xr-x |
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 |
|
1667
cefc9219bb48
runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
4 |
# This file is part of Beremiz runtime. |
647
edce87412f5a
converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents:
646
diff
changeset
|
5 |
# |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
763
diff
changeset
|
6 |
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
647
edce87412f5a
converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents:
646
diff
changeset
|
7 |
# |
1667
cefc9219bb48
runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
8 |
# See COPYING.Runtime file for copyrights details. |
647
edce87412f5a
converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents:
646
diff
changeset
|
9 |
# |
1667
cefc9219bb48
runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
10 |
# This library is free software; you can redistribute it and/or |
cefc9219bb48
runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
11 |
# modify it under the terms of the GNU Lesser General Public |
cefc9219bb48
runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
12 |
# License as published by the Free Software Foundation; either |
cefc9219bb48
runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
13 |
# version 2.1 of the License, or (at your option) any later version. |
cefc9219bb48
runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
14 |
|
cefc9219bb48
runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
15 |
# This library is distributed in the hope that it will be useful, |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
763
diff
changeset
|
16 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
1667
cefc9219bb48
runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
17 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
cefc9219bb48
runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
18 |
# Lesser General Public License for more details. |
cefc9219bb48
runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
19 |
|
cefc9219bb48
runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
20 |
# You should have received a copy of the GNU Lesser General Public |
cefc9219bb48
runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
21 |
# License along with this library; if not, write to the Free Software |
cefc9219bb48
runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
22 |
# 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
|
23 |
|
726 | 24 |
import socket, threading |
25 |
from util import Zeroconf |
|
647
edce87412f5a
converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents:
646
diff
changeset
|
26 |
|
763
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
726
diff
changeset
|
27 |
service_type = '_PYRO._tcp.local.' |
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
726
diff
changeset
|
28 |
|
647
edce87412f5a
converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents:
646
diff
changeset
|
29 |
class ServicePublisher(): |
edce87412f5a
converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents:
646
diff
changeset
|
30 |
def __init__(self): |
edce87412f5a
converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents:
646
diff
changeset
|
31 |
# type: fully qualified service type name |
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 |
763
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
726
diff
changeset
|
50 |
self.service_name = 'Beremiz_%s.%s'%(name,service_type) |
647
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( |
763
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
726
diff
changeset
|
63 |
Zeroconf.ServiceInfo(service_type, |
647
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( |
763
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
726
diff
changeset
|
75 |
Zeroconf.ServiceInfo(service_type, |
647
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()) |