Skip to content

Commit 036b0da

Browse files
authored
Fixes for comms module (#19)
* Update comms * Add KeyboardInterrupt catch * Remove unused prototype
1 parent 5009699 commit 036b0da

File tree

9 files changed

+66
-35
lines changed

9 files changed

+66
-35
lines changed

lgl_host_server.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,14 @@
2323

2424
# This module implements a host server that provides services over the network
2525
# to a layer running on a remote device.
26+
#
27+
# Run with ...
28+
# adb reverse localabstract:lglcomms tcp:63412
29+
#
2630

2731
import sys
32+
import threading
33+
2834
import lglpy.server
2935
import lglpy.service_test
3036
import lglpy.service_log
@@ -45,10 +51,16 @@ def main():
4551
print()
4652

4753
# Start it running
48-
server.run()
54+
serverThread = threading.Thread(target=server.run)
55+
serverThread.start()
4956

50-
return 0
57+
# Press to exit
58+
try:
59+
input("Press any key to exit ...")
60+
except KeyboardInterrupt:
61+
server.stop()
5162

63+
return 0
5264

5365
if __name__ == '__main__':
5466
sys.exit(main())

lglpy/server.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ def __init__(self, port: int):
9191
self.endpoints = {}
9292
self.register_endpoint(self)
9393

94+
self.shutdown = False
95+
self.listen_sockfd = None
96+
self.data_sockfd = None
97+
9498
def get_service_name(self) -> str:
9599
return 'registry'
96100

@@ -115,14 +119,20 @@ def run(self):
115119
listen_sockfd.bind(('localhost', self.port))
116120
listen_sockfd.listen(1)
117121

122+
self.listen_sockfd = listen_sockfd
123+
118124
# Accept connections from outside
119-
while True:
125+
while not self.shutdown:
120126
print('Waiting for connection')
121-
sockfd, _ = listen_sockfd.accept()
127+
try:
128+
sockfd, _ = listen_sockfd.accept()
129+
except OSError:
130+
continue
131+
132+
self.data_sockfd = sockfd
122133
print(' + Client connected')
123134

124-
# TODO: Add shutdown code to the loop
125-
while True:
135+
while not self.shutdown:
126136
# Read the header
127137
data = self.receive_data(sockfd, 14)
128138
if not data:
@@ -150,7 +160,20 @@ def run(self):
150160
if not sent:
151161
break
152162

163+
sockfd.close()
164+
self.data_sockfd = None
165+
153166
listen_sockfd.close()
167+
self.listen_sockfd = None
168+
169+
def stop(self):
170+
self.shutdown = True
171+
172+
if self.listen_sockfd is not None:
173+
self.listen_sockfd.close()
174+
175+
if self.data_sockfd is not None:
176+
self.data_sockfd.shutdown(socket.SHUT_RDWR)
154177

155178
def receive_data(self, sockfd, byte_count):
156179
data = b''

source_common/comms/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ target_include_directories(
3333
${LIB_BINARY} PRIVATE
3434
../)
3535

36+
lgl_set_build_options(${LIB_BINARY})
37+
3638
if(${LGL_UNITTEST})
3739
add_subdirectory(test)
38-
endif()
40+
endif()

source_common/comms/comms_module.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,16 @@
2828
* The implementation of the main communications module.
2929
*/
3030

31-
#include "comms_module.hpp"
32-
3331
#include <arpa/inet.h>
3432
#include <iostream>
3533
#include <sys/socket.h>
3634
#include <sys/un.h>
3735
#include <unistd.h>
3836
#include <cstring>
3937

38+
#include "framework/utils.hpp"
39+
#include "comms_module.hpp"
40+
4041

4142
namespace Comms
4243
{
@@ -48,7 +49,7 @@ CommsModule::CommsModule(
4849
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
4950
if (sockfd < 0)
5051
{
51-
std::cout << " - ERROR: Client socket create failed" << std::endl;
52+
LAYER_LOG(" - ERROR: Client UDS socket create failed");
5253
return;
5354
}
5455

@@ -62,10 +63,10 @@ CommsModule::CommsModule(
6263
int conn = connect(
6364
sockfd,
6465
reinterpret_cast<const struct sockaddr*>(&servAddr),
65-
sizeof(servAddr));
66+
offsetof(struct sockaddr_un, sun_path) + domainAddress.size() + 1);
6667
if (conn != 0)
6768
{
68-
std::cout << " - ERROR: Client connection failed" << std::endl;
69+
LAYER_LOG(" - ERROR: Client UDS connection failed");
6970
close(sockfd);
7071
sockfd = -1;
7172
return;
@@ -83,7 +84,7 @@ CommsModule::CommsModule(
8384
sockfd = socket(AF_INET, SOCK_STREAM, 0);
8485
if (sockfd < 0)
8586
{
86-
std::cout << " - ERROR: Client socket create failed" << std::endl;
87+
LAYER_LOG(" - ERROR: Client TCP socket create failed");
8788
return;
8889
}
8990

@@ -98,7 +99,7 @@ CommsModule::CommsModule(
9899
sizeof(servAddr));
99100
if (conn != 0)
100101
{
101-
std::cout << " - ERROR: Client connection failed" << std::endl;
102+
LAYER_LOG(" - ERROR: Client TCP connection failed");
102103
close(sockfd);
103104
sockfd = -1;
104105
return;
@@ -157,7 +158,7 @@ EndpointID CommsModule::getEndpointID(
157158
break;
158159
}
159160

160-
uint8_t id = (*resp)[0];
161+
uint8_t svcId = (*resp)[0];
161162
size_t size = static_cast<size_t>((*resp)[4] << 24)
162163
| static_cast<size_t>((*resp)[3] << 16)
163164
| static_cast<size_t>((*resp)[2] << 8)
@@ -169,13 +170,13 @@ EndpointID CommsModule::getEndpointID(
169170
break;
170171
}
171172

172-
std::string name(resp->begin() + 5, resp->begin() + 5 + size);
173+
std::string svcName(resp->begin() + 5, resp->begin() + 5 + size);
173174

174175
// Remove the entry we've read
175176
resp->erase(resp->begin(), resp->begin() + 5 + size);
176177

177178
// Store the persistent registry entry
178-
registry[name] = id;
179+
registry[svcName] = svcId;
179180
}
180181
}
181182

source_common/comms/comms_module.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,6 @@ class CommsModule: public CommsInterface
172172
*/
173173
std::shared_ptr<Message> dequeueMessage();
174174

175-
/**
176-
* @brief Get the host service endpoint list.
177-
*
178-
* @return The message to send.
179-
*/
180-
void getHostServiceEndpoints();
181-
182175
private:
183176
/**
184177
* @brief The socket for communications.

source_common/comms/comms_receiver.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@
2828
* The implementation of the communications module receiver worker.
2929
*/
3030

31+
#include <cinttypes>
3132
#include <iostream>
3233
#include <sys/socket.h>
3334
#include <unistd.h>
3435
#include <unordered_map>
3536

3637
#include "comms/comms_receiver.hpp"
37-
#include "comms_module.hpp"
38+
#include "comms/comms_module.hpp"
39+
#include "framework/utils.hpp"
3840

3941
namespace Comms
4042
{
@@ -46,7 +48,7 @@ Receiver::Receiver(
4648
int pipe_err = pipe(stopRequestPipe);
4749
if (pipe_err)
4850
{
49-
std::cout << " - ERROR: Client pipe create failed" << std::endl;
51+
LAYER_LOG(" - ERROR: Client pipe create failed");
5052
}
5153

5254
// Create and start a worker thread
@@ -127,7 +129,7 @@ void Receiver::wakeMessage(
127129
// Handle message not found ...
128130
if (parkingBuffer.count(messageID) == 0)
129131
{
130-
std::cout << " - ERROR: Cln: Message " << messageID << " not found" << std::endl;
132+
LAYER_LOG(" - ERROR: Client message %" PRIu64 " not found", messageID);
131133
return;
132134
}
133135

source_common/comms/comms_transmitter.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@
2727
* @file
2828
* The implementation of the communications module transmitter worker.
2929
*/
30-
#include "comms_transmitter.hpp"
31-
#include "comms_module.hpp"
32-
3330
#include <iostream>
3431
#include <sys/socket.h>
3532

33+
#include "comms/comms_transmitter.hpp"
34+
#include "comms/comms_module.hpp"
35+
#include "framework/utils.hpp"
36+
3637
namespace Comms
3738
{
3839

source_common/comms/test/comms_test_server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ CommsTestServer::CommsTestServer(
7070
int bindErr = bind(
7171
listenSockfd,
7272
reinterpret_cast<const struct sockaddr*>(&servAddr),
73-
sizeof(struct sockaddr_un));
73+
offsetof(struct sockaddr_un, sun_path) + domainAddress.size() + 1);
7474
if (bindErr)
7575
{
7676
std::cout << " - ERROR: Svr socket bind failed" << std::endl;

source_common/compiler_helper.cmake

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@ macro(lgl_set_build_options BUILD_TARGET_NAME)
6666
$<${is_clang}:-Wdocumentation>
6767

6868
# Disable warnings we don't want
69-
$<${is_gnu_fe}:-Wno-unused-private-field>
70-
71-
# Disable features we don't want
72-
$<${is_gnu_fe}:-fno-exceptions>)
69+
$<${is_gnu_fe}:-Wno-unused-private-field>)
7370

7471
target_compile_definitions(
7572
${BUILD_TARGET_NAME} PRIVATE

0 commit comments

Comments
 (0)