beremiz

WAMP: enhance feedback in case of connection problem
py2compat
15 months ago, Edouard Tisserant
0a845372bc88
Parents 5f6757e608d2
Children 4876b441a291
WAMP: enhance feedback in case of connection problem
--- a/CertManagement.py Mon Mar 10 14:59:05 2025 +0100
+++ b/CertManagement.py Wed Mar 12 13:19:11 2025 +0100
@@ -34,7 +34,7 @@
def _ensureCertdir():
certpath = _certpath()
if not os.path.exists(certpath):
- os.mkdir(certpath)
+ os.makedirs(certpath)
return certpath
--- a/connectors/WAMP/__init__.py Mon Mar 10 14:59:05 2025 +0100
+++ b/connectors/WAMP/__init__.py Wed Mar 12 13:19:11 2025 +0100
@@ -188,9 +188,12 @@
_WampConnection = threads.blockingCallFromThread(
reactor, RegisterWampClient)
if not _WampConnectEvent.wait(4):
- threads.blockingCallFromThread(
- reactor, _WampConnection.stopConnecting)
confnodesroot.logger.write_error("WAMP connection timeout\n")
+ try:
+ threads.blockingCallFromThread(
+ reactor, _WampConnection.stopConnecting)
+ except:
+ pass
return None
else:
if _WampSession is None:
--- a/runtime/WampClient.py Mon Mar 10 14:59:05 2025 +0100
+++ b/runtime/WampClient.py Wed Mar 12 13:19:11 2025 +0100
@@ -37,7 +37,7 @@
from autobahn.wamp.serializer import MsgPackSerializer
from twisted.internet.protocol import ReconnectingClientFactory
from twisted.python.components import registerAdapter
-from twisted.internet.ssl import optionsForClientTLS
+from twisted.internet.ssl import optionsForClientTLS, VerificationError
from twisted.internet._sslverify import OpenSSLCertificateAuthorities
from OpenSSL import crypto
@@ -46,6 +46,7 @@
from nevow import tags, url, static
from runtime import GetPLCObjectSingleton
from runtime.Stunnel import getPSKID
+from runtime.loglevels import LogLevelsDict
mandatoryConfigItems = ["ID", "active", "realm", "url"]
@@ -140,6 +141,8 @@
def onJoin(self, details):
global _WampSession
+ GetPLCObjectSingleton().LogMessage(LogLevelsDict["INFO"],
+ 'WAMP session joined for: '+self.config.extra["ID"])
_WampSession = self
ID = self.config.extra["ID"]
@@ -158,10 +161,10 @@
for func in DoOnJoin:
func(self)
- print('WAMP session joined (%s) by: %s' % (time.ctime(), ID))
-
def onLeave(self, details):
global _WampSession, _transportFactory
+ GetPLCObjectSingleton().LogMessage(LogLevelsDict["INFO"],
+ 'WAMP session left for: {} reason: "{}" message: "{}"'.format(self.config.extra["ID"], details.reason, details.message))
_WampSession = None
_transportFactory = None
@@ -202,17 +205,31 @@
self.resetDelay()
return ReconnectingClientFactory.buildProtocol(self, addr)
+ def _clientConnectionLostOrFailed(self, connector, reason):
+ """ report connection lost """
+ if not reason.check(VerificationError):
+ # Verification failed
+ GetPLCObjectSingleton().LogMessage(LogLevelsDict["WARNING"],
+ "WAMP TLS certificate verification failed: "+\
+ reason.getErrorMessage()+
+ "\nProvide a certicate on web interface or as wampTrustStore.crt in project files.")
+ else:
+ GetPLCObjectSingleton().LogMessage(LogLevelsDict["WARNING"],
+ "WAMP connection lost: "+reason.getErrorMessage())
+
def clientConnectionFailed(self, connector, reason):
print("WAMP Client connection failed (%s) .. retrying .." %
time.ctime())
super(ReconnectingWampWebSocketClientFactory,
self).clientConnectionFailed(connector, reason)
+ self._clientConnectionLostOrFailed(connector, reason)
def clientConnectionLost(self, connector, reason):
print("WAMP Client connection lost (%s) .. retrying .." %
time.ctime())
super(ReconnectingWampWebSocketClientFactory,
self).clientConnectionFailed(connector, reason)
+ self._clientConnectionLostOrFailed(connector, reason)
def CheckConfiguration(WampClientConf):
@@ -332,7 +349,7 @@
reactor.callInThread(_RegisterWampClient)
def _RegisterWampClient():
- global _WampSecret
+ global _WampSecret, _transportFactory
WampClientConf = GetConfiguration()
WampClientConf["secret"] = _WampSecret
@@ -361,9 +378,11 @@
connectWS(_transportFactory, contextFactory)
print("WAMP client connecting to :", WampClientConf["url"])
else:
- print("WAMP client can not connect to :", WampClientConf["url"])
+ GetPLCObjectSingleton().LogMessage(LogLevelsDict["WARNING"],
+ "WAMP configuration invalid:", WampClientConf["url"])
def MakeSecureContextFactory(verifyHostname):
+ global _transportFactory
if not verifyHostname:
return None
trustRoot=None