Files
op-pedal/src/ShutdownClient.cpp
T
Robin Davies 8ad2de8c72 Configurable CPU Governor (fixed)
modified:   .vscode/launch.json
	modified:   react/CMakeLists.txt
	modified:   react/public/var/config.json
	new file:   react/src/GovernorSettings.tsx
	modified:   react/src/JackHostStatus.tsx
	new file:   react/src/ListSelectDialog.tsx
	modified:   react/src/PiPedalModel.tsx
	modified:   react/src/SettingsDialog.tsx
	modified:   react/tmp.txt
	modified:   src/BeastServer.cpp
	modified:   src/BeastServer.hpp
	modified:   src/CMakeLists.txt
	modified:   src/ConfigMain.cpp
	new file:   src/CpuGovernor.cpp
	new file:   src/CpuGovernor.hpp
	new file:   src/GovernorSettings.cpp
	new file:   src/GovernorSettings.hpp
	new file:   src/Ipv6Helpers.cpp
	new file:   src/Ipv6Helpers.hpp
	modified:   src/JackHost.cpp
	modified:   src/PiPedalModel.cpp
2022-03-09 23:02:40 -05:00

236 lines
6.4 KiB
C++

// Copyright (c) 2022 Robin Davies
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "pch.h"
#include "ShutdownClient.hpp"
#include "PiPedalException.hpp"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "Lv2Log.hpp"
#include <sstream>
#include <sys/types.h>
#include <ifaddrs.h>
#include "Ipv6Helpers.hpp"
using namespace pipedal;
extern uint16_t g_ShutdownPort; // set in main.cpp
bool ShutdownClient::CanUseShutdownClient()
{
return g_ShutdownPort != 0;
}
bool ShutdownClient::RequestShutdown(bool restart)
{
const char*message = restart? "restart\n": "shutdown\n";
return WriteMessage(message);
}
bool ShutdownClient::WriteMessage(const char*message) {
uint16_t shutdownServerPort = g_ShutdownPort;
if (g_ShutdownPort == 0)
{
throw PiPedalException("No shutdown server available.");
}
struct sockaddr_in serv_addr;
memset(&serv_addr,0,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(shutdownServerPort);
if (inet_pton(AF_INET,"127.0.0.1",&serv_addr.sin_addr) <= 0)
{
Lv2Log::error("Failed to parse socket address.");
return false;
}
int sock = socket(AF_INET,SOCK_STREAM,0);
if (sock < 0)
{
Lv2Log::error("Can't create socket.");
return false;
}
if (connect(sock,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) < 0)
{
std::stringstream s;
s << "Connect to 127.0.0.1:" << shutdownServerPort << " failed.";
Lv2Log::error(s.str());
close(sock);
return false;
}
int length = strlen(message);
const char *p = message;
bool success = true;
while (length != 0) {
auto nWritten = write(sock,p,length);
if (nWritten < 0)
{
success = false;
Lv2Log::error("Shutdown server write failed.");
close(sock);
return false;
}
p += nWritten;
length -= nWritten;
}
shutdown(sock, SHUT_WR);
char responseBuffer[1024];
ssize_t available = sizeof(responseBuffer);
char *pWrite = responseBuffer;
char*pEndOfLine = responseBuffer;
bool eolFound = false;
while (!eolFound)
{
ssize_t nRead = read(sock,pWrite,available);
if (nRead == -1)
{
*pWrite = 0;
break;
}
if (nRead <= 0) {
Lv2Log::error("Failed to read shutdown server response.");
close(sock);
return false;
}
pWrite += nRead;
available -= nRead;
if (available == 0)
{
Lv2Log::error("Bad response from shutdown server.");
close(sock);
return false;
}
while (pEndOfLine != pWrite)
{
if (*pEndOfLine++ == '\n')
{
eolFound = true;
pEndOfLine[-1] = '\0';
break;
}
}
}
int response = atoi(responseBuffer);
close(sock);
if (response == -2) // throw exception with message
{
const char *p = responseBuffer;
while (*p != ' ' && *p != '\n' && *p != '\0')
{
++p;
}
if (*p != 0) ++p;
throw PiPedalStateException(p);
}
return response == 0;
}
bool ShutdownClient::SetJackServerConfiguration(const JackServerSettings & jackServerSettings)
{
std::stringstream s;
s << "setJackConfiguration ";
json_writer writer(s,true);
writer.write(jackServerSettings);
s << std::endl;
return WriteMessage(s.str().c_str());
}
void ShutdownClient::SetWifiConfig(const WifiConfigSettings & settings)
{
if (!CanUseShutdownClient())
{
throw PiPedalException("Can't perform this operation when debugging.");
}
std::stringstream cmd;
cmd << "WifiConfigSettings ";
json_writer writer(cmd,true);
writer.write(settings);
cmd << '\n';
bool result = WriteMessage(cmd.str().c_str());
if (!result) { // unexpected. Should throw exception on failure.
throw PiPedalException("Operation failed.");
}
}
void ShutdownClient::SetGovernorSettings(const std::string & settings)
{
if (!CanUseShutdownClient())
{
throw PiPedalException("Can't use ShutdownClient when running interactively.");
}
std::stringstream cmd;
cmd << "GovernorSettings ";
json_writer writer(cmd,true);
writer.write(settings);
cmd << '\n';
bool result = WriteMessage(cmd.str().c_str());
if (!result) { // unexpected. Should throw exception on failure.
throw PiPedalException("Operation failed.");
}
}
void ShutdownClient::MonitorGovernor(const std::string &governor)
{
if (!CanUseShutdownClient())
{
return;
}
std::stringstream cmd;
cmd << "MonitorGovernor ";
json_writer writer(cmd,true);
writer.write(governor);
cmd << '\n';
bool result = WriteMessage(cmd.str().c_str());
if (!result) { // unexpected. Should throw exception on failure.
throw PiPedalException("Operation failed.");
}
}
void ShutdownClient::UnmonitorGovernor()
{
if (!CanUseShutdownClient())
{
return;
}
std::stringstream cmd;
cmd << "UnmonitorGovernor";
cmd << '\n';
bool result = WriteMessage(cmd.str().c_str());
if (!result) { // unexpected. Should throw exception on failure.
throw PiPedalException("Operation failed.");
}
}