use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionTaskException in project open-smart-grid-platform by OSGP.
the class DlmsConnectionMessageProcessor method createAndHandleConnectionForDevice.
public void createAndHandleConnectionForDevice(final DlmsDevice device, final MessageMetadata messageMetadata, final Consumer<DlmsConnectionManager> taskForConnectionManager) throws OsgpException {
Permit permit = null;
if (this.throttlingClientConfig.clientEnabled()) {
permit = this.throttlingClientConfig.throttlingClient().requestPermitUsingNetworkSegmentIfIdsAreAvailable(messageMetadata.getBaseTransceiverStationId(), messageMetadata.getCellId());
} else {
this.throttlingService.openConnection();
}
final DlmsMessageListener dlmsMessageListener = this.createMessageListenerForDeviceConnection(device, messageMetadata);
try {
this.dlmsConnectionHelper.createAndHandleConnectionForDevice(messageMetadata, device, dlmsMessageListener, permit, taskForConnectionManager);
} catch (final ConnectionTaskException e) {
/*
* Exceptions thrown by the tasks working with the DlmsConnectionManager are wrapped in
* ConnectionTaskException in the ThrowingConsumer.
*/
throw e.getOsgpException();
} catch (final Exception e) {
/*
* Throttling permit is released in this catch block to make sure that it is made available
* again when there were problems setting up the connection and only then (exceptions thrown
* by the tasks working with the DlmsConnectionManager are wrapped as ConnectionTaskException
* and are caught in the previous catch block, so they don't end up here).
*
* If there are no problems in setting up the connection, releasing the throttling permit is
* the responsibility of the tasks for the DlmsConnectionManager, as seen in
* DeviceRequestMessageProcessor.processMessageTasks(), where
* this.doConnectionPostProcessing() is called in a finally block.
*/
if (this.throttlingClientConfig.clientEnabled()) {
this.throttlingClientConfig.throttlingClient().releasePermit(permit);
} else {
this.throttlingService.closeConnection();
}
throw e;
}
}
Aggregations