Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 1 | // |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 2 | // Copyright © 2019 Arm Ltd and Contributors. All rights reserved. |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "CommandFileParser.hpp" |
| 7 | #include "CommandLineProcessor.hpp" |
| 8 | #include "GatordMockService.hpp" |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 9 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 10 | #include <server/include/basePipeServer/ConnectionHandler.hpp> |
Finn Williams | 0c8cb99 | 2020-05-07 10:38:15 +0100 | [diff] [blame] | 11 | |
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 <signal.h> |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 14 | |
Finn Williams | e6a2ccd | 2020-02-27 16:21:41 +0000 | [diff] [blame] | 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; |
| 20 | void exit_capture(int signum) |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 21 | { |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 22 | arm::pipe::IgnoreUnused(signum); |
Finn Williams | e6a2ccd | 2020-02-27 16:21:41 +0000 | [diff] [blame] | 23 | run = false; |
| 24 | } |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 25 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 26 | bool CreateMockService(std::unique_ptr<arm::pipe::BasePipeServer> basePipeServer, |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 27 | std::string commandFile, |
| 28 | bool isEchoEnabled) |
Finn Williams | e6a2ccd | 2020-02-27 16:21:41 +0000 | [diff] [blame] | 29 | { |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 30 | GatordMockService mockService(std::move(basePipeServer), isEchoEnabled); |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 31 | |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 32 | // Send receive the stream metadata and send connection ack. |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 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. |
Finn Williams | e6a2ccd | 2020-02-27 16:21:41 +0000 | [diff] [blame] | 43 | CommandFileParser commandLineParser; |
| 44 | commandLineParser.ParseFile(commandFile, mockService); |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 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 | } |
Finn Williams | e6a2ccd | 2020-02-27 16:21:41 +0000 | [diff] [blame] | 51 | |
| 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 | |
Finn Williams | e6a2ccd | 2020-02-27 16:21:41 +0000 | [diff] [blame] | 67 | // make the socket non-blocking so we can exit the loop |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 68 | arm::pipe::ConnectionHandler connectionHandler(cmdLine.GetUdsNamespace(), true); |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 69 | |
Finn Williams | e6a2ccd | 2020-02-27 16:21:41 +0000 | [diff] [blame] | 70 | while (run) |
| 71 | { |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 72 | auto basePipeServer = connectionHandler.GetNewBasePipeServer(cmdLine.IsEchoEnabled()); |
Finn Williams | e6a2ccd | 2020-02-27 16:21:41 +0000 | [diff] [blame] | 73 | |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 74 | if (basePipeServer != nullptr) |
Finn Williams | e6a2ccd | 2020-02-27 16:21:41 +0000 | [diff] [blame] | 75 | { |
| 76 | threads.emplace_back( |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 77 | std::thread(CreateMockService, std::move(basePipeServer), commandFile, cmdLine.IsEchoEnabled())); |
Finn Williams | e6a2ccd | 2020-02-27 16:21:41 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | std::this_thread::sleep_for(std::chrono::milliseconds(100u)); |
| 81 | } |
| 82 | |
Finn Williams | e6a2ccd | 2020-02-27 16:21:41 +0000 | [diff] [blame] | 83 | std::for_each(threads.begin(), threads.end(), [](std::thread& t){t.join();}); |
| 84 | } |