Skip to content

Commit 7180752

Browse files
authored
Merge pull request #2800 from sonic-pi-net/daemon-booter
2 parents ef24bc7 + 452a8bf commit 7180752

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+4207
-1448
lines changed

app/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON)
1111
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
1212
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
1313

14+
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT ${PROJECT_NAME})
15+
1416
if(APPLE)
1517
set(CMAKE_OSX_DEPLOYMENT_TARGET '10.13')
1618
endif()

app/api/CMakeLists.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ set(OSC_SRC
2121
${API_ROOT}/src/osc/osc_server.cpp
2222
${API_ROOT}/src/osc/tcp_osc_server.cpp
2323
${API_ROOT}/src/osc/udp_osc_server.cpp
24-
24+
2525
${API_ROOT}/include/api/osc/osc_handler.h
2626
${API_ROOT}/include/api/osc/osc_sender.h
2727
${API_ROOT}/include/api/osc/osc_server.h
@@ -36,7 +36,7 @@ set(BUFFER_SRC
3636
${API_ROOT}/include/api/audio/audio_processor.h
3737
${API_ROOT}/include/api/audio/scope_buffer.hpp
3838
${API_ROOT}/include/api/audio/server_shm.hpp)
39-
39+
4040
add_library(${PROJECT_NAME} STATIC ${API_SRC} ${OSC_SRC} ${BUFFER_SRC})
4141
add_library(SonicPi::API ALIAS ${PROJECT_NAME})
4242

@@ -53,10 +53,12 @@ target_include_directories(${PROJECT_NAME}
5353
PUBLIC
5454
${API_ROOT}/include
5555
${APP_ROOT}/external/ghc_filesystem/include
56+
${APP_ROOT}/external/kissnet-master-34b751b
5657
PRIVATE
5758
${APP_ROOT}/external
5859
${APP_ROOT}/external/scsynth-boost-1.74.0
5960
${APP_ROOT}/external/TLSF-2.4.6/src
61+
6062
)
6163

6264
target_link_libraries(${PROJECT_NAME}
@@ -70,7 +72,7 @@ target_link_libraries(${PROJECT_NAME}
7072
# Windows
7173
if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
7274
find_package(crossguid CONFIG REQUIRED)
73-
target_link_libraries(${PROJECT_NAME}
75+
target_link_libraries(${PROJECT_NAME}
7476
PRIVATE
7577
crossguid
7678
)
@@ -79,7 +81,7 @@ endif()
7981
# Linux
8082
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
8183
find_package(crossguid CONFIG REQUIRED)
82-
target_link_libraries(${PROJECT_NAME}
84+
target_link_libraries(${PROJECT_NAME}
8385
PUBLIC
8486
stdc++fs
8587
PRIVATE
@@ -89,7 +91,7 @@ endif()
8991

9092
# Mac
9193
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
92-
target_link_libraries(${PROJECT_NAME}
94+
target_link_libraries(${PROJECT_NAME}
9395
PRIVATE
9496
)
9597
endif()

app/api/include/api/logger.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@ class Log
5757
{
5858
if (opened)
5959
{
60-
out << std::endl;
60+
std::cout << out.str() << std::endl;
61+
6162
#ifdef WIN32
63+
out << std::endl;
6264
OutputDebugStringA(out.str().c_str());
6365
#endif
64-
std::cout << out.str();
66+
6567
}
6668
opened = false;
6769
}

app/api/include/api/sonicpi_api.h

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
#include <map>
66
#include <string>
77
#include <thread>
8-
8+
#include <kissnet.hpp>
9+
#include <mutex>
910
#include <api/osc/osc_pkt.hh>
1011

12+
1113
#if 0
1214
#ifdef __unix__
1315
#include <experimental/filesystem>
@@ -38,38 +40,29 @@ class OscServer;
3840

3941
enum class SonicPiPath
4042
{
41-
RootPath, // Sonic Pi Application root
42-
RubyPath, // Path to ruby executable
43-
RubyServerPath, // Path to the ruby server script
44-
PortDiscoveryPath, // Path to the port discovery script
45-
FetchUrlPath, // Path to the fetch url script
46-
SamplePath, // Path to the samples folder
47-
UserPath,
48-
ServerErrorLogPath,
49-
ServerOutputLogPath,
50-
ProcessLogPath,
51-
SCSynthLogPath,
52-
InitScriptPath,
53-
ExitScriptPath,
54-
GUILogPath,
55-
TaskRegisterPath
43+
RootPath, // Sonic Pi Application root
44+
UserPath, // User-writable folder for config/saves etc.
45+
RubyPath, // Path to ruby executable
46+
BootDaemonPath, // Path to the Boot Daemon script
47+
FetchUrlPath, // Path to the fetch url script
48+
SamplePath, // Path to the samples folder
49+
LogPath, // Base log folder
50+
SpiderServerLogPath, // Log file for Spider Server output
51+
BootDaemonLogPath, // Log file for Boot Daemon output
52+
TauLogPath, // Log file for Tau IO Server output
53+
SCSynthLogPath, // Log file for SuperCollider scsynth's output
54+
GUILogPath, // Log file for GUI
5655
};
5756

5857
// NOTE: These port names returned by ruby; they match the symbols and cannot be changed.
5958
enum class SonicPiPortId
6059
{
6160
Invalid,
61+
daemon,
6262
gui_listen_to_server,
6363
gui_send_to_server,
64-
server_listen_to_gui,
65-
server_send_to_gui,
6664
scsynth,
67-
scsynth_send,
68-
server_osc_cues,
69-
erlang_router,
70-
osc_midi_out,
71-
osc_midi_in,
72-
websocket
65+
server_osc_cues
7366
};
7467

7568
// Log output of the API to the log files or the console?
@@ -263,7 +256,8 @@ class SonicPiAPI
263256
virtual bool Init(const fs::path& rootPath);
264257

265258
// Wait for the server to be in a good state
266-
virtual bool WaitForServer();
259+
virtual bool PingUntilServerCreated();
260+
virtual bool WaitUntilReady();
267261

268262
// Shut down the API, close the server, ports, osc, etc.
269263
virtual void Shutdown();
@@ -280,7 +274,7 @@ class SonicPiAPI
280274

281275
// Enable audio processing
282276
virtual void AudioProcessor_Enable(bool enable);
283-
277+
284278
// Enable FFT generation
285279
virtual void AudioProcessor_EnableFFT(bool enable);
286280

@@ -294,6 +288,7 @@ class SonicPiAPI
294288

295289
const std::string& GetGuid() const;
296290

291+
297292
virtual bool TestAudio();
298293

299294
// Get a unique path
@@ -305,7 +300,7 @@ class SonicPiAPI
305300
virtual bool SendOSC(oscpkt::Message m);
306301

307302
virtual void LoadWorkspaces();
308-
303+
309304
virtual void SaveWorkspaces(const std::map<uint32_t, std::string>& workspaces);
310305

311306
virtual uint32_t MaxWorkspaces() const;
@@ -318,28 +313,39 @@ class SonicPiAPI
318313
private:
319314
fs::path FindHomePath() const;
320315

321-
bool GetPorts();
322-
bool StartRubyServer();
316+
bool StartBootDaemon();
323317
bool StartOscServer();
318+
void StopOscServer();
324319

325-
void StopServerAndOsc();
326-
void RunCleanupScript();
320+
bool InitializePaths(const fs::path& root);
321+
void EnsurePathsAreCanonical();
327322

328323
std::error_code RunProcess(const std::vector<std::string>& args, std::string* pOutput = nullptr);
329-
std::shared_ptr<reproc::process> StartProcess(const std::vector<std::string>& args, const std::string& outPath = std::string(), const std::string& errPath = std::string());
324+
std::shared_ptr<reproc::process> StartProcess(const std::vector<std::string>& args);
325+
326+
330327

331328
private:
329+
332330
std::map<SonicPiPath, fs::path> m_paths;
333331
std::map<SonicPiPortId, int> m_ports;
334332

335333
bool m_homeDirWriteable = false;
336334
std::streambuf* m_coutbuf = nullptr;
337335
std::ofstream m_stdlog;
338336

339-
std::shared_ptr<reproc::process> m_spRubyServer;
337+
std::shared_ptr<reproc::process> m_bootDaemonProcess;
338+
std::shared_ptr<kissnet::tcp_socket> m_bootDaemonSock;
339+
340+
std::mutex m_osc_mtx;
341+
bool m_shutdown_engaged = false;
342+
std::atomic<bool> m_keep_alive = { true };
340343
LogOption m_logOption;
341344

345+
342346
std::thread m_oscServerThread;
347+
std::thread m_pingerThread;
348+
std::thread m_bootDaemonSockPingLoopThread;
343349

344350
std::shared_ptr<OscServer> m_spOscServer;
345351
std::shared_ptr<OscSender> m_spOscSender;
@@ -351,12 +357,13 @@ class SonicPiAPI
351357

352358
enum State
353359
{
354-
Start,
360+
Reset,
355361
Initializing,
356362
Invalid,
357-
Created
363+
Created,
364+
Error
358365
};
359-
State m_state = State::Start;
366+
State m_state = State::Reset;
360367
uint64_t m_startServerTime;
361368
APISettings m_settings;
362369
};

app/api/src/osc/osc_sender.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ OscSender::OscSender(int port)
2929

3030
bool OscSender::sendOSC(Message m)
3131
{
32-
LOG(DBG, "SendOSC" << m.addressPattern());
32+
LOG(DBG, "SendOSC " << m.addressPattern());
3333
UdpSocket sock;
3434
sock.connectTo("127.0.0.1", port);
3535
if (!sock.isOk())
@@ -57,4 +57,4 @@ void OscSender::bufferNewlineAndIndent(int point_line, int point_index, int firs
5757
sendOSC(msg);
5858
}
5959

60-
} // namespace SonicPi
60+
} // namespace SonicPi

app/api/src/osc/udp_osc_server.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ void OscServerUDP::start()
6060
{
6161
handler->oscMessage(sock.buffer);
6262
std::vector<char>().swap(sock.buffer);
63+
64+
// TODO what's this for???
6365
std::cout << std::flush;
6466
}
6567
}

0 commit comments

Comments
 (0)