Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2019 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "CommandFileParser.hpp" |
| 7 | #include "CommandLineProcessor.hpp" |
| 8 | #include "GatordMockService.hpp" |
Finn Williams | e6a2ccd | 2020-02-27 16:21:41 +0000 | [diff] [blame] | 9 | #include <TimelineDecoder.hpp> |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 10 | |
Colm Donelan | b682d84 | 2019-10-16 12:24:20 +0100 | [diff] [blame] | 11 | #include <iostream> |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 12 | #include <string> |
Finn Williams | e6a2ccd | 2020-02-27 16:21:41 +0000 | [diff] [blame] | 13 | #include <NetworkSockets.hpp> |
| 14 | #include <signal.h> |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 15 | |
Finn Williams | e6a2ccd | 2020-02-27 16:21:41 +0000 | [diff] [blame] | 16 | using namespace armnn; |
| 17 | using namespace gatordmock; |
| 18 | |
| 19 | // Used to capture ctrl-c so we can close any remaining sockets before exit |
| 20 | static volatile bool run = true; |
| 21 | void exit_capture(int signum) |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 22 | { |
Finn Williams | e6a2ccd | 2020-02-27 16:21:41 +0000 | [diff] [blame] | 23 | IgnoreUnused(signum); |
| 24 | run = false; |
| 25 | } |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 26 | |
Finn Williams | e6a2ccd | 2020-02-27 16:21:41 +0000 | [diff] [blame] | 27 | bool CreateMockService(armnnUtils::Sockets::Socket clientConnection, std::string commandFile, bool isEchoEnabled) |
| 28 | { |
Keith Davis | 33ed221 | 2020-03-30 10:43:41 +0100 | [diff] [blame] | 29 | GatordMockService mockService(clientConnection, isEchoEnabled); |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 30 | |
| 31 | // Send receive the strweam metadata and send connection ack. |
| 32 | if (!mockService.WaitForStreamMetaData()) |
| 33 | { |
| 34 | return EXIT_FAILURE; |
| 35 | } |
| 36 | mockService.SendConnectionAck(); |
| 37 | |
| 38 | // Prepare to receive data. |
| 39 | mockService.LaunchReceivingThread(); |
| 40 | |
| 41 | // Process the SET and WAIT command from the file. |
Finn Williams | e6a2ccd | 2020-02-27 16:21:41 +0000 | [diff] [blame] | 42 | CommandFileParser commandLineParser; |
| 43 | commandLineParser.ParseFile(commandFile, mockService); |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 44 | |
| 45 | // Once we've finished processing the file wait for the receiving thread to close. |
| 46 | mockService.WaitForReceivingThread(); |
| 47 | |
| 48 | return EXIT_SUCCESS; |
| 49 | } |
Finn Williams | e6a2ccd | 2020-02-27 16:21:41 +0000 | [diff] [blame] | 50 | |
| 51 | int main(int argc, char* argv[]) |
| 52 | { |
| 53 | // We need to capture ctrl-c so we can close any remaining sockets before exit |
| 54 | signal(SIGINT, exit_capture); |
| 55 | |
| 56 | // Process command line arguments |
| 57 | CommandLineProcessor cmdLine; |
| 58 | if (!cmdLine.ProcessCommandLine(argc, argv)) |
| 59 | { |
| 60 | return EXIT_FAILURE; |
| 61 | } |
| 62 | |
| 63 | std::vector<std::thread> threads; |
| 64 | std::string commandFile = cmdLine.GetCommandFile(); |
| 65 | |
| 66 | armnnUtils::Sockets::Initialize(); |
| 67 | armnnUtils::Sockets::Socket listeningSocket = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 68 | |
| 69 | if (!GatordMockService::OpenListeningSocket(listeningSocket, cmdLine.GetUdsNamespace(), 10)) |
| 70 | { |
| 71 | return EXIT_FAILURE; |
| 72 | } |
| 73 | std::cout << "Bound to UDS namespace: \\0" << cmdLine.GetUdsNamespace() << std::endl; |
| 74 | |
| 75 | // make the socket non-blocking so we can exit the loop |
| 76 | armnnUtils::Sockets::SetNonBlocking(listeningSocket); |
| 77 | while (run) |
| 78 | { |
| 79 | armnnUtils::Sockets::Socket clientConnection = |
| 80 | armnnUtils::Sockets::Accept(listeningSocket, nullptr, nullptr, SOCK_CLOEXEC); |
| 81 | |
| 82 | if (clientConnection > 0) |
| 83 | { |
| 84 | threads.emplace_back( |
| 85 | std::thread(CreateMockService, clientConnection, commandFile, cmdLine.IsEchoEnabled())); |
| 86 | } |
| 87 | |
| 88 | std::this_thread::sleep_for(std::chrono::milliseconds(100u)); |
| 89 | } |
| 90 | |
| 91 | armnnUtils::Sockets::Close(listeningSocket); |
| 92 | std::for_each(threads.begin(), threads.end(), [](std::thread& t){t.join();}); |
| 93 | } |