00001
00034 #include "cmsclient.h"
00035
00036 #include <iostream>
00037 #include <stdexcept>
00038
00039 #include <activemq/library/ActiveMQCPP.h>
00040 #include <cms/Connection.h>
00041 #include <cms/ConnectionFactory.h>
00042 #include <cms/Destination.h>
00043 #include <cms/MapMessage.h>
00044 #include <cms/MessageConsumer.h>
00045 #include <cms/Session.h>
00046
00047 #include "alarmconfiguration.h"
00048 #include "alarmserverconnector.h"
00049
00050 using namespace AlarmNotifications;
00051
00052 CMSClient::CMSClient ( AlarmServerConnector& asc )
00053 : _asc ( asc ),
00054 _connection ( nullptr ),
00055 _session ( nullptr ),
00056 _topicServer ( nullptr ),
00057 _consumerServer ( nullptr )
00058 {
00059 try
00060 {
00061 activemq::library::ActiveMQCPP::initializeLibrary();
00062 }
00063 catch ( std::runtime_error& ex )
00064 {
00065 std::cerr << "Runtime error while initializing ActiveMQCPP library!" << std::endl;
00066 std::cerr << ex.what() << std::endl;
00067 throw;
00068 }
00069 try
00070 {
00071 cms::ConnectionFactory* factory = cms::ConnectionFactory::createCMSConnectionFactory ( AlarmConfiguration::instance().getActiveMQURI() );
00072 _connection = factory->createConnection (
00073 AlarmConfiguration::instance().getActiveMQUsername(),
00074 AlarmConfiguration::instance().getActiveMQPassword()
00075 );
00076 }
00077 catch ( cms::CMSException& ex )
00078 {
00079 std::cerr << "Cannot create CMS connection!" << std::endl;
00080 std::cerr << ex.getMessage() << std::endl;
00081 ex.printStackTrace();
00082 _connection = nullptr;
00083 throw;
00084 }
00085 _connection->start();
00086 _connection->setExceptionListener ( this );
00087 try
00088 {
00089
00090 _session = _connection->createSession ( cms::Session::AUTO_ACKNOWLEDGE );
00091 _topicServer = _session->createTopic ( AlarmConfiguration::instance().getActiveMQTopicName() );
00092 _consumerServer = _session->createConsumer ( _topicServer );
00093 _consumerServer->setMessageListener ( this );
00094 }
00095 catch ( cms::CMSException& ex )
00096 {
00097 std::cerr << "Cannot create CMS session/topic!" << std::endl;
00098 std::cerr << ex.getMessage() << std::endl;
00099 ex.printStackTrace();
00100 if ( _session != nullptr )
00101 _session->close();
00102 _connection->close();
00103 delete _consumerServer;
00104 delete _topicServer;
00105 delete _session;
00106 delete _connection;
00107 _consumerServer = nullptr;
00108 _topicServer = nullptr;
00109 _session = nullptr;
00110 _connection = nullptr;
00111 throw;
00112 }
00113 }
00114
00115 CMSClient::~CMSClient()
00116 {
00117
00118
00119
00120
00121
00122
00123
00124 try
00125 {
00126 if ( _session != nullptr )
00127 _session->close();
00128 }
00129 catch ( ... ) {}
00130 try
00131 {
00132 if ( _connection != nullptr )
00133 _connection->close();
00134 }
00135 catch ( ... ) {}
00136 try
00137 {
00138 delete _consumerServer;
00139 }
00140 catch ( ... ) {}
00141 try
00142 {
00143 delete _topicServer;
00144 }
00145 catch ( ... ) {}
00146 try
00147 {
00148 delete _session;
00149 }
00150 catch ( ... ) {}
00151 try
00152 {
00153 delete _connection;
00154 }
00155 catch ( ... ) {}
00156 try
00157 {
00158 activemq::library::ActiveMQCPP::shutdownLibrary();
00159 }
00160 catch ( ... ) {}
00161 }
00162
00163 void CMSClient::onMessage ( const cms::Message* message ) noexcept
00164 {
00165 const cms::MapMessage*const mapmessage = dynamic_cast<const cms::MapMessage*> ( message );
00166
00167
00168 if ( mapmessage == nullptr )
00169 return;
00170 if ( !mapmessage->itemExists ( "TEXT" ) )
00171 return;
00172
00173 if ( mapmessage->getString ( "TEXT" ) != "STATE" )
00174 return;
00175 if ( !mapmessage->itemExists ( "NAME" ) || !mapmessage->itemExists ( "SEVERITY" ) || !mapmessage->itemExists ( "STATUS" ) )
00176 return;
00177 std::string rawname = mapmessage->getString ( "NAME" );
00178 const std::string name = rawname.replace ( rawname.find ( "epics://" ), 8, "" );
00179 const AlarmStatusEntry ase (
00180 name,
00181 mapmessage->getString ( "SEVERITY" ),
00182 mapmessage->getString ( "STATUS" )
00183 );
00184 _asc.notifyStatusChange ( ase );
00185 }
00186
00187 void CMSClient::onException ( const cms::CMSException& ex ) noexcept
00188 {
00189 ex.printStackTrace();
00190 }