F´ Flight Software - C/C++ Documentation  NASA-v1.6.0
A framework for building embedded system applications to NASA flight quality standards.
TcpServerComponentImpl.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title TcpServerComponentImpl.cpp
3 // \author mstarch
4 // \brief cpp file for TcpServerComponentImpl 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 
14 #include "Fw/Types/BasicTypes.hpp"
15 #include "Fw/Types/Assert.hpp"
16 
17 namespace Drv {
18 
19 // ----------------------------------------------------------------------
20 // Construction, initialization, and destruction
21 // ----------------------------------------------------------------------
22 
24  : ByteStreamDriverModelComponentBase(compName),
25  SocketReadTask() {}
26 
28  ByteStreamDriverModelComponentBase::init(instance);
29 }
30 
32  const U16 port,
33  const U32 send_timeout_seconds,
34  const U32 send_timeout_microseconds) {
35  return m_socket.configure(hostname, port, send_timeout_seconds, send_timeout_microseconds);
36 }
37 
39 
41  return this->m_socket.startup();
42 }
43 
45  this->m_socket.shutdown();
46 }
47 
48 // ----------------------------------------------------------------------
49 // Implementations for socket read task virtual methods
50 // ----------------------------------------------------------------------
51 
53  return m_socket;
54 }
55 
57  return allocate_out(0, 1024);
58 }
59 
61  Drv::RecvStatus recvStatus = (status == SOCK_SUCCESS) ? RecvStatus::RECV_OK : RecvStatus::RECV_ERROR;
62  this->recv_out(0, buffer, recvStatus);
63 }
64 
66  if (isConnected_ready_OutputPort(0)) {
67  this->ready_out(0);
68  }
69 
70 }
71 
72 // ----------------------------------------------------------------------
73 // Handler implementations for user-defined typed input ports
74 // ----------------------------------------------------------------------
75 
76 Drv::SendStatus TcpServerComponentImpl::send_handler(const NATIVE_INT_TYPE portNum, Fw::Buffer& fwBuffer) {
77  Drv::SocketIpStatus status = m_socket.send(fwBuffer.getData(), fwBuffer.getSize());
78  // Always return the buffer
79  deallocate_out(0, fwBuffer);
80  if ((status == SOCK_DISCONNECTED) || (status == SOCK_INTERRUPTED_TRY_AGAIN)) {
81  return SendStatus::SEND_RETRY;
82  } else if (status != SOCK_SUCCESS) {
83  return SendStatus::SEND_ERROR;
84  }
85  return SendStatus::SEND_OK;
86 }
87 
88 Drv::PollStatus TcpServerComponentImpl::poll_handler(const NATIVE_INT_TYPE portNum, Fw::Buffer& fwBuffer) {
89  FW_ASSERT(0); // It is an error to call this handler on IP drivers
90  return PollStatus::POLL_ERROR;
91 }
92 
93 } // end namespace Drv
Drv::TcpServerComponentImpl::TcpServerComponentImpl
TcpServerComponentImpl(const char *const compName)
construct the TcpClient component.
Definition: TcpServerComponentImpl.cpp:23
Drv::TcpServerComponentImpl::~TcpServerComponentImpl
~TcpServerComponentImpl()
Destroy the component.
Definition: TcpServerComponentImpl.cpp:38
Drv::SOCK_INTERRUPTED_TRY_AGAIN
@ SOCK_INTERRUPTED_TRY_AGAIN
Interrupted status for retries.
Definition: IpSocket.hpp:30
Drv::IpSocket::configure
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:52
Drv::TcpServerSocket::startup
SocketIpStatus startup()
Opens the server socket and listens, does not block.
Definition: TcpServerSocket.cpp:43
Fw::Buffer::getData
U8 * getData() const
Definition: Buffer.cpp:60
Drv::TcpServerComponentImpl::getBuffer
Fw::Buffer getBuffer()
returns a buffer to fill with data
Definition: TcpServerComponentImpl.cpp:56
Drv::IpSocket
Helper base-class for setting up Berkley sockets.
Definition: IpSocket.hpp:45
Drv
Definition: BlockDriver.hpp:11
Drv::TcpServerComponentImpl::shutdown
void shutdown()
shutdown the TcpServer
Definition: TcpServerComponentImpl.cpp:44
Drv::TcpServerComponentImpl::getSocketHandler
IpSocket & getSocketHandler()
returns a reference to the socket handler
Definition: TcpServerComponentImpl.cpp:52
TcpServerComponentImpl.hpp
Fw::Buffer
Definition: Buffer.hpp:43
Assert.hpp
Drv::TcpServerComponentImpl::sendBuffer
void sendBuffer(Fw::Buffer buffer, SocketIpStatus status)
sends a buffer to be filled with data
Definition: TcpServerComponentImpl.cpp:60
Drv::TcpServerComponentImpl::startup
SocketIpStatus startup()
startup the TcpServer
Definition: TcpServerComponentImpl.cpp:40
Drv::TcpServerComponentImpl::init
void init(const NATIVE_INT_TYPE instance=0)
Initialize this component.
Definition: TcpServerComponentImpl.cpp:27
Drv::TcpServerComponentImpl::connected
void connected()
called when the IPv4 system has been connected
Definition: TcpServerComponentImpl.cpp:65
Fw::Buffer::getSize
U32 getSize() const
Definition: Buffer.cpp:64
Drv::SocketReadTask
supports a task to read a given socket adaptation
Definition: SocketReadTask.hpp:27
Drv::IpSocket::send
SocketIpStatus send(const U8 *const data, const U32 size)
send data out the IP socket from the given buffer
Definition: IpSocket.cpp:137
Drv::SOCK_SUCCESS
@ SOCK_SUCCESS
Socket operation successful.
Definition: IpSocket.hpp:24
FW_ASSERT
#define FW_ASSERT(...)
Definition: Assert.hpp:8
Drv::TcpServerSocket::shutdown
void shutdown()
Shutdown client socket, and listening server socket.
Definition: TcpServerSocket.cpp:81
Drv::SocketIpStatus
SocketIpStatus
Status enumeration for socket return values.
Definition: IpSocket.hpp:23
Drv::TcpServerComponentImpl::configure
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)
Configures the TcpClient settings but does not open the connection.
Definition: TcpServerComponentImpl.cpp:31
BasicTypes.hpp
Declares ISF basic types.
Drv::SOCK_DISCONNECTED
@ SOCK_DISCONNECTED
Failed to read socket with disconnect.
Definition: IpSocket.hpp:32
NATIVE_INT_TYPE
int NATIVE_INT_TYPE
native integer type declaration
Definition: BasicTypes.hpp:29