network.h
Go to the documentation of this file.
1 /*
2  * BSD 3-Clause License
3  *
4  * Copyright (c) 2019, Analog Devices, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 #include "buffer.pb.h"
33 
34 #include <chrono>
35 #include <condition_variable>
36 #include <functional>
37 #include <thread>
38 #include <vector>
39 #include <zmq.hpp>
40 
41 #define MAX_CAMERA_NUM 10
42 
43 class Network;
44 
45 struct NetworkHandle {
47  std::mutex net_mutex;
48 };
49 
50 class Network {
51  public:
52  typedef std::function<void(void)> InterruptNotificationCallback;
53 
54  private:
55  static std::vector<std::unique_ptr<zmq::context_t>> contexts;
56  static std::vector<std::unique_ptr<zmq::socket_t>> command_socket;
57  static std::vector<std::unique_ptr<zmq::socket_t>> monitor_sockets;
58  int max_buffer_size = 10;
59  std::unique_ptr<zmq::socket_t> frame_socket;
60  std::unique_ptr<zmq::context_t> frame_context;
61 
62  std::thread threadObj[MAX_CAMERA_NUM];
63  static std::recursive_mutex m_mutex[MAX_CAMERA_NUM];
64  static std::mutex mutex_recv[MAX_CAMERA_NUM];
66  static std::condition_variable_any Cond_Var[MAX_CAMERA_NUM];
67  static std::condition_variable thread_Cond_Var[MAX_CAMERA_NUM];
68 
73  bool Connection_Closed[MAX_CAMERA_NUM] = {false, false, false, false};
75 
77 
78  static void *rawPayloads[MAX_CAMERA_NUM];
79 
81  std::chrono::steady_clock::time_point m_latestActivityTimestamp;
82 
85  void call_zmq_service(const std::string &ip);
86 
87  public:
89 
90  static payload::ClientRequest send_buff[MAX_CAMERA_NUM];
91  static payload::ServerResponse recv_buff[MAX_CAMERA_NUM];
93 
96  int ServerConnect(const std::string &ip);
97 
102  int SendCommand(void *rawPayload = nullptr);
103 
105  int recv_server_data();
106 
108  static int callback_function(std::unique_ptr<zmq::socket_t> &stx,
109  const zmq_event_t &event);
110 
112  Network(int connectionId);
113 
115  ~Network();
116 
119  bool isSend_Successful();
120 
123  bool isData_Received();
124 
126  bool isThread_Running();
127 
129  bool isServer_Connected();
130 
133 
135  int32_t getFrame(uint16_t *buffer, uint32_t buf_size);
136 
139 
141 
142  std::chrono::steady_clock::time_point getLatestActivityTimestamp();
143 };
zmq_event_t
Definition: zmq.hpp:208
Network::Cond_Var
static std::condition_variable_any Cond_Var[MAX_CAMERA_NUM]
Definition: network.h:66
Network::callback_function
static int callback_function(std::unique_ptr< zmq::socket_t > &stx, const zmq_event_t &event)
callback_function() - APi to handle zmq events
Definition: network.cpp:440
Network::closeConnectionFrameSocket
void closeConnectionFrameSocket()
closeConnectionFrameSocket() - APi to close the frame socket connection
Definition: network.cpp:585
Network::command_socket
static std::vector< std::unique_ptr< zmq::socket_t > > command_socket
Definition: network.h:56
Network::isData_Received
bool isData_Received()
Definition: network.cpp:139
Network::Send_Successful
static bool Send_Successful[MAX_CAMERA_NUM]
Definition: network.h:69
Network::registerInterruptCallback
void registerInterruptCallback(InterruptNotificationCallback &cb)
Definition: network.cpp:511
Network::Server_Connected
static bool Server_Connected[MAX_CAMERA_NUM]
Definition: network.h:71
Network::thread_mutex
std::mutex thread_mutex[MAX_CAMERA_NUM]
Definition: network.h:65
Network::frame_context
std::unique_ptr< zmq::context_t > frame_context
Definition: network.h:60
Network::Network
Network(int connectionId)
Network() - APi to initialize network parameters.
Definition: network.cpp:524
Network::threadObj
std::thread threadObj[MAX_CAMERA_NUM]
Definition: network.h:62
Network::m_intNotifCb
InterruptNotificationCallback m_intNotifCb
Definition: network.h:80
Network::isThread_Running
bool isThread_Running()
isThread_Running() - APi to check thread exist or not
Definition: network.cpp:109
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
Network::InterruptDetected
static bool InterruptDetected[MAX_CAMERA_NUM]
Definition: network.h:74
Network::Thread_Detached
static bool Thread_Detached[MAX_CAMERA_NUM]
Definition: network.h:72
Network::FrameSocketConnection
void FrameSocketConnection(std::string &ip)
FrameSocketConnection() - APi to establish Frame Socket connection.
Definition: network.cpp:599
Network::Thread_Running
int Thread_Running[MAX_CAMERA_NUM]
Definition: network.h:76
Network::ServerConnect
int ServerConnect(const std::string &ip)
Definition: network.cpp:150
Network::thread_Cond_Var
static std::condition_variable thread_Cond_Var[MAX_CAMERA_NUM]
Definition: network.h:67
Network::max_buffer_size
int max_buffer_size
Definition: network.h:58
zmq.hpp
Network::frame_socket
std::unique_ptr< zmq::socket_t > frame_socket
Definition: network.h:59
Network::monitor_sockets
static std::vector< std::unique_ptr< zmq::socket_t > > monitor_sockets
Definition: network.h:57
Network::m_mutex
static std::recursive_mutex m_mutex[MAX_CAMERA_NUM]
Definition: network.h:63
event
struct _cl_event * event
Definition: glcorearb.h:4163
Network::recv_server_data
int recv_server_data()
recv_server_data() - APi to receive data from server
Definition: network.cpp:326
Network::m_frameLength
int m_frameLength
Definition: network.h:92
Network::getLatestActivityTimestamp
std::chrono::steady_clock::time_point getLatestActivityTimestamp()
Definition: network.cpp:515
NetworkHandle::net
Network * net
Definition: network.h:46
Network::isServer_Connected
bool isServer_Connected()
isServer_Connected() - APi to check if server is connected successfully
Definition: network.cpp:97
Network::m_latestActivityTimestamp
std::chrono::steady_clock::time_point m_latestActivityTimestamp
Definition: network.h:81
Network::call_zmq_service
void call_zmq_service(const std::string &ip)
Definition: network.cpp:412
buffer
Definition: buffer_processor.h:43
MAX_CAMERA_NUM
#define MAX_CAMERA_NUM
Definition: network.h:41
Network::send_buff
static payload::ClientRequest send_buff[MAX_CAMERA_NUM]
Definition: network.h:90
Network::m_connectionId
int m_connectionId
Definition: network.h:88
Network::~Network
~Network()
~Network() - destructor for network
Definition: network.cpp:546
Network::Connection_Closed
bool Connection_Closed[MAX_CAMERA_NUM]
Definition: network.h:73
void
typedef void(APIENTRY *GLDEBUGPROCARB)(GLenum source
Network
Definition: network.h:50
Network::InterruptNotificationCallback
std::function< void(void)> InterruptNotificationCallback
Definition: network.h:52
Network::contexts
static std::vector< std::unique_ptr< zmq::context_t > > contexts
Definition: network.h:55
Network::mutex_recv
static std::mutex mutex_recv[MAX_CAMERA_NUM]
Definition: network.h:64
Network::getFrame
int32_t getFrame(uint16_t *buffer, uint32_t buf_size)
getFrame() - APi to get frame in Async
Definition: network.cpp:568
Network::Data_Received
static bool Data_Received[MAX_CAMERA_NUM]
Definition: network.h:70
Network::SendCommand
int SendCommand(void *rawPayload=nullptr)
Definition: network.cpp:280
Network::rawPayloads
static void * rawPayloads[MAX_CAMERA_NUM]
Definition: network.h:78
Network::recv_buff
static payload::ServerResponse recv_buff[MAX_CAMERA_NUM]
Definition: network.h:91
NetworkHandle::net_mutex
std::mutex net_mutex
Definition: network.h:47
Network::isSend_Successful
bool isSend_Successful()
Definition: network.cpp:126
NetworkHandle
Definition: network.h:45


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:57