author | Laurent Bessard |
Wed, 27 Feb 2013 11:25:39 +0100 | |
changeset 949 | aa24cf3b7009 |
parent 923 | 6ef6e0b3a908 |
child 969 | 1950fe687dde |
permissions | -rwxr-xr-x |
203 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
# |
|
4 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
5 |
# |
|
6 |
#See COPYING file for copyrights details. |
|
7 |
# |
|
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. |
|
12 |
# |
|
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. |
|
17 |
# |
|
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.core as pyro |
|
22 |
from Pyro.errors import PyroError |
|
477
f66a092b6e74
Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
465
diff
changeset
|
23 |
import Pyro.util |
203 | 24 |
import traceback |
25 |
from time import sleep |
|
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
26 |
import copy |
763
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
27 |
import socket |
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
28 |
service_type = '_PYRO._tcp.local.' |
203 | 29 |
|
399 | 30 |
# this module attribute contains a list of DNS-SD (Zeroconf) service types |
717 | 31 |
# supported by this connector confnode. |
399 | 32 |
# |
33 |
# for connectors that do not support DNS-SD, this attribute can be omitted |
|
34 |
# or set to an empty list. |
|
35 |
||
717 | 36 |
def PYRO_connector_factory(uri, confnodesroot): |
203 | 37 |
""" |
38 |
This returns the connector to Pyro style PLCobject |
|
39 |
""" |
|
717 | 40 |
confnodesroot.logger.write(_("Connecting to URI : %s\n")%uri) |
203 | 41 |
|
42 |
servicetype, location = uri.split("://") |
|
763
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
43 |
if location.find(service_type) != -1: |
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
44 |
try : |
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
45 |
from util.Zeroconf import Zeroconf |
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
46 |
r = Zeroconf() |
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
47 |
i=r.getServiceInfo(service_type, location) |
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
48 |
ip = str(socket.inet_ntoa(i.getAddress())) |
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
49 |
port = str(i.getPort()) |
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
50 |
newlocation = ip+':'+port |
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
51 |
confnodesroot.logger.write(_("'%s' is located at %s\n")%(location, newlocation)) |
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
52 |
location = newlocation |
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
53 |
r.close() |
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
54 |
except Exception, msg: |
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
55 |
confnodesroot.logger.write_error(_("MDNS resolution failure for '%s'\n")%location) |
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
56 |
confnodesroot.logger.write_error(traceback.format_exc()) |
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
57 |
return None |
203 | 58 |
|
59 |
# Try to get the proxy object |
|
60 |
try : |
|
61 |
RemotePLCObjectProxy = pyro.getAttrProxyForURI("PYROLOC://"+location+"/PLCObject") |
|
62 |
except Exception, msg: |
|
763
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
63 |
confnodesroot.logger.write_error(_("Connection to '%s' failed.\n")%location) |
717 | 64 |
confnodesroot.logger.write_error(traceback.format_exc()) |
203 | 65 |
return None |
66 |
||
67 |
def PyroCatcher(func, default=None): |
|
68 |
""" |
|
69 |
A function that catch a pyro exceptions, write error to logger |
|
70 |
and return defaul value when it happen |
|
71 |
""" |
|
218 | 72 |
def catcher_func(*args,**kwargs): |
203 | 73 |
try: |
74 |
return func(*args,**kwargs) |
|
493
015a803301b9
Catch ProtocolError exception when connection failed
laurent
parents:
486
diff
changeset
|
75 |
except Pyro.errors.ProtocolError, e: |
015a803301b9
Catch ProtocolError exception when connection failed
laurent
parents:
486
diff
changeset
|
76 |
pass |
486
2e0fe44044b3
Catch Pyro exception when connection closed and print message
laurent
parents:
477
diff
changeset
|
77 |
except Pyro.errors.ConnectionClosedError, e: |
717 | 78 |
confnodesroot.logger.write_error("Connection lost!\n") |
79 |
confnodesroot._connector = None |
|
477
f66a092b6e74
Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
465
diff
changeset
|
80 |
except Exception,e: |
717 | 81 |
#confnodesroot.logger.write_error(traceback.format_exc()) |
477
f66a092b6e74
Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
465
diff
changeset
|
82 |
errmess = ''.join(Pyro.util.getPyroTraceback(e)) |
717 | 83 |
confnodesroot.logger.write_error(errmess+"\n") |
477
f66a092b6e74
Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
465
diff
changeset
|
84 |
print errmess |
717 | 85 |
confnodesroot._connector = None |
203 | 86 |
return default |
218 | 87 |
return catcher_func |
203 | 88 |
|
89 |
# Check connection is effective. |
|
90 |
# lambda is for getattr of GetPLCstatus to happen inside catcher |
|
923
6ef6e0b3a908
Fixed crash (segfault) when logging debug messages
Edouard Tisserant
parents:
917
diff
changeset
|
91 |
if PyroCatcher(lambda:RemotePLCObjectProxy.GetPLCstatus())() is None: |
717 | 92 |
confnodesroot.logger.write_error(_("Cannot get PLC status - connection failed.\n")) |
203 | 93 |
return None |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
94 |
|
284
3fecc96090c8
Fixed problem with re-use of Pyro connector proxy copy across debug sessions
etisserant
parents:
235
diff
changeset
|
95 |
|
203 | 96 |
class PyroProxyProxy: |
97 |
""" |
|
98 |
A proxy proxy class to handle Beremiz Pyro interface specific behavior. |
|
99 |
And to put pyro exception catcher in between caller and pyro proxy |
|
100 |
""" |
|
284
3fecc96090c8
Fixed problem with re-use of Pyro connector proxy copy across debug sessions
etisserant
parents:
235
diff
changeset
|
101 |
def __init__(self): |
3fecc96090c8
Fixed problem with re-use of Pyro connector proxy copy across debug sessions
etisserant
parents:
235
diff
changeset
|
102 |
# 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
|
103 |
self.RemotePLCObjectProxyCopy = None |
3fecc96090c8
Fixed problem with re-use of Pyro connector proxy copy across debug sessions
etisserant
parents:
235
diff
changeset
|
104 |
|
203 | 105 |
def GetPyroProxy(self): |
106 |
""" |
|
107 |
This func returns the real Pyro Proxy. |
|
108 |
Use this if you musn't keep reference to it. |
|
109 |
""" |
|
110 |
return RemotePLCObjectProxy |
|
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
111 |
|
235 | 112 |
def _PyroStartPLC(self, *args, **kwargs): |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
113 |
""" |
717 | 114 |
confnodesroot._connector.GetPyroProxy() is used |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
115 |
rather than RemotePLCObjectProxy because |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
116 |
object is recreated meanwhile, |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
117 |
so we must not keep ref to it here |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
118 |
""" |
717 | 119 |
current_status = confnodesroot._connector.GetPyroProxy().GetPLCstatus() |
465
67d32a91d70b
Fixes in debug + reconnect to running PLC
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
120 |
if current_status == "Dirty": |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
121 |
""" |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
122 |
Some bad libs with static symbols may polute PLC |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
123 |
ask runtime to suicide and come back again |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
124 |
""" |
717 | 125 |
confnodesroot.logger.write(_("Force runtime reload\n")) |
126 |
confnodesroot._connector.GetPyroProxy().ForceReload() |
|
127 |
confnodesroot._Disconnect() |
|
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
128 |
# let remote PLC time to resurect.(freeze app) |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
129 |
sleep(0.5) |
717 | 130 |
confnodesroot._Connect() |
131 |
self.RemotePLCObjectProxyCopy = copy.copy(confnodesroot._connector.GetPyroProxy()) |
|
132 |
return confnodesroot._connector.GetPyroProxy().StartPLC(*args, **kwargs) |
|
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
133 |
StartPLC = PyroCatcher(_PyroStartPLC, False) |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
134 |
|
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
135 |
|
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
136 |
def _PyroGetTraceVariables(self): |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
137 |
""" |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
138 |
for safe use in from debug thread, must use the copy |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
139 |
""" |
465
67d32a91d70b
Fixes in debug + reconnect to running PLC
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
140 |
if self.RemotePLCObjectProxyCopy is None: |
717 | 141 |
self.RemotePLCObjectProxyCopy = copy.copy(confnodesroot._connector.GetPyroProxy()) |
446
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
411
diff
changeset
|
142 |
return self.RemotePLCObjectProxyCopy.GetTraceVariables() |
917 | 143 |
GetTraceVariables = PyroCatcher(_PyroGetTraceVariables,("Broken",None,None)) |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
144 |
|
446
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
411
diff
changeset
|
145 |
def _PyroGetPLCstatus(self): |
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
411
diff
changeset
|
146 |
return RemotePLCObjectProxy.GetPLCstatus() |
923
6ef6e0b3a908
Fixed crash (segfault) when logging debug messages
Edouard Tisserant
parents:
917
diff
changeset
|
147 |
GetPLCstatus = PyroCatcher(_PyroGetPLCstatus, ("Broken",None)) |
446
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
411
diff
changeset
|
148 |
|
699
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
493
diff
changeset
|
149 |
def _PyroRemoteExec(self, script, **kwargs): |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
493
diff
changeset
|
150 |
return RemotePLCObjectProxy.RemoteExec(script, **kwargs) |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
493
diff
changeset
|
151 |
RemoteExec = PyroCatcher(_PyroRemoteExec, (-1, "RemoteExec script failed!")) |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
493
diff
changeset
|
152 |
|
203 | 153 |
def __getattr__(self, attrName): |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
154 |
member = self.__dict__.get(attrName, None) |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
155 |
if member is None: |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
156 |
def my_local_func(*args,**kwargs): |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
157 |
return RemotePLCObjectProxy.__getattr__(attrName)(*args,**kwargs) |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
158 |
member = PyroCatcher(my_local_func, None) |
203 | 159 |
self.__dict__[attrName] = member |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
160 |
return member |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
161 |
|
203 | 162 |
return PyroProxyProxy() |
163 |
||
164 |