F´ Flight Software - C/C++ Documentation  NASA-v2.0.1
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 
16 namespace Drv {
17 
18 // ----------------------------------------------------------------------
19 // Construction, initialization, and destruction
20 // ----------------------------------------------------------------------
21 
23  : ByteStreamDriverModelComponentBase(compName),
24  SocketReadTask() {}
25 
27  ByteStreamDriverModelComponentBase::init(instance);
28 }
29 
31  const U16 port,
32  const U32 send_timeout_seconds,
33  const U32 send_timeout_microseconds) {
34  return m_socket.configure(hostname, port, send_timeout_seconds, send_timeout_microseconds);
35 }
36 
38 
40  return this->m_socket.startup();
41 }
42 
44  this->m_socket.shutdown();
45 }
46 
47 // ----------------------------------------------------------------------
48 // Implementations for socket read task virtual methods
49 // ----------------------------------------------------------------------
50 
52  return m_socket;
53 }
54 
56  return allocate_out(0, 1024);
57 }
58 
60  this->recv_out(0, buffer, (status == SOCK_SUCCESS) ? RECV_OK : RECV_ERROR);
61 }
62 
63 // ----------------------------------------------------------------------
64 // Handler implementations for user-defined typed input ports
65 // ----------------------------------------------------------------------
66 
67 Drv::SendStatus TcpServerComponentImpl::send_handler(const NATIVE_INT_TYPE portNum, Fw::Buffer& fwBuffer) {
68  Drv::SocketIpStatus status = m_socket.send(fwBuffer.getData(), fwBuffer.getSize());
69  // Always return the buffer
70  deallocate_out(0, fwBuffer);
71  if ((status == SOCK_DISCONNECTED) || (status == SOCK_INTERRUPTED_TRY_AGAIN)) {
72  return SEND_RETRY;
73  } else if (status != SOCK_SUCCESS) {
74  return SEND_ERROR;
75  }
76  return SEND_OK;
77 }
78 
79 Drv::PollStatus TcpServerComponentImpl::poll_handler(const NATIVE_INT_TYPE portNum, Fw::Buffer& fwBuffer) {
80  FW_ASSERT(0); // It is an error to call this handler on IP drivers
81  return Drv::POLL_ERROR;
82 }
83 
84 } // end namespace Drv
Drv::TcpServerComponentImpl::TcpServerComponentImpl
TcpServerComponentImpl(const char *const compName)
construct the TcpClient component.
Definition: TcpServerComponentImpl.cpp:22
Drv::SOCK_SUCCESS
@ SOCK_SUCCESS
Socket operation successful.
Definition: IpSocket.hpp:24
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
Fw::Buffer::getData
U8 * getData() const
Definition: Buffer.cpp:56
Drv::TcpServerComponentImpl::getBuffer
Fw::Buffer getBuffer()
returns a buffer to fill with data
Definition: TcpServerComponentImpl.cpp:55
Drv::IpSocket
Helper base-class for setting up Berkley sockets.
Definition: IpSocket.hpp:45
Drv
Definition: BlockDriverImpl.cpp:5
Drv::TcpServerComponentImpl::shutdown
void shutdown()
shutdown the TcpServer
Definition: TcpServerComponentImpl.cpp:43
Drv::TcpServerComponentImpl::getSocketHandler
IpSocket & getSocketHandler()
returns a reference to the socket handler
Definition: TcpServerComponentImpl.cpp:51
Drv::TcpServerComponentImpl::~TcpServerComponentImpl
~TcpServerComponentImpl(void)
Destroy the component.
Definition: TcpServerComponentImpl.cpp:37
TcpServerComponentImpl.hpp
Fw::Buffer
Definition: Buffer.hpp:43
Drv::TcpServerComponentImpl::sendBuffer
void sendBuffer(Fw::Buffer buffer, SocketIpStatus status)
sends a buffer to filled with data
Definition: TcpServerComponentImpl.cpp:59
Drv::TcpServerComponentImpl::startup
SocketIpStatus startup()
startup the TcpServer
Definition: TcpServerComponentImpl.cpp:39
Drv::TcpServerComponentImpl::init
void init(const NATIVE_INT_TYPE instance=0)
Initialize this component.
Definition: TcpServerComponentImpl.cpp:26
Fw::Buffer::getSize
U32 getSize() const
Definition: Buffer.cpp:60
Drv::TcpServerSocket::startup
SocketIpStatus startup(void)
Opens the server socket and listens, does not block.
Definition: TcpServerSocket.cpp:43
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:135
Drv::TcpServerSocket::shutdown
void shutdown(void)
Shutdown client socket, and listening server socket.
Definition: TcpServerSocket.cpp:81
FW_ASSERT
#define FW_ASSERT(...)
Definition: Assert.hpp:9
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:30
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