Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "roc/Factory.h"
00016
00017 #include "roc/CombinerModule.h"
00018 #include "roc/ReadoutModule.h"
00019 #include "roc/ReadoutApplication.h"
00020 #include "roc/UdpDevice.h"
00021 #include "roc/NxCalibrModule.h"
00022
00023 #include "dabc/Command.h"
00024 #include "dabc/logging.h"
00025 #include "dabc/Port.h"
00026 #include "dabc/ModuleSync.h"
00027
00028 #include "base/Url.h"
00029 #include "mbs/LmdInput.h"
00030 #include "roc/FileInput.h"
00031
00032 dabc::FactoryPlugin rocfactory(new roc::Factory("roc"));
00033
00034
00035
00036 roc::Factory::Factory(const char* name) :
00037 dabc::Factory(name),
00038 base::BoardFactory(),
00039 fDevs()
00040 {
00041 }
00042
00043 dabc::Application* roc::Factory::CreateApplication(const char* classname, dabc::Command cmd)
00044 {
00045 if (strcmp(classname, xmlReadoutAppClass)==0)
00046 return new roc::ReadoutApplication();
00047
00048 return dabc::Factory::CreateApplication(classname, cmd);
00049 }
00050
00051 dabc::Module* roc::Factory::CreateModule(const char* classname, const char* modulename, dabc::Command cmd)
00052 {
00053 DOUT2(("roc::Factory::CreateModule called for class:%s, module:%s", classname, modulename));
00054
00055 if (strcmp(classname, "roc::CombinerModule")==0) {
00056 DOUT2(("roc::Factory::CreateModule - Created roc::CombinerModule %s ", modulename));
00057 return new roc::CombinerModule(modulename, cmd);
00058 } else
00059 if (strcmp(classname, "roc::ReadoutModule")==0) {
00060 DOUT2(("roc::Factory::CreateModule - Created roc::ReadoutModule module %s ", modulename));
00061 return new roc::ReadoutModule(modulename, cmd);
00062 } else
00063 if (strcmp(classname, "roc::NxCalibrModule")==0) {
00064 DOUT2(("roc::Factory::CreateModule - Created roc::NxCalibrModule module %s ", modulename));
00065 return new roc::NxCalibrModule(modulename, cmd);
00066 }
00067
00068 return 0;
00069 }
00070
00071 dabc::Device* roc::Factory::CreateDevice(const char* classname, const char* devname, dabc::Command cmd)
00072 {
00073 const char* thrdname = cmd.Field("Thread").AsStr();
00074
00075 if (strcmp(classname, roc::typeUdpDevice)==0) {
00076 DOUT2(("roc::Factory::CreateDevice - Creating ROC UdpDevice %s ...", devname));
00077
00078 roc::UdpDevice* dev = new roc::UdpDevice(devname, thrdname, cmd);
00079
00080 if (!dev->IsConnected()) {
00081 dabc::Object::Destroy(dev);
00082 return 0;
00083 }
00084
00085 return dev;
00086 }
00087
00088 return 0;
00089 }
00090
00091 void roc::Factory::ShowDebug(int lvl, const char* msg)
00092 {
00093 if (lvl<0) EOUT((msg));
00094 else DOUT0((msg));
00095 }
00096
00097 bool roc::Factory::IsFactoryFor(const char* name)
00098 {
00099 if (roc::Board::IsLmdFileAddress(name)) return true;
00100
00101 if (roc::Board::IsUdpAddress(name)) return true;
00102
00103 return false;
00104 }
00105
00106
00107 base::Board* roc::Factory::DoConnect(const char* name, base::ClientRole role)
00108 {
00109 if (dabc::mgr.null()) {
00110 if (!dabc::Factory::CreateManager("dabc")) return 0;
00111 dabc::SetDebugLevel(0);
00112 dabc::lgr()->SetLogLimit(1000000);
00113 dabc::lgr()->SetDebugMask(dabc::lgr()->GetDebugMask() & ~dabc::Logger::lTStamp);
00114 dabc::Manager::SetAutoDestroy(true);
00115 }
00116
00117 if (roc::Board::IsLmdFileAddress(name)) {
00118
00119 base::Url url(name);
00120
00121 int selectroc = -1;
00122
00123 std::string s = url.GetOptionValue("roc");
00124 if (s.length()>0) selectroc = atoi(s.c_str());
00125
00126 for (int n=0;(n<255) && (selectroc<0);n++) {
00127 std::string rname = dabc::format("roc%d",n);
00128 if (url.HasOption(rname)) selectroc = n;
00129 }
00130
00131 DOUT0(("Creating LMD-file input %s selectroc = %d", url.GetFullName().c_str(), selectroc));
00132
00133 mbs::LmdInput* inp = new mbs::LmdInput(url.GetFullName().c_str(), 0x100000);
00134
00135 if (!inp->Init()) {
00136 EOUT(("Cannot create LMD input %s", url.GetFullName().c_str()));
00137 delete inp;
00138 return 0;
00139 }
00140
00141 return new roc::FileInput(inp, selectroc);
00142 }
00143
00144 if (!roc::Board::IsUdpAddress(name)) return 0;
00145
00146 static int brdcnt = 0;
00147
00148 std::string devname = dabc::format("/roc/Udp%d", brdcnt++);
00149 if (!dabc::mgr.FindDevice(devname).null()) {
00150 EOUT(("Device with name %s already exists!!!", devname.c_str()));
00151 return 0;
00152 }
00153
00154 DOUT3(("Create device"));
00155
00156 std::string brdname = name;
00157
00158 base::Url url(name);
00159
00160 if (url.IsValid()) {
00161
00162 brdname = url.GetHostName();
00163 if (url.GetPort()>0) {
00164 brdname += ":";
00165 brdname += url.GetPortStr();
00166 }
00167
00168 if (url.HasOption(roleToString(base::roleDAQ))) role = base::roleDAQ; else
00169 if (url.HasOption(roleToString(base::roleControl))) role = base::roleControl; else
00170 if (url.HasOption(roleToString(base::roleObserver))) role = base::roleObserver;
00171 }
00172
00173 if (role==base::roleNone) role = base::roleObserver;
00174
00175 dabc::CmdCreateDevice cmd(typeUdpDevice, devname.c_str());
00176 cmd.SetStr(roc::xmlBoardAddr, brdname);
00177 cmd.SetStr(roc::xmlRole, base::roleToString(role));
00178 if (!dabc::mgr.Execute(cmd)) return 0;
00179
00180 UdpDeviceRef dev = dabc::mgr.FindDevice(devname);
00181
00182 DOUT3(("Create device dev = %p", dev()));
00183
00184 if (dev.null()) {
00185 EOUT(("UDP Device for board %s cannot be created!!!", brdname.c_str()));
00186 return 0;
00187 }
00188
00189 if (!dev()->InitAsBoard()) {
00190 dev.Destroy();
00191 return 0;
00192 }
00193
00194 roc::Board* brd = static_cast<roc::Board*> (dev());
00195
00196 fDevs.Add(dev);
00197
00198 return brd;
00199
00200 }
00201
00202 bool roc::Factory::DoClose(base::Board* brd)
00203 {
00204 roc::FileInput* inp = dynamic_cast<roc::FileInput*> (brd);
00205 if (inp!=0) {
00206 delete inp;
00207 return true;
00208 }
00209
00210 if (dabc::mgr.null()) return false;
00211
00212 for (unsigned n=0;n<fDevs.GetSize();n++) {
00213 UdpDevice* dev = (UdpDevice*) fDevs.GetObject(n);
00214
00215 if (static_cast<roc::Board*>(dev) == brd) {
00216
00217 dev->CloseAsBoard();
00218 dev->Execute(dabc::Command(roc::UdpDevice::CmdPutDisconnect()).SetTimeout(4.));
00219 fDevs.TakeRef(n).Destroy();
00220
00221 return true;
00222 }
00223 }
00224
00225
00226 EOUT(("Board::Close FAIL - brd %p not registered. Check if board closed second time", brd));
00227 return false;
00228 }