00001 #include "config.h"
00002 #include "Options.h"
00003 #include "Config.h"
00004 #include "Priority.h"
00005 #include "Parameter.h"
00006 #include "SingleValue.h"
00007 #include "BlobValue.h"
00008 #include "Address.h"
00009 #include <rcudb/Server.h>
00010 #include <rcudb/Sql.h>
00011 #include <iostream>
00012 #include <sstream>
00013 #include <stdexcept>
00014 #include <algorithm>
00015
00016 void
00017 createValue(RcuDb::Server& s,
00018 int configId, int paramId, bool blob, int addrId)
00019 {
00020 if (blob) {
00021 std::vector<unsigned int> array;
00022 for (size_t i = 0; i < 10; i++) array.push_back(i+1);
00023 RcuConf::BlobValue b(configId, paramId, addrId, array);
00024 if (addrId > 0) b.Print();
00025 if (b.Insert(s)) return;
00026 }
00027 else {
00028 RcuConf::SingleValue v(configId, paramId, addrId, paramId);
00029 if (addrId > 0) v.Print();
00030 if (v.Insert(s)) return;
00031 }
00032 std::stringstream errstr;
00033 errstr << "Failed to insert " << (blob ? "Blob" : "Single")
00034 << "Value for (" << configId << "," << paramId << ","
00035 << addrId << "): " << s.ErrorString();
00036 throw std::runtime_error(errstr.str());
00037 }
00038
00039
00040
00041 int
00042 main(int argc, char** argv)
00043 {
00044 using namespace RcuConf;
00045 Option<bool> hOpt('h', "help", "Show this help", false,false);
00046 Option<bool> VOpt('V', "version", "Show version number", false,false);
00047 Option<unsigned> tOpt('t', "tag", "Tag to use", 0);
00048 Option<unsigned> xOpt('x', "x", "X coordinate", 0);
00049 Option<unsigned> yOpt('y', "y", "Y coordinate", 0);
00050 Option<unsigned> zOpt('z', "z", "Z coordinate", 0);
00051 Option<std::string> cOpt('c', "connection", "Database connection url",
00052 "mysql://config@localhost/RCU");
00053 CommandLine cl("");
00054 cl.Add(hOpt);
00055 cl.Add(VOpt);
00056 cl.Add(cOpt);
00057 cl.Add(tOpt);
00058 cl.Add(xOpt);
00059 cl.Add(yOpt);
00060 cl.Add(zOpt);
00061
00062 if (!cl.Process(argc, argv)) return 1;
00063 if (hOpt.IsSet()) {
00064 cl.Help();
00065 return 0;
00066 }
00067 if (VOpt.IsSet()) {
00068 std::cout << "raw2root version " << VERSION << std::endl;
00069 return 0;
00070 }
00071
00072
00073 std::string con = cOpt;
00074 try {
00075 RcuDb::Server* server = RcuDb::Server::Connect(con);
00076 if (!server)
00077 throw std::runtime_error("Failed to open connection to server!");
00078
00079
00080
00081 Config::List confs;
00082 if (!Config::Select(confs, *server, tOpt, xOpt, yOpt, zOpt)) {
00083 std::stringstream s;
00084 s << "Failed to get config (" << tOpt << "," << xOpt << ","
00085 << zOpt << "): " << server->ErrorString();
00086 throw std::runtime_error(s.str());
00087 }
00088 Config* config = *(confs.begin());
00089
00090
00091 Priority::List priorities;
00092 if (!Priority::Select(priorities, *server, *config)) {
00093 std::stringstream s;
00094 s << "Failed to get priority for configuration " << config->Id()
00095 << ": " << server->ErrorString();
00096 throw std::runtime_error(s.str());
00097 }
00098 if (priorities.size() < 0)
00099 throw std::runtime_error("Too few matches on default priority");
00100 if (priorities.size() > 1)
00101 throw std::runtime_error("Too many matches on default priority");
00102 Priority* priority = *(priorities.begin());
00103
00104
00105 std::vector<int> order;
00106 priority->Params(order);
00107 RcuDb::Sql parSel;
00108 for (std::vector<int>::iterator i = order.begin(); i != order.end(); ++i){
00109 if (i != order.begin()) parSel << " OR";
00110 parSel << " id=" << *i;
00111 }
00112
00113
00114 Parameter::List params;
00115 if (!Parameter::Select(params, *server, parSel)) {
00116 std::stringstream s;
00117 s << "Failed to get parameters: " << server->ErrorString();
00118 throw std::runtime_error(s.str());
00119 }
00120 int nParam = params.size();
00121
00122
00123 Address::List addrs;
00124 if (!Address::Select(addrs, *server, "")) {
00125 std::stringstream s;
00126 s << "Failed to get addresss: " << server->ErrorString();
00127 throw std::runtime_error(s.str());
00128 }
00129 int nAddress = addrs.size();
00130
00131
00132 for (Parameter::List::iterator i = params.begin(); i != params.end(); ++i){
00133 createValue(*server, config->Id(), (*i)->Id(), (*i)->IsBlob(), -1);
00134 if ((*i)->Destination() == Parameter::kRcu) continue;
00135
00136 if ((*i)->Name() == "K1") {
00137
00138 createValue(*server, config->Id(), (*i)->Id(), (*i)->IsBlob(), -1);
00139 createValue(*server, config->Id(), (*i)->Id(), (*i)->IsBlob(), -1);
00140
00141 unsigned a = (*(addrs.begin()))->Id();
00142 createValue(*server, config->Id(), (*i)->Id(), (*i)->IsBlob(), a);
00143 createValue(*server, config->Id(), (*i)->Id(), (*i)->IsBlob(), a);
00144
00145 createValue(*server, config->Id(), (*i)->Id(), (*i)->IsBlob(), -1);
00146 createValue(*server, config->Id(), (*i)->Id(), (*i)->IsBlob(), -1);
00147
00148 createValue(*server, config->Id(), (*i)->Id(), (*i)->IsBlob(), a);
00149 createValue(*server, config->Id(), (*i)->Id(), (*i)->IsBlob(), a);
00150 }
00151
00152 if (rand() % nParam > nParam / 3) continue;
00153
00154 int m = std::min(nAddress, 4);
00155 Address::List::iterator iter = addrs.begin();
00156 while (m > 0 && iter != addrs.end()) {
00157 if ((rand() % nAddress) < m) {
00158 std::cout << "Creating value with address id "
00159 << (*iter)->Id() << std::endl;
00160 createValue(*server, config->Id(), (*i)->Id(), (*i)->IsBlob(),
00161 (*iter)->Id());
00162 m--;
00163 }
00164 ++iter;
00165 }
00166 }
00167
00168
00169 SingleValue::List singles;
00170 if (!SingleValue::Select(singles, *server, "")) {
00171 std::stringstream s;
00172 s << "Failed to get priorities: " << server->ErrorString();
00173 throw std::runtime_error(s.str());
00174 }
00175 for (SingleValue::List::iterator i = singles.begin();
00176 i != singles.end(); ++i)
00177 (*i)->Print();
00178
00179
00180 BlobValue::List blobs;
00181 if (!BlobValue::Select(blobs, *server, "")) {
00182 std::stringstream s;
00183 s << "Failed to get priorities: " << server->ErrorString();
00184 throw std::runtime_error(s.str());
00185 }
00186 for (BlobValue::List::iterator i = blobs.begin(); i != blobs.end(); ++i)
00187 (*i)->Print();
00188
00189 }
00190 catch (std::exception& e) {
00191 std::cerr << e.what() << std::endl;
00192 return 1;
00193 }
00194 return 0;
00195 }
00196
00197
00198
00199