Skip to content

Commit

Permalink
Merge branch 'bugfix/8602_let_events_fail' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed Dec 16, 2013
2 parents dbaec5b + bc30d7e commit 2e8e7c3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <Poco/Runnable.h>
#include <Poco/Thread.h>

#include <map>

// Time we'll wait on a receive call (in seconds)
const long RECV_TIMEOUT = 30;
// Sleep time in case we need to wait for the data to become available (in milliseconds)
Expand Down Expand Up @@ -143,7 +145,7 @@ namespace Mantid

// receive a header and check if it's valid
template <typename T>
void Receive(T buffer, const std::string& head, const std::string &msg)
void Receive(T &buffer, const std::string& head, const std::string &msg)
{
long timeout = 0;
while( m_socket.available() < static_cast<int>(sizeof(buffer)) )
Expand Down Expand Up @@ -196,7 +198,12 @@ namespace Mantid
int m_numberOfSpectra;

/// buffer to collect data that cannot be processed
char* junk_buffer[1000];
char junk_buffer[1000];

/// list of warnings for repeated conditions
/// If the same condition happens repeatedly the warning is issued once
/// and is deleted from the list
std::map<std::string, std::string> m_warnings;

/// reference to the logger class
static Kernel::Logger& g_log;
Expand Down
53 changes: 33 additions & 20 deletions Code/Mantid/Framework/LiveData/src/ISISLiveEventDataListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ ISISLiveEventDataListener::ISISLiveEventDataListener():API::ILiveListener(),
m_isConnected(false),
m_stopThread(false)
{
m_warnings["period"] = "Period number is outside the range. Changed to 0.";
}

/**
Expand Down Expand Up @@ -69,7 +70,7 @@ bool ISISLiveEventDataListener::connect(const Poco::Net::SocketAddress &address)
// localhost on the default port
if (address.host().toString().compare( "0.0.0.0") == 0)
{
Poco::Net::SocketAddress tempAddress("127.0.0.1:10000");
Poco::Net::SocketAddress tempAddress("127.0.0.1:10000");
try {
m_socket.connect( tempAddress); // BLOCKING connect
} catch (...) {
Expand Down Expand Up @@ -98,6 +99,12 @@ bool ISISLiveEventDataListener::connect(const Poco::Net::SocketAddress &address)
daeName.erase( i );
}

if ( daeName == "0.0.0.0" )
{
// to connect to fake dae
daeName = "127.0.0.1";
}

// set IDC reporter function for errors
IDCsetreportfunc(&ISISLiveEventDataListener::IDCReporter);

Expand All @@ -110,6 +117,9 @@ bool ISISLiveEventDataListener::connect(const Poco::Net::SocketAddress &address)
m_numberOfPeriods = getInt("NPER");
m_numberOfSpectra = getInt("NSP1");

g_log.notice() << "Number of periods " << m_numberOfPeriods << std::endl;
g_log.notice() << "Number of spectra " << m_numberOfSpectra << std::endl;

TCPStreamEventDataSetup setup;
Receive(setup, "Setup", "Wrong version");
m_startTime.set_from_time_t(setup.head_setup.start_time);
Expand All @@ -131,9 +141,16 @@ void ISISLiveEventDataListener::start(Kernel::DateAndTime startTime)
// return a workspace with collected events
boost::shared_ptr<API::Workspace> ISISLiveEventDataListener::extractData()
{
if ( !m_eventBuffer[0] )
if ( m_eventBuffer.empty() || !m_eventBuffer[0] )
{
throw LiveData::Exception::NotYet("The workspace has not yet been initialized.");
// extractData() is called too early
throw LiveData::Exception::NotYet("The workspace has not yet been initialized.");
}

if ( !m_isConnected )
{
// the background thread stopped because of an error. the error message has been logged at this point
throw std::runtime_error("Background thread stopped.");
}

Poco::ScopedLock<Poco::FastMutex> scopedLock( m_mutex);
Expand Down Expand Up @@ -252,26 +269,23 @@ void ISISLiveEventDataListener::run()

} catch (std::runtime_error &e) { // exception handler for generic runtime exceptions

g_log.fatal() << "Caught a runtime exception." << std::endl
<< "Exception message: " << e.what() << std::endl
<< "Thread will exit." << std::endl;
g_log.error() << "Caught a runtime exception." << std::endl
<< "Exception message: " << e.what() << std::endl;
m_isConnected = false;

m_backgroundException = boost::shared_ptr<std::runtime_error>( new std::runtime_error( e));

} catch (std::invalid_argument &e) { // TimeSeriesProperty (and possibly some other things) can
// can throw these errors
g_log.fatal() << "Caught an invalid argument exception." << std::endl
<< "Exception message: " << e.what() << std::endl
<< "Thread will exit." << std::endl;
g_log.error() << "Caught an invalid argument exception." << std::endl
<< "Exception message: " << e.what() << std::endl;
m_isConnected = false;
std::string newMsg( "Invalid argument exception thrown from the background thread: ");
newMsg += e.what();
m_backgroundException = boost::shared_ptr<std::runtime_error>( new std::runtime_error( newMsg) );

} catch (...) { // Default exception handler
g_log.fatal() << "Uncaught exception in SNSLiveEventDataListener network read thread."
<< " Thread is exiting." << std::endl;
g_log.error() << "Uncaught exception in ISISLiveEventDataListener network read thread." << std::endl;
m_isConnected = false;
m_backgroundException =
boost::shared_ptr<std::runtime_error>( new std::runtime_error( "Unknown error in backgound thread") );
Expand Down Expand Up @@ -338,6 +352,12 @@ void ISISLiveEventDataListener::saveEvents(const std::vector<TCPStreamEventNeutr

if ( period >= static_cast<size_t>(m_numberOfPeriods) )
{
auto warn = m_warnings.find("period");
if ( warn != m_warnings.end() )
{
g_log.warning() << warn->second << std::endl;
m_warnings.erase( warn );
}
period = 0;
}

Expand All @@ -360,15 +380,6 @@ void ISISLiveEventDataListener::loadSpectraMap()
std::vector<int> spec;
getIntArray( "UDET", udet, ndet);
getIntArray( "SPEC", spec, ndet);

// Assign spectra ids to the spectra
int nspec = m_numberOfSpectra;
if ( ndet < nspec ) nspec = ndet;
for(size_t i = 0; i < static_cast<size_t>(nspec); ++i)
{
m_eventBuffer[0]->getSpectrum(i)->setSpectrumNo( spec[i] );
}

// set up the mapping
m_eventBuffer[0]->updateSpectraUsing(API::SpectrumDetectorMapping(spec, udet));
}
Expand All @@ -388,6 +399,7 @@ void ISISLiveEventDataListener::loadInstrument(const std::string &instrName)
const char *warningMessage = "Failed to load instrument ";
try
{
g_log.notice() << "Loading instrument " << instrName << " ... " << std::endl;
API::Algorithm_sptr alg = API::AlgorithmFactory::Instance().create("LoadInstrument",-1);
alg->initialize();
alg->setPropertyValue("InstrumentName",instrName);
Expand All @@ -400,6 +412,7 @@ void ISISLiveEventDataListener::loadInstrument(const std::string &instrName)
{
g_log.warning() << warningMessage << instrName << std::endl;
}
g_log.notice() << "Instrument loaded." << std::endl;
}
catch(std::exception& e)
{
Expand Down

0 comments on commit 2e8e7c3

Please sign in to comment.