DABC (Data Acquisition Backbone Core)  2.9.9
SocketFactory.cxx
Go to the documentation of this file.
1 // $Id: SocketFactory.cxx 4476 2020-04-15 14:12:38Z 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 "dabc/SocketFactory.h"
17 
18 #include <sys/types.h>
19 #include <unistd.h>
20 
21 #include "dabc/SocketDevice.h"
22 #include "dabc/SocketTransport.h"
24 #include "dabc/Url.h"
25 
26 // as long as sockets integrated into libDabcBase, SocketFactory should be created directly by manager
27 // dabc::FactoryPlugin socketfactory(new dabc::SocketFactory("sockets"));
28 
29 
31 {
32 }
33 
34 
35 dabc::Reference dabc::SocketFactory::CreateObject(const std::string &classname, const std::string &objname, Command cmd)
36 {
37  if (classname == "SocketCommandChannel") {
38 
39  dabc::SocketServerAddon* addon = nullptr;
40 
41  if (cmd.GetBool("WithServer", true)) {
42  std::string host = cmd.GetStr("ServerHost");
43  int nport = cmd.GetInt("ServerPort");
44  if (nport <= 0) nport = dabc::defaultDabcPort;
45 
46  addon = dabc::SocketThread::CreateServerAddon(host, nport);
47  if (addon == 0) {
48  EOUT("Cannot open cmd socket on port %d", nport);
49  return 0;
50  }
51  addon->SetDeliverEventsToWorker(true);
52 
53  cmd.SetStr("localaddr", addon->ServerId());
54  DOUT0("Start DABC server on %s", addon->ServerId().c_str());
55  }
56 
57  return new SocketCommandChannel(objname, addon, cmd);
58  }
59 
60  return 0;
61 }
62 
63 
64 dabc::Device* dabc::SocketFactory::CreateDevice(const std::string &classname,
65  const std::string &devname, Command cmd)
66 {
67  if (classname == dabc::typeSocketDevice)
68  return new SocketDevice(devname, cmd);
69 
70  return 0;
71 }
72 
73 dabc::Reference dabc::SocketFactory::CreateThread(Reference parent, const std::string &classname, const std::string &thrdname, const std::string &thrddev, Command cmd)
74 {
75  dabc::Thread* thrd = 0;
76 
77  if (classname == typeSocketThread)
78  thrd = new SocketThread(parent, thrdname, cmd);
79 
80  return Reference(thrd);
81 }
82 
83 dabc::Module* dabc::SocketFactory::CreateTransport(const Reference& ref, const std::string &typ, Command cmd)
84 {
85  dabc::PortRef port = ref;
86 
87  dabc::Url url(typ);
88  if (url.IsValid() && (url.GetProtocol()=="udp") && !port.null()) {
89 
90  int fhandle = dabc::SocketThread::CreateUdp();
91  if (fhandle<0) return 0;
92 
93  SocketNetworkInetrface* addon = 0;
94 
95  if (port.IsOutput()) {
96 
97  addon = new SocketNetworkInetrface(fhandle, true);
98 
99  addon->SetSendAddr(url.GetHostName(), url.GetPort());
100 
101  } else {
102 
103  int udp_port = dabc::SocketThread::BindUdp(fhandle, url.GetPort());
104 
105  if (udp_port<=0) {
107  return 0;
108  }
109 
110  bool mcast = url.HasOption("mcast");
111 
112  if (mcast && !dabc::SocketThread::AttachMulticast(fhandle, url.GetHostName())) {
114  return 0;
115  }
116 
117  addon = new SocketNetworkInetrface(fhandle, true);
118 
119  if (mcast) addon->SetMCastAddr(url.GetHostName());
120  }
121 
122 
123  PortRef inpport, outport;
124 
125  if (port.IsOutput()) {
126  outport << port;
127  } else {
128  inpport << port;
129  }
130 
131  return new dabc::NetworkTransport(dabc::Command(), inpport, outport, false, addon);
132  }
133 
134  return dabc::Factory::CreateTransport(port, typ, cmd);
135 }
136 
137 
Represents command with its arguments.
Definition: Command.h:99
bool SetStr(const std::string &name, const char *value)
Definition: Command.h:134
std::string GetStr(const std::string &name, const std::string &dflt="") const
Definition: Command.h:136
bool GetBool(const std::string &name, bool dflt=false) const
Definition: Command.h:142
int GetInt(const std::string &name, int dflt=0) const
Definition: Command.h:139
Base class for device implementation.
Definition: Device.h:43
virtual Module * CreateTransport(const Reference &port, const std::string &typ, Command cmd)
Factory method to create transport.
Definition: Factory.cxx:61
Base for dabc::ModuleSync and dabc::ModuleAsync classes.
Definition: Module.h:42
Network transport.
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
bool null() const
Returns true if reference contains nullptr.
Definition: Reference.h:151
void SetDeliverEventsToWorker(bool on=true)
Definition: SocketThread.h:121
Provides command channel to the dabc process.
Device for establishing socket connections
Definition: SocketDevice.h:33
virtual Reference CreateObject(const std::string &classname, const std::string &objname, Command cmd)
Factory method to create object.
virtual Reference CreateThread(Reference parent, const std::string &classname, const std::string &thrdname, const std::string &thrddev, Command cmd)
Factory method to create thread.
virtual Module * CreateTransport(const Reference &port, const std::string &typ, Command cmd)
Factory method to create transport.
virtual Device * CreateDevice(const std::string &classname, const std::string &devname, Command cmd)
Factory method to create device.
virtual void Initialize()
Method called by the manager during application start.
void SetSendAddr(const std::string &host="", int port=0)
Set destination address for all send operations,.
Specific implementation of network transport for socket.
void SetMCastAddr(const std::string addr)
Set mcast address, required to correctly close socket.
Socket addon for handling connection requests on server side.
Definition: SocketThread.h:285
std::string ServerId()
Definition: SocketThread.h:308
Special thread class for handling sockets.
Definition: SocketThread.h:355
static void CloseUdp(int fd)
Close datagram (udp) socket.
static int CreateUdp()
Create datagram (udp) socket.
static bool AttachMulticast(int handle, const std::string &addr)
Attach datagram socket to multicast group to make receiving.
static int BindUdp(int fd, int nport, int portmin=-1, int portmax=-1)
Bind UDP socket to specified port.
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 ...
Represent thread functionality.
Definition: Thread.h:109
Uniform Resource Locator interpreter.
Definition: Url.h:33
bool HasOption(const std::string &optname) const
Definition: Url.h:70
std::string GetHostName() const
Definition: Url.h:58
std::string GetProtocol() const
Definition: Url.h:57
int GetPort() const
Definition: Url.h:59
bool IsValid() const
Definition: Url.h:55
#define DOUT0(args ...)
Definition: logging.h:156
#define EOUT(args ...)
Definition: logging.h:150
const char * typeSocketThread
Definition: Object.cxx:80
const char * typeSocketDevice
Definition: Object.cxx:79
@ defaultDabcPort
Definition: ConfigBase.h:92