Skip to content

Commit

Permalink
Added HttpServer, SocketServer classes
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkwhoffmann committed Dec 24, 2024
1 parent c9ecf64 commit f3924a7
Show file tree
Hide file tree
Showing 20 changed files with 546 additions and 377 deletions.
6 changes: 5 additions & 1 deletion Emulator/Base/Defaults.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@ Defaults::Defaults()
setFallback(OPT_SRV_PROTOCOL, SRVPROT_DEFAULT, { SERVER_RSH });
setFallback(OPT_SRV_AUTORUN, false, { SERVER_RSH });
setFallback(OPT_SRV_VERBOSE, true, { SERVER_RSH });
setFallback(OPT_SRV_PORT, 8082, { SERVER_GDB });
setFallback(OPT_SRV_PORT, 8082, { SERVER_PROM });
setFallback(OPT_SRV_PROTOCOL, SRVPROT_DEFAULT, { SERVER_PROM });
setFallback(OPT_SRV_AUTORUN, false, { SERVER_PROM });
setFallback(OPT_SRV_VERBOSE, true, { SERVER_PROM });
setFallback(OPT_SRV_PORT, 8083, { SERVER_GDB });
setFallback(OPT_SRV_PROTOCOL, SRVPROT_DEFAULT, { SERVER_GDB });
setFallback(OPT_SRV_AUTORUN, false, { SERVER_GDB });
setFallback(OPT_SRV_VERBOSE, true, { SERVER_GDB });
Expand Down
14 changes: 11 additions & 3 deletions Emulator/Misc/RemoteServers/GdbServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,14 @@ GdbServer::attach(const string &name)
this->processName = name;
this->segList = { };

readSegList();

if (readSegList()) {

retroShell << "Successfully attached to process '" << processName << "'\n\n";
retroShell << " Data segment: " << util::hexstr <8> (dataSeg()) << "\n";
retroShell << " Code segment: " << util::hexstr <8> (codeSeg()) << "\n";
retroShell << " BSS segment: " << util::hexstr <8> (bssSeg()) << "\n\n";
}

if (segList.empty()) {

retroShell << "Waiting for process '" << processName << "' to launch.\n";
Expand Down Expand Up @@ -160,11 +166,13 @@ GdbServer::readSegList()
// Try to find the segment list in memory
osDebugger.read(processName, segList);
if (segList.empty()) return false;


/*
retroShell << "Successfully attached to process '" << processName << "'\n\n";
retroShell << " Data segment: " << util::hexstr <8> (dataSeg()) << "\n";
retroShell << " Code segment: " << util::hexstr <8> (codeSeg()) << "\n";
retroShell << " BSS segment: " << util::hexstr <8> (bssSeg()) << "\n\n";
*/
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion Emulator/Misc/RemoteServers/GdbServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class GdbServer final : public SocketServer {

GdbServer& operator= (const GdbServer& other) {

RemoteServer::operator = (other);
SocketServer::operator = (other);
return *this;
}

Expand Down
42 changes: 42 additions & 0 deletions Emulator/Misc/RemoteServers/HttpServer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// -----------------------------------------------------------------------------
// This file is part of vAmiga
//
// Copyright (C) Dirk W. Hoffmann. www.dirkwhoffmann.de
// Licensed under the Mozilla Public License v2
//
// See https://mozilla.org/MPL/2.0 for license information
// -----------------------------------------------------------------------------

#include "config.h"
#include "HttpServer.h"
#include "httplib.h"

namespace vamiga {

void
HttpServer::_dump(Category category, std::ostream& os) const
{
using namespace util;

if (category == Category::State) {

RemoteServer::_dump(category, os);
// os << tab("...");
// os << dec(...) << std::endl;

} else {

RemoteServer::_dump(category, os);
}
}

void
HttpServer::disconnect()
{
SUSPENDED

debug(SRV_DEBUG, "Disconnecting...\n");
if (srv) srv->stop();
}

}
62 changes: 62 additions & 0 deletions Emulator/Misc/RemoteServers/HttpServer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// -----------------------------------------------------------------------------
// This file is part of vAmiga
//
// Copyright (C) Dirk W. Hoffmann. www.dirkwhoffmann.de
// Licensed under the Mozilla Public License v2
//
// See https://mozilla.org/MPL/2.0 for license information
// -----------------------------------------------------------------------------

#pragma once

#include "RemoteServer.h"

namespace httplib { class Server; class Request; }

namespace vamiga {

class HttpServer : public RemoteServer {

using RemoteServer::RemoteServer;

protected:

HttpServer& operator= (const HttpServer& other) {

RemoteServer::operator = (other);
return *this;
}

// A simple (third-party) HTTP server
httplib::Server *srv = nullptr;


//
// Methods from CoreObject
//

protected:

void _dump(Category category, std::ostream& os) const override;


//
// Methods from RemoteServer
//

public:

virtual void disconnect() override;


//
// Running the server
//

private:

// The main thread function
// void main() override;
};

}
81 changes: 28 additions & 53 deletions Emulator/Misc/RemoteServers/PromServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,64 +16,15 @@
namespace vamiga {

void
PromServer::start()
PromServer::_dump(Category category, std::ostream& os) const
{
if (state == SRV_STATE_OFF) {
using namespace util;

debug(true, "start()\n");

try {

// Start the server in a separate thread
serverThread = std::thread(&PromServer::startServer, this);

} catch (std::exception &err) {

debug(SRV_DEBUG, "Server thread interrupted: %s\n", err.what());
}

std::cout << "Started\n";
state = SRV_STATE_CONNECTED;
}
}

void
PromServer::startServer()
{
debug(true, "startServer()\n");

// Create an HTTP server
httplib::Server svr;

// Define the "/metrics" endpoint where Prometheus will scrape metrics
svr.Get("/metrics", [this](const httplib::Request& req, httplib::Response& res) {

// Generate the metrics as a string
std::string metrics = generate_metrics();

// Set the response headers to indicate it's plain text
res.set_content(metrics, "text/plain");
});

// Start the server to listen on localhost, port 8080
std::cout << "Starting Prometheus data provider on http://localhost:8080/metrics\n";
svr.listen("localhost", 8080);
}

void
PromServer::stop()
{
if (state == SRV_STATE_CONNECTED) {

debug(true, "stop()");

state = SRV_STATE_OFF;
}
HttpServer::_dump(category, os);
}

// Function to generate some example metrics in the Prometheus format
string
PromServer::generate_metrics()
PromServer::respond(const httplib::Request& request)
{
auto emuStats = emulator.getStats();

Expand All @@ -89,4 +40,28 @@ PromServer::generate_metrics()
return metrics;
}

void
PromServer::main()
{
try {

// Create the HTTP server
if (!srv) srv = new httplib::Server();

// Define the "/metrics" endpoint where Prometheus will scrape metrics
srv->Get("/metrics", [this](const httplib::Request& req, httplib::Response& res) {
res.set_content(respond(req), "text/plain");
});

// Start the server to listen on localhost
debug(SRV_DEBUG, "Starting Prometheus data provider\n");
srv->listen("localhost", (int)config.port);

} catch (std::exception &err) {

debug(SRV_DEBUG, "Server thread interrupted\n");
handleError(err.what());
}
}

}
81 changes: 12 additions & 69 deletions Emulator/Misc/RemoteServers/PromServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,21 @@

#pragma once

#include "SubComponent.h"
#include "RemoteServerTypes.h"

std::string generate_metrics();
int createPromServer();
#include "HttpServer.h"

namespace vamiga {

class PromServer final : public SubComponent {

friend class RemoteManager;

Descriptions descriptions = {{

.name = "PromServer",
.description = "Prometheus Server",
.shell = "prom"
}};

ConfigOptions options = {

};

// The current server state
SrvState state = SRV_STATE_OFF;
class PromServer final : public HttpServer {

// The server thread
std::thread serverThread;

public:

using SubComponent::SubComponent;
using HttpServer::HttpServer;

protected:

PromServer& operator= (const PromServer& other) {

HttpServer::operator = (other);
return *this;
}

Expand All @@ -54,61 +34,24 @@ class PromServer final : public SubComponent {

protected:

void _dump(Category category, std::ostream& os) const override { };

public:

const Descriptions &getDescriptions() const override { return descriptions; }
void _dump(Category category, std::ostream& os) const override;


//
// Methods from CoreComponent
//

private:

template <class T>
void serialize(T& worker)
{

} SERIALIZERS(serialize);


//
// Methods from Configurable
// Methods from RemoteServer
//

public:

// const PromServerConfig &getConfig() const { return config; }
const ConfigOptions &getOptions() const override { return options; }
// i64 getOption(Option option) const override;
// void checkOption(Option opt, i64 value) override;
// void setOption(Option option, i64 value) override;
void main() override;


//
// Experimental
// Handling requests
//

public:

// Launch the remote server
void start();

// Shuts down the remote server
void stop();

// Used by the launch daemon to determine if actions should be taken
bool shouldRun() { return false; }

// Experimental
string generate_metrics();

private:

void startServer();

// Generate a response
string respond(const httplib::Request& request);
};

}
1 change: 1 addition & 0 deletions Emulator/Misc/RemoteServers/RemoteManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ RemoteManager::RemoteManager(Amiga& ref) : SubComponent(ref)

&serServer,
&rshServer,
&promServer,
&gdbServer
};
}
Expand Down
8 changes: 4 additions & 4 deletions Emulator/Misc/RemoteServers/RemoteManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ class RemoteManager final : public SubComponent, public Inspectable<RemoteManage
// The remote servers
SerServer serServer = SerServer(amiga, SERVER_SER);
RshServer rshServer = RshServer(amiga, SERVER_RSH);
PromServer promServer = PromServer(amiga, SERVER_PROM);
GdbServer gdbServer = GdbServer(amiga, SERVER_GDB);
PromServer promServer = PromServer(amiga);

// Convenience wrapper

// Convenience access
std::vector <RemoteServer *> servers = {
&serServer, &rshServer, &gdbServer
&serServer, &rshServer, &gdbServer, &promServer
};


Expand Down
Loading

0 comments on commit f3924a7

Please sign in to comment.