author | Edouard Tisserant |
Mon, 17 Oct 2011 21:43:20 +0200 | |
changeset 629 | 61347cc521d1 |
parent 606 | 780bd150ebba |
child 643 | 1013a69ed1ea |
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 |
||
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 | 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 |
|
39 |
||
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 | 43 |
self.service_name = 'Beremiz_%s.%s'%(name,self.service_type) |
44 |
self.name = name |
|
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 | 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 | 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 | 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 | 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 | 57 |
self.server.registerService( |
58 |
Zeroconf.ServiceInfo(self.service_type, |
|
59 |
self.service_name, |
|
60 |
self.ip_32b, |
|
61 |
self.port, |
|
62 |
properties = self.serviceproperties)) |
|
63 |
||
64 |
def UnRegisterService(self): |
|
65 |
self.server.unregisterService( |
|
66 |
Zeroconf.ServiceInfo(self.service_type, |
|
67 |
self.service_name, |
|
68 |
self.ip_32b, |
|
69 |
self.port, |
|
70 |
properties = self.serviceproperties)) |
|
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 | 73 |
|
74 |
def gethostaddr(self, dst = '224.0.1.41'): |
|
75 |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
|
76 |
try: |
|
77 |
s.connect((dst, 7)) |
|
78 |
(host, port) = s.getsockname() |
|
79 |
s.close() |
|
80 |
if host != '0.0.0.0': |
|
81 |
return host |
|
82 |
except error: |
|
83 |
pass |
|
84 |
return socket.gethostbyname(socket.gethostname()) |