Index: /trunk/FACT++/src/fadctrl.cc
===================================================================
--- /trunk/FACT++/src/fadctrl.cc	(revision 11191)
+++ /trunk/FACT++/src/fadctrl.cc	(revision 11192)
@@ -25,4 +25,7 @@
 #include "tools.h"
 
+#include "DimDescriptionService.h"
+#include "EventBuilderWrapper.h"
+
 namespace ba = boost::asio;
 namespace bs = boost::system;
@@ -36,4 +39,7 @@
 class ConnectionFAD : public Connection
 {
+    uint16_t fSlot;
+//    tcp::endpoint fEndpoint;
+
     vector<uint16_t> fBuffer;
 
@@ -155,4 +161,6 @@
         }
 
+        EventBuilderWrapper::This->debugStream(fSlot*7, fBuffer.data(), bytes_received);
+
         if (type==kReadHeader)
         {
@@ -183,4 +191,6 @@
             UpdateEventHeader();
 
+            EventBuilderWrapper::This->debugHead(fSlot*40, fEventHeader);
+
             fBuffer.resize(fEventHeader.fPackageLength-sizeof(FAD::EventHeader)/2);
             AsyncRead(ba::buffer(fBuffer), kReadData);
@@ -225,29 +235,5 @@
             }
 
-            uint16_t *data = reinterpret_cast<uint16_t*>(ptr);
-
-            /*
-            for (uint16_t *d=data; d<data+fChannelHeader[i].fRegionOfInterest; d++)
-            {
-                const bool sign     = *d & 0x2000;
-                const bool overflow = *d & 0x1000;
-
-                if (sign)
-                    *d |= 0xf000;   // no overflow, nagative
-                else
-                    *d &= 0x07ff;   // no overlow,  positive
-
-                // max = [-2047;2048]
-
-                if (overflow)
-                {
-                    if (sign)
-                        *d = 0xF800;   // overflow, negative
-                    else
-                        *d = 0x0800;   // overflow, positive
-                }
-            }
-            */
-
+            const uint16_t *data = reinterpret_cast<uint16_t*>(ptr);
             UpdateData(data, fChannelHeader[i].fRegionOfInterest*2);
             ptr += fChannelHeader[i].fRegionOfInterest*2;
@@ -366,6 +352,6 @@
 
 public:
-    ConnectionFAD(ba::io_service& ioservice, MessageImp &imp) :
-        Connection(ioservice, imp()),
+    ConnectionFAD(ba::io_service& ioservice, MessageImp &imp, uint16_t slot) :
+        Connection(ioservice, imp()), fSlot(slot),
         fIsVerbose(false), fIsHexOutput(false), fIsDataOutput(false),
         fBlockTransmission(false), fCounter(0)
@@ -379,4 +365,7 @@
     }
 
+//    void SetTcpEndpoint(const tcp::endpoint &ep) { fEndpoint = ep; }
+//    const tcp::endpoint &GetTcpEndpoint() const { return fEndpoint; }
+
     void Cmd(FAD::Enable cmd, bool on=true)
     {
@@ -557,8 +546,4 @@
 
 // ------------------------------------------------------------------------
-#include "DimDescriptionService.h"
-#include "EventBuilderWrapper.h"
-
-// ------------------------------------------------------------------------
 
 template <class T>
@@ -566,6 +551,5 @@
 {
 private:
-    typedef pair<tcp::endpoint, ConnectionFAD*> Connection;
-    typedef map<uint8_t, Connection>            BoardList;
+    typedef map<uint8_t, ConnectionFAD*> BoardList;
 
     BoardList fBoards;
@@ -590,5 +574,5 @@
     {
         for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
-            i->second.second->Cmd(command);
+            i->second->Cmd(command);
 
         return T::GetCurrentState();
@@ -607,5 +591,5 @@
 
         for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
-            i->second.second->PostCmd(evt.GetUInt());
+            i->second->PostCmd(evt.GetUInt());
 
         return T::GetCurrentState();
@@ -632,5 +616,5 @@
 
         for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
-            i->second.second->PostCmd(ptr[0], ptr[1]);
+            i->second->PostCmd(ptr[0], ptr[1]);
 
         return T::GetCurrentState();
@@ -643,5 +627,5 @@
 
         for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
-            i->second.second->Cmd(command, evt.GetBool());
+            i->second->Cmd(command, evt.GetBool());
 
         return T::GetCurrentState();
@@ -680,5 +664,5 @@
 
         for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
-            i->second.second->CmdSetRegister(dat[0], dat[1]);
+            i->second->CmdSetRegister(dat[0], dat[1]);
 
         return T::GetCurrentState();
@@ -693,5 +677,5 @@
 
         for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
-            if (!i->second.second->CmdSetRoi(dat[0], dat[1]))
+            if (!i->second->CmdSetRoi(dat[0], dat[1]))
             {
                 ostringstream msg;
@@ -716,5 +700,5 @@
 
         for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
-            i->second.second->CmdSetDacValue(dat[0], dat[1]);
+            i->second->CmdSetDacValue(dat[0], dat[1]);
 
         return T::GetCurrentState();
@@ -725,5 +709,5 @@
         for (int nn=0; nn<n; nn++)
             for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
-                i->second.second->Cmd(FAD::kCmdSingleTrigger);
+                i->second->Cmd(FAD::kCmdSingleTrigger);
 
         return T::GetCurrentState();
@@ -746,5 +730,5 @@
 
         for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
-            i->second.second->Cmd(FAD::kCmdRun, start);
+            i->second->Cmd(FAD::kCmdRun, start);
 
         return T::GetCurrentState();
@@ -757,5 +741,5 @@
 
         for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
-            i->second.second->CmdPhaseShift(evt.GetShort());
+            i->second->CmdPhaseShift(evt.GetShort());
 
         return T::GetCurrentState();
@@ -776,5 +760,5 @@
 
         for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
-            i->second.second->CmdSetTriggerRate(evt.GetUInt());
+            i->second->CmdSetTriggerRate(evt.GetUInt());
 
         return T::GetCurrentState();
@@ -797,5 +781,5 @@
 
         for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
-            i->second.second->CmdSetRunNumber(num);
+            i->second->CmdSetRunNumber(num);
 
         return T::GetCurrentState();
@@ -840,5 +824,5 @@
 
         for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
-            i->second.second->SetVerbose(evt.GetBool());
+            i->second->SetVerbose(evt.GetBool());
 
         return T::GetCurrentState();
@@ -851,5 +835,5 @@
 
         for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
-            i->second.second->SetHexOutput(evt.GetBool());
+            i->second->SetHexOutput(evt.GetBool());
 
         return T::GetCurrentState();
@@ -862,5 +846,5 @@
 
         for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
-            i->second.second->SetDataOutput(evt.GetBool());
+            i->second->SetDataOutput(evt.GetBool());
 
         return T::GetCurrentState();
@@ -873,5 +857,5 @@
 
         for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
-            i->second.second->SetDebugTx(evt.GetBool());
+            i->second->SetDebugTx(evt.GetBool());
 
         return T::GetCurrentState();
@@ -901,5 +885,5 @@
         {
             for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
-                i->second.second->PrintEvent();
+                i->second->PrintEvent();
         }
         else
@@ -907,5 +891,5 @@
             const BoardList::iterator it=GetSlot(slot);
             if (it!=fBoards.end())
-                it->second.second->PrintEvent();
+                it->second->PrintEvent();
         }
 
@@ -922,5 +906,5 @@
         const BoardList::iterator it=GetSlot(slot);
         if (it!=fBoards.end())
-            it->second.second->SetBlockTransmission(evt.Get<uint8_t>(2));
+            it->second->SetBlockTransmission(evt.Get<uint8_t>(2));
 
         return T::GetCurrentState();
@@ -939,5 +923,5 @@
             const BoardList::iterator it=GetSlot(i);
             if (it!=fBoards.end())
-                it->second.second->SetBlockTransmission(block);
+                it->second->SetBlockTransmission(block);
         }
 
@@ -1013,5 +997,5 @@
         for (BoardList::const_iterator i=fBoards.begin(); i!=fBoards.end(); i++)
         {
-            if (i->second.first==endpoint)
+            if (i->second->GetEndpoint()==endpoint)
             {
                T::Warn("Address "+addr+" already known.... ignored.");
@@ -1039,5 +1023,5 @@
         ConnectSlot(slot, tcp::endpoint());
 
-        delete it->second.second;
+        delete it->second;
         fBoards.erase(it);
 
@@ -1049,19 +1033,17 @@
         for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
         {
-            const int        &idx  = i->first;
-            const Connection &slot = i->second;
+            const int           &idx = i->first;
+            const ConnectionFAD *fad = i->second;
 
             ostringstream str;
-            str << "Slot " << setw(2) << idx << ": " << slot.first;
-
-            const ConnectionFAD *c = slot.second;
-
-            if (c->IsConnecting())
+            str << "Slot " << setw(2) << idx << ": " << fad->GetEndpoint();
+
+            if (fad->IsConnecting())
                 str << " (0:connecting, ";
             else
             {
-                if (c->IsClosed())
+                if (fad->IsClosed())
                     str << " (0:disconnected, ";
-                if (c->IsConnected())
+                if (fad->IsConnected())
                     str << " (0:connected, ";
             }
@@ -1074,5 +1056,5 @@
             }
 
-            if (c->IsTransmissionBlocked())
+            if (fad->IsTransmissionBlocked())
                 str << " [cmd_blocked]";
 
@@ -1093,10 +1075,6 @@
     }
 
-    void EnableConnection(Connection &c, bool enable=true)
-    {
-        ConnectionFAD *ptr = c.second;
-        if (!ptr)
-            return;
-
+    void EnableConnection(ConnectionFAD *ptr, bool enable=true)
+    {
         if (!enable)
         {
@@ -1105,14 +1083,13 @@
         }
 
-        ostringstream str;
-        str << c.first;
-
         if (!ptr->IsDisconnected())
         {
+            ostringstream str;
+            str << ptr->GetEndpoint();
+
             T::Warn("Connection to "+str.str()+" already in progress.");
             return;
         }
 
-        ptr->SetEndpoint(str.str());
 	ptr->StartConnect();
     }
@@ -1142,5 +1119,5 @@
 
         EnableConnection(it->second, enable);
-        ConnectSlot(it->first, enable ? it->second.first : tcp::endpoint());
+        ConnectSlot(it->first, enable ? it->second->GetEndpoint() : tcp::endpoint());
 
         return T::GetCurrentState();
@@ -1158,8 +1135,8 @@
             return T::GetCurrentState();
 
-        const bool enable = it->second.second->IsDisconnected() && fStatus2[slot]==0;
+        const bool enable = it->second->IsDisconnected() && fStatus2[slot]==0;
 
         EnableConnection(it->second, enable);
-        ConnectSlot(it->first, enable ? it->second.first : tcp::endpoint());
+        ConnectSlot(it->first, enable ? it->second->GetEndpoint() : tcp::endpoint());
 
         return T::GetCurrentState();
@@ -1171,5 +1148,5 @@
 
         for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
-            addr[i->first] = i->second.first;
+            addr[i->first] = i->second->GetEndpoint();
 
         StartThread(addr);
@@ -1219,4 +1196,6 @@
         int cnt = 0; // counter for enabled board
 
+        const bool runs = IsThreadRunning();
+
         for (int idx=0; idx<40; idx++)
         {
@@ -1225,5 +1204,5 @@
             if (slot!=fBoards.end())
             {
-                const ConnectionFAD *c = slot->second.second;
+                const ConnectionFAD *c = slot->second;
                 if (c->IsDisconnected())
                 {
@@ -1231,5 +1210,5 @@
                     nclosed1++;
 
-                    DisconnectSlot(idx);
+                    //DisconnectSlot(idx);
                 }
                 if (c->IsConnecting())
@@ -1248,7 +1227,11 @@
 
             // ----- Event builder -----
+
+            if (!runs)
+                continue;
+
             stat2[idx] = GetNumConnected(idx);
 
-            if (!IsConnected(idx) && !IsDisconnected(idx))
+            if (IsConnecting(idx))
                 nconnecting2++;
 
@@ -1276,5 +1259,13 @@
 
         if (nconnecting1>0 || nconnecting2>0 || nconnected1!=nconnected2)
+        {
+            cout << nconnecting1 << " " << nconnecting2 << " " << nconnected1 << " " << nconnected2 << endl;
+
+            if (nconnecting2>0)
+                for (int i=0; i<40; i++)
+                    cout << i << ": " << GetNumConnected(i) << " " <<IsConnected(i) << " " << IsConnecting(i) << " " << IsDisconnected(i) << endl;
+
             return FAD::kConnecting;
+        }
 
         // nconnected1 == nconnected2 == 0
@@ -1298,22 +1289,18 @@
         }
 
-        fBoards[i] = make_pair(addr, new ConnectionFAD(*this, *this));
-        fBoards[i].second->SetVerbose(fIsVerbose);
-        fBoards[i].second->SetHexOutput(fIsHexOutput);
-        fBoards[i].second->SetDataOutput(fIsDataOutput);
-        fBoards[i].second->SetDebugTx(fDebugTx);
+        ConnectionFAD *fad = new ConnectionFAD(*this, *this, i);
+
+        fad->SetEndpoint(addr);
+        fad->SetVerbose(fIsVerbose);
+        fad->SetHexOutput(fIsHexOutput);
+        fad->SetDataOutput(fIsDataOutput);
+        fad->SetDebugTx(fDebugTx);
+
+        fBoards[i] = fad;
     }
 
 
     DimDescribedService fDimConnection;
- /*
-    template<class T>
-        void Update(DimDescribedService &svc, const T &data) const
-    {
-        //cout << "Update: " << svc.getName() << " (" << sizeof(T) << ")" << endl;
-        svc.setData(const_cast<T*>(&data), sizeof(T));
-        svc.updateService();
-    }
- */
+
     void UpdateConnectionStatus(const vector<uint8_t> &stat1, const vector<uint8_t> &stat2, bool thread)
     {
@@ -1504,4 +1491,14 @@
              "|enable[bool]:Whether the event builder should ignore data from this slot (yes) or allowed (no)");
 
+        T::AddEvent("CLOSE_OPEN_FILES", FAD::kConnecting, FAD::kConnected)
+            (boost::bind(&StateMachineFAD::CloseOpenFiles, this))
+            ("Close all run files opened by the EventBuilder.");
+
+        T::AddEvent("TEST", "S:1")
+           (boost::bind(&StateMachineFAD::Test, this, _1))
+            ("");
+
+
+
         // Conenction commands
         T::AddEvent("START", FAD::kOffline)
@@ -1529,11 +1526,4 @@
             ("");
 
-        T::AddEvent("CLOSE_OPEN_FILES", FAD::kConnecting, FAD::kConnected)
-            (boost::bind(&StateMachineFAD::CloseOpenFiles, this))
-            ("Close all run files opened by the EventBuilder.");
-
-        T::AddEvent("TEST", "S:1")
-            (boost::bind(&StateMachineFAD::Test, this, _1))
-            ("");
 
         T::AddEvent("ADD_ADDRESS", "C", FAD::kOffline)
@@ -1553,5 +1543,5 @@
     {
         for (BoardList::const_iterator i=fBoards.begin(); i!=fBoards.end(); i++)
-            delete i->second.second;
+            delete i->second;
         fBoards.clear();
     }
