Ocean
Loading...
Searching...
No Matches
MaintenanceTCPConnector.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef FACEBOOK_NETWORK_MAINTENANCE_TCP_CONNECTOR_H
9#define FACEBOOK_NETWORK_MAINTENANCE_TCP_CONNECTOR_H
10
14
16#include "ocean/base/Thread.h"
18
19#include <deque>
20
21namespace Ocean
22{
23
24namespace Network
25{
26
27/**
28 * This class implements a network connector between two maintenance managers using the TCP protocol.
29 * The connector allows to transmit maintenance data from one manager to another manager by application of a network.<br>
30 * This connector can be either a sender or a receiver (not both concurrently) depending on the configuration.<br>
31 * The sender extracts maintenance data from the local maintenance manager and sends this data to the remote connector (configured as receiver).<br>
32 * The receiver receives maintenance data from a remote connector (configured as sender) and places this data into the local maintenance manager.<br>
33 * Beware: The maintenance connector must be released explicitly before the program terminates.<br>
34 * @see configurateAsSender(), configurateAsReceiver(), release(), MaintenanceUDPConnector.
35 * @ingroup network
36 */
37class OCEAN_NETWORK_EXPORT MaintenanceTCPConnector :
39 protected Thread
40{
41 protected:
42
43 /**
44 * Definition of a vector holding bytes.
45 */
46 typedef std::vector<uint8_t> Buffer;
47
48 /**
49 * Definition of a vector storing buffers.
50 */
51 typedef std::vector<Buffer> Buffers;
52
53 /**
54 * Definition of a buffer queue.
55 */
56 typedef std::deque<Buffer> BufferQueue;
57
58 /**
59 * Definition of a map mapping connection ids to buffer queues.
60 */
61 typedef std::unordered_map<TCPServer::ConnectionId, BufferQueue> BufferQueueMap;
62
63 public:
64
65 /**
66 * Creates a new maintenance connector object.
67 */
69
70 /**
71 * Destructs a maintenance connector object.
72 */
74
75 /**
76 * Returns whether this connector is configured as sender.
77 * @return True, if so
78 * @see configurateAsSender(), isReceiver().
79 */
80 inline bool isSender() const;
81
82 /**
83 * Returns whether this connector is configured as receiver.
84 * @return True, if so
85 * @see configurateAsReceiver(), isSender().
86 */
87 inline bool isReceiver() const;
88
89 /**
90 * Configures this connector as sender.
91 * @param targetAddress The address of the remote host to which the maintenance data will be sent
92 * @param targetPort The port of the remote host to which the maintenance data will be sent
93 * @see configurateAsReceiver().
94 */
95 void configurateAsSender(const Address4& targetAddress, const Port& targetPort);
96
97 /**
98 * Configures this connector as receiver.
99 * @param port The local port of this receiver connector
100 */
101 void configurateAsReceiver(const Port& port);
102
103 protected:
104
105 /**
106 * Internal thread run function.
107 */
108 void threadRun() override;
109
110 /**
111 * TCP data receive event function.
112 * @param connectionId The id of the connection from which the data has been received
113 * @param data The data which has been received
114 * @param size The size of the data, in bytes
115 */
116 void onReceiveTCPData(const TCPServer::ConnectionId connectionId, const void* data, const size_t size);
117
118 /**
119 * Connection request event function.
120 * @param address The Address of the remote client requesting the connection
121 * @param port The port of the remote client requesting the connection
122 * @param connectionId The id of the connection
123 * @return True, to accept the connection
124 */
125 bool onConnectionRequest(const Address4& address, const Port& port, TCPServer::ConnectionId connectionId);
126
127 /**
128 * Extracts one related buffer from a queue of messages.
129 * @param bufferQueue The queue of buffers from which the related buffer will be extracted
130 * @param buffer The resulting related buffer, if any
131 * @return True, if one related buffer could be extracted
132 */
133 static bool extractRelatedBuffer(BufferQueue& bufferQueue, Buffer& buffer);
134
135 protected:
136
137 /// The lock of this connector.
138 mutable Lock lock_;
139
140 /// The target address if this connector is a sender.
142
143 /// The target port if this connector is a sender.
145
146 /// The source port if this connector is a receiver.
148
149 /// The TCP client of this connector, used if this connector is a sender.
151
152 /// The TCP server of this connector, used if this connector is a receiver.
154
155 /// The map of received buffers.
157
158 /// The encoded buffer for the sender
160};
161
163{
164 const ScopedLock scopedLock(lock_);
165
167
169}
170
172{
173 const ScopedLock scopedLock(lock_);
174
176
177 return serverSourcePort_.isValid();
178}
179
180}
181
182}
183
184#endif // FACEBOOK_NETWORK_MAINTENANCE_TCP_CONNECTOR_H
This class implements a recursive lock object.
Definition Lock.h:31
This class is the base class for all maintenance connectors.
Definition Maintenance.h:49
This class wraps an address number with 32 bits.
Definition Address4.h:26
bool isValid() const
Returns whether this address hold a valid address.
Definition Address4.h:154
unsigned int ConnectionId
Definition of a connection id.
Definition ConnectionOrientedServer.h:34
This class implements a network connector between two maintenance managers using the TCP protocol.
Definition MaintenanceTCPConnector.h:40
~MaintenanceTCPConnector() override
Destructs a maintenance connector object.
Port serverSourcePort_
The source port if this connector is a receiver.
Definition MaintenanceTCPConnector.h:147
void configurateAsReceiver(const Port &port)
Configures this connector as receiver.
Port clientTargetPort_
The target port if this connector is a sender.
Definition MaintenanceTCPConnector.h:144
Lock lock_
The lock of this connector.
Definition MaintenanceTCPConnector.h:138
bool onConnectionRequest(const Address4 &address, const Port &port, TCPServer::ConnectionId connectionId)
Connection request event function.
static bool extractRelatedBuffer(BufferQueue &bufferQueue, Buffer &buffer)
Extracts one related buffer from a queue of messages.
std::unordered_map< TCPServer::ConnectionId, BufferQueue > BufferQueueMap
Definition of a map mapping connection ids to buffer queues.
Definition MaintenanceTCPConnector.h:61
bool isSender() const
Returns whether this connector is configured as sender.
Definition MaintenanceTCPConnector.h:162
std::vector< Buffer > Buffers
Definition of a vector storing buffers.
Definition MaintenanceTCPConnector.h:51
void onReceiveTCPData(const TCPServer::ConnectionId connectionId, const void *data, const size_t size)
TCP data receive event function.
TCPClient tcpClient
The TCP client of this connector, used if this connector is a sender.
Definition MaintenanceTCPConnector.h:150
bool isReceiver() const
Returns whether this connector is configured as receiver.
Definition MaintenanceTCPConnector.h:171
std::deque< Buffer > BufferQueue
Definition of a buffer queue.
Definition MaintenanceTCPConnector.h:56
MaintenanceTCPConnector()
Creates a new maintenance connector object.
Buffer encodedSenderBuffer_
The encoded buffer for the sender.
Definition MaintenanceTCPConnector.h:159
std::vector< uint8_t > Buffer
Definition of a vector holding bytes.
Definition MaintenanceTCPConnector.h:46
void threadRun() override
Internal thread run function.
TCPServer tcpServer
The TCP server of this connector, used if this connector is a receiver.
Definition MaintenanceTCPConnector.h:153
void configurateAsSender(const Address4 &targetAddress, const Port &targetPort)
Configures this connector as sender.
Address4 clientTargetAddress_
The target address if this connector is a sender.
Definition MaintenanceTCPConnector.h:141
BufferQueueMap bufferQueueMap_
The map of received buffers.
Definition MaintenanceTCPConnector.h:156
This class wraps a port number with 16 bits.
Definition Port.h:26
bool isValid() const
Returns whether this port hold a non-zero value.
Definition Port.h:131
This class implements a TCP client.
Definition TCPClient.h:27
This class implements a TCP server.
Definition TCPServer.h:25
This class implements a scoped lock object for recursive lock objects.
Definition Lock.h:135
This class implements a thread.
Definition Thread.h:115
The namespace covering the entire Ocean framework.
Definition Accessor.h:15