1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3
4#include <base/logger.h>
5#include <base/system.h>
6
7#include <engine/console.h>
8#include <engine/engine.h>
9#include <engine/shared/config.h>
10#include <engine/shared/jobs.h>
11#include <engine/shared/network.h>
12#include <engine/storage.h>
13
14class CEngine : public IEngine
15{
16 IConsole *m_pConsole;
17 IStorage *m_pStorage;
18
19 bool m_Logging;
20 std::shared_ptr<CFutureLogger> m_pFutureLogger;
21
22 char m_aAppName[256];
23
24 CJobPool m_JobPool;
25
26 static void Con_DbgLognetwork(IConsole::IResult *pResult, void *pUserData)
27 {
28 CEngine *pEngine = static_cast<CEngine *>(pUserData);
29
30 if(pEngine->m_Logging)
31 {
32 CNetBase::CloseLog();
33 pEngine->m_Logging = false;
34 }
35 else
36 {
37 char aBuf[32];
38 str_timestamp(buffer: aBuf, buffer_size: sizeof(aBuf));
39 char aFilenameSent[IO_MAX_PATH_LENGTH], aFilenameRecv[IO_MAX_PATH_LENGTH];
40 str_format(buffer: aFilenameSent, buffer_size: sizeof(aFilenameSent), format: "dumps/network_sent_%s.txt", aBuf);
41 str_format(buffer: aFilenameRecv, buffer_size: sizeof(aFilenameRecv), format: "dumps/network_recv_%s.txt", aBuf);
42 CNetBase::OpenLog(DataLogSent: pEngine->m_pStorage->OpenFile(pFilename: aFilenameSent, Flags: IOFLAG_WRITE, Type: IStorage::TYPE_SAVE),
43 DataLogRecv: pEngine->m_pStorage->OpenFile(pFilename: aFilenameRecv, Flags: IOFLAG_WRITE, Type: IStorage::TYPE_SAVE));
44 pEngine->m_Logging = true;
45 }
46 }
47
48public:
49 CEngine(bool Test, const char *pAppname, std::shared_ptr<CFutureLogger> pFutureLogger, int Jobs) :
50 m_pFutureLogger(std::move(pFutureLogger))
51 {
52 str_copy(dst&: m_aAppName, src: pAppname);
53 if(!Test)
54 {
55 dbg_msg(sys: "engine", fmt: "running on %s-%s-%s", CONF_FAMILY_STRING, CONF_PLATFORM_STRING, CONF_ARCH_STRING);
56 dbg_msg(sys: "engine", fmt: "arch is %s", CONF_ARCH_ENDIAN_STRING);
57
58 char aVersionStr[128];
59 if(os_version_str(version: aVersionStr, length: sizeof(aVersionStr)))
60 {
61 dbg_msg(sys: "engine", fmt: "operating system version: %s", aVersionStr);
62 }
63
64 // init the network
65 net_init();
66 CNetBase::Init();
67 }
68
69 m_JobPool.Init(NumThreads: Jobs);
70
71 m_Logging = false;
72 }
73
74 ~CEngine() override
75 {
76 CNetBase::CloseLog();
77 }
78
79 void Init() override
80 {
81 m_pConsole = Kernel()->RequestInterface<IConsole>();
82 m_pStorage = Kernel()->RequestInterface<IStorage>();
83
84 if(!m_pConsole || !m_pStorage)
85 return;
86
87 m_pConsole->Register(pName: "dbg_lognetwork", pParams: "", Flags: CFGFLAG_SERVER | CFGFLAG_CLIENT, pfnFunc: Con_DbgLognetwork, pUser: this, pHelp: "Log the network");
88 }
89
90 void AddJob(std::shared_ptr<IJob> pJob) override
91 {
92 if(g_Config.m_Debug)
93 dbg_msg(sys: "engine", fmt: "job added");
94 m_JobPool.Add(pJob: std::move(pJob));
95 }
96
97 void ShutdownJobs() override
98 {
99 m_JobPool.Shutdown();
100 }
101
102 void SetAdditionalLogger(std::shared_ptr<ILogger> &&pLogger) override
103 {
104 m_pFutureLogger->Set(pLogger);
105 }
106};
107
108IEngine *CreateEngine(const char *pAppname, std::shared_ptr<CFutureLogger> pFutureLogger, int Jobs) { return new CEngine(false, pAppname, std::move(pFutureLogger), Jobs); }
109IEngine *CreateTestEngine(const char *pAppname, int Jobs) { return new CEngine(true, pAppname, nullptr, Jobs); }
110