ArmNN
 21.02
GatordMockMain.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "CommandFileParser.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;
20 void exit_capture(int signum)
21 {
23  run = false;
24 }
25 
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 
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 }
bool ProcessCommandLine(int argc, char *argv[])
int main(int argc, char *argv[])
void exit_capture(int signum)
bool CreateMockService(std::unique_ptr< arm::pipe::BasePipeServer > basePipeServer, std::string commandFile, bool isEchoEnabled)
void ParseFile(std::string CommandFile, GatordMockService &mockService)
Copyright (c) 2021 ARM Limited and Contributors.
void IgnoreUnused(Ts &&...)
bool WaitForStreamMetaData()
Once the connection is open wait to receive the stream meta data packet from the client.
bool LaunchReceivingThread()
Start the thread that will receive all packets and print them nicely to stdout.
Use Boost program options to process the command line.
A class that implements a Mock Gatord server.
This class parses a command file for the GatordMockService.
void WaitForReceivingThread()
This is a placeholder method to prevent main exiting.
void SendConnectionAck()
Send a connection acknowledged packet back to the client.