IpbusMock  1.0.0
Library which integrates IPbus use in Phoenix
Loading...
Searching...
No Matches
PIpBusSocket Class Reference

PhoenixSocket API bridge to IpBus backend. More...

#include <PIpBusBackend.h>

+ Collaboration diagram for PIpBusSocket:

Public Types

typedef UaMessage Message
 Define the type of message used by the PAbstractSocketManager.
 
typedef PIpBusParam Param
 Define the type of extra parameters which can be used to create a Socket.
 

Public Member Functions

void close ()
 Close the socket.
 
bool createClientSocket (const Param &extraParam)
 Create a client socket.
 
bool createServerSocket (const Param &extraParam)
 Create a server socket.
 
bool isConnected () const
 
 PIpBusSocket ()
 Default constructor of PIpBusSocket.
 
template<typename T>
PRecvStatus::PRecvStatus recvData (T &data, PRecvFlag::PRecvFlag flag=PRecvFlag::BLOCK)
 Recieved data with the socket.
 
PRecvStatus::PRecvStatus recvMsg (Message &msg, PRecvFlag::PRecvFlag flag=PRecvFlag::BLOCK)
 Receive a message from the given socket.
 
template<typename T>
PSendStatus::PSendStatus sendData (const T &data, PSendFlag::PSendFlag flag=PSendFlag::BLOCK)
 Send data with the socket.
 
PSendStatus::PSendStatus sendMsg (Message &msg, PSendFlag::PSendFlag flag=PSendFlag::BLOCK)
 
virtual ~PIpBusSocket ()
 Destructor of PIpBusSocket.
 

Private Attributes

IpBusImplp_uaSocket
 UA socket.
 

Detailed Description

PhoenixSocket API bridge to IpBus backend.

Definition at line 28 of file PIpBusBackend.h.

Member Typedef Documentation

◆ Message

Define the type of message used by the PAbstractSocketManager.

Definition at line 31 of file PIpBusBackend.h.

◆ Param

Define the type of extra parameters which can be used to create a Socket.

Definition at line 33 of file PIpBusBackend.h.

Constructor & Destructor Documentation

◆ PIpBusSocket()

PIpBusSocket::PIpBusSocket ( )

Default constructor of PIpBusSocket.

Definition at line 11 of file PIpBusBackend.cpp.

12: p_uaSocket(nullptr)
13{
14
15}
IpBusImpl * p_uaSocket
UA socket.

References p_uaSocket.

◆ ~PIpBusSocket()

PIpBusSocket::~PIpBusSocket ( )
virtual

Destructor of PIpBusSocket.

Definition at line 18 of file PIpBusBackend.cpp.

19{
20 close();
21}
void close()
Close the socket.

References close().

+ Here is the call graph for this function:

Member Function Documentation

◆ close()

void PIpBusSocket::close ( )

Close the socket.

Definition at line 47 of file PIpBusBackend.cpp.

47 {
48 if(p_uaSocket != nullptr){
49 p_uaSocket->close();
50 p_uaSocket = nullptr;
51 }
52}

References p_uaSocket.

Referenced by ~PIpBusSocket().

+ Here is the caller graph for this function:

◆ createClientSocket()

bool PIpBusSocket::createClientSocket ( const Param & extraParam)

Create a client socket.

Parameters
[out]extraParam: extra customisable parameters for the creation of the socket
Returns
true if the socket has been created, false otherwise

Definition at line 28 of file PIpBusBackend.cpp.

28 {
29 p_uaSocket = IpBusImpl::pipbus_createSocket(extraParam.connectionFilePath, extraParam.connectionId);
30 return p_uaSocket != nullptr;
31}
static IpBusImpl * pipbus_createSocket(const std::string &connectionFilePath, const std::string &connectionId)
Creates an IpBusImpl instance.
Definition ipbrw.cpp:117

References PIpBusParam::connectionFilePath, PIpBusParam::connectionId, p_uaSocket, and IpBusImpl::pipbus_createSocket().

Referenced by PIpBusBackend::createClientSocket(), and createServerSocket().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ createServerSocket()

bool PIpBusSocket::createServerSocket ( const Param & extraParam)

Create a server socket.

Parameters
[out]extraParam: extra customisable parameters for the creation of the socket
Returns
true if the socket has been created, false otherwise

Definition at line 38 of file PIpBusBackend.cpp.

38 {
39 return createClientSocket(extraParam);
40}
bool createClientSocket(const Param &extraParam)
Create a client socket.

References createClientSocket().

Referenced by PIpBusBackend::createServerSocket().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isConnected()

bool PIpBusSocket::isConnected ( ) const

Definition at line 42 of file PIpBusBackend.cpp.

42 {
43 return p_uaSocket != nullptr;
44}

References p_uaSocket.

◆ recvData()

template<typename T>
PRecvStatus::PRecvStatus PIpBusSocket::recvData ( T & data,
PRecvFlag::PRecvFlag flag = PRecvFlag::BLOCK )

Recieved data with the socket.

Parameters
[out]data: data to be recieved with the socket
flag: recieving flag (BLOCK, NON_BLOCK)
Returns
status of the recv

Definition at line 71 of file PIpBusBackend_impl.h.

71 {
72 PRecvStatus::PRecvStatus recvStatus = PRecvStatus::OK;
73 Message msg;
74 recvStatus = recvMsg(msg, flag);
75 if(recvStatus == PRecvStatus::OK){
76 //If the message is empty we cannot initialise the given data, so, this is an error
77 if(msg.size() != 0lu){
78 DataStreamIter iter = (DataStreamIter)msg.dataStreamMsg.data();
79 if(!data_message_load<T>(iter, data)){
80 return PRecvStatus::CANNOT_DESERIALIZE_DATA;
81 }
82 }else{
83 return PRecvStatus::NO_MESSAGE_RECEIVED;
84 }
85 }
86
87 return recvStatus;
88}
PRecvStatus::PRecvStatus recvMsg(Message &msg, PRecvFlag::PRecvFlag flag=PRecvFlag::BLOCK)
Receive a message from the given socket.
UaMessage Message
Define the type of message used by the PAbstractSocketManager.

References UaMessage::dataStreamMsg, recvMsg(), and UaMessage::size().

+ Here is the call graph for this function:

◆ recvMsg()

PRecvStatus::PRecvStatus PIpBusSocket::recvMsg ( PIpBusSocket::Message & msg,
PRecvFlag::PRecvFlag flag = PRecvFlag::BLOCK )

Receive a message from the given socket.

Parameters
msgthe message to be received.
flagreceive flag (BLOCK, NON_BLOCK)
Returns
RecvStatus indicating the result of the receive operation or throw an error. THE BEHAVIOR OF THIS FUNCTION NEEDS TO BE DISCUSSED TO KNOW WHAT KIND OF ERROR WE WANT TO RAISE

Definition at line 190 of file PIpBusBackend.cpp.

190 {
191 if(p_uaSocket == nullptr){
192 return PRecvStatus::BROKEN_SOCKET;
193 }
194
195 try {
196 bool success = false;
197 if(msg.sizeValue > 1){
198 success = p_uaSocket->readBlock(msg.nodeId, *msg.value.get(), msg.sizeValue);
199 }
200 else if(msg.sizeValue == 1){
201 uint32_t readValue = 0;
202 success = p_uaSocket->read(msg.nodeId, readValue);
203 if(success) {
204 msg.value.get()->clear();
205 msg.value.get()->push_back(readValue);
206 }
207 }
208 else {
209 // Invalid size (0)
210 return PRecvStatus::NO_MESSAGE_RECEIVED;
211 }
212
213 return success ? PRecvStatus::OK : PRecvStatus::BROKEN_BACKEND;
214 }
215 catch (const uhal::exception::ReadAccessDenied& e) {
216 // Read access denied for this node
217 return PRecvStatus::BROKEN_BACKEND;
218 }
219 catch (const uhal::exception::BulkTransferOnSingleRegister& e) {
220 // Attempted block transfer on a single register
221 return PRecvStatus::BROKEN_BACKEND;
222 }
223 catch (const uhal::exception::BulkTransferRequestedTooLarge& e) {
224 // Block transfer too large
225 return PRecvStatus::BROKEN_BACKEND;
226 }
227 catch (const uhal::exception::NoBranchFoundWithGivenUID& e) {
228 // Node ID not found
229 return PRecvStatus::BROKEN_BACKEND;
230 }
231 catch (const std::out_of_range& e) {
232 // Out of range error accessing vector
233 return PRecvStatus::CANNOT_DESERIALIZE_DATA;
234 }
235 catch (const std::bad_alloc& e) {
236 // Memory allocation problem
237 return PRecvStatus::BROKEN_BACKEND;
238 }
239 catch (...) {
240 // Any other non-standard exception
241 return PRecvStatus::BROKEN_BACKEND;
242 }
243}
std::string nodeId
Definition uaMessage.h:15
std::unique_ptr< std::vector< uint32_t > > value
Definition uaMessage.h:17
uint32_t sizeValue
Definition uaMessage.h:16

References UaMessage::nodeId, p_uaSocket, UaMessage::sizeValue, and UaMessage::value.

Referenced by recvData().

+ Here is the caller graph for this function:

◆ sendData()

template<typename T>
PSendStatus::PSendStatus PIpBusSocket::sendData ( const T & data,
PSendFlag::PSendFlag flag = PSendFlag::BLOCK )

Send data with the socket.

Parameters
data: data to be sent with the socket
flag: sending flag (BLOCK, NON_BLOCK)
Returns
status of the send

Definition at line 53 of file PIpBusBackend_impl.h.

53 {
54 size_t dataSize(data_size<T>(data));
55 Message msg;
56 msg.dataStreamMsg.resize(dataSize);
57 DataStreamIter iter = (DataStreamIter)msg.dataStreamMsg.data();
58 if(data_message_save<T>(iter, data)){ //Save the message
59 return sendMsg(msg, flag);
60 }else{
61 return PSendStatus::CANNOT_SERIALIZE_DATA;
62 }
63}
PSendStatus::PSendStatus sendMsg(Message &msg, PSendFlag::PSendFlag flag=PSendFlag::BLOCK)

References UaMessage::dataStreamMsg, and sendMsg().

+ Here is the call graph for this function:

◆ sendMsg()

PSendStatus::PSendStatus PIpBusSocket::sendMsg ( PIpBusSocket::Message & msg,
PSendFlag::PSendFlag flag = PSendFlag::BLOCK )

Send a message on the given socket.

Parameters
msgMessage to be sent.
flagFlags to be used for sending the message (BLOCK, NON_BLOCK, etc.).
Returns
PSendStatus indicating the result of the send operation. THE BEHVAIOR OF THIS FUNCTION NEEDS TO BE DISCUSSED TO KNOW WHAT KIND OF ERROR WE WANT TO RAISE

Definition at line 136 of file PIpBusBackend.cpp.

136 {
137 if(p_uaSocket == nullptr){
138 return PSendStatus::BROKEN_SOCKET;
139 }
140 try {
141 bool success;
142 if(msg.value.get()->size() > 1){
143 success = p_uaSocket->writeBlock(msg.nodeId, *msg.value.get());
144 }
145 else if(msg.value.get()->size() == 1){
146 success = p_uaSocket->write(msg.nodeId, msg.value.get()->at(0));
147 }
148 else {
149 // Empty value vector
150 return PSendStatus::CANNOT_SERIALIZE_DATA;
151 }
152
153 return success ? PSendStatus::OK : PSendStatus::BROKEN_BACKEND;
154 }
155 catch (const uhal::exception::WriteAccessDenied& e) {
156 // Write access denied for this node
157 return PSendStatus::BROKEN_BACKEND;
158 }
159 catch (const uhal::exception::BulkTransferOnSingleRegister& e) {
160 // Attempted block transfer on a single register
161 return PSendStatus::BROKEN_BACKEND;
162 }
163 catch (const uhal::exception::BulkTransferRequestedTooLarge& e) {
164 // Block transfer too large
165 return PSendStatus::BROKEN_BACKEND;
166 }
167 catch (const uhal::exception::NoBranchFoundWithGivenUID& e) {
168 // Node ID not found
169 return PSendStatus::BROKEN_BACKEND;
170 }
171 catch (const std::out_of_range& e) {
172 // Out of range error accessing vector
173 return PSendStatus::CANNOT_SERIALIZE_DATA;
174 }
175 catch (const std::bad_alloc& e) {
176 // Memory allocation problem
177 return PSendStatus::BROKEN_BACKEND;
178 }
179 catch (...) {
180 // Any other non-standard exception
181 return PSendStatus::BROKEN_BACKEND;
182 }
183}

References UaMessage::nodeId, p_uaSocket, and UaMessage::value.

Referenced by sendData().

+ Here is the caller graph for this function:

Member Data Documentation

◆ p_uaSocket

IpBusImpl* PIpBusSocket::p_uaSocket
private

UA socket.

Definition at line 54 of file PIpBusBackend.h.

Referenced by close(), createClientSocket(), isConnected(), PIpBusSocket(), recvMsg(), and sendMsg().


The documentation for this class was generated from the following files: