blob: cda1ff148f07be45544e19cf676ac9c0a04153eb [file] [log] [blame]
Colm Donelana21620d2019-10-11 13:09:49 +01001//
Jim Flynnbbfe6032020-07-20 16:57:44 +01002// Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
Colm Donelana21620d2019-10-11 13:09:49 +01003// SPDX-License-Identifier: MIT
4//
5
6#include "CommandFileParser.hpp"
7#include "CommandLineProcessor.hpp"
8#include "GatordMockService.hpp"
Colm Donelana21620d2019-10-11 13:09:49 +01009
Jim Flynnbbfe6032020-07-20 16:57:44 +010010#include <server/include/basePipeServer/ConnectionHandler.hpp>
Finn Williams0c8cb992020-05-07 10:38:15 +010011
Colm Donelana21620d2019-10-11 13:09:49 +010012#include <string>
Finn Williamse6a2ccd2020-02-27 16:21:41 +000013#include <signal.h>
Colm Donelana21620d2019-10-11 13:09:49 +010014
Finn Williamse6a2ccd2020-02-27 16:21:41 +000015using namespace armnn;
16using namespace gatordmock;
17
18// Used to capture ctrl-c so we can close any remaining sockets before exit
19static volatile bool run = true;
20void exit_capture(int signum)
Colm Donelana21620d2019-10-11 13:09:49 +010021{
Jim Flynnbbfe6032020-07-20 16:57:44 +010022 arm::pipe::IgnoreUnused(signum);
Finn Williamse6a2ccd2020-02-27 16:21:41 +000023 run = false;
24}
Colm Donelana21620d2019-10-11 13:09:49 +010025
Jim Flynnbbfe6032020-07-20 16:57:44 +010026bool CreateMockService(std::unique_ptr<arm::pipe::BasePipeServer> basePipeServer,
Finn Williams2ed809c2020-04-20 21:21:07 +010027 std::string commandFile,
28 bool isEchoEnabled)
Finn Williamse6a2ccd2020-02-27 16:21:41 +000029{
Finn Williams2ed809c2020-04-20 21:21:07 +010030 GatordMockService mockService(std::move(basePipeServer), isEchoEnabled);
Colm Donelana21620d2019-10-11 13:09:49 +010031
Finn Williams2ed809c2020-04-20 21:21:07 +010032 // Send receive the stream metadata and send connection ack.
Colm Donelana21620d2019-10-11 13:09:49 +010033 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 Williamse6a2ccd2020-02-27 16:21:41 +000043 CommandFileParser commandLineParser;
44 commandLineParser.ParseFile(commandFile, mockService);
Colm Donelana21620d2019-10-11 13:09:49 +010045
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 Williamse6a2ccd2020-02-27 16:21:41 +000051
52int 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 Williamse6a2ccd2020-02-27 16:21:41 +000067 // make the socket non-blocking so we can exit the loop
Jim Flynnbbfe6032020-07-20 16:57:44 +010068 arm::pipe::ConnectionHandler connectionHandler(cmdLine.GetUdsNamespace(), true);
Finn Williams2ed809c2020-04-20 21:21:07 +010069
Finn Williamse6a2ccd2020-02-27 16:21:41 +000070 while (run)
71 {
Finn Williams2ed809c2020-04-20 21:21:07 +010072 auto basePipeServer = connectionHandler.GetNewBasePipeServer(cmdLine.IsEchoEnabled());
Finn Williamse6a2ccd2020-02-27 16:21:41 +000073
Finn Williams2ed809c2020-04-20 21:21:07 +010074 if (basePipeServer != nullptr)
Finn Williamse6a2ccd2020-02-27 16:21:41 +000075 {
76 threads.emplace_back(
Finn Williams2ed809c2020-04-20 21:21:07 +010077 std::thread(CreateMockService, std::move(basePipeServer), commandFile, cmdLine.IsEchoEnabled()));
Finn Williamse6a2ccd2020-02-27 16:21:41 +000078 }
79
80 std::this_thread::sleep_for(std::chrono::milliseconds(100u));
81 }
82
Finn Williamse6a2ccd2020-02-27 16:21:41 +000083 std::for_each(threads.begin(), threads.end(), [](std::thread& t){t.join();});
84}