FairMQ  1.4.14
C++ Message Queuing Library and Framework
FairMQChannel.h
1 /********************************************************************************
2  * Copyright (C) 2014-2018 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 
9 #ifndef FAIRMQCHANNEL_H_
10 #define FAIRMQCHANNEL_H_
11 
12 #include <FairMQTransportFactory.h>
13 #include <FairMQUnmanagedRegion.h>
14 #include <FairMQSocket.h>
15 #include <fairmq/Transports.h>
16 #include <FairMQLogger.h>
17 #include <FairMQParts.h>
18 #include <fairmq/Properties.h>
19 #include <FairMQMessage.h>
20 
21 #include <string>
22 #include <memory> // unique_ptr, shared_ptr
23 #include <vector>
24 #include <mutex>
25 #include <stdexcept>
26 #include <utility> // std::move
27 #include <cstddef> // size_t
28 #include <cstdint> // int64_t
29 
31 {
32  friend class FairMQDevice;
33 
34  public:
36  FairMQChannel();
37 
40  FairMQChannel(const std::string& name);
41 
46  FairMQChannel(const std::string& type, const std::string& method, const std::string& address);
47 
52  FairMQChannel(const std::string& name, const std::string& type, std::shared_ptr<FairMQTransportFactory> factory);
53 
60  FairMQChannel(const std::string& name, const std::string& type, const std::string& method, const std::string& address, std::shared_ptr<FairMQTransportFactory> factory);
61 
62  FairMQChannel(const std::string& name, int index, const fair::mq::Properties& properties);
63 
66 
68  FairMQChannel(const FairMQChannel&, const std::string& name);
69 
71  FairMQChannel(FairMQChannel&&) = delete;
72 
75 
78 
80  virtual ~FairMQChannel()
81  {
82  // LOG(debug) << "Destroying channel " << fName;
83  }
84 
85  struct ChannelConfigurationError : std::runtime_error { using std::runtime_error::runtime_error; };
86 
87  FairMQSocket& GetSocket() const;
88 
89  bool Bind(const std::string& address)
90  {
91  fMethod = "bind";
92  fAddress = address;
93  return fSocket->Bind(address);
94  }
95 
96  bool Connect(const std::string& address)
97  {
98  fMethod = "connect";
99  fAddress = address;
100  return fSocket->Connect(address);
101  }
102 
105  std::string GetChannelName() const __attribute__((deprecated("Use GetName()"))) { return GetName(); }
106  std::string GetName() const ;
107 
110  std::string GetChannelPrefix() const __attribute__((deprecated("Use GetPrefix()"))) { return GetPrefix(); }
111  std::string GetPrefix() const;
112 
115  std::string GetChannelIndex() const __attribute__((deprecated("Use GetIndex()"))) { return GetIndex(); }
116  std::string GetIndex() const;
117 
120  std::string GetType() const;
121 
124  std::string GetMethod() const;
125 
128  std::string GetAddress() const;
129 
132  std::string GetTransportName() const;
133 
136  fair::mq::Transport GetTransportType() const;
137 
140  int GetSndBufSize() const;
141 
144  int GetRcvBufSize() const;
145 
148  int GetSndKernelSize() const;
149 
152  int GetRcvKernelSize() const;
153 
156  int GetLinger() const;
157 
160  int GetRateLogging() const;
161 
164  int GetPortRangeMin() const;
165 
168  int GetPortRangeMax() const;
169 
172  bool GetAutoBind() const;
173 
176  void UpdateType(const std::string& type);
177 
180  void UpdateMethod(const std::string& method);
181 
184  void UpdateAddress(const std::string& address);
185 
188  void UpdateTransport(const std::string& transport);
189 
192  void UpdateSndBufSize(const int sndBufSize);
193 
196  void UpdateRcvBufSize(const int rcvBufSize);
197 
200  void UpdateSndKernelSize(const int sndKernelSize);
201 
204  void UpdateRcvKernelSize(const int rcvKernelSize);
205 
208  void UpdateLinger(const int duration);
209 
212  void UpdateRateLogging(const int rateLogging);
213 
216  void UpdatePortRangeMin(const int minPort);
217 
220  void UpdatePortRangeMax(const int maxPort);
221 
224  void UpdateAutoBind(const bool autobind);
225 
228  void UpdateChannelName(const std::string& name) __attribute__((deprecated("Use UpdateName()"))) { UpdateName(name); }
229  void UpdateName(const std::string& name);
230 
233  bool IsValid() const;
234 
237  bool ValidateChannel() __attribute__((deprecated("Use Validate()"))) { return Validate(); }
238 
241  bool Validate();
242 
243  void Init();
244 
245  bool ConnectEndpoint(const std::string& endpoint);
246 
247  bool BindEndpoint(std::string& endpoint);
248 
250  void ResetChannel();
251 
256  int Send(FairMQMessagePtr& msg, int sndTimeoutInMs = -1)
257  {
258  CheckSendCompatibility(msg);
259  return fSocket->Send(msg, sndTimeoutInMs);
260  }
261 
266  int Receive(FairMQMessagePtr& msg, int rcvTimeoutInMs = -1)
267  {
268  CheckReceiveCompatibility(msg);
269  return fSocket->Receive(msg, rcvTimeoutInMs);
270  }
271 
276  int64_t Send(std::vector<FairMQMessagePtr>& msgVec, int sndTimeoutInMs = -1)
277  {
278  CheckSendCompatibility(msgVec);
279  return fSocket->Send(msgVec, sndTimeoutInMs);
280  }
281 
286  int64_t Receive(std::vector<FairMQMessagePtr>& msgVec, int rcvTimeoutInMs = -1)
287  {
288  CheckReceiveCompatibility(msgVec);
289  return fSocket->Receive(msgVec, rcvTimeoutInMs);
290  }
291 
296  int64_t Send(FairMQParts& parts, int sndTimeoutInMs = -1)
297  {
298  return Send(parts.fParts, sndTimeoutInMs);
299  }
300 
305  int64_t Receive(FairMQParts& parts, int rcvTimeoutInMs = -1)
306  {
307  return Receive(parts.fParts, rcvTimeoutInMs);
308  }
309 
310  unsigned long GetBytesTx() const { return fSocket->GetBytesTx(); }
311  unsigned long GetBytesRx() const { return fSocket->GetBytesRx(); }
312  unsigned long GetMessagesTx() const { return fSocket->GetMessagesTx(); }
313  unsigned long GetMessagesRx() const { return fSocket->GetMessagesRx(); }
314 
315  auto Transport() -> FairMQTransportFactory*
316  {
317  return fTransportFactory.get();
318  };
319 
320  template<typename... Args>
321  FairMQMessagePtr NewMessage(Args&&... args)
322  {
323  return Transport()->CreateMessage(std::forward<Args>(args)...);
324  }
325 
326  template<typename T>
327  FairMQMessagePtr NewSimpleMessage(const T& data)
328  {
329  return Transport()->NewSimpleMessage(data);
330  }
331 
332  template<typename T>
333  FairMQMessagePtr NewStaticMessage(const T& data)
334  {
335  return Transport()->NewStaticMessage(data);
336  }
337 
338  FairMQUnmanagedRegionPtr NewUnmanagedRegion(const size_t size, FairMQRegionCallback callback = nullptr, const std::string& path = "", int flags = 0)
339  {
340  return Transport()->CreateUnmanagedRegion(size, callback, path, flags);
341  }
342 
343  FairMQUnmanagedRegionPtr NewUnmanagedRegion(const size_t size, const int64_t userFlags, FairMQRegionCallback callback = nullptr, const std::string& path = "", int flags = 0)
344  {
345  return Transport()->CreateUnmanagedRegion(size, userFlags, callback, path, flags);
346  }
347 
348  static constexpr fair::mq::Transport DefaultTransportType = fair::mq::Transport::DEFAULT;
349  static constexpr const char* DefaultTransportName = "default";
350  static constexpr const char* DefaultName = "";
351  static constexpr const char* DefaultType = "unspecified";
352  static constexpr const char* DefaultMethod = "unspecified";
353  static constexpr const char* DefaultAddress = "unspecified";
354  static constexpr int DefaultSndBufSize = 1000;
355  static constexpr int DefaultRcvBufSize = 1000;
356  static constexpr int DefaultSndKernelSize = 0;
357  static constexpr int DefaultRcvKernelSize = 0;
358  static constexpr int DefaultLinger = 500;
359  static constexpr int DefaultRateLogging = 1;
360  static constexpr int DefaultPortRangeMin = 22000;
361  static constexpr int DefaultPortRangeMax = 23000;
362  static constexpr bool DefaultAutoBind = true;
363 
364  private:
365  std::shared_ptr<FairMQTransportFactory> fTransportFactory;
366  fair::mq::Transport fTransportType;
367  std::unique_ptr<FairMQSocket> fSocket;
368 
369  std::string fName;
370  std::string fType;
371  std::string fMethod;
372  std::string fAddress;
373  int fSndBufSize;
374  int fRcvBufSize;
375  int fSndKernelSize;
376  int fRcvKernelSize;
377  int fLinger;
378  int fRateLogging;
379  int fPortRangeMin;
380  int fPortRangeMax;
381  bool fAutoBind;
382 
383  bool fIsValid;
384 
385  bool fMultipart;
386  bool fModified;
387  bool fReset;
388 
389  mutable std::mutex fMtx;
390 
391  void CheckSendCompatibility(FairMQMessagePtr& msg)
392  {
393  if (fTransportType != msg->GetType()) {
394  FairMQMessagePtr msgWrapper(NewMessage(
395  msg->GetData(),
396  msg->GetSize(),
397  [](void* /*data*/, void* _msg) { delete static_cast<FairMQMessage*>(_msg); },
398  msg.get()
399  ));
400  msg.release();
401  msg = move(msgWrapper);
402  }
403  }
404 
405  void CheckSendCompatibility(std::vector<FairMQMessagePtr>& msgVec)
406  {
407  for (auto& msg : msgVec) {
408  if (fTransportType != msg->GetType()) {
409 
410  FairMQMessagePtr msgWrapper(NewMessage(
411  msg->GetData(),
412  msg->GetSize(),
413  [](void* /*data*/, void* _msg) { delete static_cast<FairMQMessage*>(_msg); },
414  msg.get()
415  ));
416  msg.release();
417  msg = move(msgWrapper);
418  }
419  }
420  }
421 
422  void CheckReceiveCompatibility(FairMQMessagePtr& msg)
423  {
424  if (fTransportType != msg->GetType()) {
425  FairMQMessagePtr newMsg(NewMessage());
426  msg = move(newMsg);
427  }
428  }
429 
430  void CheckReceiveCompatibility(std::vector<FairMQMessagePtr>& msgVec)
431  {
432  for (auto& msg : msgVec) {
433  if (fTransportType != msg->GetType()) {
434 
435  FairMQMessagePtr newMsg(NewMessage());
436  msg = move(newMsg);
437  }
438  }
439  }
440 
441  void InitTransport(std::shared_ptr<FairMQTransportFactory> factory)
442  {
443  fTransportFactory = factory;
444  fTransportType = factory->GetType();
445  }
446 
447  auto SetModified(const bool modified) -> void;
448 };
449 
450 #endif /* FAIRMQCHANNEL_H_ */
int GetPortRangeMax() const
Definition: FairMQChannel.cxx:309
int GetSndBufSize() const
Definition: FairMQChannel.cxx:246
std::string GetTransportName() const
Definition: FairMQChannel.cxx:227
int Send(FairMQMessagePtr &msg, int sndTimeoutInMs=-1)
Definition: FairMQChannel.h:256
void UpdateAutoBind(const bool autobind)
Definition: FairMQChannel.cxx:459
bool GetAutoBind() const
Definition: FairMQChannel.cxx:318
std::string GetType() const
Definition: FairMQChannel.cxx:200
int Receive(FairMQMessagePtr &msg, int rcvTimeoutInMs=-1)
Definition: FairMQChannel.h:266
fair::mq::Transport GetTransportType() const
Definition: FairMQChannel.cxx:236
void UpdateChannelName(const std::string &name) __attribute__((deprecated("Use UpdateName()")))
Definition: FairMQChannel.h:228
void UpdatePortRangeMin(const int minPort)
Definition: FairMQChannel.cxx:437
int64_t Send(std::vector< FairMQMessagePtr > &msgVec, int sndTimeoutInMs=-1)
Definition: FairMQChannel.h:276
int GetRateLogging() const
Definition: FairMQChannel.cxx:291
std::string GetAddress() const
Definition: FairMQChannel.cxx:218
int GetRcvKernelSize() const
Definition: FairMQChannel.cxx:273
Definition: FairMQTransportFactory.h:30
int GetPortRangeMin() const
Definition: FairMQChannel.cxx:300
void UpdateRcvBufSize(const int rcvBufSize)
Definition: FairMQChannel.cxx:382
FairMQChannel & operator=(const FairMQChannel &)
Assignment operator.
Definition: FairMQChannel.cxx:134
Definition: FairMQChannel.h:30
int GetSndKernelSize() const
Definition: FairMQChannel.cxx:264
bool IsValid() const
Definition: FairMQChannel.cxx:490
bool ValidateChannel() __attribute__((deprecated("Use Validate()")))
Definition: FairMQChannel.h:237
void UpdateRcvKernelSize(const int rcvKernelSize)
Definition: FairMQChannel.cxx:404
void UpdateAddress(const std::string &address)
Definition: FairMQChannel.cxx:349
void UpdateTransport(const std::string &transport)
Definition: FairMQChannel.cxx:360
int64_t Receive(FairMQParts &parts, int rcvTimeoutInMs=-1)
Definition: FairMQChannel.h:305
int64_t Receive(std::vector< FairMQMessagePtr > &msgVec, int rcvTimeoutInMs=-1)
Definition: FairMQChannel.h:286
Definition: FairMQSocket.h:19
std::string GetChannelIndex() const __attribute__((deprecated("Use GetIndex()")))
Definition: FairMQChannel.h:115
void UpdateRateLogging(const int rateLogging)
Definition: FairMQChannel.cxx:426
int GetLinger() const
Definition: FairMQChannel.cxx:282
void UpdateSndBufSize(const int sndBufSize)
Definition: FairMQChannel.cxx:371
bool Validate()
Definition: FairMQChannel.cxx:499
void UpdateMethod(const std::string &method)
Definition: FairMQChannel.cxx:338
std::string GetMethod() const
Definition: FairMQChannel.cxx:209
FairMQParts is a lightweight convenience wrapper around a vector of unique pointers to FairMQMessage...
Definition: FairMQParts.h:20
int GetRcvBufSize() const
Definition: FairMQChannel.cxx:255
void UpdateLinger(const int duration)
Definition: FairMQChannel.cxx:415
void ResetChannel()
Resets the channel (requires validation to be used again).
Definition: FairMQChannel.cxx:716
Definition: FairMQDevice.h:53
void UpdatePortRangeMax(const int maxPort)
Definition: FairMQChannel.cxx:448
void UpdateType(const std::string &type)
Definition: FairMQChannel.cxx:327
Definition: FairMQMessage.h:20
void UpdateSndKernelSize(const int sndKernelSize)
Definition: FairMQChannel.cxx:393
FairMQChannel()
Default constructor.
Definition: FairMQChannel.cxx:47
std::string GetChannelName() const __attribute__((deprecated("Use GetName()")))
Definition: FairMQChannel.h:105
std::string GetChannelPrefix() const __attribute__((deprecated("Use GetPrefix()")))
Definition: FairMQChannel.h:110
virtual ~FairMQChannel()
Destructor.
Definition: FairMQChannel.h:80
int64_t Send(FairMQParts &parts, int sndTimeoutInMs=-1)
Definition: FairMQChannel.h:296
Definition: FairMQChannel.h:85

privacy