use of org.opensmartgridplatform.adapter.protocol.iec60870.domain.exceptions.ClientConnectionAlreadyInCacheException in project open-smart-grid-platform by OSGP.
the class ClientConnectionService method createConnection.
private ClientConnection createConnection(final RequestMetadata requestMetadata) throws ConnectionFailureException {
final String deviceIdentification = requestMetadata.getDeviceIdentification();
final Iec60870Device device = this.getIec60870Device(deviceIdentification);
final Iec60870Device connectionDevice = this.getConnectionDevice(device);
final String connectionDeviceIdentification = device.getConnectionDeviceIdentification();
final ConnectionParameters connectionParameters = this.createConnectionParameters(connectionDevice, requestMetadata.getIpAddress());
final ResponseMetadata responseMetadata = ResponseMetadata.from(requestMetadata, connectionDeviceIdentification, connectionDevice.getDeviceType());
final ClientConnectionEventListener eventListener = new ClientConnectionEventListener.Builder().withDeviceIdentification(connectionDeviceIdentification).withClientAsduHandlerRegistry(this.clientAsduHandlerRegistry).withClientConnectionCache(this.connectionCache).withLoggingService(this.loggingService).withLogItemFactory(this.logItemFactory).withResponseMetadata(responseMetadata).withResponseMetadataFactory(this.responseMetadataFactory).build();
final ClientConnection newDeviceConnection = this.iec60870Client.connect(connectionParameters, eventListener);
try {
this.connectionCache.addConnection(connectionDeviceIdentification, newDeviceConnection);
} catch (final ClientConnectionAlreadyInCacheException e) {
LOGGER.warn("Client connection for device {} already exists. Closing new connection and returning existing connection", connectionDeviceIdentification);
LOGGER.debug("Exception: ", e);
newDeviceConnection.getConnection().close();
return e.getClientConnection();
}
return newDeviceConnection;
}
Aggregations