Ignore:
Timestamp:
07/13/11 18:38:23 (13 years ago)
Author:
tbretz
Message:
Implemented sending triggers from the ftm to the fad via Dim.
File:
1 edited

Legend:

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

    r10865 r11380  
    1212#include "Converter.h"
    1313
     14#include "Dim.h"
    1415#include "HeadersFTM.h"
    1516
     
    5354                               void (tcp_connection::*handler)(const bs::error_code&))// const
    5455    {
    55         timer.expires_from_now(boost::posix_time::seconds(seconds));
     56        timer.expires_from_now(boost::posix_time::milliseconds(seconds));
    5657        timer.async_wait(boost::bind(handler, shared_from_this(), dummy::error));
    5758    }
     
    6162    // The constructor is prvate to force the obtained pointer to be shared
    6263    tcp_connection(ba::io_service& ioservice) : ba::ip::tcp::socket(ioservice),
    63         fTriggerDynData(ioservice)
     64        fTriggerDynData(ioservice), fTriggerSendData(ioservice)
    6465    {
    6566        //deadline_.expires_at(boost::posix_time::pos_infin);
     
    156157    bool fReportsDisabled;
    157158
     159    ba::deadline_timer fTriggerSendData;
     160
    158161    void SendDynamicData()
    159162    {
     
    175178        fHeader.fType=kDynamicData;     // FtuList
    176179        fHeader.fDataSize=sizeof(FTM::DynamicData)/2+1;
    177         fHeader.fTriggerCounter = fCounter++;
     180        fHeader.fTriggerCounter = fCounter;
    178181        fHeader.fTimeStamp = fTimeStamp++*1000000;//lrint(Time().UnixTime());
    179182
     
    190193        fHeader.fType=kStaticData;     // FtuList
    191194        fHeader.fDataSize=sizeof(FTM::StaticData)/2+1;
    192         fHeader.fTriggerCounter = fCounter++;
     195        fHeader.fTriggerCounter = fCounter;
    193196        fHeader.fTimeStamp = fTimeStamp*1000000;//lrint(Time().UnixTime());
    194197
     
    242245                fHeader.fType=kFtuList;     // FtuList
    243246                fHeader.fDataSize=sizeof(FTM::FtuList)/2+1;
    244                 fHeader.fTriggerCounter = fCounter++;
     247                fHeader.fTriggerCounter = fCounter;
    245248                fHeader.fTimeStamp = fTimeStamp*1000000;//lrint(Time().UnixTime());
    246249
     
    330333                fCounter = 0;
    331334                fTimeStamp = 0;
     335                fHeader.fTriggerCounter = fCounter;
    332336
    333337                fBufCommand.resize(5);
    334338                AsyncRead(ba::buffer(fBufCommand));
     339
     340                AsyncWait(fTriggerSendData, 0, &tcp_connection::TriggerSendData);
    335341                return;
    336342
    337343            case kCmdStopRun:
    338344                fHeader.fState = FTM::kFtmIdle;
     345
     346                fTriggerSendData.cancel();
    339347
    340348                fCounter = 0;
     
    365373                fHeader.fType=kRegister;     // FtuList
    366374                fHeader.fDataSize=2;
    367                 fHeader.fTriggerCounter = fCounter++;
    368375                fHeader.fTimeStamp = fTimeStamp*1000000;//lrint(Time().UnixTime());
    369376
     
    430437        SendDynamicData();
    431438
    432         AsyncWait(fTriggerDynData, 1, &tcp_connection::SendDynData);
     439        AsyncWait(fTriggerDynData, 1000, &tcp_connection::SendDynData);
     440    }
     441
     442    void TriggerSendData(const boost::system::error_code &ec)
     443    {
     444        if (!is_open())
     445        {
     446            // For example: Here we could schedule a new accept if we
     447            // would not want to allow two connections at the same time.
     448            return;
     449        }
     450
     451        if (ec==ba::error::basic_errors::operation_aborted)
     452            return;
     453
     454        // Check whether the deadline has passed. We compare the deadline
     455        // against the current time since a new asynchronous operation
     456        // may have moved the deadline before this actor had a chance
     457        // to run.
     458        if (fTriggerSendData.expires_at() > ba::deadline_timer::traits_type::now())
     459            return;
     460
     461        Dim::SendCommand("FAD/TRIGGER");
     462        fCounter++;
     463
     464        const uint16_t time = 100*float(rand())/RAND_MAX+50;
     465
     466        AsyncWait(fTriggerSendData, time, &tcp_connection::TriggerSendData);
    433467    }
    434468
     
    449483        AsyncRead(ba::buffer(fBufCommand));
    450484
    451         AsyncWait(fTriggerDynData, 1, &tcp_connection::SendDynData);
     485        AsyncWait(fTriggerDynData, 1000, &tcp_connection::SendDynData);
    452486
    453487//        AsyncWrite(ba::buffer(ba::const_buffer(&fHeader, sizeof(FTM::Header))));
     
    508542};
    509543
     544#include "Configuration.h"
     545
     546void SetupConfiguration(::Configuration &conf)
     547{
     548    const string n = conf.GetName()+".log";
     549
     550    po::options_description config("Program options");
     551    config.add_options()
     552        ("dns",       var<string>("localhost"), "Dim nameserver host name (Overwites DIM_DNS_NODE environment variable)")
     553        ("port,p",    var<uint16_t>(5000), "")
     554        ;
     555
     556    po::positional_options_description p;
     557    p.add("port", 1); // The first positional options
     558    p.add("num",  1); // The second positional options
     559
     560    conf.AddEnv("dns", "DIM_DNS_NODE");
     561
     562    conf.AddOptions(config);
     563    conf.SetArgumentPositions(p);
     564}
     565
    510566int main(int argc, const char **argv)
    511567{
     568    ::Configuration conf(argv[0]);
     569
     570    SetupConfiguration(conf);
     571
     572    po::variables_map vm;
     573    try
     574    {
     575        vm = conf.Parse(argc, argv);
     576    }
     577#if BOOST_VERSION > 104000
     578    catch (po::multiple_occurrences &e)
     579    {
     580        cerr << "Program options invalid due to: " << e.what() << " of '" << e.get_option_name() << "'." << endl;
     581        return -1;
     582    }
     583#endif
     584    catch (exception& e)
     585    {
     586        cerr << "Program options invalid due to: " << e.what() << endl;
     587        return -1;
     588    }
     589
     590    if (conf.HasVersion() || conf.HasPrint() || conf.HasHelp())
     591        return -1;
     592
     593    Dim::Setup(conf.Get<string>("dns"));
     594
    512595    //try
    513596    {
    514597        ba::io_service io_service;
    515598
    516         Port = argc==2 ? lexical_cast<int>(argv[1]) : 5000;
     599        Port = conf.Get<uint16_t>("port");
    517600
    518601        tcp_server server(io_service, Port);
Note: See TracChangeset for help on using the changeset viewer.