1 //
2 // Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #include "CommandFileParser.hpp"
7 #include "CommandLineProcessor.hpp"
8 #include "GatordMockService.hpp"
9
10 #include <server/include/basePipeServer/ConnectionHandler.hpp>
11
12 #include <string>
13 #include <signal.h>
14
15 using namespace armnn;
16 using namespace gatordmock;
17
18 // Used to capture ctrl-c so we can close any remaining sockets before exit
19 static volatile bool run = true;
exit_capture(int signum)20 void exit_capture(int signum)
21 {
22 arm::pipe::IgnoreUnused(signum);
23 run = false;
24 }
25
CreateMockService(std::unique_ptr<arm::pipe::BasePipeServer> basePipeServer,std::string commandFile,bool isEchoEnabled)26 bool CreateMockService(std::unique_ptr<arm::pipe::BasePipeServer> basePipeServer,
27 std::string commandFile,
28 bool isEchoEnabled)
29 {
30 GatordMockService mockService(std::move(basePipeServer), isEchoEnabled);
31
32 // Send receive the stream metadata and send connection ack.
33 if (!mockService.WaitForStreamMetaData())
34 {
35 return EXIT_FAILURE;
36 }
37 mockService.SendConnectionAck();
38
39 // Prepare to receive data.
40 mockService.LaunchReceivingThread();
41
42 // Process the SET and WAIT command from the file.
43 CommandFileParser commandLineParser;
44 commandLineParser.ParseFile(commandFile, mockService);
45
46 // Once we've finished processing the file wait for the receiving thread to close.
47 mockService.WaitForReceivingThread();
48
49 return EXIT_SUCCESS;
50 }
51
main(int argc,char * argv[])52 int main(int argc, char* argv[])
53 {
54 // We need to capture ctrl-c so we can close any remaining sockets before exit
55 signal(SIGINT, exit_capture);
56
57 // Process command line arguments
58 CommandLineProcessor cmdLine;
59 if (!cmdLine.ProcessCommandLine(argc, argv))
60 {
61 return EXIT_FAILURE;
62 }
63
64 std::vector<std::thread> threads;
65 std::string commandFile = cmdLine.GetCommandFile();
66
67 // make the socket non-blocking so we can exit the loop
68 arm::pipe::ConnectionHandler connectionHandler(cmdLine.GetUdsNamespace(), true);
69
70 while (run)
71 {
72 auto basePipeServer = connectionHandler.GetNewBasePipeServer(cmdLine.IsEchoEnabled());
73
74 if (basePipeServer != nullptr)
75 {
76 threads.emplace_back(
77 std::thread(CreateMockService, std::move(basePipeServer), commandFile, cmdLine.IsEchoEnabled()));
78 }
79
80 std::this_thread::sleep_for(std::chrono::milliseconds(100u));
81 }
82
83 std::for_each(threads.begin(), threads.end(), [](std::thread& t){t.join();});
84 }