Mantid
Loading...
Searching...
No Matches
LiveListenerFactory.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
10#include "MantidKernel/Logger.h"
11#include <Poco/Net/SocketAddress.h>
12
13using std::shared_ptr;
14
15namespace Mantid::API {
16namespace {
18Kernel::Logger g_log("LiveListenerFactory");
19} // namespace
20
37std::shared_ptr<ILiveListener> LiveListenerFactoryImpl::create(const std::string &instrumentName, bool connect,
38 const API::IAlgorithm *callingAlgorithm,
39 const std::string &listenerConnectionName) const {
40 try {
41 // Look up LiveListenerInfo based on given instrument and listener names
42 auto inst = Kernel::ConfigService::Instance().getInstrument(instrumentName);
43 auto info = inst.liveListenerInfo(listenerConnectionName);
44
45 // Defer creation logic to other create overload
46 return create(info, connect, callingAlgorithm);
47
49 // Could not determine LiveListenerInfo for instrumentName
50 // Attempt to interpret instrumentName as listener class name instead, to
51 // support legacy usage in unit tests.
52 Kernel::LiveListenerInfo info(instrumentName);
53 return create(info, connect, callingAlgorithm);
54 }
55}
56
70std::shared_ptr<ILiveListener> LiveListenerFactoryImpl::create(const Kernel::LiveListenerInfo &info, bool connect,
71 const API::IAlgorithm *callingAlgorithm) const {
72
74
75 // Give LiveListener additional properties if provided
76 if (callingAlgorithm) {
77 listener->setAlgorithm(*callingAlgorithm);
78 }
79
80 if (connect) {
81 try {
82 Poco::Net::SocketAddress address;
83
84 // To allow listener::connect to be called even when no address is given
85 if (!info.address().empty())
86 address = Poco::Net::SocketAddress(info.address());
87
88 // If we can't connect, throw an exception to be handled below
89 if (!listener->connect(address)) {
90 throw Poco::Exception("Connection attempt failed.");
91 }
92 }
93 // The Poco SocketAddress can throw all manner of exceptions if the address
94 // string it gets is badly formed, or it can't successfully look up the
95 // server given, or .......
96 // Just catch the base class exception
97 catch (Poco::Exception &pocoEx) {
98 std::stringstream ss;
99 ss << "Unable to connect listener [" << info.listener() << "] to [" << info.address() << "]: " << pocoEx.what();
100 g_log.debug(ss.str());
101 throw std::runtime_error(ss.str());
102 }
103 }
104
105 // If we get to here, it's all good!
106 return listener;
107}
108
118ILiveListener *LiveListenerFactoryImpl::createUnwrapped(const std::string &className) const {
119 UNUSED_ARG(className)
120 throw Kernel::Exception::NotImplementedError("Don't use this method - use the safe one!!!");
121}
122
123} // namespace Mantid::API
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition: System.h:64
IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:45
ILiveListener is the interface implemented by classes which connect directly to instrument data acqui...
Definition: ILiveListener.h:31
std::shared_ptr< ILiveListener > create(const std::string &instrumentName, bool connect=false, const API::IAlgorithm *callingAlgorithm=nullptr, const std::string &listenerConnectionName="") const
Creates an instance of the appropriate listener for the given instrument, and establishes the connect...
ILiveListener * createUnwrapped(const std::string &className) const override
Override the DynamicFactory::createUnwrapped() method.
virtual std::shared_ptr< Base > create(const std::string &className) const
Creates a new instance of the class with the given name.
Exception for when an item is not found in a collection.
Definition: Exception.h:145
Marks code as not implemented yet.
Definition: Exception.h:138
A class that holds information about a LiveListener connection.
const std::string & address() const
Returns the address string of this LiveListener connection.
const std::string & listener() const
Returns the classname of the specific LiveListener to use.
void debug(const std::string &msg)
Logs at debug level.
Definition: Logger.cpp:114
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< ILiveListener > ILiveListener_sptr
Shared pointer to an ILiveListener.
Kernel::Logger g_log("ExperimentInfo")
static logger object