author | lbessard |
Sun, 07 Sep 2008 16:43:05 +0200 | |
changeset 248 | 444d4db3faea |
parent 217 | f3eb35df4d87 |
child 262 | 141a7145c099 |
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 |
|
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 | 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 | 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 |