Index: /trunk/FACT++/src/agilentctrl.cc
===================================================================
--- /trunk/FACT++/src/agilentctrl.cc	(revision 14324)
+++ /trunk/FACT++/src/agilentctrl.cc	(revision 14325)
@@ -59,268 +59,268 @@
     }
     
-    boost::asio::deadline_timer fCheckStatusTimer;
-    boost::asio::deadline_timer fAntiFloddingTimer;
-
-    void PostStatusRequest()
-    {
-        PostMessage(string("*IDN?\n"));
-        PostMessage(string("meas:volt?\n"));
-        PostMessage(string("meas:curr?\n"));
-    }
-
+boost::asio::deadline_timer fCheckStatusTimer;
+boost::asio::deadline_timer fAntiFloddingTimer;
+
+void PostStatusRequest()
+{
+    PostMessage(string("*IDN?\n"));
+    PostMessage(string("meas:volt?\n"));
+    PostMessage(string("meas:curr?\n"));
+}
+
+
+void RequestStatus()
+{
+    PostStatusRequest();
+
+    fCheckStatusTimer.expires_from_now(boost::posix_time::seconds(60));
+    fCheckStatusTimer.async_wait(boost::bind(&ConnectionAgilent::HandleRequest,
+                                      this, bapla::error));
+}
+
+void HandleRequest(const bs::error_code &error)
+{
+    // 125: Operation canceled (bs::error_code(125, bs::system_category))
+    if (error && error!=ba::error::basic_errors::operation_aborted)
+    {
+        ostringstream str;
+        str << "Write timeout of " << URL() << ": " << error.message() << " (" << error << ")";// << endl;
+        Error(str);
+
+        PostClose(false);
+        return;
+    }
+
+    if (!is_open())
+    {
+        // For example: Here we could schedule a new accept if we
+        // would not want to allow two connections at the same time.
+        PostClose(true);
+        return;
+    }
+
+    // Check whether the deadline has passed. We compare the deadline
+    // against the current time since a new asynchronous operation
+    // may have moved the deadline before this actor had a chance
+    // to run.
+    if (fCheckStatusTimer.expires_at() > ba::deadline_timer::traits_type::now())
+        return;
+
+    RequestStatus();
+}
+private:
+
+int  fLineCounter;
+
+void Start_async_read_until()
+{
+    // Bind Received Data Handler to the event: "received <enter> character"
+    //   this Handler will try to parse the incoming data
+    ba::async_read_until(*this, fBuffer, "\n",
+                         boost::bind(&ConnectionAgilent::ReceivedStatusHandler, this,
+                                     bapla::error, bapla::bytes_transferred, 0));
+
+    // FIXME: Add timeout here
+}
+
+void ReceivedStatusHandler(const bs::error_code& err, size_t bytes_received, int /*type*/)
+{
     
-    void RequestStatus()
-    {
-        PostStatusRequest();
-
-        fCheckStatusTimer.expires_from_now(boost::posix_time::seconds(60));
-        fCheckStatusTimer.async_wait(boost::bind(&ConnectionAgilent::HandleRequest,
-                                          this, bapla::error));
-    }
-
-    void HandleRequest(const bs::error_code &error)
-    {
-        // 125: Operation canceled (bs::error_code(125, bs::system_category))
-        if (error && error!=ba::error::basic_errors::operation_aborted)
+    // Do not schedule a new read if the connection failed.
+    if (bytes_received==0 || err)
+    {
+        if (err==ba::error::eof)
+            Warn("Connection closed by remote host (FTM).");
+
+        // 107: Transport endpoint is not connected (bs::error_code(107, bs::system_category))
+        // 125: Operation canceled
+        if (err && err!=ba::error::eof &&                     // Connection closed by remote host
+            err!=ba::error::basic_errors::not_connected &&    // Connection closed by remote host
+            err!=ba::error::basic_errors::operation_aborted)  // Connection closed by us
         {
             ostringstream str;
-            str << "Write timeout of " << URL() << ": " << error.message() << " (" << error << ")";// << endl;
+            str << "Reading from " << URL() << ": " << err.message() << " (" << err << ")";// << endl;
             Error(str);
-
-            PostClose(false);
-            return;
         }
-
-        if (!is_open())
+        PostClose(err!=ba::error::basic_errors::operation_aborted);
+        return;
+    }
+
+    if (fIsVerbose)
+    {
+       Out() << kBold << "Received (" << bytes_received << " bytes):" << endl;
+    }
+    // FIXME: the piece of code below causes a Seg Fault in case 
+    // The Agilent is not listening during program start, then after a while is 
+    // listening and after connection established.
+    // It sends: 1.) a string and 2.) and empty line (just an <Enter> pressed)
+    //
+    // then the agilent_ctrl segfaults ... was able to reproduce it.
+    // gdp just gave me the hint, that Time().GetAdStr() caused the Seg Fault in a way...
+    // Is Time threadsafe?
+    /*
+    if (fDump)
+    {
+        ostringstream msg;
+        msg << "--- " << Time().GetAsStr() << " --- received " << bytes_received << " bytes.";
+        Dump(msg.str());
+    }
+    */
+
+
+    istream is(&fBuffer);
+    fLineCounter++;
+
+    if (fLineCounter == 1)
+    {
+        // this is the Agilent identity string, do nothing
+        string s;
+        getline(is,s, '\n');
+        Out() << "ID string: " << s << endl;
+    }
+    else if (fLineCounter == 2)
+    {
+        // this should be a float containing the measured voltage
+        is >> fMeasuredVoltage;
+        Out() << "voltage: " << fMeasuredVoltage << endl;
+    }
+    else if (fLineCounter >= 3)
+    {
+        // this should be a float containing the measured voltage
+        is >> fMeasuredCurrent;
+        Out() << "current: " << fMeasuredCurrent << endl;
+        fLineCounter = 0;
+
+        // data should contain two floats:
+        //  * measured output voltage in volts
+        //  * measured ouput current in amperes
+        vector<float> data(2);
+        data[0] = fMeasuredVoltage;
+        data[1] = fMeasuredCurrent;
+        UpdateDim(data);
+    }
+   
+    // read the buffer empty ...
+    // FIXME: I surely misunderstand that damn fBuffer thing.
+    // I thought it should be emtpy by now...
+    string buffer;
+    while (getline(is,buffer, '\n'))
+    {
+        buffer = Tools::Trim(buffer);
+        if (buffer.empty()) continue;
+    }
+
+
+    if (fMeasuredVoltage > 1.0)
+    {
+        fState = State::kVoltage_On;
+    }
+    else
+    {
+        fState = State::kVoltage_Off;
+    }
+    Start_async_read_until();
+}
+
+
+// This is called when a connection was established
+void ConnectionEstablished()
+{
+    // DN 07.08.2012: The next line is imho not needed.
+    // But I'm in a train and cannot test it.
+    fState = State::kConnected;
+    Start_async_read_until();
+    RequestStatus();
+    
+    fLineCounter = 0;
+    fBuffer.prepare(1000);
+}
+
+public:
+
+ConnectionAgilent(ba::io_service& ioservice, MessageImp &imp) : Connection(ioservice, imp()),
+    fIsVerbose(true),
+    fDump(true),
+    fCheckStatusTimer(ioservice),
+    fAntiFloddingTimer(ioservice)
+{
+    SetLogStream(&imp);
+    fState = State::kDisconnected;
+    fMeasuredVoltage=-1;
+    fMeasuredCurrent=-1;
+}
+
+void SetVerbose(bool b)
+{
+    fIsVerbose = b;
+}
+
+void SetDumpStream(bool b)
+{
+    fDump = b;
+}
+
+void SetOutput(bool b)
+{
+    if (b)
+    {
+        // check if the previous 'outp off' is some time ago
+        if (fAntiFloddingTimer.expires_at() < ba::deadline_timer::traits_type::now())
         {
-            // For example: Here we could schedule a new accept if we
-            // would not want to allow two connections at the same time.
-            PostClose(true);
-            return;
+            PostMessage(string("outp on\n"));
         }
-
-        // Check whether the deadline has passed. We compare the deadline
-        // against the current time since a new asynchronous operation
-        // may have moved the deadline before this actor had a chance
-        // to run.
-        if (fCheckStatusTimer.expires_at() > ba::deadline_timer::traits_type::now())
-            return;
-
-        RequestStatus();
-    }
+    }
+    else
+    {
+        PostMessage(string("outp off\n"));
+        // start a Timer, which runs out in 60sec making sure, that the 
+        // camera can't be switched off&on on short time scales.
+        // sending repetetive outp off is possible
+        // sending repetivitve outp on is also posible
+        // switching off immediately after switching on is also possible.
+        fAntiFloddingTimer.expires_from_now(boost::posix_time::seconds(60));
+    }
+    RequestStatus();
+}
+
+int GetState() const
+{
+    // fState is set in ReceivedStatusHandler
+    return fState;
+}
+
+
+};
+
+// ------------------------------------------------------------------------
+
+#include "DimDescriptionService.h"
+
+class ConnectionDimAgilent : public ConnectionAgilent
+{
 private:
 
-    int  fLineCounter;
-
-    void Start_async_read_until()
-    {
-        // Bind Received Data Handler to the event: "received <enter> character"
-        //   this Handler will try to parse the incoming data
-        ba::async_read_until(*this, fBuffer, "\n",
-                             boost::bind(&ConnectionAgilent::ReceivedStatusHandler, this,
-                                         bapla::error, bapla::bytes_transferred, 0));
-
-        // FIXME: Add timeout here
-    }
-
-    void ReceivedStatusHandler(const bs::error_code& err, size_t bytes_received, int /*type*/)
-    {
-        
-        // Do not schedule a new read if the connection failed.
-        if (bytes_received==0 || err)
-        {
-            if (err==ba::error::eof)
-                Warn("Connection closed by remote host (FTM).");
-
-            // 107: Transport endpoint is not connected (bs::error_code(107, bs::system_category))
-            // 125: Operation canceled
-            if (err && err!=ba::error::eof &&                     // Connection closed by remote host
-                err!=ba::error::basic_errors::not_connected &&    // Connection closed by remote host
-                err!=ba::error::basic_errors::operation_aborted)  // Connection closed by us
-            {
-                ostringstream str;
-                str << "Reading from " << URL() << ": " << err.message() << " (" << err << ")";// << endl;
-                Error(str);
-            }
-            PostClose(err!=ba::error::basic_errors::operation_aborted);
-            return;
-        }
-
-        if (fIsVerbose)
-        {
-           Out() << kBold << "Received (" << bytes_received << " bytes):" << endl;
-        }
-        // FIXME: the piece of code below causes a Seg Fault in case 
-        // The Agilent is not listening during program start, then after a while is 
-        // listening and after connection established.
-        // It sends: 1.) a string and 2.) and empty line (just an <Enter> pressed)
-        //
-        // then the agilent_ctrl segfaults ... was able to reproduce it.
-        // gdp just gave me the hint, that Time().GetAdStr() caused the Seg Fault in a way...
-        // Is Time threadsafe?
-        /*
-        if (fDump)
-        {
-            ostringstream msg;
-            msg << "--- " << Time().GetAsStr() << " --- received " << bytes_received << " bytes.";
-            Dump(msg.str());
-        }
-        */
-
-
-        istream is(&fBuffer);
-        fLineCounter++;
-
-        if (fLineCounter == 1)
-        {
-            // this is the Agilent identity string, do nothing
-            string s;
-            getline(s,buffer, '\n');
-            Out() << "ID string: " << s << endl;
-        }
-        else if (fLineCounter == 2)
-        {
-            // this should be a float containing the measured voltage
-            is >> fMeasuredVoltage;
-            Out() << "voltage: " << fMeasuredVoltage << endl;
-        }
-        else if (fLineCounter >= 3)
-        {
-            // this should be a float containing the measured voltage
-            is >> fMeasuredCurrent;
-            Out() << "current: " << fMeasuredCurrent << endl;
-            fLineCounter = 0;
-
-            // data should contain two floats:
-            //  * measured output voltage in volts
-            //  * measured ouput current in amperes
-            vector<float> data(2);
-            data[0] = fMeasuredVoltage;
-            data[1] = fMeasuredCurrent;
-            UpdateDim(data);
-        }
-       
-        // read the buffer empty ...
-        // FIXME: I surely misunderstand that damn fBuffer thing.
-        // I thought it should be emtpy by now...
-        string buffer;
-        while (getline(is,buffer, '\n'))
-        {
-            buffer = Tools::Trim(buffer);
-            if (buffer.empty()) continue;
-        }
-
-
-        if (fMeasuredVoltage > 1.0)
-        {
-            fState = State::kVoltage_On;
-        }
-        else
-        {
-            fState = State::kVoltage_Off;
-        }
-        Start_async_read_until();
-    }
-
-
-    // This is called when a connection was established
-    void ConnectionEstablished()
-    {
-        // DN 07.08.2012: The next line is imho not needed.
-        // But I'm in a train and cannot test it.
-        fState = State::kConnected;
-        Start_async_read_until();
-        RequestStatus();
-        
-        fLineCounter = 0;
-        fBuffer.prepare(1000);
-    }
+DimDescribedService fDim;
+
+void Update(DimDescribedService &svc, vector<float> data) const
+{
+    svc.Update(data);
+}
+
+void UpdateDim(const vector<float> &data)
+{
+    Update(fDim, data);
+}
+
 
 public:
-
-    ConnectionAgilent(ba::io_service& ioservice, MessageImp &imp) : Connection(ioservice, imp()),
-        fIsVerbose(true),
-        fDump(true),
-        fCheckStatusTimer(ioservice),
-        fAntiFloddingTimer(ioservice)
-    {
-        SetLogStream(&imp);
-        fState = State::kDisconnected;
-        fMeasuredVoltage=-1;
-        fMeasuredCurrent=-1;
-    }
-
-    void SetVerbose(bool b)
-    {
-        fIsVerbose = b;
-    }
-
-    void SetDumpStream(bool b)
-    {
-        fDump = b;
-    }
-
-    void SetOutput(bool b)
-    {
-        if (b)
-        {
-            // check if the previous 'outp off' is some time ago
-            if (fAntiFloddingTimer.expires_at() > ba::deadline_timer::traits_type::now())
-            {
-                PostMessage(string("outp on\n"));
-            }
-        }
-        else
-        {
-            PostMessage(string("outp off\n"));
-            // start a Timer, which runs out in 60sec making sure, that the 
-            // camera can't be switched off&on on short time scales.
-            // sending repetetive outp off is possible
-            // sending repetivitve outp on is also posible
-            // switching off immediately after switching on is also possible.
-            fAntiFloddingTimer.expires_from_now(boost::posix_time::seconds(60));
-        }
-        RequestStatus();
-    }
-    
-    int GetState() const
-    {
-        // fState is set in ReceivedStatusHandler
-        return fState;
-    }
-
-
-};
-
-// ------------------------------------------------------------------------
-
-#include "DimDescriptionService.h"
-
-class ConnectionDimAgilent : public ConnectionAgilent
-{
-private:
-
-    DimDescribedService fDim;
-
-    void Update(DimDescribedService &svc, vector<float> data) const
-    {
-        svc.Update(data);
-    }
-
-    void UpdateDim(const vector<float> &data)
-    {
-        Update(fDim, data);
-    }
-
-
-public:
-    ConnectionDimAgilent(ba::io_service& ioservice, MessageImp &imp) :
-        ConnectionAgilent(ioservice, imp),
-        fDim("AGILENT_CONTROL/DATA", "F:1;F:1",
-                    "|U[V]: output voltage"
-                    "|I[A]: output current")
-    {
-        // nothing happens here.
-    }
+ConnectionDimAgilent(ba::io_service& ioservice, MessageImp &imp) :
+    ConnectionAgilent(ioservice, imp),
+    fDim("AGILENT_CONTROL/DATA", "F:1;F:1",
+                "|U[V]: output voltage"
+                "|I[A]: output current")
+{
+    // nothing happens here.
+}
 };
 
@@ -331,50 +331,50 @@
 {
 private:
-    S fAgilent;
-
-    int Disconnect()
-    {
-        // Close all connections
-        fAgilent.PostClose(false);
-
-        /*
-         // Now wait until all connection have been closed and
-         // all pending handlers have been processed
-         poll();
-         */
-
-        return T::GetCurrentState();
-    }
-
-    int Reconnect(const EventImp &evt)
-    {
-        // Close all connections to supress the warning in SetEndpoint
-        fAgilent.PostClose(false);
-
-        // Now wait until all connection have been closed and
-        // all pending handlers have been processed
-        poll();
-
-        if (evt.GetBool())
-            fAgilent.SetEndpoint(evt.GetString());
-
-        // Now we can reopen the connection
-        fAgilent.PostClose(true);
-
-        return T::GetCurrentState();
-    }
-
-    int Execute()
-    {
-        // Dispatch (execute) at most one handler from the queue. In contrary
-        // to run_one(), it doesn't wait until a handler is available
-        // which can be dispatched, so poll_one() might return with 0
-        // handlers dispatched. The handlers are always dispatched/executed
-        // synchronously, i.e. within the call to poll_one()
-        poll_one();
-        
-        if ( fAgilent.IsConnected() )
-            return fAgilent.GetState();
-        else
+S fAgilent;
+
+int Disconnect()
+{
+    // Close all connections
+    fAgilent.PostClose(false);
+
+    /*
+     // Now wait until all connection have been closed and
+     // all pending handlers have been processed
+     poll();
+     */
+
+    return T::GetCurrentState();
+}
+
+int Reconnect(const EventImp &evt)
+{
+    // Close all connections to supress the warning in SetEndpoint
+    fAgilent.PostClose(false);
+
+    // Now wait until all connection have been closed and
+    // all pending handlers have been processed
+    poll();
+
+    if (evt.GetBool())
+        fAgilent.SetEndpoint(evt.GetString());
+
+    // Now we can reopen the connection
+    fAgilent.PostClose(true);
+
+    return T::GetCurrentState();
+}
+
+int Execute()
+{
+    // Dispatch (execute) at most one handler from the queue. In contrary
+    // to run_one(), it doesn't wait until a handler is available
+    // which can be dispatched, so poll_one() might return with 0
+    // handlers dispatched. The handlers are always dispatched/executed
+    // synchronously, i.e. within the call to poll_one()
+    poll_one();
+    
+    if ( fAgilent.IsConnected() )
+        return fAgilent.GetState();
+    else
             return State::kDisconnected;
     }
