// how to compile : g++ -o server server.cc -lboost_thread -lboost_system `root-config --cflags --libs` #include #include #include #include #include "TFile.h" #include "TTree.h" unsigned long events = 0; unsigned long pre_events = 0; unsigned int pid=0; unsigned int sid=0; unsigned int tof=0; unsigned int pl=0; unsigned int pr=0; unsigned int epoch=0; TFile* rootfile; TTree* roottree; void timer(); void counter(const boost::system::error_code& e, boost::asio::deadline_timer* t); void timer() { boost::asio::io_service io; boost::asio::deadline_timer t(io, boost::posix_time::seconds(1)); t.async_wait(boost::bind(counter, boost::asio::placeholders::error, &t)); io.run(); } void counter(const boost::system::error_code& e, boost::asio::deadline_timer* t) { std::cout << "Event rate : " << events - pre_events << " , File Size = " << rootfile->GetSize() << std::endl; pre_events = events; t->expires_at(t->expires_at() + boost::posix_time::seconds(1)); t->async_wait(boost::bind(counter, boost::asio::placeholders::error, t)); } int main() { boost::asio::io_service io_service; boost::asio::ip::tcp::acceptor acceptor(io_service); boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 3000); acceptor.open(endpoint.protocol()); acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); acceptor.bind(endpoint); acceptor.listen(); boost::asio::ip::tcp::socket sock(io_service); acceptor.accept(sock); int buflen = 65536; char buf[buflen]; char pbuf[1024]; rootfile = new TFile("test.root", "recreate"); roottree = new TTree("EventTree", "DC-TOF Event Data"); roottree->Branch("pid", &pid, "pid/i"); roottree->Branch("sid", &sid, "sid/i"); roottree->Branch("tof", &tof, "tof/i"); roottree->Branch("pl", &pl, "pl/i"); roottree->Branch("pr", &pr, "pr/i"); roottree->Branch("epoch", &epoch, "epoch"); boost::thread thread(timer); while (true) { sock.receive(boost::asio::buffer(buf, 2)); unsigned int len = (unsigned int)(*((unsigned short*)&buf)); sock.receive(boost::asio::buffer(buf, len)); unsigned int pid = (unsigned int)buf[0]; unsigned int sid = (unsigned int)buf[1]; std::string response(&buf[2], 3); if (response == "IRD") { unsigned int buffno = (unsigned int)buf[5]; unsigned int nevents = (unsigned int)(*((unsigned short*)&buf[6])); for(unsigned int i=0; iFill(); std::sprintf(pbuf, "%u %u %s %u %u %u %u %u %u %u",pid,sid,"DAT", stx, pid, sid, tof, pl, pr, epoch); //if ( events % 10000 == 0 ) std::cout << pbuf << std::endl; events++; } } } roottree->Write(); rootfile->Close(); }