RequestQueue.cpp
Go to the documentation of this file.
1 #include "RequestQueue.h"
2 
3 #include <boost/lockfree/policies.hpp>
4 #include <boost/lockfree/spsc_queue.hpp>
6 
9 
15 
17 {
19  {
20  static constexpr size_t queueSize = 100;
21 
22  boost::lockfree::spsc_queue<std::shared_ptr<RequestBase>,
23  boost::lockfree::capacity<queueSize>>
25  };
26 
27 
28  RequestQueue::RequestQueue() : pimpl(std::make_unique<QueueImpl>())
29  {
30  }
31 
32  RequestQueue::~RequestQueue() = default; // Need for std::unique_ptr to incomplete type
33 
34 
37  {
38  swap(pimpl, rhs.pimpl);
39  return *this;
40  }
41 
42  std::shared_ptr<RequestBase>
44  {
45  std::shared_ptr<RequestBase> request;
46  if (!pimpl->queue.pop(request))
47  {
48  return nullptr;
49  }
50  return request;
51  }
52 
53  void
54  RequestQueue::postReply(std::shared_ptr<RequestBase>&& request)
55  {
56  request->setProcessed();
57  requestCV.notify_all();
58  }
59 
60  bool
61  RequestQueue::postRequest(const std::shared_ptr<RequestBase>& request)
62  {
63  static std::mutex mtx;
64  std::unique_lock<std::mutex> lck(mtx);
65 
66  if(pimpl->queue.write_available() < 10)
67  {
68  GENERAL_ERROR("Queue is running full.");
69  }
70 
71 
72  pimpl->queue.push(request);
73  requestCV.wait(lck,
74  [&, request]() { return request->isProcessed() || request->hasFailed(); });
75  return !request->hasFailed();
76  }
77 
78 
79  bool
81  int* buflen,
82  unsigned char* buf,
83  bool completeAccess)
84  {
85  std::shared_ptr<SDOUpdateRequest> request =
86  std::make_shared<SDOUpdateRequest>(sdoIdentifier, buflen, buf, true, completeAccess);
87  return postSDOUpdateRequest(request);
88  }
89 
90  bool
92  int buflen,
93  unsigned char* buf,
94  bool completeAccess)
95  {
96  std::shared_ptr<SDOUpdateRequest> request =
97  std::make_shared<SDOUpdateRequest>(sdoIdentifier, &buflen, buf, false, completeAccess);
98  return postSDOUpdateRequest(request);
99  }
100 
101 
102  bool
103  RequestQueue::postChangeStateRequest(std::uint16_t slaveIndex,
104  EtherCATState state,
105  bool validate,
106  EtherCATState* actualState)
107  {
108  std::shared_ptr<ChangeStateRequest> request =
109  std::make_shared<ChangeStateRequest>(slaveIndex, state, validate, actualState);
110  return postRequest(request);
111  }
112 
113 
114  bool
116  {
117  auto request = std::make_shared<ReadStatesRequest>(state);
118  return postRequest(request);
119  }
120 
121  bool
123  {
124  auto request = std::make_shared<RegisterResetRequest>(slaveIndex);
125  return postRequest(request);
126  }
127 
128  bool
129  RequestQueue::postRegisterReadRequest(std::vector<RegisterDataList>& registerData)
130  {
131  auto request = std::make_shared<RegisterReadRequest>(&registerData);
132  return postRequest(request);
133  }
134 
135  bool
137  std::uint16_t amountFramesToRead)
138  {
139  auto request = std::make_shared<RegisterReadRequest>(frames, amountFramesToRead);
140  return postRequest(request);
141  }
142 
143  bool
144  RequestQueue::postSDOUpdateRequest(const std::shared_ptr<SDOUpdateRequest>& request)
145  {
146  return postRequest(request);
147  }
148 } // namespace armarx::control::ethercat
armarx::control::ethercat::RequestQueue::postSDOWriteRequest
bool postSDOWriteRequest(SDOIdentifier sdoIdentifier, int buflen, unsigned char *buf, bool completeAccess)
Definition: RequestQueue.cpp:91
armarx::control::ethercat::RequestQueue::operator=
RequestQueue & operator=(RequestQueue rhs)
Definition: RequestQueue.cpp:36
armarx::control::ethercat::EtherCATState
This class is a wrapper around an enum containing the different EtherCAT states.
Definition: EtherCATState.h:25
EtherCATState.h
ChangeStateRequest.h
armarx::control::ethercat::RequestQueue::QueueImpl::queueSize
static constexpr size_t queueSize
Definition: RequestQueue.cpp:20
armarx::armem::client::util::swap
void swap(SubscriptionHandle &first, SubscriptionHandle &second)
Definition: SubscriptionHandle.cpp:66
RegisterReadRequest.h
armarx::control::ethercat::RequestQueue::QueueImpl
Definition: RequestQueue.cpp:18
ReadStatesRequest.h
RequestQueue.h
armarx::control::ethercat::RequestQueue::postReply
void postReply(std::shared_ptr< RequestBase > &&request)
Definition: RequestQueue.cpp:54
SDOUpdateRequest.h
armarx::control::ethercat::RequestQueue
Brief description of class RequestQueue.
Definition: RequestQueue.h:51
armarx::control::ethercat::RequestQueue::postRegisterResetRequest
bool postRegisterResetRequest(std::uint16_t slaveIndex)
Definition: RequestQueue.cpp:122
armarx::control::ethercat::RequestQueue::postReadStatesRequest
bool postReadStatesRequest(EtherCATState *state)
Definition: RequestQueue.cpp:115
armarx::control::ethercat::EtherCATFrameList
The EtherCATFrameList struct holds a list of EtherCAT frames that can be scheduled in round-robin-sty...
Definition: EtherCATFrame.h:124
armarx::control::ethercat::RequestQueue::RequestQueue
RequestQueue()
Definition: RequestQueue.cpp:28
armarx::control::ethercat
Definition: Bus.cpp:24
armarx::control::ethercat::RequestQueue::getNextRequest
std::shared_ptr< RequestBase > getNextRequest()
Definition: RequestQueue.cpp:43
armarx::control::ethercat::RequestQueue::QueueImpl::queue
boost::lockfree::spsc_queue< std::shared_ptr< RequestBase >, boost::lockfree::capacity< queueSize > > queue
Definition: RequestQueue.cpp:24
ErrorReporting.h
armarx::control::ethercat::RequestQueue::~RequestQueue
~RequestQueue()
armarx::navigation::client::validate
void validate(const std::vector< WaypointTarget > &path)
Definition: ice_conversions.h:70
armarx::control::ethercat::RequestQueue::postChangeStateRequest
bool postChangeStateRequest(std::uint16_t slaveIndex, EtherCATState state, bool validate, EtherCATState *actualState)
Definition: RequestQueue.cpp:103
std
Definition: Application.h:66
GENERAL_ERROR
#define GENERAL_ERROR(...)
Definition: ErrorReporting.h:255
ControlThreadOutputBuffer.h
armarx::control::ethercat::RequestQueue::postRegisterReadRequest
bool postRegisterReadRequest(std::vector< RegisterDataList > &registerData)
Definition: RequestQueue.cpp:129
armarx::control::ethercat::SDOIdentifier
Definition: SDOUpdateRequest.h:9
armarx::control::ethercat::RequestQueue::postSDOReadRequest
bool postSDOReadRequest(SDOIdentifier sdoIdentifier, int *buflen, unsigned char *buf, bool completeAccess)
Definition: RequestQueue.cpp:80
RegisterResetRequest.h