diff --git a/include/ChannelData.hpp b/include/ChannelData.hpp index 9fd59c9a47f074e461448804daf3a687083c6b81..42cc2a206d0d5470489fc1f88092cf34b6bae619 100644 --- a/include/ChannelData.hpp +++ b/include/ChannelData.hpp @@ -40,7 +40,7 @@ public: //- reset : initialize members void reset(std::string channelName) { - channel_name_ = channelName; + channel_name_ = channelName; trigger_time_ = "NaN"; wave_array_count_ = 0; vertical_offset_ = 0.; @@ -57,19 +57,21 @@ public: //- operator= ChannelData& operator=(const ChannelData& src) { - this->channel_name_ = src.channel_name_; - this->trigger_time_ = src.trigger_time_; - this->wave_array_count_ = src.wave_array_count_; - this->vertical_offset_ = src.vertical_offset_; - this->vertical_gain_ = src.vertical_gain_; - this->horizontal_offset_ = src.horizontal_offset_; - this->horizontal_interval_ = src.horizontal_interval_; - this->nominal_bits_ = src.nominal_bits_; - this->wave_array_1_ = src.wave_array_1_; - this->wave_array_2_ = src.wave_array_2_; - this->vertical_scaled_data_ = src.vertical_scaled_data_; - this->raw_waveform_data_ = src.raw_waveform_data_; - + if(&src != this) + { + this->channel_name_ = src.channel_name_; + this->trigger_time_ = src.trigger_time_; + this->wave_array_count_ = src.wave_array_count_; + this->vertical_offset_ = src.vertical_offset_; + this->vertical_gain_ = src.vertical_gain_; + this->horizontal_offset_ = src.horizontal_offset_; + this->horizontal_interval_ = src.horizontal_interval_; + this->nominal_bits_ = src.nominal_bits_; + this->wave_array_1_ = src.wave_array_1_; + this->wave_array_2_ = src.wave_array_2_; + this->vertical_scaled_data_ = src.vertical_scaled_data_; + this->raw_waveform_data_ = src.raw_waveform_data_; + } return *this; } diff --git a/include/SocketLecroy.h b/include/SocketLecroy.h index 81f13c46fee4462d1f4689496976bd4d53effc40..a8b996c46f15750113398d016e7234f523d8cdee 100644 --- a/include/SocketLecroy.h +++ b/include/SocketLecroy.h @@ -27,7 +27,6 @@ #include <winsock2.h> #endif #include "SocketException.h" -#include <yat/threading/Mutex.h> //////////////////////////////////////////////////////////////////// // // SocketLecroy :: DEFINITION @@ -44,14 +43,13 @@ typedef struct //- Constants definitions //----------------------------------------------- -const int SERVER_PORT = 1861; //- It's the same port number for all Lecroy scope's. - -const int EOI_FLAG = 0x01; -const int SRQ_FLAG = 0x08; -const int CLEAR_FLAG = 0x10; +const int SERVER_PORT = 1861; //- It's the same port number for all Lecroy scope's. +const int EOI_FLAG = 0x01; +const int SRQ_FLAG = 0x08; +const int CLEAR_FLAG = 0x10; const int LOCKOUT_FLAG = 0x20; -const int REMOTE_FLAG = 0x40; -const int DATA_FLAG = 0x80; +const int REMOTE_FLAG = 0x40; +const int DATA_FLAG = 0x80; const unsigned int TCP_MINIMUM_PACKET_SIZE = 64; @@ -59,12 +57,10 @@ class SocketLecroy { private: - SocketLecroy(); //- just one instance of SocketLecroy must be done for all waveforms of a same Lecroy device + SocketLecroy(); //- just one instance of SocketLecroy must be done for all waveforms of a same Lecroy device ~SocketLecroy(); - yat::Mutex lock_; - - static SocketLecroy* SL_instance; //- ptr on the SocketLecroy instance + static SocketLecroy* SL_instance; //- ptr on the SocketLecroy instance static bool sConnectedFlag; @@ -79,11 +75,11 @@ public: void write_read (char* in_buf, unsigned int in_length, char* out_buf, int* out_length, bool eoi_flag); - void TCP_WriteDevice (char *buf, int length,bool eoi_flag) throw (lecroy::SocketException); //- send a cmd to the device - void TCP_ReadDevice (char *buf, unsigned int length, int* nb_byte_received) throw (lecroy::SocketException); //- read the device replie - void TCP_Connect (char *ip_address) throw (lecroy::SocketException); //- build TCP/IP connection - void TCP_Disconnect (void) throw (lecroy::SocketException); //- disconnect the device - void TCP_ClearDevice (void) throw (lecroy::SocketException); //- disconnect and reconnect the device + void TCP_WriteDevice (char *buf, int length,bool eoi_flag); //- send a cmd to the device + void TCP_ReadDevice (char *buf, unsigned int length, int* nb_byte_received); //- read the device replie + void TCP_Connect (char *ip_address); //- build TCP/IP connection + void TCP_Disconnect (void); //- disconnect the device + void TCP_ClearDevice (void); //- disconnect and reconnect the device }; diff --git a/include/Waveform.h b/include/Waveform.h index 19afd7a8475c1eed5f9eca6a271f3124aedcb0db..d933023d1636462b32999a9e524b9132e62ae801 100644 --- a/include/Waveform.h +++ b/include/Waveform.h @@ -51,7 +51,7 @@ typedef struct //- Blocks : long wave_descriptor; //- length (in bytes) of block WAVEDESC long user_text; //- length (in bytes) of block USERTEXT - long reserved_res_desc1; //- RESERVED + long reserved_res_desc1; //- RESERVED //- Arrays : long trigtime_array; //- length (in bytes) of TRIGTIME long ris_time_array; //- length (in bytes) of RIS_TIME @@ -73,9 +73,9 @@ typedef struct short nominal_bits; //- intrinsic presision char reserved_4[2]; //- RESERVED float horizontal_interval; //- sampling interval for time domain waveforms - double horizontal_offset; //- trigger offset for the first sweep of the trigger, seconds between + double horizontal_offset; //- trigger offset for the first sweep of the trigger, seconds between //- the trigger and the first data point - double pixel_offset; //- needed to know how to display the waveform + double pixel_offset; //- needed to know how to display the waveform char vertical_unit[48]; //- units of the vertical axis char horizontal_unit[48]; //- units of the horizontal axis float reserved_5; @@ -99,18 +99,17 @@ class WaveForm_data { private: - char ptrRawData[MAX_WAVEFORM_DATA_LENGTH]; //- ptr on the received waveform data + char ptrRawData[MAX_WAVEFORM_DATA_LENGTH]; //- ptr on the received waveform data SocketLecroy* ptr_com; //- Waveform description : - WAVEDESC_BLOCK *waveBlockData; //- ptr on the struct WAVEDESC_BLOCK + WAVEDESC_BLOCK *waveBlockData; //- ptr on the struct WAVEDESC_BLOCK //- time info to acquire waveform data std::string time_to_get_waveformData; - void get_waveform_data (ChannelData* channelData) - throw (lecroy::WaveformException); //- acquire the waveform data from the scope + void get_waveform_data (ChannelData* channelData); //- acquire the waveform data from the scope void get_trigger_time_value (ChannelData* channelData); diff --git a/include/WaveformMgr.hpp b/include/WaveformMgr.hpp index a6cd474de4d1853ed150f40f6d634cff5d49306e..fd158b6576367a0ecae2e83594b0dc1fac34253e 100644 --- a/include/WaveformMgr.hpp +++ b/include/WaveformMgr.hpp @@ -52,18 +52,18 @@ public: //- check socket is opened bool is_connected() { - return connected_; + return connected_; } std::string get_errors() { - yat::AutoMutex<> guard(this->error_mutex_); - return errors_; + yat::AutoMutex<> guard(this->error_mutex_); + return errors_; } protected: //- process_message (implements yat4tango::DeviceTask pure virtual method) - virtual void process_message (yat::Message& msg) throw (Tango::DevFailed); + virtual void process_message (yat::Message& msg); //- internal mutex yat::Mutex data_mutex_; diff --git a/pom.xml b/pom.xml index b89ee7775b8b7564a7b1aa12f96ab508105ccdd4..96792cd6b595ff0cda374a652f4f488f9dcb3e7c 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ <groupId>fr.soleil.device</groupId> <artifactId>AcquireWaveformLecroy-${aol}-${mode}</artifactId> - <version>1.2.0</version> + <version>1.2.1</version> <packaging>nar</packaging> <name>AcquireWaveformLecroy</name> <description>AcquireWaveformLecroy device</description> diff --git a/src/AcquireWaveformLecroy.cpp b/src/AcquireWaveformLecroy.cpp index ae47d2f8e08bbc51a8ab51a7d29586d80b489071..a311bf94528d57088387354b4bf7c6c9b30d8751 100644 --- a/src/AcquireWaveformLecroy.cpp +++ b/src/AcquireWaveformLecroy.cpp @@ -98,9 +98,11 @@ static const char *RcsId = "$Header: /users/chaize/newsvn/cvsroot/Instrumentatio // //=================================================================== -#include <AcquireWaveformLecroy.h> -#include <AcquireWaveformLecroyClass.h> +// #include <AcquireWaveformLecroy.h> +#include "AcquireWaveformLecroyClass.h" #include <yat/time/Timer.h> +#include <yat4tango/DeviceInfo.h> +#include <yat4tango/Logging.h> static const std::size_t MAX_STRING_LENGTH = 1024; @@ -199,14 +201,8 @@ void AcquireWaveformLecroy::delete_device() this->waveform_mgr_ = 0; } -/*std::cout << "removing log" << std::endl; - // DEBUG_STREAM << "Remove the InnerAppender." << endl; - yat4tango::InnerAppender::release(this); - - // DEBUG_STREAM << "Remove the YatLogAdapter." << endl; - yat4tango::YatLogAdapter::release(); - -std::cout << "removed log : " << log_deleted << std::endl;*/ + yat4tango::DeviceInfo::release(this); + yat4tango::Logging::release(this); } //+---------------------------------------------------------------------------- @@ -265,48 +261,39 @@ void AcquireWaveformLecroy::init_device() //--------------------------------------------------------- //- instanciate the log_adapter & inner_appender in order to manage logs //--------------------------------------------------------- -// try -// { -// DEBUG_STREAM << "Create the YatLogAdapter in order to use logs from third party library." << endl; -// yat4tango::YatLogAdapter::initialize(this); -// -// DEBUG_STREAM << "Create the InnerAppender in order to manage logs." << endl; -// yat4tango::InnerAppender::initialize(this, 512); -// } -// catch (Tango::DevFailed& df) -// { -// ERROR_STREAM << df << endl; -// set_state(Tango::FAULT); -// set_status("OUT OF MEMORY : Failed to create log manager !"); -// return; -// } + yat4tango::DeviceInfo::initialize( this, YAT_XSTR(PROJECT_NAME), YAT_XSTR(PROJECT_VERSION) ); + yat4tango::Logging::initialize(this); char * ipAdd = 0; if (!waveform_mgr_) { - //- transform string to char* - std::size_t length = iPaddress.size(); - ipAdd = new char[length+1]; - for(std::size_t i=0; i<length; i++) - ipAdd[i] = iPaddress[i]; - ipAdd[length] = '\0'; - //- create the waveform obj - waveform_mgr_ = new WaveformMgr(this, ipAdd); + //- transform string to char* + std::size_t length = iPaddress.size(); + ipAdd = new char[length+1]; + for(std::size_t i=0; i<length; i++) + { + ipAdd[i] = iPaddress[i]; + } + ipAdd[length] = '\0'; + //- create the waveform obj + waveform_mgr_ = new WaveformMgr(this, ipAdd); } //- prepare task if ( waveform_mgr_ ) { - //- add channel to monitor - waveform_mgr_->add_channel(channelName); - //- start thread (if not already started) - if ( !waveform_mgr_->periodic_msg_enabled() ) - waveform_mgr_->go(); + //- add channel to monitor + waveform_mgr_->add_channel(channelName); + //- start thread (if not already started) + if ( !waveform_mgr_->periodic_msg_enabled() ) + { + waveform_mgr_->go(); + } } //- release temporary allocation if (ipAdd) { - delete [] ipAdd; - ipAdd = 0; + delete [] ipAdd; + ipAdd = 0; } } @@ -380,7 +367,9 @@ void AcquireWaveformLecroy::get_device_property() // End of Automatic code generation //------------------------------------------------------------------ if( !data_put.empty() ) + { get_db_device()->put_property(data_put); + } } //+---------------------------------------------------------------------------- @@ -406,12 +395,12 @@ void AcquireWaveformLecroy::read_attr_hardware(vector<long> &attr_list) { //- DEBUG_STREAM << "In read_attr_hardware for " << attr_list.size(); //- DEBUG_STREAM << " attribute(s)" << endl; -yat::Timer t; + yat::Timer t; //- check if ( !waveform_mgr_ ) { - FATAL_STREAM << "read_attr_hardware : device initialization failed!" << std::endl; + FATAL_STREAM << "read_attr_hardware : device initialization failed!" << std::endl; Tango::Except::throw_exception("COMMUNICATION_BROKEN", "Device initialization failed : no Waveform manager.", "AcquireWaveformLecroy::read_attr_hardware()" @@ -421,10 +410,10 @@ yat::Timer t; try { channel_data_ = waveform_mgr_->get_channel_data(); - if (channel_data_) - { - data_length_ = channel_data_->wave_array_count_; - } + if (channel_data_) + { + data_length_ = channel_data_->wave_array_count_; + } } catch(const lecroy::WaveformException& we) { @@ -454,7 +443,7 @@ yat::Timer t; throw; } - INFO_STREAM << "READ_HW : all data are now updated. DONE in " << t.elapsed_msec() << " milliseconds.\n" << std::endl; + DEBUG_STREAM << "READ_HW : all data are now updated. DONE in " << t.elapsed_msec() << " milliseconds.\n" << std::endl; } //+---------------------------------------------------------------------------- @@ -467,15 +456,11 @@ yat::Timer t; void AcquireWaveformLecroy::read_verticalScaledData(Tango::Attribute &attr) { //- DEBUG_STREAM << "AcquireWaveformLecroy::read_verticalScaledData(Tango::Attribute &attr) entering... "<< endl; -yat::Timer t; - -// for(std::size_t idx = 0; idx < data_length_; idx++) -// attr_verticalScaledData_read[idx] = channel_data_->vertical_scaled_data_[idx]; -// attr.set_value(attr_verticalScaledData_read, data_length_); if ( data_length_ ) - attr.set_value(&channel_data_->vertical_scaled_data_[0], data_length_); - DEBUG_STREAM << "read_verticalScaledData : " << t.elapsed_msec() << " milliseconds.\n" << std::endl; + { + attr.set_value(&channel_data_->vertical_scaled_data_[0], data_length_); + } } //+---------------------------------------------------------------------------- @@ -488,14 +473,11 @@ yat::Timer t; void AcquireWaveformLecroy::read_triggerTime(Tango::Attribute &attr) { //- DEBUG_STREAM << "AcquireWaveformLecroy::read_triggerTime(Tango::Attribute &attr) entering... "<< endl; -yat::Timer t; - // Add your own code here std::string response = channel_data_->trigger_time_; - strcpy(*attr_triggerTime_read, response.c_str()); + ::strcpy(*attr_triggerTime_read, response.c_str()); attr.set_value(attr_triggerTime_read); - DEBUG_STREAM << "read_triggerTime : " << t.elapsed_msec() << " milliseconds.\n" << std::endl; } //+---------------------------------------------------------------------------- @@ -508,13 +490,11 @@ yat::Timer t; void AcquireWaveformLecroy::read_verticalOffset(Tango::Attribute &attr) { //- DEBUG_STREAM << "AcquireWaveformLecroy::read_verticalOffset(Tango::Attribute &attr) entering... "<< endl; -yat::Timer t; //- Add your own code here *attr_verticalOffset_read = channel_data_->vertical_offset_; attr.set_value(attr_verticalOffset_read); - DEBUG_STREAM << "read_verticalOffset : " << t.elapsed_msec() << " milliseconds.\n" << std::endl; } //+---------------------------------------------------------------------------- @@ -527,13 +507,10 @@ yat::Timer t; void AcquireWaveformLecroy::read_verticalGain(Tango::Attribute &attr) { //- DEBUG_STREAM << "AcquireWaveformLecroy::read_verticalGain(Tango::Attribute &attr) entering... "<< endl; -yat::Timer t; - //- Add your own code here *attr_verticalGain_read = channel_data_->vertical_gain_; attr.set_value(attr_verticalGain_read); - DEBUG_STREAM << "read_verticalGain : " << t.elapsed_msec() << " milliseconds.\n" << std::endl; } //+---------------------------------------------------------------------------- @@ -546,13 +523,10 @@ yat::Timer t; void AcquireWaveformLecroy::read_horizontalOffset(Tango::Attribute &attr) { //- DEBUG_STREAM << "AcquireWaveformLecroy::read_horizontalOffset(Tango::Attribute &attr) entering... "<< endl; - yat::Timer t; - //- Add your own code here *attr_horizontalOffset_read = channel_data_->horizontal_offset_; attr.set_value(attr_horizontalOffset_read); - DEBUG_STREAM << "read_horizontalOffset : " << t.elapsed_msec() << " milliseconds.\n" << std::endl; } //+---------------------------------------------------------------------------- @@ -565,13 +539,11 @@ void AcquireWaveformLecroy::read_horizontalOffset(Tango::Attribute &attr) void AcquireWaveformLecroy::read_horizontalInterval(Tango::Attribute &attr) { //- DEBUG_STREAM << "AcquireWaveformLecroy::read_horizontalInterval(Tango::Attribute &attr) entering... "<< endl; - yat::Timer t; //- Add your own code here *attr_horizontalInterval_read = channel_data_->horizontal_interval_; attr.set_value(attr_horizontalInterval_read); - DEBUG_STREAM << "read_horizontalInterval : " << t.elapsed_msec() << " milliseconds.\n" << std::endl; } //+---------------------------------------------------------------------------- @@ -584,12 +556,10 @@ void AcquireWaveformLecroy::read_horizontalInterval(Tango::Attribute &attr) void AcquireWaveformLecroy::read_nominalBits(Tango::Attribute &attr) { //- DEBUG_STREAM << "AcquireWaveformLecroy::read_nominalBits(Tango::Attribute &attr) entering... "<< endl; -yat::Timer t; //- Add your own code here *attr_nominalBits_read = channel_data_->nominal_bits_; attr.set_value(attr_nominalBits_read); - DEBUG_STREAM << "read_nominalBits : " << t.elapsed_msec() << " milliseconds.\n" << std::endl; } //+---------------------------------------------------------------------------- @@ -602,12 +572,10 @@ yat::Timer t; void AcquireWaveformLecroy::read_waveArrayCount(Tango::Attribute &attr) { //- DEBUG_STREAM << "AcquireWaveformLecroy::read_waveArrayCount(Tango::Attribute &attr) entering... "<< endl; -yat::Timer t; //- Add your own code here *attr_waveArrayCount_read = data_length_; attr.set_value(attr_waveArrayCount_read); - DEBUG_STREAM << "read_waveArrayCount : " << t.elapsed_msec() << " milliseconds.\n" << std::endl; } //+---------------------------------------------------------------------------- @@ -620,12 +588,10 @@ yat::Timer t; void AcquireWaveformLecroy::read_waveArray2(Tango::Attribute &attr) { //- DEBUG_STREAM << "AcquireWaveformLecroy::read_waveArray2(Tango::Attribute &attr) entering... "<< endl; -yat::Timer t; // Add your own code here *attr_waveArray2_read = channel_data_->wave_array_2_; attr.set_value(attr_waveArray2_read); - DEBUG_STREAM << "read_waveArray2 : " << t.elapsed_msec() << " milliseconds.\n" << std::endl; } //+---------------------------------------------------------------------------- @@ -638,12 +604,10 @@ yat::Timer t; void AcquireWaveformLecroy::read_waveArray1(Tango::Attribute &attr) { //- DEBUG_STREAM << "AcquireWaveformLecroy::read_waveArray1(Tango::Attribute &attr) entering... "<< endl; -yat::Timer t; //- Add your own code here *attr_waveArray1_read = channel_data_->wave_array_1_; attr.set_value(attr_waveArray1_read); - DEBUG_STREAM << "read_waveArray1 : " << t.elapsed_msec() << " milliseconds.\n" << std::endl; } //+---------------------------------------------------------------------------- @@ -657,15 +621,11 @@ void AcquireWaveformLecroy::read_rawWaveformData(Tango::Attribute &attr) { //- DEBUG_STREAM << "AcquireWaveformLecroy::read_rawWaveformData(Tango::Attribute &attr) entering... "<< endl; //- Add your own code here - yat::Timer t; - //- get waveform data - //-for(std::size_t idx = 0; idx < data_length_; idx++) - //- attr_rawWaveformData_read[idx] = channel_data_->raw_waveform_data_[idx]; - //-attr.set_value(attr_rawWaveformData_read, data_length_); if ( data_length_ ) - attr.set_value(&channel_data_->raw_waveform_data_[0], data_length_); - DEBUG_STREAM << "read_rawWaveformData : " << t.elapsed_msec() << " milliseconds.\n" << std::endl; + { + attr.set_value(&channel_data_->raw_waveform_data_[0], data_length_); + } } //+---------------------------------------------------------------------------- @@ -725,7 +685,7 @@ Tango::DevString AcquireWaveformLecroy::write_read(Tango::DevString argin) // (chapter : Writing a TANGO DS / Exchanging data) //------------------------------------------------------------ //- DEBUG_STREAM << "AcquireWaveformLecroy::write_read(): entering... !" << endl; -yat::Timer t; + yat::Timer t; Tango::DevString argout; std::string resp = waveform_mgr_->write_read(argin); @@ -733,7 +693,9 @@ yat::Timer t; std::size_t length = resp.size(); if ( length > MAX_STRING_LENGTH ) - length = MAX_STRING_LENGTH; + { + length = MAX_STRING_LENGTH; + } argout = new char[length+1]; ::strcpy(argout, resp.c_str()); @@ -777,25 +739,24 @@ Tango::DevState AcquireWaveformLecroy::dev_state() argout = Tango::ALARM; set_status("No socket connection with Lecroy."); } - - if ( !data_length_ ) - { - argout = Tango::ALARM; - set_status("Failed to gather channel data : no data."); - } - + if ( argout != Tango::FAULT && argout != Tango::ALARM ) { errors_ = waveform_mgr_->get_errors(); - if ( errors_.empty() ) + if ( !errors_.empty() ) { - argout = Tango::ON; - set_status("No error(s)."); + argout = Tango::ALARM; + set_status(errors_.c_str()); } + else if ( !data_length_ ) + { + argout = Tango::ALARM; + set_status("Failed to gather channel data : no data."); + } else { - argout = Tango::ALARM; - set_status(errors_.c_str()); + argout = Tango::ON; + set_status("No error(s)."); } } diff --git a/src/AcquireWaveformLecroy.h b/src/AcquireWaveformLecroy.h index b21b4643665f8e98ce8dc6daf90e3f21c275ec19..d82d46fb754b710f0a6e6ab733500a97b6314041 100644 --- a/src/AcquireWaveformLecroy.h +++ b/src/AcquireWaveformLecroy.h @@ -63,8 +63,6 @@ #define _ACQUIREWAVEFORMLECROY_H #include <tango.h> -// #include <yat4tango/InnerAppender.h> -// #include <yat4tango/YatLogAdapter.h> //using namespace Tango; #include "SocketException.h" #include "WaveformMgr.hpp" diff --git a/src/LinuxSocketLecroy.cpp b/src/LinuxSocketLecroy.cpp index 8e948e865917a22a315acd336bdb2ebac8c52a49..239f1f40f9feed58d45e4d0c252dcadbfc3b3460 100644 --- a/src/LinuxSocketLecroy.cpp +++ b/src/LinuxSocketLecroy.cpp @@ -43,55 +43,64 @@ //- perf!!! #include <yat/time/Timer.h> -static int hSocket; +static int hSocket = 0; static int sTimeout = 2; //- second(s) static char sCurrentAddress[256]; bool SocketLecroy::sConnectedFlag = false; -const int CMD_BUF_LEN = 8192; -static char sCommandBuffer[CMD_BUF_LEN]; +const int CMD_BUF_LEN = 8192; +static char sCommandBuffer[CMD_BUF_LEN]; //- init of the static instance SocketLecroy* SocketLecroy::SL_instance = 0; //- ptr on the SocketLecroy instance +// ============================================================================ +// SocketLecroy::get_instance +// ============================================================================ SocketLecroy* SocketLecroy::get_instance() { if( !SL_instance ) + { SL_instance = new SocketLecroy(); + } return SL_instance; - } +// ============================================================================ +// SocketLecroy::delete_instance +// ============================================================================ void SocketLecroy::delete_instance() { -// std::cout << "\t\t\tSocketLecroy::deleted_instance ... SL_instance = " << SL_instance << std::endl; if(SL_instance) { delete SL_instance; SL_instance = 0; } -// std::cout << "\t\t\tSocketLecroy::deleted_instance DONE -> SL_instance = " << SL_instance << std::endl; } -//- CTOR +// ============================================================================ +// SocketLecroy::SocketLecroy() +// ============================================================================ SocketLecroy::SocketLecroy() { - sConnectedFlag=false; + sConnectedFlag = false; } -//- DTOR +// ============================================================================ +// SocketLecroy::~SocketLecroy() +// ============================================================================ SocketLecroy::~SocketLecroy() { -// std::cout << "\t\t\tSocketLecroy::DTOR SocketLecroy ..." << std::endl; TCP_Disconnect(); -// std::cout << "\t\t\tSocketLecroy::DTOR DONE." << std::endl; } +// ============================================================================ +// SocketLecroy::TCP_Connect +// ============================================================================ //- Build the connection -void SocketLecroy::TCP_Connect(char *ip_address) throw (lecroy::SocketException) +void SocketLecroy::TCP_Connect(char *ip_address) { -yat::Timer t; struct sockaddr_in serverAddr; int result; const int resp = 1; @@ -104,7 +113,9 @@ yat::Timer t; //- connection test if (sConnectedFlag) + { return; + } strcpy(sCurrentAddress, ip_address); tval.tv_sec = 1; @@ -136,6 +147,13 @@ yat::Timer t; "Unable to set socket option to TCP_NODELAY.", "SocketLecroy::TCP_Connect( )."); } + //- set Timeout for write operations + if (setsockopt(hSocket, SOL_SOCKET, SO_SNDTIMEO, (const char*)&tval, sizeof(struct timeval)) != 0) + { + throw lecroy::SocketException("COMMUNICATION_BROKEN ", + "Unable to set socket option to TCP_NODELAY.", + "SocketLecroy::TCP_Connect( )."); + } if (setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (char*)&resp, sizeof(resp)) != 0) { @@ -151,7 +169,9 @@ yat::Timer t; int opts; opts = fcntl (hSocket, F_GETFL); if (opts >= 0) + { opts = (opts | O_NONBLOCK); + } fcntl (hSocket, F_SETFL, opts); //connect(hSocket, (SOCKADDR FAR *) &serverAddr, sockAddrSize); int status = ::connect(hSocket, ( sockaddr *)&serverAddr, sizeof(serverAddr)); @@ -177,7 +197,9 @@ yat::Timer t; } }// Connection is OK. else + { break; + } }//TODO : throw ; // Too much attempts, so failure ! }// TODO : throw ; // Not the right error, so failure ! }// Connected at first attempt ! @@ -195,60 +217,76 @@ yat::Timer t; } sConnectedFlag = true; -// std::cout << "\t\t\t SocketLecroy::TCP_Connect done in " << t.elapsed_msec() << " ms" << std::endl; } +// ============================================================================ +// SocketLecroy::TCP_Disconnect() +// ============================================================================ //- DisconnectFromScope: disconnect from a network device -void SocketLecroy::TCP_Disconnect(void) throw (lecroy::SocketException) +void SocketLecroy::TCP_Disconnect(void) { -// std::cout << "\t\t\tSocketLecroy::comm closing socket ENTREING ...." << std::endl; if (sConnectedFlag) { -// std::cout << "\t\t\tSocketLecroy::comm closing socket ...." << std::endl; close(hSocket); -// std::cout << "\t\t\tSocketLecroy::comm closed." << std::endl; sConnectedFlag = false; } -// std::cout << "\t\t\tSocketLecroy::comm closing socket DONE ...." << std::endl; } +// ============================================================================ +// SocketLecroy::TCP_ClearDevice +// ============================================================================ //- Clear a connection -void SocketLecroy::TCP_ClearDevice(void) throw (lecroy::SocketException) +void SocketLecroy::TCP_ClearDevice(void) { if ( !sConnectedFlag ) + { throw lecroy::SocketException("COMMUNICATION_BROKEN", "Disconnection already done.", "SocketLecroy::TCP_ClearDevice( )."); + } TCP_Disconnect(); TCP_Connect(sCurrentAddress); } +// ============================================================================ +// SocketLecroy::write_read +// ============================================================================ void SocketLecroy::write_read(char* in_buf, unsigned int in_length, char* out_buf, int* out_length, bool eoi_flag) { -yat::Timer t; - yat::AutoMutex<> guard(lock_); + try + { TCP_WriteDevice(in_buf, in_length, eoi_flag); -// std::cout << "\t\t\t SocketLecroy::write_read WRITE done in " << t.elapsed_msec() << " ms" << std::endl; TCP_ReadDevice(out_buf, *out_length, out_length); -// std::cout << "\t\t\t SocketLecroy::write_read done in " << t.elapsed_msec() << " ms" << std::endl; + } + catch(...) + { + //- Noop (TODO) + } } +// ============================================================================ +// SocketLecroy::TCP_WriteDevice +// ============================================================================ //- Send commands to the remote device -void SocketLecroy::TCP_WriteDevice(char *buf, int len, bool eoi_flag) throw (lecroy::SocketException) +void SocketLecroy::TCP_WriteDevice(char *buf, int len, bool eoi_flag) { TCP_HEADER header; int result, bytes_more, bytes_xferd; - char *idxPtr; -// std::cout << "\t\t\t TCP_WriteDevice -> ENTERING ..." << std::endl; + char *idxPtr = 0; + //- test connection if ( !sConnectedFlag ) + { throw lecroy::SocketException("COMMUNICATION_BROKEN ", "Device not connected.", "SocketLecroy::TCP_WriteDevice( )."); + } if (len < CMD_BUF_LEN) + { strcpy(sCommandBuffer, buf); + } //- set the header info header.bEOI_Flag = DATA_FLAG; @@ -258,16 +296,12 @@ void SocketLecroy::TCP_WriteDevice(char *buf, int len, bool eoi_flag) throw (lec header.reserved[2] = 0; header.iLength = htonl(len); -// std::cout << "\t\t\t TCP_WriteDevice -> send ..." << std::endl; //- write the header first - if (send(hSocket, (char *) &header, sizeof(TCP_HEADER), 0) != sizeof(TCP_HEADER)) + if (send(hSocket, (char *) &header, sizeof(TCP_HEADER), 0) < 0) { - throw lecroy::SocketException("COMMUNICATION_BROKEN ", - "Unable to send header info to the server.", - "SocketLecroy::TCP_WriteDevice( )."); + return; } -// std::cout << "\t\t\t TCP_WriteDevice -> sent!" << std::endl; bytes_more = len; idxPtr = buf; bytes_xferd = 0; @@ -278,10 +312,7 @@ void SocketLecroy::TCP_WriteDevice(char *buf, int len, bool eoi_flag) throw (lec if ((result = send(hSocket, (char *) idxPtr, bytes_more, 0)) < 0) { -// std::cout << "\t\t\tFATAL TCP_WriteDevice -> send ..." << std::endl; - throw lecroy::SocketException("COMMUNICATION_BROKEN ", - "Unable to send data to the server.", - "SocketLecroy::TCP_WriteDevice( )."); + return; } bytes_xferd += result; @@ -289,11 +320,13 @@ void SocketLecroy::TCP_WriteDevice(char *buf, int len, bool eoi_flag) throw (lec if (bytes_more <= 0) break; } -// std::cout << "\t\t\tFATAL TCP_WriteDevice -> DONE." << std::endl; } +// ============================================================================ +// SocketLecroy::TCP_ReadDevice +// ============================================================================ //- Read the device answer -void SocketLecroy::TCP_ReadDevice(char *buf, unsigned int len, int *recv_count) throw (lecroy::SocketException) +void SocketLecroy::TCP_ReadDevice(char *buf, unsigned int len, int *recv_count) { TCP_HEADER header; int result; @@ -306,16 +339,20 @@ void SocketLecroy::TCP_ReadDevice(char *buf, unsigned int len, int *recv_count) //- test connection if ( !sConnectedFlag ) + { throw lecroy::SocketException("COMMUNICATION_BROKEN ", "Device not connected.", "SocketLecroy::TCP_ReadDevice( )."); + } *recv_count = 0; if (!buf) + { throw lecroy::SocketException("COMMUNICATION_BROKEN ", "Buffer memory not allocated.", "SocketLecroy::TCP_ReadDevice( )."); + } FD_SET(hSocket, &rd_set); tval.tv_sec = 0; @@ -330,14 +367,13 @@ void SocketLecroy::TCP_ReadDevice(char *buf, unsigned int len, int *recv_count) result = select(hSocket, &rd_set, NULL, NULL, &tval); if (result < 0) { -// std::cout << "\tSocketLecroy::TCP_ReadDevice -> result < 0." << std::endl; TCP_ClearDevice(); throw lecroy::SocketException("COMMUNICATION_BROKEN ", "Read Timeout.", "SocketLecroy::TCP_ReadDevice( )."); } - //- get the header info first + //- get the header info first accum = 0; while (1) { @@ -356,16 +392,12 @@ void SocketLecroy::TCP_ReadDevice(char *buf, unsigned int len, int *recv_count) break; } -// header.iLength = ntohl(header.iLength); unsigned int headerLength = ntohl(header.iLength); //- only read to len amount if (headerLength > space_left) { headerLength = space_left; -/* throw lecroy::SocketException("COMMUNICATION_BROKEN ", - "Read buffer size is too small.", - "SocketLecroy::TCP_ReadDevice( ).");*/ } //- read the rest of the block diff --git a/src/Waveform.cpp b/src/Waveform.cpp index 660601748a9a9f66cc3698b083d7043cd2929c48..dfb9fe1e7ba534fb9ae138c13b431f2b80865fed 100644 --- a/src/Waveform.cpp +++ b/src/Waveform.cpp @@ -23,7 +23,9 @@ #include "Waveform.h" #include <yat/time/Timer.h> -//- CTOR +// ============================================================================ +// WaveForm_data::WaveForm_data +// ============================================================================ WaveForm_data::WaveForm_data(char* lecroyIPAdd) : ptr_com(0), time_to_get_waveformData("") @@ -38,30 +40,39 @@ WaveForm_data::WaveForm_data(char* lecroyIPAdd) ptr_com->TCP_Connect(lecroyIPAdd); } -//- DTOR +// ============================================================================ +// WaveForm_data::~WaveForm_data +// ============================================================================ WaveForm_data::~WaveForm_data() { -// std::cout << "\t\tWaveForm_data::DTOR." << std::endl; - //- close the socket -// ptr_com->TCP_Disconnect(); -// std::cout << "\t\tWaveForm_data::TCP_Disconnect DNE." << std::endl; //- delete the SocketLecroy obj if ( ptr_com ) - SocketLecroy::delete_instance(); -// std::cout << "\t\tWaveForm_data::delete_instance DNE." << std::endl; -// std::cout << "\t\tWaveForm_data::ptrRawData DNE => ptr_com add = " << ptr_com << std::endl; + { + SocketLecroy::delete_instance(); + } } +// ============================================================================ +// WaveForm_data::update_channel_data +// ============================================================================ void WaveForm_data::update_channel_data(ChannelData* chdata) { -// yat::Timer t; - get_waveform_data(chdata); - get_trigger_time_value(chdata); -// std::cout << "\t\tWaveForm_data::update_channel_data -> DONE in " << t.elapsed_msec() << " ms." << std::endl; + yat::Timer t; + try{ + get_waveform_data(chdata); + + get_trigger_time_value(chdata); + } + catch(const lecroy::SocketException& se) + {//- TODO + } } +// ============================================================================ +// WaveForm_data::get_waveform_data +// ============================================================================ //- Method to return the raw data of the acquired waveform -void WaveForm_data::get_waveform_data(ChannelData* channelData ) throw (lecroy::WaveformException) +void WaveForm_data::get_waveform_data(ChannelData* channelData) { std::string cmd(""); char* cmdStr = 0; @@ -69,9 +80,7 @@ unsigned short OFFSET_STRUCT = 0; std::ostringstream oss; //- FOR DEBUG : comment out Timer and STD::COUT !! -// yat::Timer t; - -// std::cout << "\t\t\tget_waveform_data ENTERING ..." << std::endl; + yat::Timer t; //- init ptr waveblocdata which point on wavedesc_blocs structure waveBlockData = 0; @@ -79,7 +88,6 @@ std::ostringstream oss; //- get channel name std::string ch_name("") ; ch_name = channelData->channel_name_; -// std::cout << "\t\t\tget_waveform_data -> ch name = " << ch_name << std::endl; //- prepare the cmd to get the waveform data cmd = ch_name + std::string(":WF? ALL"); @@ -88,22 +96,32 @@ std::ostringstream oss; ::strncpy(cmdStr, cmd.c_str(), in_length); //- send the request -// std::cout << "\t\tget_waveform_data -> write cmd..." << ptr_com << std::endl; int out_length = MAX_WAVEFORM_DATA_LENGTH; try { - //- erase previsous data - ::memset (ptrRawData, 0, out_length); - ptr_com->write_read(cmdStr, in_length, ptrRawData, &out_length, true); -// time_to_read_data = t.elapsed_msec(); -// std::cout << "\t\tWaveForm_data::Time to WRITE_READ resp lgth = " << out_length << " data : " << t.elapsed_msec() << std::endl; + //- erase previsous data + ::memset (ptrRawData, 0, out_length); + ptr_com->write_read(cmdStr, in_length, ptrRawData, &out_length, true); + } + catch(const lecroy::SocketException& se) + {//- TODO + //- delete cmd allocation + if(cmdStr) + { + delete [] cmdStr; + cmdStr = 0; + } + return; } catch(const lecroy::WaveformException &) { - //- XE : - throw lecroy::WaveformException("OPERATION_FAILED", - "The TCP_ReadDevice() method failed.", - "WaveForm_data::get_waveform_data( )."); + //- delete cmd allocation + if(cmdStr) + { + delete [] cmdStr; + cmdStr = 0; + } + return; } //- delete cmd allocation @@ -114,7 +132,7 @@ std::ostringstream oss; } //- calculation of the offset of the structure (it can be 15 or 21) - unsigned short i = 0; + std::size_t i = 0; for(i=0; i < 22 ; i++) { if(ptrRawData[i] == 'W' && ptrRawData[i+1] == 'A') @@ -127,17 +145,16 @@ std::ostringstream oss; //- test if the OFFSET_STRUCT is found if(!OFFSET_STRUCT) - throw lecroy::WaveformException("DATA_OUT_OF_RANGE", - "The offset of the structure is not found.", - "WaveForm_data::get_waveform_data( )."); -// std::cout << "\t\tOFFSET of struct = " << OFFSET_STRUCT << std::endl; + { + return; + } //- update the struct WAVEDESC_BLOC waveBlockData = (WAVEDESC_BLOCK*) (ptrRawData+OFFSET_STRUCT); if(!waveBlockData) - throw lecroy::WaveformException("OUT_OF_MEMORY", - "The pointer for the receive data can't be allocated before the read operation.", - "WaveForm_data::get_waveform_data( )."); + { + return; + } //- copy data channelData->wave_array_count_ = waveBlockData->wave_array_count; @@ -152,45 +169,61 @@ std::ostringstream oss; channelData->raw_waveform_data_.resize(waveBlockData->wave_array_count); channelData->vertical_scaled_data_.resize(waveBlockData->wave_array_count); //- populate vectors - for(long idx=0; idx < waveBlockData->wave_array_count; idx++) + for(std::size_t idx=0; idx < waveBlockData->wave_array_count; idx++) { //- raw data channelData->raw_waveform_data_[idx] = (ptrRawData + OFFSET_STRUCT + waveBlockData->wave_descriptor)[idx]; //- compute and copy scaled data channelData->vertical_scaled_data_[idx] = (waveBlockData->vertical_gain * channelData->raw_waveform_data_[idx]) - waveBlockData->vertical_offset; } - -// std::cout << "\t\tWaveForm_data::get_waveform_data done in : " << t.elapsed_msec() << " ms." << std::endl; } +// ============================================================================ +// WaveForm_data::get_trigger_time_value +// ============================================================================ //- Method to return the trigger time of the acquired waveform void WaveForm_data::get_trigger_time_value (ChannelData* channelData) { //- The format is : "Date = month, day, year ; Time = hours:minutes:seconds" -std::string str_seconds(""); -std::string str_minutes(""); -std::string str_hours (""); -std::string str_days (""); -std::string str_months (""); -std::string str_years (""); - - //- hours, min, sec with 2 digits - str_seconds = XString<double>::convertToString(waveBlockData->trigger_time_seconds); - if(waveBlockData->trigger_time_seconds<10) - str_seconds = "0" + str_seconds; - - str_minutes = XString<int>::convertToString((int)waveBlockData->trigger_time_minutes); - if(waveBlockData->trigger_time_minutes<10) - str_minutes = "0" + str_minutes; - - str_hours = XString<int>::convertToString(waveBlockData->trigger_time_hours); - if(waveBlockData->trigger_time_hours<10) - str_hours = "0" + str_hours; - - str_days = XString<int>::convertToString(waveBlockData->trigger_time_days); - str_months= XString<int>::convertToString(waveBlockData->trigger_time_months); - str_years = XString<short>::convertToString(waveBlockData->trigger_time_year); +std::string str_seconds("00"); +std::string str_minutes("00"); +std::string str_hours ("00"); +std::string str_days ("00"); +std::string str_months ("00"); +std::string str_years ("00"); + try + { + if(waveBlockData) + { + //- hours, min, sec with 2 digits + str_seconds = XString<double>::convertToString(waveBlockData->trigger_time_seconds); + if(waveBlockData->trigger_time_seconds<10) + { + str_seconds = "0" + str_seconds; + } + + str_minutes = XString<int>::convertToString((int)waveBlockData->trigger_time_minutes); + if(waveBlockData->trigger_time_minutes<10) + { + str_minutes = "0" + str_minutes; + } + + str_hours = XString<int>::convertToString(waveBlockData->trigger_time_hours); + if(waveBlockData->trigger_time_hours<10) + { + str_hours = "0" + str_hours; + } + + str_days = XString<int>::convertToString(waveBlockData->trigger_time_days); + str_months= XString<int>::convertToString(waveBlockData->trigger_time_months); + str_years = XString<short>::convertToString(waveBlockData->trigger_time_year); + } + } + catch(...) + { + //- Noop + } //- Construct the string Trigger Time: channelData->trigger_time_ = "Date = " + str_months + "/" + str_days + "/" + str_years + " ; Time = " + str_hours + ":" + str_minutes + ":" + str_seconds; } diff --git a/src/WaveformMgr.cpp b/src/WaveformMgr.cpp index 8552553bfabb4d200e1f45f4e82df17dcc3b8f7e..89d2d29ca1673015324f954cead0d8429ac5f5b4 100644 --- a/src/WaveformMgr.cpp +++ b/src/WaveformMgr.cpp @@ -18,15 +18,15 @@ #include "WaveformMgr.hpp" #include "SocketLecroy.h" //- write/read -const std::size_t MAX_RESPONSE_LENGTH = 150000; +const std::size_t MAX_RESPONSE_LENGTH = 150000; -const std::size_t PERIODIC_MSG_PERIOD = 1100; //- (in ms) : time to update one channel data, it cannot be less than 1100 ms! +const std::size_t PERIODIC_MSG_PERIOD = 1100; //- (in ms) : time to update one channel data, it cannot be less than 1100 ms! -const std::size_t MSG_TIMEOUT = 2000; //- in ms +const std::size_t MSG_TIMEOUT = 2000; //- in ms // ============================================================================ // SOME USER DEFINED MESSAGES // ============================================================================ -const std::size_t WRITE_READ = yat::FIRST_USER_MSG + 10; +const std::size_t WRITE_READ = yat::FIRST_USER_MSG + 10; // ====================================================================== // WaveformMgr::WaveformMgr @@ -36,9 +36,9 @@ WaveformMgr::WaveformMgr (Tango::DeviceImpl* host_device, char* ipAdd) waveform_ptr_(0), channel_data_(0), lecroy_resp_(""), - ipAddress_(ipAdd), - connected_(false), - errors_("") + ipAddress_(ipAdd), + connected_(false), + errors_("") { //- noop } @@ -56,7 +56,7 @@ WaveformMgr::~WaveformMgr (void) // ============================================================================ // WaveformMgr::process_message // ============================================================================ -void WaveformMgr::process_message (yat::Message& _msg) throw (Tango::DevFailed) +void WaveformMgr::process_message (yat::Message& _msg) { DEBUG_STREAM << "WaveformMgr::handle_message::receiving msg " << _msg.to_string() << std::endl; @@ -71,24 +71,27 @@ void WaveformMgr::process_message (yat::Message& _msg) throw (Tango::DevFailed) //---------------------------------------------------- yat::Timer t; - if ( !waveform_ptr_ ) - waveform_ptr_ = new WaveForm_data(ipAddress_); - if (waveform_ptr_) - { - //- check socket is up - connected_ = SocketLecroy::get_instance()->is_connected(); - //- configure optional msg handling - this->enable_timeout_msg(false); - this->enable_periodic_msg(true); - this->set_periodic_msg_period(PERIODIC_MSG_PERIOD); - this->errors_.clear(); - INFO_STREAM << "\tWaveformMgr::TASK_INIT finished in " << t.elapsed_msec() << " ms." << std::endl; - } - else - { - yat::AutoMutex<> guard(this->error_mutex_); - errors_ = "Manager : failed to create channel structure to update data."; - } + if ( !waveform_ptr_ ) + { + waveform_ptr_ = new WaveForm_data(ipAddress_); + } + + if (waveform_ptr_) + { + //- check socket is up + connected_ = SocketLecroy::get_instance()->is_connected(); + //- configure optional msg handling + this->enable_timeout_msg(false); + this->enable_periodic_msg(true); + this->set_periodic_msg_period(PERIODIC_MSG_PERIOD); + this->errors_.clear(); + INFO_STREAM << "\tWaveformMgr::TASK_INIT finished in " << t.elapsed_msec() << " ms." << std::endl; + } + else + { + yat::AutoMutex<> guard(this->error_mutex_); + errors_ = "Manager : failed to create channel structure to update data."; + } } break; //- THREAD_EXIT ---------------------- @@ -97,26 +100,22 @@ void WaveformMgr::process_message (yat::Message& _msg) throw (Tango::DevFailed) DEBUG_STREAM << "WaveformMgr::handle_message::THREAD_EXIT::thread is quitting" << std::endl; //- "release" code goes here //---------------------------------------------------- -// yat::Timer t; - if ( waveform_ptr_ ) - { - delete waveform_ptr_; - waveform_ptr_ = 0; - } -// std::cout << "\tWaveformMgr::TASK_EXIT -> waveform_ptr_ in " << t.elapsed_msec() << " ms." << std::endl; - yat::AutoMutex<> guard(this->data_mutex_); - if ( channel_data_ ) - { - delete channel_data_; - channel_data_ = 0; - } -// std::cout << "\tWaveformMgr::TASK_EXIT -> channel_data_" << t.elapsed_msec() << " ms." << std::endl; + if ( waveform_ptr_ ) + { + delete waveform_ptr_; + waveform_ptr_ = 0; + } + yat::AutoMutex<> guard(this->data_mutex_); + if ( channel_data_ ) + { + delete channel_data_; + channel_data_ = 0; + } } break; //- THREAD_PERIODIC ------------------ case yat::TASK_PERIODIC: { - DEBUG_STREAM << "WaveformMgr::handle_message::handling THREAD_PERIODIC msg" << std::endl; //- code relative to the task's periodic job goes here //---------------------------------------------------- yat::Timer t; @@ -125,53 +124,64 @@ void WaveformMgr::process_message (yat::Message& _msg) throw (Tango::DevFailed) { try { - ChannelData tmp_channel(channel_data_->channel_name_); - waveform_ptr_->update_channel_data(&tmp_channel); -// std::cout << "\n\n\tWaveformMgr::TASK_PERIODIC -> update_channel_data in " << t.elapsed_msec() << " ms." << std::endl; - {//- critical section - yat::AutoMutex<> guard(this->data_mutex_); - *channel_data_ = tmp_channel; - } - {//- no error - yat::AutoMutex<> guard(this->error_mutex_); - errors_.clear(); - } - INFO_STREAM << "\tWaveformMgr::TASK_PERIODIC -> done in " << t.elapsed_msec() << " ms." << std::endl; + ChannelData tmp_channel(channel_data_->channel_name_); + waveform_ptr_->update_channel_data(&tmp_channel); + {//- critical section + yat::AutoMutex<> guard(this->data_mutex_); + *channel_data_ = tmp_channel; + } + {//- no error + yat::AutoMutex<> guard(this->error_mutex_); + errors_.clear(); + } + INFO_STREAM << "\tWaveformMgr::TASK_PERIODIC -> done in " << t.elapsed_msec() << " ms." << std::endl; + } + catch(lecroy::SocketException& se) + { + ERROR_STREAM << "[...] WaveformMgr::handle_message::handling THREAD_PERIODIC -> [SocketException]" << std::endl; + Tango::DevFailed df = lecroy_to_tango_exception(se); + ERROR_STREAM << df << std::endl; + { + yat::AutoMutex<> guard(this->error_mutex_); + errors_ = "Manager : failed to update channel structure to update data.\n Caught a lecroy::SocketException exception."; + } + throw df; } catch(const lecroy::WaveformException& we) { Tango::DevFailed df = lecroy_to_tango_exception(we); - FATAL_STREAM << df << std::endl; - { - yat::AutoMutex<> guard(this->error_mutex_); - errors_ = "Manager : failed to update channel structure to update data.\n Caught a Lecroy exception -> check logs."; - } -// std::cout << "[LECROY] WaveformMgr::handle_message::handling THREAD_PERIODIC -> [...]" << std::endl; + ERROR_STREAM << df << std::endl; + { + yat::AutoMutex<> guard(this->error_mutex_); + errors_ = "Manager : failed to update channel structure to update data.\n Caught a lecroy::WaveformException exception -> check logs."; + } + throw df; } catch(Tango::DevFailed& df) { ERROR_STREAM << "[DF] WaveformMgr::handle_message::handling THREAD_PERIODIC -> caught DF :\n" << df << std::endl; - { - yat::AutoMutex<> guard(this->error_mutex_); - errors_ = "Manager : failed to update channel structure to update data.\n Caught a DevFailed exception -> check logs."; - } + { + yat::AutoMutex<> guard(this->error_mutex_); + errors_ = "Manager : failed to update channel structure to update data.\n Caught a DevFailed exception -> check logs."; + } + throw df; } catch(...) { ERROR_STREAM << "[...] WaveformMgr::handle_message::handling THREAD_PERIODIC -> [...]" << std::endl; - { - yat::AutoMutex<> guard(this->error_mutex_); - errors_ = "Manager : failed to update channel structure to update data.\n Caught a generic exception."; - } + { + yat::AutoMutex<> guard(this->error_mutex_); + errors_ = "Manager : failed to update channel structure to update data.\n Caught a generic exception."; + } + throw; } } else - { + { ERROR_STREAM << "[ELSE] WaveformMgr::handle_message::handling THREAD_PERIODIC -> Cannot update channel data" << std::endl; - yat::AutoMutex<> guard(this->error_mutex_); - errors_ = "Failed to create channel structure to update data."; - } - + yat::AutoMutex<> guard(this->error_mutex_); + errors_ = "Failed to create channel structure to update data."; + } } break; //- WRITE_READ @@ -179,87 +189,87 @@ void WaveformMgr::process_message (yat::Message& _msg) throw (Tango::DevFailed) { //- code relative to the task's STORE_DATA handling goes here YAT_LOG("CurrentTrendFileTask::handle_message::handling WRITE_READ msg"); -// std::cout << "\nCurrentTrendFileTask::handle_message::handling WRITE_READ msg" << std::endl; yat::Timer t; - try - { - SocketLecroy* ptr_com = SocketLecroy::get_instance(); - - std::string cmd = _msg.get_data<std::string>(); - int bytes_received = -1; - char resp[MAX_RESPONSE_LENGTH]; - char * command = 0; - - if(ptr_com) - { - ::strcpy(resp, "No response"); - //- transform string to char* - std::size_t length = cmd.size(); - command = new char[length+1]; - for(std::size_t i=0; i<length; i++) - command[i] = cmd[i]; - command[length] = '\0'; + try + { + SocketLecroy* ptr_com = SocketLecroy::get_instance(); + + std::string cmd = _msg.get_data<std::string>(); + int bytes_received = -1; + char resp[MAX_RESPONSE_LENGTH]; + char * command = 0; + + if(ptr_com) + { + ::strcpy(resp, "No response"); + //- transform string to char* + std::size_t length = cmd.size(); + command = new char[length+1]; + for(std::size_t i=0; i<length; i++) + command[i] = cmd[i]; + command[length] = '\0'; - ptr_com->TCP_WriteDevice(command, length, true); - if( cmd.rfind('?') != std::string::npos ) - { - #ifdef WIN32 - Sleep(10); //- milliseconds - #else - usleep(10000); - #endif - ptr_com->TCP_ReadDevice(resp, MAX_RESPONSE_LENGTH, &bytes_received); - } - } - else - ::strcpy(resp, "No communication with Lecroy device!"); - - if (command) - { - delete [] command; - command = 0; - } + ptr_com->TCP_WriteDevice(command, length, true); + if( cmd.rfind('?') != std::string::npos ) + { +#ifdef WIN32 + Sleep(10); //- milliseconds +#else + usleep(10000); +#endif + ptr_com->TCP_ReadDevice(resp, MAX_RESPONSE_LENGTH, &bytes_received); + } + } + else + { + ::strcpy(resp, "No communication with Lecroy device!"); + } + + if (command) + { + delete [] command; + command = 0; + } - this->lecroy_resp_ = resp; - {//- no error - yat::AutoMutex<> guard(this->error_mutex_); - errors_.clear(); - } - INFO_STREAM << "\t******WaveformMgr::WRITE_READ done in : " << t.elapsed_msec() << " ms.\n" << std::endl; - } - catch(const lecroy::WaveformException& we) - { - Tango::DevFailed df = lecroy_to_tango_exception(we); - FATAL_STREAM << df << std::endl; - { - yat::AutoMutex<> guard(this->error_mutex_); - errors_ = "WriteRead command failed : caught a Lecroy exception -> check logs."; - } - } - catch(Tango::DevFailed& df) - { - ERROR_STREAM << "WaveformMgr::handle_message::handling WRITE_READ -> caught DF :\n" << df << std::endl; - { - yat::AutoMutex<> guard(this->error_mutex_); - errors_ = "WriteRead command failed : caught a DevFailed exception -> check logs."; - } - } - catch(...) - { - ERROR_STREAM << "WaveformMgr::handle_message::handling WRITE_READ -> [...]" << std::endl; - { - yat::AutoMutex<> guard(this->error_mutex_); - errors_ = "WriteRead command failed : caught a generic exception."; - } - } + this->lecroy_resp_ = resp; + {//- no error + yat::AutoMutex<> guard(this->error_mutex_); + errors_.clear(); + } + INFO_STREAM << "\t******WaveformMgr::WRITE_READ done in : " << t.elapsed_msec() << " ms.\n" << std::endl; + } + catch(const lecroy::WaveformException& we) + { + Tango::DevFailed df = lecroy_to_tango_exception(we); + FATAL_STREAM << df << std::endl; + { + yat::AutoMutex<> guard(this->error_mutex_); + errors_ = "WriteRead command failed : caught a Lecroy exception -> check logs."; + } + } + catch(Tango::DevFailed& df) + { + ERROR_STREAM << "WaveformMgr::handle_message::handling WRITE_READ -> caught DF :\n" << df << std::endl; + { + yat::AutoMutex<> guard(this->error_mutex_); + errors_ = "WriteRead command failed : caught a DevFailed exception -> check logs."; + } + } + catch(...) + { + ERROR_STREAM << "WaveformMgr::handle_message::handling WRITE_READ -> [...]" << std::endl; + { + yat::AutoMutex<> guard(this->error_mutex_); + errors_ = "WriteRead command failed : caught a generic exception."; + } + } } break; //- THREAD_TIMEOUT ------------------- case yat::TASK_TIMEOUT: { //- code relative to the task's tmo handling goes here - std::cout << "WaveformMgr::handle_message::handling THREAD_TIMEOUT msg" << std::endl; } break; //- UNHANDLED MSG -------------------- @@ -282,7 +292,9 @@ void WaveformMgr::add_channel(std::string ch_name) //- insert the new channel (in critical section) yat::AutoMutex<> guard(this->data_mutex_); if ( !channel_data_ ) - channel_data_ = new ChannelData(ch_name); + { + channel_data_ = new ChannelData(ch_name); + } //- allocate data memory channel_data_->vertical_scaled_data_.reserve(MAX_WAVEFORM_DATA_LENGTH); diff --git a/src/Win32SocketLecroy.cpp b/src/Win32SocketLecroy.cpp index 1ef6904c4fe13bf9d7f989d94e6e7343bcd29aa4..8510aeab28c7c0aa49b5c911ccc1faf3aec3fa90 100644 --- a/src/Win32SocketLecroy.cpp +++ b/src/Win32SocketLecroy.cpp @@ -31,14 +31,22 @@ static char sCommandBuffer[CMD_BUF_LEN]; //- init of the static instance SocketLecroy* SocketLecroy::SL_instance = 0; //- ptr on the SocketLecroy instance +// ============================================================================ +// SocketLecroy::get_instance +// ============================================================================ SocketLecroy* SocketLecroy::get_instance() { if( !SL_instance ) + { SL_instance = new SocketLecroy(); + } return SL_instance; } +// ============================================================================ +// SocketLecroy::delete_instance +// ============================================================================ void SocketLecroy::delete_instance(SocketLecroy* SL_instance) { if(SL_instance) @@ -48,18 +56,25 @@ void SocketLecroy::delete_instance(SocketLecroy* SL_instance) } } -//- CTOR +// ============================================================================ +// SocketLecroy::SocketLecroy +// ============================================================================ SocketLecroy::SocketLecroy() { sConnectedFlag = false; } -//- DTOR +// ============================================================================ +// SocketLecroy::~SocketLecroy +// ============================================================================ SocketLecroy::~SocketLecroy() { TCP_Disconnect(); } +// ============================================================================ +// SocketLecroy::TCP_Connect +// ============================================================================ //- Build the connection void SocketLecroy::TCP_Connect(char *ip_address) throw (lecroy::SocketException) { @@ -162,6 +177,9 @@ void SocketLecroy::TCP_Connect(char *ip_address) throw (lecroy::SocketException) sConnectedFlag = true; } +// ============================================================================ +// SocketLecroy::TCP_Disconnect +// ============================================================================ //- DisconnectFromScope: disconnect from a network device void SocketLecroy::TCP_Disconnect(void) { @@ -172,20 +190,28 @@ void SocketLecroy::TCP_Disconnect(void) } } +// ============================================================================ +// SocketLecroy::TCP_ClearDevice +// ============================================================================ //- Clear a connection -void SocketLecroy::TCP_ClearDevice(void) throw (lecroy::SocketException) +void SocketLecroy::TCP_ClearDevice(void) { if ( !sConnectedFlag ) + { throw lecroy::SocketException("COMMUNICATION_BROKEN", "Disconnection already done.", "SocketLecroy::TCP_ClearDevice( )."); + } TCP_Disconnect(); TCP_Connect(sCurrentAddress); } +// ============================================================================ +// SocketLecroy::TCP_WriteDevice +// ============================================================================ //- Send commands to the remote device -void SocketLecroy::TCP_WriteDevice(char *buf, int len, bool eoi_flag) throw (lecroy::SocketException) +void SocketLecroy::TCP_WriteDevice(char *buf, int len, bool eoi_flag) { TCP_HEADER header; int result, bytes_more, bytes_xferd; @@ -193,12 +219,16 @@ void SocketLecroy::TCP_WriteDevice(char *buf, int len, bool eoi_flag) throw (lec //- test connection if ( !sConnectedFlag ) + { throw lecroy::SocketException("COMMUNICATION_BROKEN ", "Device not connected.", "SocketLecroy::TCP_WriteDevice( )."); + } if (len < CMD_BUF_LEN) + { strcpy(sCommandBuffer, buf); + } //- set the header info header.bEOI_Flag = DATA_FLAG; @@ -238,8 +268,11 @@ void SocketLecroy::TCP_WriteDevice(char *buf, int len, bool eoi_flag) throw (lec } } +// ============================================================================ +// SocketLecroy::TCP_ReadDevice +// ============================================================================ //- Read the device answer -void SocketLecroy::TCP_ReadDevice(char *buf, int len, int *recv_count) throw (lecroy::SocketException) +void SocketLecroy::TCP_ReadDevice(char *buf, int len, int *recv_count) { TCP_HEADER header; int result, accum, space_left, bytes_more, buf_count; @@ -250,16 +283,20 @@ void SocketLecroy::TCP_ReadDevice(char *buf, int len, int *recv_count) throw (le //- test connection if ( !sConnectedFlag ) + { throw lecroy::SocketException("COMMUNICATION_BROKEN ", "Device not connected.", "SocketLecroy::TCP_ReadDevice( )."); + } *recv_count = 0; if (!buf) + { throw lecroy::SocketException("COMMUNICATION_BROKEN ", "Buffer memory not allocated.", "SocketLecroy::TCP_ReadDevice( )."); + } rd_set.fd_array[0] = hSocket; tval.tv_sec = sTimeout; @@ -294,7 +331,9 @@ void SocketLecroy::TCP_ReadDevice(char *buf, int len, int *recv_count) throw (le accum += result; if (accum>=sizeof(header)) + { break; + } } header.iLength = ntohl(header.iLength);