Changeset 10835 for trunk/FACT++


Ignore:
Timestamp:
05/26/11 16:16:30 (13 years ago)
Author:
tbretz
Message:
Added --max-mem program option; updated allowed states for CONNECT and DISCONNECT; added the offline state; propagate IP address to event-builder; moved EventBuilderWrapper to new file.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/fadctrl.cc

    r10824 r10835  
    1 #include <boost/bind.hpp>
    21#include <boost/bind.hpp>
    32#include <boost/array.hpp>
     
    1110#include <boost/asio/error.hpp>
    1211#include <boost/asio/deadline_timer.hpp>
     12#include <boost/date_time/posix_time/posix_time_types.hpp>
    1313
    1414#include "Dim.h"
     
    1818#include "Connection.h"
    1919#include "Configuration.h"
    20 #include "Timers.h"
    2120#include "Console.h"
    2221#include "Converter.h"
     
    8685
    8786private:
    88     void HandleReceivedData(const bs::error_code& err, size_t bytes_received, int/* type*/)
     87    enum
     88    {
     89        kReadHeader = 1,
     90        kReadData   = 2,
     91    };
     92
     93    void HandleReceivedData(const bs::error_code& err, size_t bytes_received, int type)
    8994    {
    9095        // Do not schedule a new read if the connection failed.
     
    110115        // FIXME FIXME FIXME. The data block could have the same size!!!!!
    111116        // !!!!!!!!!!!!!!!!!!!
    112         if (bytes_received == sizeof(FAD::EventHeader))
    113         {
     117        if (type==kReadHeader)
     118        {
     119            if (bytes_received!=sizeof(FAD::EventHeader))
     120            {
     121                ostringstream str;
     122                str << "Bytes received (" << bytes_received << " don't match header size " << sizeof(FAD::EventHeader);
     123                Error(str);
     124                PostClose(false);
     125                return;
     126            }
     127
    114128            fEventHeader = fBuffer;
    115129
     
    132146
    133147            fBuffer.resize(fEventHeader.fPackageLength-sizeof(FAD::EventHeader)/2);
    134             AsyncRead(ba::buffer(fBuffer));
     148            AsyncRead(ba::buffer(fBuffer), kReadData);
    135149            AsyncWait(fInTimeout, 50, &Connection::HandleReadTimeout);
    136150
     
    201215
    202216        fBuffer.resize(sizeof(FAD::EventHeader)/2);
    203         AsyncRead(ba::buffer(fBuffer));
     217        AsyncRead(ba::buffer(fBuffer), kReadHeader);
    204218    }
    205219
     
    248262
    249263        fBuffer.resize(sizeof(FAD::EventHeader)/2);
    250         AsyncRead(ba::buffer(fBuffer));
     264        AsyncRead(ba::buffer(fBuffer), kReadHeader);
    251265
    252266//        for (int i=0; i<36; i++)
     
    262276        msg << "Sending command:" << hex;
    263277        msg << " 0x" << setw(4) << setfill('0') << cmd[0];
     278        msg << " (+ " << cmd.size()-1 << " bytes data)";
    264279        Message(msg);
    265280
     
    296311    Connection(ioservice, imp()),
    297312    fIsVerbose(true), fIsHexOutput(false), fIsDataOutput(false), fCounter(0)
    298 
    299     {
     313    {
     314        // Maximum possible needed space:
     315        // The full header, all channels with all DRS bins
     316        // Two trailing shorts
     317        fBuffer.reserve(sizeof(FAD::EventHeader) + FAD::kNumChannels*(sizeof(FAD::ChannelHeader) + FAD::kMaxBins*sizeof(uint16_t)) + 2*sizeof(uint16_t));
     318
    300319        SetLogStream(&imp);
    301320    }
     
    500519*/
    501520// ------------------------------------------------------------------------
    502 extern "C"
    503 {
    504     extern void *readFAD(void*);
    505     extern void *procEvt(void*);
    506     extern void *writeEvt(void*);
    507     extern void initReadFAD();
    508 };
    509 
    510 #include "EventBuilder.h"
    511 
    512 class EventBuilderWrapper
    513 {
    514 public:
    515     // FIXME
    516     static EventBuilderWrapper *This;
    517 
    518 private:
    519     boost::thread fThread;
    520 
    521     enum CommandStates_t // g_runStat
    522     {
    523         kAbort      = -2,  // quit as soon as possible ('abort')
    524         kExit       = -1,  // stop reading, quit when buffered events done ('exit')
    525         kInitialize =  0,  // 'initialize' (e.g. dim not yet started)
    526         kHybernate  =  1,  // do nothing for long time ('hybernate') [wakeup within ~1sec]
    527         kSleep      =  2,  // do nothing ('sleep')                   [wakeup within ~10msec]
    528         kModeFlush  = 10,  // read data from camera, but skip them ('flush')
    529         kModeTest   = 20,  // read data and process them, but do not write to disk ('test')
    530         kModeFlag   = 30,  // read data, process and write all to disk ('flag')
    531         kModeRun    = 40,  // read data, process and write selected to disk ('run')
    532     };
    533 
    534     MessageImp &fMsg;
    535 
    536 public:
    537     EventBuilderWrapper(MessageImp &msg) : fMsg(msg)
    538     {
    539         if (This)
    540             throw runtime_error("EventBuilderWrapper cannot be instantiated twice.");
    541 
    542         This = this;
    543         Start();
    544     }
    545 
    546     void Update(ostringstream &out, int severity)
    547     {
    548         fMsg.Update(out, severity);
    549     }
    550 
    551     void Start()
    552     {
    553         if (fThread.joinable())
    554         {
    555             fMsg.Warn("Start - EventBuilder still running");
    556             return;
    557         }
    558 
    559         fMsg.Message("Initializing EventBuilder");
    560         initReadFAD();
    561 
    562         g_runStat = kHybernate;
    563         fThread = boost::thread(readFAD, (void*)NULL);
    564 
    565         fMsg.Message("EventBuilder started");
    566     }
    567     void Abort()
    568     {
    569         fMsg.Message("Waiting for EventBuilder to abort...");
    570         g_runStat = kAbort;
    571         fThread.join();
    572         fMsg.Message("EventBuilder stopped.");
    573     }
    574 
    575     void Exit()
    576     {
    577         fMsg.Message("Waiting for EventBuilder to exit - be patient...");
    578         g_runStat = kExit;
    579     }
    580 
    581     void Wait()
    582     {
    583         fThread.join();
    584         fMsg.Message("EventBuilder stopped.");
    585     }
    586 
    587     void Hybernate() { g_runStat = kHybernate; }
    588     void Sleep()     { g_runStat = kSleep;     }
    589     void FlushMode() { g_runStat = kModeFlush; }
    590     void TestMode()  { g_runStat = kModeTest;  }
    591     void FlagMode()  { g_runStat = kModeFlag;  }
    592     void RunMode()   { g_runStat = kModeRun;   }
    593 
    594     // FIXME: To be removed
    595     void SetMode(int mode) { g_runStat = mode; }
    596 
    597     bool IsConnected(int i) const     { return gi_NumConnect[i]==7; }
    598     bool IsDisconnected(int i) const  { return gi_NumConnect[i]==0; }
    599     int  GetNumConnected(int i) const { return gi_NumConnect[i]; }
    600 
    601     void Restart()
    602     {
    603         Abort();
    604         Start();
    605     }
    606 
    607     ~EventBuilderWrapper()
    608     {
    609         Abort();
    610     }
    611 };
    612 /*
    613 extern "C" {
    614 
    615 void Error(int severity, int errnum, const char *fmt, ...)
    616 {
    617     va_list ap;
    618     va_start(ap, fmt);
    619 
    620     int n=256;
    621 
    622     char *ret=0;
    623     while (1)
    624     {
    625         ret = new char[n+1];
    626 
    627         const int sz = vsnprintf(ret, n, fmt, ap);
    628         if (sz<=n)
    629             break;
    630 
    631         n *= 2;
    632         delete [] ret;
    633     };
    634 
    635     va_end(ap);
    636 
    637     ostringstream str;
    638     str << ret << " (" << errnum << ":" << strerror(errnum) << ")";
    639 
    640     delete [] ret;
    641 
    642     EventBuilderWrapper::This->Update(str, severity);
    643 }
    644 
    645 }
    646 */
    647 
    648 EventBuilderWrapper *EventBuilderWrapper::This = 0;
     521
     522#include "EventBuilderWrapper.h"
    649523
    650524// ------------------------------------------------------------------------
     
    943817            }
    944818
    945             T::Message(str.str());
     819            T::Out() << str.str() << endl;
    946820        }
    947821
     
    1000874    int Connect()
    1001875    {
    1002         T::Error("FIXME - Propagate IP Addresses to EventBuilder");
    1003 
    1004         Start();
     876        vector<string> addr;
     877        for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
     878            addr.push_back(i->second.first);
     879
     880        Start(addr);
    1005881        EnableAll(true);
    1006882
     
    11261002
    11271003        if (nconnected1==0 && nconnected2==0)
    1128             return FAD::kDisconnected;
     1004            return IsThreadRunning() ? FAD::kOffline : FAD::kDisconnected;
    11291005
    11301006        // FIXME: Evaluate event builder status
     
    11701046
    11711047        // State names
     1048        T::AddStateName(FAD::kDisconnected, "Offline",
     1049                        "All enabled FAD boards are disconnected and the event-builer thread is not running.");
     1050
    11721051        T::AddStateName(FAD::kDisconnected, "Disconnected",
    1173                         "All enabled FAD boards are disconnected.");
     1052                        "All enabled FAD boards are disconnected, but the event-builder thread is running.");
    11741053
    11751054        T::AddStateName(FAD::kConnected, "Connected",
     
    12691148            ("");*/
    12701149
    1271         T::AddEvent("CONNECT", FAD::kDisconnected)
     1150        T::AddEvent("CONNECT", FAD::kOffline)
    12721151            (boost::bind(&StateMachineFAD::Connect, this))
    12731152            ("");
    12741153
    1275         T::AddEvent("DISCONNECT")
     1154        T::AddEvent("DISCONNECT", FAD::kDisconnected, FAD::kConnecting, FAD::kConnected)
    12761155            (boost::bind(&StateMachineFAD::Disconnect, this))
    12771156            ("");
     
    12811160            ("");
    12821161
    1283         T::AddEvent("ADD_ADDRESS", "C", FAD::kDisconnected)
     1162        T::AddEvent("ADD_ADDRESS", "C", FAD::kOffline)
    12841163            (boost::bind(&StateMachineFAD::AddAddress, this, _1))
    12851164            ("Add the address of a DRS4 board to the first free slot"
    12861165             "|IP[string]:address in the format <address:port>");
    1287         T::AddEvent("REMOVE_SLOT", "S:1", FAD::kDisconnected)
     1166        T::AddEvent("REMOVE_SLOT", "S:1", FAD::kOffline)
    12881167            (boost::bind(&StateMachineFAD::RemoveSlot, this, _1))
    12891168            ("Remove the Iaddress in slot n. For a list see LIST"
     
    13071186        fIsDataOutput = conf.Get<bool>("data-out");
    13081187
     1188        SetMaxMemory(conf.Get<unsigned int>("max-mem"));
     1189
    13091190        if (!(conf.Has("base-addr") ^ conf.Has("addr")))
    13101191        {
     
    13711252        }
    13721253
    1373         EnableAll();
     1254        Connect();
    13741255
    13751256        return true;
     
    14711352        ("hex-out",    po_bool(),  "Enable printing contents of all printed messages also as hex data.")
    14721353        ("data-out",   po_bool(),  "Enable printing received event data.")
    1473         ("addr",       vars<string>(), "Network address of FAD")
    1474         ("base-addr",  var<string>(),  "Base address of all FAD")
     1354        ("addr",       vars<string>(),     "Network address of FAD")
     1355        ("base-addr",  var<string>(),      "Base address of all FAD")
     1356        ("max-mem,m",  var<unsigned int>(100), "Maximum memory the event builder thread is allowed to consume for its event buffer")
    14751357        ;
    14761358
Note: See TracChangeset for help on using the changeset viewer.