DABC (Data Acquisition Backbone Core)  2.9.9
Factory.cxx
Go to the documentation of this file.
1 // $Id: Factory.cxx 4479 2020-04-15 14:30:52Z linev $
2 
3 /************************************************************
4  * The Data Acquisition Backbone Core (DABC) *
5  ************************************************************
6  * Copyright (C) 2009 - *
7  * GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
8  * Planckstr. 1, 64291 Darmstadt, Germany *
9  * Contact: http://dabc.gsi.de *
10  ************************************************************
11  * This software can be used under the GPL license *
12  * agreements as stated in LICENSE.txt file *
13  * which is part of the distribution. *
14  ************************************************************/
15 
16 #include "mbs/Factory.h"
17 
18 #include "mbs/LmdInput.h"
19 #include "mbs/LmdOutput.h"
20 #include "mbs/TextInput.h"
21 #include "mbs/GeneratorInput.h"
22 #include "mbs/ServerTransport.h"
23 #include "mbs/ClientTransport.h"
24 #include "mbs/CombinerModule.h"
25 #include "mbs/api.h"
26 
28 
29 
30 dabc::Reference mbs::Factory::CreateObject(const std::string &classname, const std::string &objname, dabc::Command cmd)
31 {
32  if (classname=="mbs_iter")
33  return new mbs::EventsIterator(objname);
34 
35  return dabc::Factory::CreateObject(classname, objname, cmd);
36 }
37 
38 dabc::Module *mbs::Factory::CreateTransport(const dabc::Reference& port, const std::string &typ, dabc::Command cmd)
39 {
40  dabc::Url url(typ);
41 
42  dabc::PortRef portref = port;
43  std::string prot = url.GetProtocol(), host = url.GetHostName();
44  int kind = 0, portnum = url.GetPort();
45  if (prot == mbs::protocolMbss) kind = mbs::StreamServer; else
46  if (prot == mbs::protocolMbst) kind = mbs::TransportServer; else
47  if (prot == mbs::protocolMbs) {
48  kind = mbs::StreamServer;
50  kind = mbs::TransportServer;
51  if (StrToServerKind(host) != NoServer) {
52  kind = StrToServerKind(host);
53  host = "";
54  }
55  }
56 
57  if (portref.IsOutput() && (kind>0)) {
58 
59  if (portnum==0) portnum = DefualtServerPort(kind);
60 
62 
63  if (!addon) {
64  DOUT3("Fail assign MBS server to port:%d", portnum);
65  return nullptr;
66  }
67 
68  return new mbs::ServerTransport(cmd, portref, kind, portnum, addon, url);
69  }
70 
71  return dabc::Factory::CreateTransport(port, typ, cmd);
72 }
73 
74 
75 dabc::DataInput* mbs::Factory::CreateDataInput(const std::string &typ)
76 {
77  dabc::Url url(typ);
78  if ((url.GetProtocol()==mbs::protocolLmd) && (url.GetFullName() == "Generator")) {
79  return new mbs::GeneratorInput(url);
80  } else
81  if (url.GetProtocol()==mbs::protocolLmd) {
82  return new mbs::LmdInput(url);
83  } else
84  if (url.GetProtocol()=="lmdtxt") {
85  return new mbs::TextInput(url);
86  } else
87  if (((url.GetProtocol() == mbs::protocolMbs) && !url.GetHostName().empty()) ||
88  (url.GetProtocol() == mbs::protocolMbss) || (url.GetProtocol() == mbs::protocolMbst)) {
89  DOUT3("Try to create new MBS data input typ %s", typ.c_str());
90 
91  int kind = mbs::NoServer;
92  if (url.GetProtocol()==mbs::protocolMbss) kind = mbs::StreamServer; else
93  if (url.GetProtocol()==mbs::protocolMbst) kind = mbs::TransportServer;
94 
95  int portnum = url.GetPort();
96 
97  if ((kind == mbs::NoServer) && !url.GetFileName().empty())
98  kind = StrToServerKind(url.GetFileName());
99 
100  if (kind == mbs::NoServer) {
103  }
104 
105  if (portnum<=0) portnum = DefualtServerPort(kind);
106 
107  if ((kind == mbs::NoServer) || (portnum==0)) {
108  EOUT("MBS server in url %s not specified correctly", typ.c_str());
109  return 0;
110  }
111 
112  int fd = dabc::SocketThread::StartClient(url.GetHostName(), portnum);
113 
114  if (fd<=0) {
115  DOUT3("Fail connecting to host:%s port:%d", url.GetHostName().c_str(), portnum);
116  return 0;
117  }
118 
119  DOUT0("Connect MBS %s server %s:%d", mbs::ServerKindToStr(kind), url.GetHostName().c_str(), portnum);
120 
121  return new mbs::ClientTransport(fd, kind);
122  }
123 
124  return 0;
125 }
126 
127 dabc::DataOutput* mbs::Factory::CreateDataOutput(const std::string &typ)
128 {
129  DOUT2("Factory::CreateDataOutput typ:%s", typ.c_str());
130 
131  dabc::Url url(typ);
132  if (url.GetProtocol()==mbs::protocolLmd) {
133  DOUT0("LMD output file name %s", url.GetFullName().c_str());
134  return new mbs::LmdOutput(url);
135  }
136 
137  return 0;
138 }
139 
140 dabc::Module* mbs::Factory::CreateModule(const std::string &classname, const std::string &modulename, dabc::Command cmd)
141 {
142  if (classname == "mbs::CombinerModule")
143  return new mbs::CombinerModule(modulename, cmd);
144 
145  if (classname == "mbs::Monitor")
146  return new mbs::Monitor(modulename, cmd);
147 
148  if (classname == "mbs::ReadoutModule")
149  return new mbs::ReadoutModule(modulename, cmd);
150 
151  return dabc::Factory::CreateModule(classname, modulename, cmd);
152 }
153 
Represents command with its arguments.
Definition: Command.h:99
Interface for implementing any kind of data input.
Definition: DataIO.h:61
Interface for implementing any kind of data output.
Definition: DataIO.h:158
Small helper class to correctly instantiate user-specific factories.
Definition: Factory.h:106
virtual Module * CreateTransport(const Reference &port, const std::string &typ, Command cmd)
Factory method to create transport.
Definition: Factory.cxx:61
virtual Module * CreateModule(const std::string &classname, const std::string &modulename, Command cmd)
Factory method to create module.
Definition: Factory.h:67
virtual Reference CreateObject(const std::string &classname, const std::string &objname, Command cmd)
Factory method to create object.
Definition: Factory.h:58
Base for dabc::ModuleSync and dabc::ModuleAsync classes.
Definition: Module.h:42
Reference on the dabc::Port class
Definition: Port.h:195
bool IsOutput() const
Returns true if it is output port.
Definition: Port.h:202
Reference on the arbitrary object
Definition: Reference.h:73
Socket addon for handling connection requests on server side.
Definition: SocketThread.h:285
static int StartClient(const std::string &host, int nport, bool nonblocking=true)
static SocketServerAddon * CreateServerAddon(const std::string &host, int nport, int portmin=-1, int portmax=-1)
Create handle for server-side connection If hostname == 0, any available address will be selected If ...
Uniform Resource Locator interpreter.
Definition: Url.h:33
Client transport for different kinds of MBS server.
Combiner of several MBS inputs - event building.
Factory of MBS classes
Definition: Factory.h:29
virtual dabc::DataInput * CreateDataInput(const std::string &typ)
Factory method to create data input.
Definition: Factory.cxx:75
virtual dabc::Module * CreateModule(const std::string &classname, const std::string &modulename, dabc::Command cmd)
Factory method to create module.
Definition: Factory.cxx:140
virtual dabc::Reference CreateObject(const std::string &classname, const std::string &objname, dabc::Command cmd)
Factory method to create object.
Definition: Factory.cxx:30
virtual dabc::DataOutput * CreateDataOutput(const std::string &typ)
Factory method to create data output.
Definition: Factory.cxx:127
virtual dabc::Module * CreateTransport(const dabc::Reference &port, const std::string &typ, dabc::Command cmd)
Factory method to create transport.
Definition: Factory.cxx:38
Input object for MBS-events generator.
Input for LMD files (lmd:)
Definition: LmdInput.h:31
Output for LMD files (lmd:)
Definition: LmdOutput.h:31
Interface module for MBS monitoring and control.
Definition: Monitor.h:224
Module, used to readout MBS data source and provide data to the user.
Definition: api.h:42
Server transport for different kinds of MBS server.
Text LMD input.
Definition: TextInput.h:33
#define DOUT2(args ...)
Definition: logging.h:170
#define DOUT0(args ...)
Definition: logging.h:156
#define DOUT3(args ...)
Definition: logging.h:176
#define EOUT(args ...)
Definition: logging.h:150
const char * protocolMbst
Definition: MbsTypeDefs.cxx:34
@ NoServer
Definition: MbsTypeDefs.h:353
@ StreamServer
Definition: MbsTypeDefs.h:355
@ TransportServer
Definition: MbsTypeDefs.h:354
const char * protocolMbss
Definition: MbsTypeDefs.cxx:33
const char * protocolMbs
Definition: MbsTypeDefs.cxx:32
int DefualtServerPort(int kind)
int StrToServerKind(const std::string &str)
const char * ServerKindToStr(int kind)
const char * protocolLmd
Definition: MbsTypeDefs.cxx:31
dabc::FactoryPlugin mbsfactory(new mbs::Factory("mbs"))