author | Edouard Tisserant |
Tue, 22 Nov 2011 02:06:15 +0100 | |
changeset 646 | 3b48c7ed7837 |
parent 645 | 616f86c13443 |
child 647 | edce87412f5a |
permissions | -rwxr-xr-x |
203 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of Beremiz, a Integrated Development Environment for |
|
5 |
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival. |
|
6 |
# |
|
7 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
8 |
# |
|
9 |
#See COPYING file for copyrights details. |
|
10 |
# |
|
11 |
#This library is free software; you can redistribute it and/or |
|
12 |
#modify it under the terms of the GNU General Public |
|
13 |
#License as published by the Free Software Foundation; either |
|
14 |
#version 2.1 of the License, or (at your option) any later version. |
|
15 |
# |
|
16 |
#This library is distributed in the hope that it will be useful, |
|
17 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
19 |
#General Public License for more details. |
|
20 |
# |
|
21 |
#You should have received a copy of the GNU General Public |
|
22 |
#License along with this library; if not, write to the Free Software |
|
23 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
||
646
3b48c7ed7837
added periodic retry for zeroconf, in case network interface is not ready
Edouard Tisserant
parents:
645
diff
changeset
|
25 |
import Zeroconf, socket, threading |
203 | 26 |
|
262 | 27 |
class ServicePublisher(): |
28 |
def __init__(self): |
|
29 |
# type: fully qualified service type name |
|
30 |
self.service_type = '_PYRO._tcp.local.' |
|
31 |
# properties: dictionary of properties (or a string holding the bytes for the text field) |
|
32 |
self.serviceproperties = {'description':'Beremiz remote PLC'} |
|
33 |
||
34 |
self.name = None |
|
35 |
self.ip_32b = None |
|
36 |
self.port = None |
|
37 |
self.server = None |
|
38 |
self.service_name = None |
|
646
3b48c7ed7837
added periodic retry for zeroconf, in case network interface is not ready
Edouard Tisserant
parents:
645
diff
changeset
|
39 |
self.retrytimer = None |
262 | 40 |
|
41 |
def RegisterService(self, name, ip, port): |
|
646
3b48c7ed7837
added periodic retry for zeroconf, in case network interface is not ready
Edouard Tisserant
parents:
645
diff
changeset
|
42 |
try: |
3b48c7ed7837
added periodic retry for zeroconf, in case network interface is not ready
Edouard Tisserant
parents:
645
diff
changeset
|
43 |
self._RegisterService(name, ip, port) |
3b48c7ed7837
added periodic retry for zeroconf, in case network interface is not ready
Edouard Tisserant
parents:
645
diff
changeset
|
44 |
except Exception,e: |
3b48c7ed7837
added periodic retry for zeroconf, in case network interface is not ready
Edouard Tisserant
parents:
645
diff
changeset
|
45 |
self.retrytimer = threading.Timer(2,self.RegisterService,[name, ip, port]) |
3b48c7ed7837
added periodic retry for zeroconf, in case network interface is not ready
Edouard Tisserant
parents:
645
diff
changeset
|
46 |
self.retrytimer.start() |
217
f3eb35df4d87
Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents:
203
diff
changeset
|
47 |
|
646
3b48c7ed7837
added periodic retry for zeroconf, in case network interface is not ready
Edouard Tisserant
parents:
645
diff
changeset
|
48 |
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
|
49 |
# name: fully qualified service name |
262 | 50 |
self.service_name = 'Beremiz_%s.%s'%(name,self.service_type) |
51 |
self.name = name |
|
52 |
self.port = port |
|
217
f3eb35df4d87
Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents:
203
diff
changeset
|
53 |
# No ip params -> get host ip |
645
616f86c13443
ServicePublisher.py was missing in previous commit
Edouard Tisserant
parents:
643
diff
changeset
|
54 |
if ip is None: |
262 | 55 |
ip = self.gethostaddr() |
217
f3eb35df4d87
Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents:
203
diff
changeset
|
56 |
|
262 | 57 |
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
|
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 |
262 | 60 |
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
|
61 |
|
262 | 62 |
self.server.registerService( |
63 |
Zeroconf.ServiceInfo(self.service_type, |
|
64 |
self.service_name, |
|
65 |
self.ip_32b, |
|
66 |
self.port, |
|
67 |
properties = self.serviceproperties)) |
|
646
3b48c7ed7837
added periodic retry for zeroconf, in case network interface is not ready
Edouard Tisserant
parents:
645
diff
changeset
|
68 |
print "MDNS brodcasting on :"+ip |
3b48c7ed7837
added periodic retry for zeroconf, in case network interface is not ready
Edouard Tisserant
parents:
645
diff
changeset
|
69 |
self.retrytimer=None |
262 | 70 |
|
71 |
def UnRegisterService(self): |
|
646
3b48c7ed7837
added periodic retry for zeroconf, in case network interface is not ready
Edouard Tisserant
parents:
645
diff
changeset
|
72 |
if self.retrytimer is not None: |
3b48c7ed7837
added periodic retry for zeroconf, in case network interface is not ready
Edouard Tisserant
parents:
645
diff
changeset
|
73 |
self.retrytimer.cancel() |
3b48c7ed7837
added periodic retry for zeroconf, in case network interface is not ready
Edouard Tisserant
parents:
645
diff
changeset
|
74 |
|
262 | 75 |
self.server.unregisterService( |
76 |
Zeroconf.ServiceInfo(self.service_type, |
|
77 |
self.service_name, |
|
78 |
self.ip_32b, |
|
79 |
self.port, |
|
80 |
properties = self.serviceproperties)) |
|
81 |
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
|
82 |
self.server = None |
262 | 83 |
|
84 |
def gethostaddr(self, dst = '224.0.1.41'): |
|
85 |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
|
86 |
try: |
|
87 |
s.connect((dst, 7)) |
|
88 |
(host, port) = s.getsockname() |
|
89 |
s.close() |
|
90 |
if host != '0.0.0.0': |
|
91 |
return host |
|
643 | 92 |
except Exception,e: |
262 | 93 |
pass |
94 |
return socket.gethostbyname(socket.gethostname()) |