author | lbessard |
Fri, 22 Aug 2008 13:34:58 +0200 | |
changeset 213 | d8dc1e51c3bf |
parent 210 | ca3bfcf61192 |
child 217 | f3eb35df4d87 |
permissions | -rw-r--r-- |
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 |
||
25 |
import os, sys, getopt, socket |
|
26 |
||
27 |
def usage(): |
|
206 | 28 |
print """ |
29 |
Usage of Beremiz PLC execution service :\n |
|
30 |
%s {[-a ip] [-d path] [-p port]|-h|--help} |
|
31 |
-a, --address - authorized ip to connect (x.x.x.x) |
|
32 |
-d, --directory path - set the working directory |
|
33 |
-p, --port port number - set the port number |
|
34 |
-h, --help - print this help text and quit |
|
35 |
"""%sys.argv[0] |
|
203 | 36 |
|
37 |
try: |
|
210
ca3bfcf61192
Fixed oddities in arguments/options parsing and passing to plugins initialization...
etisserant
parents:
208
diff
changeset
|
38 |
opts, args = getopt.getopt(sys.argv[1:], "a:p:h", ["help"]) |
206 | 39 |
except getopt.GetoptError, err: |
203 | 40 |
# print help information and exit: |
206 | 41 |
print str(err) # will print something like "option -a not recognized" |
203 | 42 |
usage() |
43 |
sys.exit(2) |
|
44 |
||
206 | 45 |
# default values |
46 |
ip = "" |
|
47 |
port = 3000 |
|
210
ca3bfcf61192
Fixed oddities in arguments/options parsing and passing to plugins initialization...
etisserant
parents:
208
diff
changeset
|
48 |
print opts |
203 | 49 |
for o, a in opts: |
50 |
if o in ("-h", "--help"): |
|
51 |
usage() |
|
52 |
sys.exit() |
|
206 | 53 |
elif o in ("-a", "--address"): |
54 |
if len(a.split(".")) == 4 or a == "localhost": |
|
55 |
ip = a |
|
56 |
elif o in ("-p", "--port"): |
|
57 |
# port: port that the service runs on |
|
58 |
port = int(a) |
|
59 |
else: |
|
60 |
usage() |
|
61 |
sys.exit() |
|
203 | 62 |
|
210
ca3bfcf61192
Fixed oddities in arguments/options parsing and passing to plugins initialization...
etisserant
parents:
208
diff
changeset
|
63 |
if len(args) > 1: |
ca3bfcf61192
Fixed oddities in arguments/options parsing and passing to plugins initialization...
etisserant
parents:
208
diff
changeset
|
64 |
usage() |
ca3bfcf61192
Fixed oddities in arguments/options parsing and passing to plugins initialization...
etisserant
parents:
208
diff
changeset
|
65 |
sys.exit() |
ca3bfcf61192
Fixed oddities in arguments/options parsing and passing to plugins initialization...
etisserant
parents:
208
diff
changeset
|
66 |
elif len(args) == 1: |
ca3bfcf61192
Fixed oddities in arguments/options parsing and passing to plugins initialization...
etisserant
parents:
208
diff
changeset
|
67 |
WorkingDir = args[0] |
ca3bfcf61192
Fixed oddities in arguments/options parsing and passing to plugins initialization...
etisserant
parents:
208
diff
changeset
|
68 |
elif len(args) == 0: |
ca3bfcf61192
Fixed oddities in arguments/options parsing and passing to plugins initialization...
etisserant
parents:
208
diff
changeset
|
69 |
WorkingDir = os.getcwd() |
ca3bfcf61192
Fixed oddities in arguments/options parsing and passing to plugins initialization...
etisserant
parents:
208
diff
changeset
|
70 |
else: |
ca3bfcf61192
Fixed oddities in arguments/options parsing and passing to plugins initialization...
etisserant
parents:
208
diff
changeset
|
71 |
usage() |
ca3bfcf61192
Fixed oddities in arguments/options parsing and passing to plugins initialization...
etisserant
parents:
208
diff
changeset
|
72 |
sys.exit() |
ca3bfcf61192
Fixed oddities in arguments/options parsing and passing to plugins initialization...
etisserant
parents:
208
diff
changeset
|
73 |
|
203 | 74 |
from runtime import PLCObject, ServicePublisher |
75 |
import Pyro.core as pyro |
|
76 |
||
77 |
if not os.path.isdir(WorkingDir): |
|
78 |
os.mkdir(WorkingDir) |
|
79 |
||
80 |
# type: fully qualified service type name |
|
81 |
type = '_PYRO._tcp.local.' |
|
82 |
# name: fully qualified service name |
|
83 |
name = 'First test.%s'%(type) |
|
84 |
# address: IP address as unsigned short, network byte order |
|
85 |
||
86 |
def gethostaddr(dst = '224.0.1.41'): |
|
87 |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
|
88 |
try: |
|
89 |
s.connect((dst, 7)) |
|
90 |
(host, port) = s.getsockname() |
|
91 |
s.close() |
|
92 |
if host != '0.0.0.0': |
|
93 |
return host |
|
94 |
except error: |
|
95 |
pass |
|
96 |
return socket.gethostbyname(socket.gethostname()) |
|
97 |
||
98 |
# properties: dictionary of properties (or a string holding the bytes for the text field) |
|
99 |
serviceproperties = {'description':'Remote control for PLC'} |
|
100 |
||
101 |
pyro.initServer() |
|
102 |
daemon=pyro.Daemon(host=ip, port=port) |
|
210
ca3bfcf61192
Fixed oddities in arguments/options parsing and passing to plugins initialization...
etisserant
parents:
208
diff
changeset
|
103 |
uri = daemon.connect(PLCObject(WorkingDir, daemon, args),"PLCObject") |
207 | 104 |
|
203 | 105 |
print "The daemon runs on port :",daemon.port |
106 |
print "The object's uri is :",uri |
|
107 |
print "The working directory :",WorkingDir |
|
108 |
||
109 |
# Configure and publish service |
|
207 | 110 |
# Not publish service if localhost in address params |
111 |
if ip != "localhost" and ip != "127.0.0.1": |
|
112 |
# No ip params -> get host ip |
|
113 |
if ip == "": |
|
114 |
ip_32b = socket.inet_aton(gethostaddr(ip)) |
|
115 |
else: |
|
116 |
ip_32b = ip |
|
117 |
print "Publish service on local network" |
|
118 |
service = ServicePublisher.PublishService() |
|
119 |
service.ConfigureService(type, name, ip_32b, port, serviceproperties) |
|
120 |
service.PublishService() |
|
203 | 121 |
|
122 |
daemon.requestLoop() |