#include "config.h"
#include "Configurator.h"
#include <rcudb/Server.h>
#include <rcudb/Sql.h>
#include <rcuxx/Rcu.h>
#include <rcuxx/Bc.h>
#include <rcuxx/Fmd.h>
#include <rcuxx/Altro.h>
#include "Rcu.h"
#include "Bc.h"
#include "Fmd.h"
#include "Altro.h"
#include <stdexcept>
#include <iostream>
#include "Options.h"
int
main(int argc, char** argv)
{
using namespace RcuConf;
Option<bool> hOpt('h', "help", "Show this help", false,false);
Option<bool> VOpt('V', "version", "Show version number", false,false);
Option<bool> fOpt('f', "fmd", "Use the FMD", false,false);
Option<std::string> dOpt('d', "debug", "Debugging output");
Option<bool> eOpt('e', "emul", "Emulation output", false,false);
Option<unsigned> tOpt('t', "tag", "Tag to use\t", 0);
Option<unsigned> xOpt('x', "x", "X coordinate\t", 0);
Option<unsigned> yOpt('y', "y", "Y coordinate\t", 0);
Option<unsigned> zOpt('z', "z", "Z coordinate\t", 0);
Option<std::string> cOpt('c', "db", "Database connection url",
"mysql://config@localhost/RCU");
Option<std::string> uOpt('r', "rcu", "Rcu++ connection url",
"fee://localhost/FMD1");
CommandLine cl("");
cl.Add(hOpt);
cl.Add(VOpt);
cl.Add(cOpt);
cl.Add(tOpt);
cl.Add(xOpt);
cl.Add(yOpt);
cl.Add(zOpt);
cl.Add(fOpt);
cl.Add(uOpt);
cl.Add(dOpt);
cl.Add(eOpt);
if (!cl.Process(argc, argv)) return 1;
if (hOpt.IsSet()) {
cl.Help();
return 0;
}
if (VOpt.IsSet()) {
std::cout << "rcuconf version " << VERSION << std::endl;
return 0;
}
std::string con = cOpt;
std::string url = uOpt;
std::string deb = dOpt;
try {
RcuDb::Server* server = RcuDb::Server::Connect(con);
if (!server)
throw std::runtime_error("Failed to open connection to server!");
std::cout << "Debug string: " << deb << " "
<< deb.find("rcu") << " "
<< (deb.find("rcu") != std::string::npos) << std::endl;
Rcuxx::Rcu* rcu = Rcuxx::Rcu::Open(url.c_str(), eOpt.IsSet(),
deb.find("rcu")!=std::string::npos);
if (!rcu)
throw std::runtime_error("Failed to open connection to RCU");
if (deb.find("backend") != std::string::npos)
rcu->SetDebug(Rcuxx::Rcu::kBackend, 1);
Rcuxx::Bc* bc = 0;
if (fOpt.IsSet()) bc = new Rcuxx::Fmd(*rcu);
else bc = new Rcuxx::Bc(*rcu);
bc->SetDebug(deb.find("bc") != std::string::npos ||
(fOpt.IsSet() && deb.find("fmd") != std::string::npos));
Rcuxx::Altro altro(*rcu);
altro.SetDebug(deb.find("altro") != std::string::npos);
Rcu rcuComp(*rcu);
Bc bcComp(*bc);
Altro altroComp(altro);
Configurator configurator(*server, rcuComp, bcComp, altroComp);
int ret = configurator.Write(tOpt, xOpt, yOpt, zOpt);
if (ret < 0)
throw std::runtime_error(configurator.ErrorString());
const Rcuxx::Rcu::Block_t& b = rcu->GetBlock();
std::cout << "Block: " << std::endl;
for (int i = 0; i < ret; i++){
std::cout << "\t" << i << "\t0x" << std::setfill('0')
<< std::hex << std::setw(8) << b[i] << std::dec
<< std::setfill(' ') << std::endl;
}
}
catch (std::exception& e) {
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}