1
2
3
4
5
6 #include <cmajor/build/Connection.hpp>
7 #include <cmajor/build/BuildServerMessage.hpp>
8 #include <cmajor/build/Message.hpp>
9 #include <cmajor/build/BuildOption.hpp>
10 #include <cmajor/build/BuildServer.hpp>
11 #include <sngjson/json/JsonParser.hpp>
12 #include <sngjson/json/JsonLexer.hpp>
13
14 namespace cmajor { namespace build {
15
16 Connection::Connection(Log* log_) : log(log_), serverAlive(false), server(nullptr)
17 {
18 }
19
20 Connection::~Connection()
21 {
22 }
23
24 void Connection::Send(soulng::util::JsonObject* messageObject)
25 {
26 if (log)
27 {
28 const std::string& actor = GetActor();
29 log->Write(actor + ": send: ", messageObject);
30 }
31 DoSend(messageObject);
32 }
33
34 std::std::unique_ptr<MessageBase>Connection::Receive()
35 {
36 std::unique_ptr<soulng::util::JsonObject> messageObject = DoReceive();
37 if (messageObject)
38 {
39 if (log)
40 {
41 const std::string& actor = GetActor();
42 log->Write(actor + ": receive: ", messageObject.get());
43 }
44 std::unique_ptr<MessageBase> message = MessageFactory::Instance().CreateMessage(messageObject.get());
45 return message;
46 }
47 else
48 {
49 return std::unique_ptr<MessageBase>(new CloseConnectionRequest());
50 }
51 }
52
53 void Connection::Close()
54 {
55 if (log)
56 {
57 const std::string& actor = GetActor();
58 log->Write(actor + ": closing connection\n", nullptr);
59 }
60 DoClose();
61 if (server)
62 {
63 server->Exit();
64 }
65 }
66
67 void Connection::SetServerAlive(bool alive)
68 {
69 serverAlive = alive;
70 if (log)
71 {
72 const std::string& actor = GetActor();
73 log->Write(actor + ": alive = " + BoolStr(alive) + "\n", nullptr);
74 }
75 }
76
77 } }