KiCad PCB EDA Suite
Loading...
Searching...
No Matches
kinng.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2023 Jon Evans <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <kinng.h>
22#include <nng/nng.h>
23#include <nng/protocol/reqrep0/rep.h>
24#include <wx/log.h>
25
26
31static const wxChar TraceNng[] = wxT( "KINNG" );
32
33
34KINNG_REQUEST_SERVER::KINNG_REQUEST_SERVER( const std::string& aSocketUrl ) :
35 m_socketUrl( aSocketUrl ),
37{
38}
39
40
45
46
48{
49 return m_thread.joinable();
50}
51
52
54{
55 if( m_thread.joinable() )
56 return true;
57
58 m_shutdown.store( false );
59 m_pendingReply.clear();
60 m_hasReply = false;
61 m_thread = std::thread( [&]() { listenThread(); } );
62 return true;
63}
64
65
67{
68 if( !m_thread.joinable() )
69 return;
70
71 {
72 // Set under the lock so the listener cannot test the flag and then miss this wakeup
73 std::lock_guard<std::mutex> lock( m_mutex );
74 m_shutdown.store( true );
75 }
76
77 m_replyReady.notify_all();
78 m_thread.join();
79}
80
81
82void KINNG_REQUEST_SERVER::Reply( const std::string& aReply )
83{
84 std::lock_guard<std::mutex> lock( m_mutex );
85 m_pendingReply = aReply;
86 m_hasReply = true;
87 m_replyReady.notify_all();
88}
89
90
92{
93 nng_socket socket;
94 nng_listener listener;
95 int retCode = 0;
96
97 wxLogTrace( TraceNng, wxS( "KINNG_REQUEST_SERVER starting" ) );
98
99 retCode = nng_rep0_open( &socket );
100
101 if( retCode != 0 )
102 {
103 wxLogTrace( TraceNng,
104 wxString::Format( wxS( "Got error code %d from nng_rep0_open!" ), retCode ) );
105 return;
106 }
107
108 retCode = nng_listener_create( &listener, socket, m_socketUrl.c_str() );
109
110 if( retCode != 0 )
111 {
112 wxLogTrace( TraceNng,
113 wxString::Format( wxS( "Got error code %d from nng_listener_create!" ),
114 retCode ) );
115 nng_close( socket );
116 return;
117 }
118
119 nng_socket_set_ms( socket, NNG_OPT_RECVTIMEO, 500 );
120
121 retCode = nng_listener_start( listener, 0 );
122
123 if( retCode != 0 )
124 {
125 wxLogTrace( TraceNng,
126 wxString::Format( wxS( "Got error code %d from nng_listener_start!" ),
127 retCode ) );
128 nng_close( socket );
129 return;
130 }
131
132 wxLogTrace( TraceNng, wxS( "KINNG_REQUEST_SERVER listener has started" ) );
133
134 while( !m_shutdown.load() )
135 {
136 char* buf = nullptr;
137 size_t sz = 0;
138
139 retCode = nng_recv( socket, &buf, &sz, NNG_FLAG_ALLOC );
140
141 if( retCode == NNG_ETIMEDOUT )
142 continue;
143
144 if( retCode != 0 )
145 {
146 if( buf )
147 nng_free( buf, sz );
148
149 wxLogTrace( TraceNng,
150 wxString::Format( wxS( "Got error code %d from nngc_recv!" ), retCode ) );
151 break;
152 }
153
154 m_sharedMessage.assign( buf, sz );
155 nng_free( buf, sz );
156 buf = nullptr;
157
158 if( m_callback )
160
161 std::unique_lock<std::mutex> lock( m_mutex );
162 m_replyReady.wait( lock, [&]() { return m_shutdown.load() || m_hasReply; } );
163
164 if( m_shutdown.load() )
165 break;
166
167 retCode = nng_send( socket, const_cast<std::string::value_type*>( m_pendingReply.c_str() ),
168 m_pendingReply.length(), 0 );
169
170 if( retCode != 0 )
171 {
172 wxLogTrace( TraceNng,
173 wxString::Format( wxS( "Got error code %d from nng_send!" ), retCode ) );
174 }
175
176 m_pendingReply.clear();
177 m_hasReply = false;
178 }
179
180 wxLogTrace( TraceNng, wxS( "KINNG_REQUEST_SERVER shutting down" ) );
181
182 nng_close( socket );
183}
bool Running() const
Definition kinng.cpp:47
std::string m_socketUrl
Definition kinng.h:57
std::string m_pendingReply
Definition kinng.h:63
std::mutex m_mutex
Definition kinng.h:70
KINNG_REQUEST_SERVER(const std::string &aSocketUrl)
Definition kinng.cpp:34
bool m_hasReply
Set by Reply(); an empty reply is valid, so emptiness cannot signal readiness.
Definition kinng.h:66
void Reply(const std::string &aReply)
Definition kinng.cpp:82
std::string m_sharedMessage
Definition kinng.h:61
std::function< void(std::string *)> m_callback
Definition kinng.h:59
std::thread m_thread
Definition kinng.h:53
std::atomic< bool > m_shutdown
Definition kinng.h:55
std::condition_variable m_replyReady
Definition kinng.h:68
static const wxChar TraceNng[]
Trace nng server debug output.
Definition kinng.cpp:31