blob: 029c58f5e840643ed8216d1b0f35730fea656eb3 [file] [log] [blame]
Colm Donelana21620d2019-10-11 13:09:49 +01001//
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 Williamse6a2ccd2020-02-27 16:21:41 +00009#include <TimelineDecoder.hpp>
Colm Donelana21620d2019-10-11 13:09:49 +010010
Colm Donelanb682d842019-10-16 12:24:20 +010011#include <iostream>
Colm Donelana21620d2019-10-11 13:09:49 +010012#include <string>
Finn Williamse6a2ccd2020-02-27 16:21:41 +000013#include <NetworkSockets.hpp>
14#include <signal.h>
Colm Donelana21620d2019-10-11 13:09:49 +010015
Finn Williamse6a2ccd2020-02-27 16:21:41 +000016using namespace armnn;
17using namespace gatordmock;
18
19// Used to capture ctrl-c so we can close any remaining sockets before exit
20static volatile bool run = true;
21void exit_capture(int signum)
Colm Donelana21620d2019-10-11 13:09:49 +010022{
Finn Williamse6a2ccd2020-02-27 16:21:41 +000023 IgnoreUnused(signum);
24 run = false;
25}
Colm Donelana21620d2019-10-11 13:09:49 +010026
Finn Williamse6a2ccd2020-02-27 16:21:41 +000027bool CreateMockService(armnnUtils::Sockets::Socket clientConnection, std::string commandFile, bool isEchoEnabled)
28{
Keith Davis33ed2212020-03-30 10:43:41 +010029 GatordMockService mockService(clientConnection, isEchoEnabled);
Colm Donelana21620d2019-10-11 13:09:49 +010030
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 Williamse6a2ccd2020-02-27 16:21:41 +000042 CommandFileParser commandLineParser;
43 commandLineParser.ParseFile(commandFile, mockService);
Colm Donelana21620d2019-10-11 13:09:49 +010044
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 Williamse6a2ccd2020-02-27 16:21:41 +000050
51int 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}