F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
TcpClientComponentImpl.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title TcpClientComponentImpl.cpp
3 // \author mstarch
4 // \brief cpp file for TcpClientComponentImpl component implementation class
5 //
6 // \copyright
7 // Copyright 2009-2020, by the California Institute of Technology.
8 // ALL RIGHTS RESERVED. United States Government Sponsorship
9 // acknowledged.
10 //
11 // ======================================================================
12 
13 #include <limits>
15 #include <Fw/FPrimeBasicTypes.hpp>
16 #include "Fw/Types/Assert.hpp"
17 
18 
19 namespace Drv {
20 
21 // ----------------------------------------------------------------------
22 // Construction, initialization, and destruction
23 // ----------------------------------------------------------------------
24 
26  : TcpClientComponentBase(compName) {}
27 
29  const U16 port,
30  const U32 send_timeout_seconds,
31  const U32 send_timeout_microseconds,
32  FwSizeType buffer_size) {
33 
34  // Check that ensures the configured buffer size fits within the limits fixed-width type, U32
35  FW_ASSERT(buffer_size <= std::numeric_limits<U32>::max(), static_cast<FwAssertArgType>(buffer_size));
36  m_allocation_size = buffer_size; // Store the buffer size
37  return m_socket.configure(hostname, port, send_timeout_seconds, send_timeout_microseconds);
38 }
39 
41 
42 // ----------------------------------------------------------------------
43 // Implementations for socket read task virtual methods
44 // ----------------------------------------------------------------------
45 
47  return m_socket;
48 }
49 
51  return allocate_out(0, static_cast<U32>(m_allocation_size));
52 }
53 
56  if (status == SOCK_SUCCESS) {
57  recvStatus = ByteStreamStatus::OP_OK;
58  }
59  else if (status == SOCK_NO_DATA_AVAILABLE) {
60  recvStatus = ByteStreamStatus::RECV_NO_DATA;
61  }
62  else {
63  recvStatus = ByteStreamStatus::OTHER_ERROR;
64  }
65  this->recv_out(0, buffer, recvStatus);
66 }
67 
70  this->ready_out(0);
71  }
72 
73 }
74 
75 // ----------------------------------------------------------------------
76 // Handler implementations for user-defined typed input ports
77 // ----------------------------------------------------------------------
78 
79 void TcpClientComponentImpl::send_handler(const FwIndexType portNum, Fw::Buffer& fwBuffer) {
80  Drv::SocketIpStatus status = send(fwBuffer.getData(), fwBuffer.getSize());
81  Drv::ByteStreamStatus returnStatus;
82  switch (status) {
84  returnStatus = ByteStreamStatus::SEND_RETRY;
85  break;
86  case SOCK_SUCCESS:
87  returnStatus = ByteStreamStatus::OP_OK;
88  break;
89  default:
90  returnStatus = ByteStreamStatus::OTHER_ERROR;
91  break;
92  }
93  // Return the buffer and status to the caller
94  this->dataReturnOut_out(0, fwBuffer, returnStatus);
95 }
96 
97 } // end namespace Drv
Fw::Buffer getBuffer() override
returns a buffer to fill with data
Interrupted status for retries.
Definition: IpSocket.hpp:36
PlatformSizeType FwSizeType
~TcpClientComponentImpl()
Destroy the component.
SocketIpStatus send(const U8 *const data, const U32 size)
send data to the IP socket from the given buffer
Auto-generated base for TcpClient component.
SocketIpStatus configure(const char *hostname, const U16 port, const U32 send_timeout_seconds=SOCKET_SEND_TIMEOUT_SECONDS, const U32 send_timeout_microseconds=SOCKET_SEND_TIMEOUT_MICROSECONDS, FwSizeType buffer_size=1024)
Configures the TcpClient settings but does not open the connection.
TcpClientComponentImpl(const char *const compName)
construct the TcpClient component.
U8 * getData() const
Definition: Buffer.cpp:68
void recv_out(FwIndexType portNum, Fw::Buffer &buffer, const Drv::ByteStreamStatus &status)
Invoke output port recv.
Error occurred, retrying may succeed.
void dataReturnOut_out(FwIndexType portNum, Fw::Buffer &buffer, const Drv::ByteStreamStatus &status)
Invoke output port dataReturnOut.
Receive worked, but there was no data.
IpSocket & getSocketHandler() override
returns a reference to the socket handler
Status returned by the send call.
Socket operation successful.
Definition: IpSocket.hpp:30
Operation worked as expected.
void connected() override
called when the IPv4 system has been connected
Fw::Buffer allocate_out(FwIndexType portNum, U32 size)
Invoke output port allocate.
virtual SocketIpStatus configure(const char *hostname, const U16 port, const U32 send_timeout_seconds, const U32 send_timeout_microseconds)
configure the ip socket with host and transmission timeouts
Definition: IpSocket.cpp:53
PlatformIndexType FwIndexType
SocketIpStatus
Status enumeration for socket return values.
Definition: IpSocket.hpp:29
Helper base-class for setting up Berkeley sockets.
Definition: IpSocket.hpp:57
No data available or read operation would block.
Definition: IpSocket.hpp:45
void sendBuffer(Fw::Buffer buffer, SocketIpStatus status) override
sends a buffer to be filled with data
SizeType getSize() const
Definition: Buffer.cpp:72
#define FW_ASSERT(...)
Definition: Assert.hpp:14
bool isConnected_ready_OutputPort(FwIndexType portNum)
void ready_out(FwIndexType portNum)
Invoke output port ready.