author | Edouard Tisserant |
Mon, 17 Oct 2011 21:43:20 +0200 | |
changeset 629 | 61347cc521d1 |
parent 493 | 015a803301b9 |
child 699 | 6ff64cadb1ff |
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 |
203 | 27 |
|
399 | 28 |
# this module attribute contains a list of DNS-SD (Zeroconf) service types |
29 |
# supported by this connector plugin. |
|
30 |
# |
|
31 |
# for connectors that do not support DNS-SD, this attribute can be omitted |
|
32 |
# or set to an empty list. |
|
33 |
supported_dnssd_services = ["_PYRO._tcp.local."] |
|
34 |
||
203 | 35 |
def PYRO_connector_factory(uri, pluginsroot): |
36 |
""" |
|
37 |
This returns the connector to Pyro style PLCobject |
|
38 |
""" |
|
361 | 39 |
pluginsroot.logger.write(_("Connecting to URI : %s\n")%uri) |
203 | 40 |
|
41 |
servicetype, location = uri.split("://") |
|
42 |
||
43 |
# Try to get the proxy object |
|
44 |
try : |
|
45 |
RemotePLCObjectProxy = pyro.getAttrProxyForURI("PYROLOC://"+location+"/PLCObject") |
|
46 |
except Exception, msg: |
|
361 | 47 |
pluginsroot.logger.write_error(_("Wrong URI, please check it !\n")) |
203 | 48 |
pluginsroot.logger.write_error(traceback.format_exc()) |
49 |
return None |
|
50 |
||
51 |
def PyroCatcher(func, default=None): |
|
52 |
""" |
|
53 |
A function that catch a pyro exceptions, write error to logger |
|
54 |
and return defaul value when it happen |
|
55 |
""" |
|
218 | 56 |
def catcher_func(*args,**kwargs): |
203 | 57 |
try: |
58 |
return func(*args,**kwargs) |
|
493
015a803301b9
Catch ProtocolError exception when connection failed
laurent
parents:
486
diff
changeset
|
59 |
except Pyro.errors.ProtocolError, e: |
015a803301b9
Catch ProtocolError exception when connection failed
laurent
parents:
486
diff
changeset
|
60 |
pass |
486
2e0fe44044b3
Catch Pyro exception when connection closed and print message
laurent
parents:
477
diff
changeset
|
61 |
except Pyro.errors.ConnectionClosedError, e: |
2e0fe44044b3
Catch Pyro exception when connection closed and print message
laurent
parents:
477
diff
changeset
|
62 |
pluginsroot.logger.write_error("Connection lost!\n") |
2e0fe44044b3
Catch Pyro exception when connection closed and print message
laurent
parents:
477
diff
changeset
|
63 |
pluginsroot._connector = None |
477
f66a092b6e74
Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
465
diff
changeset
|
64 |
except Exception,e: |
203 | 65 |
#pluginsroot.logger.write_error(traceback.format_exc()) |
477
f66a092b6e74
Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
465
diff
changeset
|
66 |
errmess = ''.join(Pyro.util.getPyroTraceback(e)) |
f66a092b6e74
Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
465
diff
changeset
|
67 |
pluginsroot.logger.write_error(errmess+"\n") |
f66a092b6e74
Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
465
diff
changeset
|
68 |
print errmess |
354 | 69 |
pluginsroot._connector = None |
203 | 70 |
return default |
218 | 71 |
return catcher_func |
203 | 72 |
|
73 |
# Check connection is effective. |
|
74 |
# lambda is for getattr of GetPLCstatus to happen inside catcher |
|
75 |
if PyroCatcher(lambda:RemotePLCObjectProxy.GetPLCstatus())() == None: |
|
361 | 76 |
pluginsroot.logger.write_error(_("Cannot get PLC status - connection failed.\n")) |
203 | 77 |
return None |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
78 |
|
284
3fecc96090c8
Fixed problem with re-use of Pyro connector proxy copy across debug sessions
etisserant
parents:
235
diff
changeset
|
79 |
|
203 | 80 |
class PyroProxyProxy: |
81 |
""" |
|
82 |
A proxy proxy class to handle Beremiz Pyro interface specific behavior. |
|
83 |
And to put pyro exception catcher in between caller and pyro proxy |
|
84 |
""" |
|
284
3fecc96090c8
Fixed problem with re-use of Pyro connector proxy copy across debug sessions
etisserant
parents:
235
diff
changeset
|
85 |
def __init__(self): |
3fecc96090c8
Fixed problem with re-use of Pyro connector proxy copy across debug sessions
etisserant
parents:
235
diff
changeset
|
86 |
# 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
|
87 |
self.RemotePLCObjectProxyCopy = None |
3fecc96090c8
Fixed problem with re-use of Pyro connector proxy copy across debug sessions
etisserant
parents:
235
diff
changeset
|
88 |
|
203 | 89 |
def GetPyroProxy(self): |
90 |
""" |
|
91 |
This func returns the real Pyro Proxy. |
|
92 |
Use this if you musn't keep reference to it. |
|
93 |
""" |
|
94 |
return RemotePLCObjectProxy |
|
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
95 |
|
235 | 96 |
def _PyroStartPLC(self, *args, **kwargs): |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
97 |
""" |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
98 |
pluginsroot._connector.GetPyroProxy() is used |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
99 |
rather than RemotePLCObjectProxy because |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
100 |
object is recreated meanwhile, |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
101 |
so we must not keep ref to it here |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
102 |
""" |
465
67d32a91d70b
Fixes in debug + reconnect to running PLC
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
103 |
current_status = pluginsroot._connector.GetPyroProxy().GetPLCstatus() |
67d32a91d70b
Fixes in debug + reconnect to running PLC
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
104 |
if current_status == "Dirty": |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
105 |
""" |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
106 |
Some bad libs with static symbols may polute PLC |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
107 |
ask runtime to suicide and come back again |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
108 |
""" |
361 | 109 |
pluginsroot.logger.write(_("Force runtime reload\n")) |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
110 |
pluginsroot._connector.GetPyroProxy().ForceReload() |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
111 |
pluginsroot._Disconnect() |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
112 |
# let remote PLC time to resurect.(freeze app) |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
113 |
sleep(0.5) |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
114 |
pluginsroot._Connect() |
284
3fecc96090c8
Fixed problem with re-use of Pyro connector proxy copy across debug sessions
etisserant
parents:
235
diff
changeset
|
115 |
self.RemotePLCObjectProxyCopy = copy.copy(pluginsroot._connector.GetPyroProxy()) |
235 | 116 |
return pluginsroot._connector.GetPyroProxy().StartPLC(*args, **kwargs) |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
117 |
StartPLC = PyroCatcher(_PyroStartPLC, False) |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
118 |
|
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
119 |
|
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
120 |
def _PyroGetTraceVariables(self): |
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 |
for safe use in from debug thread, must use the copy |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
123 |
""" |
465
67d32a91d70b
Fixes in debug + reconnect to running PLC
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
124 |
if self.RemotePLCObjectProxyCopy is None: |
67d32a91d70b
Fixes in debug + reconnect to running PLC
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
125 |
self.RemotePLCObjectProxyCopy = copy.copy(pluginsroot._connector.GetPyroProxy()) |
446
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
411
diff
changeset
|
126 |
return self.RemotePLCObjectProxyCopy.GetTraceVariables() |
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
411
diff
changeset
|
127 |
GetTraceVariables = PyroCatcher(_PyroGetTraceVariables,("Broken",None,None)) |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
128 |
|
446
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
411
diff
changeset
|
129 |
def _PyroGetPLCstatus(self): |
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
411
diff
changeset
|
130 |
return RemotePLCObjectProxy.GetPLCstatus() |
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
411
diff
changeset
|
131 |
GetPLCstatus = PyroCatcher(_PyroGetPLCstatus, "Broken") |
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
411
diff
changeset
|
132 |
|
203 | 133 |
def __getattr__(self, attrName): |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
134 |
member = self.__dict__.get(attrName, None) |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
135 |
if member is None: |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
136 |
def my_local_func(*args,**kwargs): |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
137 |
return RemotePLCObjectProxy.__getattr__(attrName)(*args,**kwargs) |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
138 |
member = PyroCatcher(my_local_func, None) |
203 | 139 |
self.__dict__[attrName] = member |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
140 |
return member |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
141 |
|
203 | 142 |
return PyroProxyProxy() |
143 |
||
144 |