MMDVMHost-Private/RemoteCommand.cpp

112 lines
2.5 KiB
C++
Raw Normal View History

2019-01-07 13:23:11 +00:00
/*
2020-09-06 11:54:08 +00:00
* Copyright (C) 2019,2020 by Jonathan Naylor G4KLX
2019-01-07 13:23:11 +00:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "RemoteCommand.h"
#include "UDPSocket.h"
2019-01-08 16:32:12 +00:00
#include "Log.h"
2019-01-07 13:23:11 +00:00
#include <cstdio>
#include <chrono>
#include <thread>
const unsigned int BUFFER_LENGTH = 100U;
2019-01-07 13:23:11 +00:00
int main(int argc, char** argv)
{
if (argc < 3) {
::fprintf(stderr, "Usage: RemoteCommand <port> <command>\n");
return 1;
}
unsigned int port = (unsigned int)::atoi(argv[1]);
2019-01-08 14:13:18 +00:00
std::string cmd = std::string(argv[2]);
2019-01-07 13:23:11 +00:00
for (int i = 3; i < argc; i++) {
cmd += " ";
cmd += std::string(argv[i]);
}
2019-01-07 13:23:11 +00:00
if (port == 0U) {
::fprintf(stderr, "RemoteCommand: invalid port number - %s\n", argv[1]);
return 1;
}
2019-01-08 14:13:18 +00:00
CRemoteCommand* command = new CRemoteCommand(port);
2019-01-07 13:23:11 +00:00
2019-01-08 14:13:18 +00:00
return command->send(cmd);
2019-01-07 13:23:11 +00:00
}
CRemoteCommand::CRemoteCommand(unsigned int port) :
m_port(port)
{
2020-09-20 20:31:32 +00:00
CUDPSocket::startup();
::LogInitialise(false, ".", "RemoteCommand", 2U, 2U, false);
2019-01-07 13:23:11 +00:00
}
CRemoteCommand::~CRemoteCommand()
{
2019-01-08 16:32:12 +00:00
::LogFinalise();
2020-09-20 20:31:32 +00:00
CUDPSocket::shutdown();
2019-01-07 13:23:11 +00:00
}
int CRemoteCommand::send(const std::string& command)
{
2020-09-06 11:54:08 +00:00
sockaddr_storage addr;
unsigned int addrLen;
char buffer[BUFFER_LENGTH];
int retStatus = 0;
2020-09-06 11:54:08 +00:00
if (CUDPSocket::lookup("127.0.0.1", m_port, addr, addrLen) != 0) {
::fprintf(stderr, "Unable to resolve the address of the host\n");
2020-09-06 11:54:08 +00:00
return 1;
}
2019-01-07 13:23:11 +00:00
CUDPSocket socket(0U);
2020-09-06 11:54:08 +00:00
bool ret = socket.open(addr);
2019-01-07 13:23:11 +00:00
if (!ret)
return 1;
2020-09-06 11:54:08 +00:00
ret = socket.write((unsigned char*)command.c_str(), command.length(), addr, addrLen);
2019-01-07 13:23:11 +00:00
if (!ret) {
socket.close();
return 1;
}
::fprintf(stdout, "Command sent: \"%s\" to port: %u\n", command.c_str(), m_port);
2019-01-09 11:23:14 +00:00
std::this_thread::sleep_for(std::chrono::milliseconds(50));
int len = socket.read((unsigned char*)buffer, BUFFER_LENGTH, addr, addrLen);
if (len > 0) {
buffer[len] = '\0';
::fprintf(stdout, "%s\n", buffer);
}
else
{
retStatus = 1;
}
2019-01-07 13:23:11 +00:00
socket.close();
return retStatus;
2019-01-07 13:23:11 +00:00
}