Index: /trunk/FACT++/src/fad.cc
===================================================================
--- /trunk/FACT++/src/fad.cc	(revision 11379)
+++ /trunk/FACT++/src/fad.cc	(revision 11380)
@@ -14,4 +14,7 @@
 #include "HeadersFAD.h"
 
+#include "dis.hxx"
+#include "Dim.h"
+
 using namespace std;
 using namespace FAD;
@@ -24,4 +27,30 @@
 using ba::ip::tcp;
 
+class tcp_connection;
+
+class Trigger : public DimCommandHandler
+{
+    DimCommand fCmd;
+
+    vector<tcp_connection*> vec;
+
+public:
+    Trigger() : fCmd("FAD/TRIGGER", "C", this)
+    {
+    }
+
+    void Add(tcp_connection *ptr)
+    {
+        vec.push_back(ptr);
+    }
+
+    void Remove(tcp_connection *ptr)
+    {
+        vec.erase(find(vec.begin(), vec.end(), ptr));
+    }
+
+    void commandHandler();
+};
+
 // ------------------------------------------------------------------------
 
@@ -29,4 +58,6 @@
 {
 public:
+    static Trigger fTrigger;
+
     const int fBoardId;
 
@@ -55,6 +86,13 @@
     // The constructor is prvate to force the obtained pointer to be shared
     tcp_connection(ba::io_service& ioservice, int boardid) : ba::ip::tcp::socket(ioservice),
-        fBoardId(boardid), fRamRoi(kNumChannels), fTriggerSendData(ioservice)
-    {
+        fBoardId(boardid), fRamRoi(kNumChannels), fTriggerSendData(ioservice),
+        fTriggerEnabled(false)
+    {
+        fTrigger.Add(this);
+    }
+    void PostTrigger()
+    {
+        if (fTriggerEnabled)
+            get_io_service().post(boost::bind(&tcp_connection::SendData, this));
     }
 
@@ -380,4 +418,5 @@
     ~tcp_connection()
     {
+        fTrigger.Remove(this);
         fSockets.clear();
     }
@@ -390,4 +429,15 @@
     }
 };
+
+Trigger tcp_connection::fTrigger;
+
+void Trigger::commandHandler()
+{
+    for (vector<tcp_connection*>::iterator it=vec.begin();
+         it!=vec.end(); it++)
+        {
+            (*it)->PostTrigger();
+        }
+}
 
 
@@ -482,12 +532,64 @@
 };
 
+#include "Configuration.h"
+
+void SetupConfiguration(::Configuration &conf)
+{
+    const string n = conf.GetName()+".log";
+
+    po::options_description config("Program options");
+    config.add_options()
+        ("dns",       var<string>("localhost"), "Dim nameserver host name (Overwites DIM_DNS_NODE environment variable)")
+        ("port,p",    var<uint16_t>(4000), "")
+        ("num,n",     var<uint16_t>(40),   "")
+        ;
+
+    po::positional_options_description p;
+    p.add("port", 1); // The first positional options
+    p.add("num",  1); // The second positional options
+
+    conf.AddEnv("dns", "DIM_DNS_NODE");
+
+    conf.AddOptions(config);
+    conf.SetArgumentPositions(p);
+}
+
 int main(int argc, const char **argv)
 {
+    ::Configuration conf(argv[0]);
+
+    SetupConfiguration(conf);
+
+    po::variables_map vm;
+    try
+    {
+        vm = conf.Parse(argc, argv);
+    }
+#if BOOST_VERSION > 104000
+    catch (po::multiple_occurrences &e)
+    {
+        cerr << "Program options invalid due to: " << e.what() << " of '" << e.get_option_name() << "'." << endl;
+        return -1;
+    }
+#endif
+    catch (exception& e)
+    {
+        cerr << "Program options invalid due to: " << e.what() << endl;
+        return -1;
+    }
+
+    if (conf.HasVersion() || conf.HasPrint() || conf.HasHelp())
+        return -1;
+
+    Dim::Setup(conf.Get<string>("dns"));
+
+    DimServer::start("FAD");
+
     //try
     {
         ba::io_service io_service;
 
-        int port = argc>=2 ? lexical_cast<int>(argv[1]) : 5000;
-        int n    = argc==3 ? lexical_cast<int>(argv[2]) :    1;
+        const uint16_t n = conf.Get<uint16_t>("num");
+        uint16_t port = conf.Get<uint16_t>("port");
 
         vector<shared_ptr<tcp_server>> servers;
Index: /trunk/FACT++/src/ftm.cc
===================================================================
--- /trunk/FACT++/src/ftm.cc	(revision 11379)
+++ /trunk/FACT++/src/ftm.cc	(revision 11380)
@@ -12,4 +12,5 @@
 #include "Converter.h"
 
+#include "Dim.h"
 #include "HeadersFTM.h"
 
@@ -53,5 +54,5 @@
                                void (tcp_connection::*handler)(const bs::error_code&))// const
     {
-        timer.expires_from_now(boost::posix_time::seconds(seconds));
+        timer.expires_from_now(boost::posix_time::milliseconds(seconds));
         timer.async_wait(boost::bind(handler, shared_from_this(), dummy::error));
     }
@@ -61,5 +62,5 @@
     // The constructor is prvate to force the obtained pointer to be shared
     tcp_connection(ba::io_service& ioservice) : ba::ip::tcp::socket(ioservice),
-        fTriggerDynData(ioservice)
+        fTriggerDynData(ioservice), fTriggerSendData(ioservice)
     {
         //deadline_.expires_at(boost::posix_time::pos_infin);
@@ -156,4 +157,6 @@
     bool fReportsDisabled;
 
+    ba::deadline_timer fTriggerSendData;
+
     void SendDynamicData()
     {
@@ -175,5 +178,5 @@
         fHeader.fType=kDynamicData;     // FtuList
         fHeader.fDataSize=sizeof(FTM::DynamicData)/2+1;
-        fHeader.fTriggerCounter = fCounter++;
+        fHeader.fTriggerCounter = fCounter;
         fHeader.fTimeStamp = fTimeStamp++*1000000;//lrint(Time().UnixTime());
 
@@ -190,5 +193,5 @@
         fHeader.fType=kStaticData;     // FtuList
         fHeader.fDataSize=sizeof(FTM::StaticData)/2+1;
-        fHeader.fTriggerCounter = fCounter++;
+        fHeader.fTriggerCounter = fCounter;
         fHeader.fTimeStamp = fTimeStamp*1000000;//lrint(Time().UnixTime());
 
@@ -242,5 +245,5 @@
                 fHeader.fType=kFtuList;     // FtuList
                 fHeader.fDataSize=sizeof(FTM::FtuList)/2+1;
-                fHeader.fTriggerCounter = fCounter++;
+                fHeader.fTriggerCounter = fCounter;
                 fHeader.fTimeStamp = fTimeStamp*1000000;//lrint(Time().UnixTime());
 
@@ -330,11 +333,16 @@
                 fCounter = 0;
                 fTimeStamp = 0;
+                fHeader.fTriggerCounter = fCounter;
 
                 fBufCommand.resize(5);
                 AsyncRead(ba::buffer(fBufCommand));
+
+                AsyncWait(fTriggerSendData, 0, &tcp_connection::TriggerSendData);
                 return;
 
             case kCmdStopRun:
                 fHeader.fState = FTM::kFtmIdle;
+
+                fTriggerSendData.cancel();
 
                 fCounter = 0;
@@ -365,5 +373,4 @@
                 fHeader.fType=kRegister;     // FtuList
                 fHeader.fDataSize=2;
-                fHeader.fTriggerCounter = fCounter++;
                 fHeader.fTimeStamp = fTimeStamp*1000000;//lrint(Time().UnixTime());
 
@@ -430,5 +437,32 @@
         SendDynamicData();
 
-        AsyncWait(fTriggerDynData, 1, &tcp_connection::SendDynData);
+        AsyncWait(fTriggerDynData, 1000, &tcp_connection::SendDynData);
+    }
+
+    void TriggerSendData(const boost::system::error_code &ec)
+    {
+        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.
+            return;
+        }
+
+        if (ec==ba::error::basic_errors::operation_aborted)
+            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 (fTriggerSendData.expires_at() > ba::deadline_timer::traits_type::now())
+            return;
+
+        Dim::SendCommand("FAD/TRIGGER");
+        fCounter++;
+
+        const uint16_t time = 100*float(rand())/RAND_MAX+50;
+
+        AsyncWait(fTriggerSendData, time, &tcp_connection::TriggerSendData);
     }
 
@@ -449,5 +483,5 @@
         AsyncRead(ba::buffer(fBufCommand));
 
-        AsyncWait(fTriggerDynData, 1, &tcp_connection::SendDynData);
+        AsyncWait(fTriggerDynData, 1000, &tcp_connection::SendDynData);
 
 //        AsyncWrite(ba::buffer(ba::const_buffer(&fHeader, sizeof(FTM::Header))));
@@ -508,11 +542,60 @@
 };
 
+#include "Configuration.h"
+
+void SetupConfiguration(::Configuration &conf)
+{
+    const string n = conf.GetName()+".log";
+
+    po::options_description config("Program options");
+    config.add_options()
+        ("dns",       var<string>("localhost"), "Dim nameserver host name (Overwites DIM_DNS_NODE environment variable)")
+        ("port,p",    var<uint16_t>(5000), "")
+        ;
+
+    po::positional_options_description p;
+    p.add("port", 1); // The first positional options
+    p.add("num",  1); // The second positional options
+
+    conf.AddEnv("dns", "DIM_DNS_NODE");
+
+    conf.AddOptions(config);
+    conf.SetArgumentPositions(p);
+}
+
 int main(int argc, const char **argv)
 {
+    ::Configuration conf(argv[0]);
+
+    SetupConfiguration(conf);
+
+    po::variables_map vm;
+    try
+    {
+        vm = conf.Parse(argc, argv);
+    }
+#if BOOST_VERSION > 104000
+    catch (po::multiple_occurrences &e)
+    {
+        cerr << "Program options invalid due to: " << e.what() << " of '" << e.get_option_name() << "'." << endl;
+        return -1;
+    }
+#endif
+    catch (exception& e)
+    {
+        cerr << "Program options invalid due to: " << e.what() << endl;
+        return -1;
+    }
+
+    if (conf.HasVersion() || conf.HasPrint() || conf.HasHelp())
+        return -1;
+
+    Dim::Setup(conf.Get<string>("dns"));
+
     //try
     {
         ba::io_service io_service;
 
-        Port = argc==2 ? lexical_cast<int>(argv[1]) : 5000;
+        Port = conf.Get<uint16_t>("port");
 
         tcp_server server(io_service, Port);
