1 // =================================
 2 // Copyright (c) 2021 Seppo Laakko
 3 // Distributed under the MIT license
 4 // =================================
 5 
 6 #include <cmajor/build/LocalExecutionContext.hpp>
 7 #include <cmajor/build/BuildServerMessage.hpp>
 8 #include <stdexcept>
 9 
10 namespace cmajor { namespace build {
11 
12 void RunSocketServer(SocketServer* server)
13 {
14     try
15     {
16         server->Run(std::string());
17     }
18     catch (const std::exception& ex;)
19     {
20         if (server->GetLog())
21         {
22             server->GetLog()->Write("RunSocketServer got exception '" + std::string(ex.what()) + "'"nullptr);
23         }
24     }
25 }
26 
27 LocalExecutionContext::LocalExecutionContext() : ExecutionContext()
28 {
29     socketServer.reset(new SocketServer(GetLog()"local"false));
30     serverThread = std::thread({RunSocketServersocketServer.get() });
31     socketServer->WaitForRunning();
32     socketClient.reset(new SocketClient(GetLog()"local"));
33     socketClient->Run();
34 }
35 
36 LocalExecutionContext::~LocalExecutionContext()
37 {
38     try
39     {
40         Connection* connection = socketClient->GetConnection();
41         if (connection)
42         {
43             if (connection->ServerAlive())
44             {
45                 connection->SetServerAlive(false);
46                 if (connection->GetLog())
47                 {
48                     connection->GetLog()->Write("closing connection\n"nullptr);
49                 }
50                 CloseConnectionRequest closeConnectionRequest;
51                 closeConnectionRequest.SendTo(*connection);
52             }
53         }
54         serverThread.join();
55     }
56     catch (const std::exception&;)
57     {
58     }
59 }
60 
61 BuildClient* LocalExecutionContext::GetClient()
62 {
63     return socketClient->GetClient();
64 }
65 
66 Connection* LocalExecutionContext::GetConnection()
67 {
68     return socketClient->GetConnection();
69 }
70 
71 } } // namespace cmajor::build