author | Andrey Skvortsov <andrej.skvortzov@gmail.com> |
Wed, 24 Aug 2016 13:08:13 +0300 | |
changeset 1520 | 6addc58f63a6 |
parent 1455 | 4ba27ed51e48 |
child 1571 | 486f94a8032c |
permissions | -rwxr-xr-x |
203 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
# |
|
1455 | 4 |
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
203 | 5 |
# |
1455 | 6 |
# See COPYING file for copyrights details. |
203 | 7 |
# |
1455 | 8 |
# This library is free software; you can redistribute it and/or |
9 |
# modify it under the terms of the GNU General Public |
|
10 |
# License as published by the Free Software Foundation; either |
|
11 |
# version 2.1 of the License, or (at your option) any later version. |
|
203 | 12 |
# |
1455 | 13 |
# This library is distributed in the hope that it will be useful, |
14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
16 |
# General Public License for more details. |
|
203 | 17 |
# |
1455 | 18 |
# You should have received a copy of the GNU General Public |
19 |
# License along with this library; if not, write to the Free Software |
|
20 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
21 |
import Pyro |
|
22 |
import Pyro.core |
|
23 |
import Pyro.util |
|
203 | 24 |
from Pyro.errors import PyroError |
25 |
import traceback |
|
26 |
from time import sleep |
|
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
27 |
import copy |
763
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
28 |
import socket |
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
29 |
service_type = '_PYRO._tcp.local.' |
1455 | 30 |
import os.path |
399 | 31 |
# this module attribute contains a list of DNS-SD (Zeroconf) service types |
717 | 32 |
# supported by this connector confnode. |
399 | 33 |
# |
34 |
# for connectors that do not support DNS-SD, this attribute can be omitted |
|
35 |
# or set to an empty list. |
|
36 |
||
717 | 37 |
def PYRO_connector_factory(uri, confnodesroot): |
203 | 38 |
""" |
39 |
This returns the connector to Pyro style PLCobject |
|
40 |
""" |
|
1455 | 41 |
confnodesroot.logger.write(_("PYRO connecting to URI : %s\n") % uri) |
203 | 42 |
|
43 |
servicetype, location = uri.split("://") |
|
1455 | 44 |
if servicetype == "PYROS": |
45 |
schemename = "PYROLOCSSL" |
|
46 |
# Protect against name->IP substitution in Pyro3 |
|
47 |
Pyro.config.PYRO_DNS_URI = True |
|
48 |
# Beware Pyro lib need str path, not unicode |
|
49 |
# don't rely on PYRO_STORAGE ! see documentation |
|
50 |
Pyro.config.PYROSSL_CERTDIR = os.path.abspath(str(confnodesroot.ProjectPath) + '/certs') |
|
51 |
if not os.path.exists(Pyro.config.PYROSSL_CERTDIR): |
|
52 |
confnodesroot.logger.write_error( |
|
53 |
'Error : the directory %s is missing for SSL certificates (certs_dir).' |
|
54 |
'Please fix it in your project.\n' % Pyro.config.PYROSSL_CERTDIR) |
|
55 |
return None |
|
56 |
else: |
|
57 |
confnodesroot.logger.write(_("PYRO using certificates in '%s' \n") |
|
58 |
% (Pyro.config.PYROSSL_CERTDIR)) |
|
59 |
Pyro.config.PYROSSL_CERT = "client.crt" |
|
60 |
Pyro.config.PYROSSL_KEY = "client.key" |
|
61 |
# Ugly Monkey Patching |
|
62 |
def _gettimeout(self): |
|
63 |
return self.timeout |
|
64 |
||
65 |
def _settimeout(self, timeout): |
|
66 |
self.timeout = timeout |
|
67 |
from M2Crypto.SSL import Connection |
|
68 |
Connection.timeout = None |
|
69 |
Connection.gettimeout = _gettimeout |
|
70 |
Connection.settimeout = _settimeout |
|
71 |
# M2Crypto.SSL.Checker.WrongHost: Peer certificate commonName does not |
|
72 |
# match host, expected 127.0.0.1, got server |
|
73 |
Connection.clientPostConnectionCheck = None |
|
74 |
else: |
|
75 |
schemename = "PYROLOC" |
|
763
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
76 |
if location.find(service_type) != -1: |
1455 | 77 |
try: |
763
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
78 |
from util.Zeroconf import Zeroconf |
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
79 |
r = Zeroconf() |
1455 | 80 |
i = r.getServiceInfo(service_type, location) |
81 |
if i is None: |
|
82 |
raise Exception("'%s' not found" % location) |
|
763
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
83 |
ip = str(socket.inet_ntoa(i.getAddress())) |
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
84 |
port = str(i.getPort()) |
1455 | 85 |
newlocation = ip + ':' + port |
86 |
confnodesroot.logger.write(_("'%s' is located at %s\n") % (location, newlocation)) |
|
763
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
87 |
location = newlocation |
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
88 |
r.close() |
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
89 |
except Exception, msg: |
1455 | 90 |
confnodesroot.logger.write_error(_("MDNS resolution failure for '%s'\n") % location) |
763
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
91 |
confnodesroot.logger.write_error(traceback.format_exc()) |
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
92 |
return None |
1434
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1116
diff
changeset
|
93 |
|
203 | 94 |
# Try to get the proxy object |
1455 | 95 |
try: |
96 |
RemotePLCObjectProxy = Pyro.core.getAttrProxyForURI(schemename + "://" + location + "/PLCObject") |
|
203 | 97 |
except Exception, msg: |
1455 | 98 |
confnodesroot.logger.write_error(_("Connection to '%s' failed.\n") % location) |
717 | 99 |
confnodesroot.logger.write_error(traceback.format_exc()) |
203 | 100 |
return None |
101 |
||
102 |
def PyroCatcher(func, default=None): |
|
103 |
""" |
|
1455 | 104 |
A function that catch a Pyro exceptions, write error to logger |
105 |
and return default value when it happen |
|
203 | 106 |
""" |
1455 | 107 |
def catcher_func(*args, **kwargs): |
203 | 108 |
try: |
1455 | 109 |
return func(*args, **kwargs) |
1116
300f98a8d4c6
Fixed bug connector not resetted when connection is lost
Laurent Bessard
parents:
1070
diff
changeset
|
110 |
except Pyro.errors.ConnectionClosedError, e: |
300f98a8d4c6
Fixed bug connector not resetted when connection is lost
Laurent Bessard
parents:
1070
diff
changeset
|
111 |
confnodesroot.logger.write_error("Connection lost!\n") |
300f98a8d4c6
Fixed bug connector not resetted when connection is lost
Laurent Bessard
parents:
1070
diff
changeset
|
112 |
confnodesroot._SetConnector(None) |
493
015a803301b9
Catch ProtocolError exception when connection failed
laurent
parents:
486
diff
changeset
|
113 |
except Pyro.errors.ProtocolError, e: |
1455 | 114 |
confnodesroot.logger.write_error("Pyro exception: " + str(e) + "\n") |
115 |
except Exception, e: |
|
116 |
# confnodesroot.logger.write_error(traceback.format_exc()) |
|
477
f66a092b6e74
Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
465
diff
changeset
|
117 |
errmess = ''.join(Pyro.util.getPyroTraceback(e)) |
1455 | 118 |
confnodesroot.logger.write_error(errmess + "\n") |
477
f66a092b6e74
Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
465
diff
changeset
|
119 |
print errmess |
1116
300f98a8d4c6
Fixed bug connector not resetted when connection is lost
Laurent Bessard
parents:
1070
diff
changeset
|
120 |
confnodesroot._SetConnector(None) |
1070
86ee833e33ef
Added exception printing on Pyro connector, tracking random connection failure...
Edouard Tisserant
parents:
1035
diff
changeset
|
121 |
return default |
218 | 122 |
return catcher_func |
203 | 123 |
|
1434
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1116
diff
changeset
|
124 |
# Check connection is effective. |
203 | 125 |
# lambda is for getattr of GetPLCstatus to happen inside catcher |
1455 | 126 |
if PyroCatcher(lambda: RemotePLCObjectProxy.GetPLCstatus())() is None: |
717 | 127 |
confnodesroot.logger.write_error(_("Cannot get PLC status - connection failed.\n")) |
203 | 128 |
return None |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
129 |
|
1441
826730e60407
Added auto-reconnect for runtime. Fixed Beremiz closing problem caused by remaining twisted reactor thread in IDE.
Edouard Tisserant
parents:
1440
diff
changeset
|
130 |
class PyroProxyProxy(object): |
203 | 131 |
""" |
132 |
A proxy proxy class to handle Beremiz Pyro interface specific behavior. |
|
1455 | 133 |
And to put Pyro exception catcher in between caller and Pyro proxy |
203 | 134 |
""" |
284
3fecc96090c8
Fixed problem with re-use of Pyro connector proxy copy across debug sessions
etisserant
parents:
235
diff
changeset
|
135 |
def __init__(self): |
3fecc96090c8
Fixed problem with re-use of Pyro connector proxy copy across debug sessions
etisserant
parents:
235
diff
changeset
|
136 |
# for safe use in from debug thread, must create a copy |
3fecc96090c8
Fixed problem with re-use of Pyro connector proxy copy across debug sessions
etisserant
parents:
235
diff
changeset
|
137 |
self.RemotePLCObjectProxyCopy = None |
3fecc96090c8
Fixed problem with re-use of Pyro connector proxy copy across debug sessions
etisserant
parents:
235
diff
changeset
|
138 |
|
203 | 139 |
def GetPyroProxy(self): |
140 |
""" |
|
141 |
This func returns the real Pyro Proxy. |
|
142 |
Use this if you musn't keep reference to it. |
|
143 |
""" |
|
144 |
return RemotePLCObjectProxy |
|
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
145 |
|
235 | 146 |
def _PyroStartPLC(self, *args, **kwargs): |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
147 |
""" |
1434
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1116
diff
changeset
|
148 |
confnodesroot._connector.GetPyroProxy() is used |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
149 |
rather than RemotePLCObjectProxy because |
1434
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1116
diff
changeset
|
150 |
object is recreated meanwhile, |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
151 |
so we must not keep ref to it here |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
152 |
""" |
969 | 153 |
current_status, log_count = confnodesroot._connector.GetPyroProxy().GetPLCstatus() |
465
67d32a91d70b
Fixes in debug + reconnect to running PLC
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
154 |
if current_status == "Dirty": |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
155 |
""" |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
156 |
Some bad libs with static symbols may polute PLC |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
157 |
ask runtime to suicide and come back again |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
158 |
""" |
717 | 159 |
confnodesroot.logger.write(_("Force runtime reload\n")) |
160 |
confnodesroot._connector.GetPyroProxy().ForceReload() |
|
161 |
confnodesroot._Disconnect() |
|
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
162 |
# let remote PLC time to resurect.(freeze app) |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
163 |
sleep(0.5) |
717 | 164 |
confnodesroot._Connect() |
165 |
self.RemotePLCObjectProxyCopy = copy.copy(confnodesroot._connector.GetPyroProxy()) |
|
166 |
return confnodesroot._connector.GetPyroProxy().StartPLC(*args, **kwargs) |
|
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
167 |
StartPLC = PyroCatcher(_PyroStartPLC, False) |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
168 |
|
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
169 |
def _PyroGetTraceVariables(self): |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
170 |
""" |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
171 |
for safe use in from debug thread, must use the copy |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
172 |
""" |
465
67d32a91d70b
Fixes in debug + reconnect to running PLC
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
173 |
if self.RemotePLCObjectProxyCopy is None: |
717 | 174 |
self.RemotePLCObjectProxyCopy = copy.copy(confnodesroot._connector.GetPyroProxy()) |
446
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
411
diff
changeset
|
175 |
return self.RemotePLCObjectProxyCopy.GetTraceVariables() |
1455 | 176 |
GetTraceVariables = PyroCatcher(_PyroGetTraceVariables, ("Broken", None)) |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
177 |
|
446
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
411
diff
changeset
|
178 |
def _PyroGetPLCstatus(self): |
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
411
diff
changeset
|
179 |
return RemotePLCObjectProxy.GetPLCstatus() |
1455 | 180 |
GetPLCstatus = PyroCatcher(_PyroGetPLCstatus, ("Broken", None)) |
446
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
411
diff
changeset
|
181 |
|
699
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
493
diff
changeset
|
182 |
def _PyroRemoteExec(self, script, **kwargs): |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
493
diff
changeset
|
183 |
return RemotePLCObjectProxy.RemoteExec(script, **kwargs) |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
493
diff
changeset
|
184 |
RemoteExec = PyroCatcher(_PyroRemoteExec, (-1, "RemoteExec script failed!")) |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
493
diff
changeset
|
185 |
|
203 | 186 |
def __getattr__(self, attrName): |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
187 |
member = self.__dict__.get(attrName, None) |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
188 |
if member is None: |
1455 | 189 |
def my_local_func(*args, **kwargs): |
190 |
return RemotePLCObjectProxy.__getattr__(attrName)(*args, **kwargs) |
|
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
191 |
member = PyroCatcher(my_local_func, None) |
203 | 192 |
self.__dict__[attrName] = member |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
193 |
return member |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
194 |
|
203 | 195 |
return PyroProxyProxy() |