diff options
| author | Paul Oliver <contact@pauloliver.dev> | 2026-05-25 05:48:07 +0200 |
|---|---|---|
| committer | Paul Oliver <contact@pauloliver.dev> | 2026-05-25 05:48:58 +0200 |
| commit | 43764cc5172156d02d88a042c31f0c9d204cf0e6 (patch) | |
| tree | 312de7c5fb739401f5785ceb642a130203826883 | |
| parent | ad21f51f4f14da9b9283fa72f1574cdb7286c4d9 (diff) | |
Makes all comms between client and server JSON
| -rw-r--r-- | data/server.c | 33 | ||||
| -rwxr-xr-x | salis.py | 18 |
2 files changed, 27 insertions, 24 deletions
diff --git a/data/server.c b/data/server.c index c2fd276..2d96b81 100644 --- a/data/server.c +++ b/data/server.c @@ -1,6 +1,7 @@ #include <arpa/inet.h> #include <json-c/json.h> #include <signal.h> +#include <string.h> #include <threads.h> #include "logger.c" @@ -48,6 +49,11 @@ void respond_hash(int client_fd) { json_object_put(git_hash); } +void respond_data(int client_fd, struct json_object *request_json) { + assert(request_json); + (void)client_fd; +} + int handle_client(struct Client *client) { assert(client); @@ -55,23 +61,28 @@ int handle_client(struct Client *client) { inet_ntop(AF_INET, &client->addr.sin_addr, client_ip, INET_ADDRSTRLEN); log_info("Client connected: %s:%d", client_ip, ntohs(client->addr.sin_port)); - char request = '\0'; - recv(client->fd, &request, 1, 0); + struct json_object *request_json = json_object_from_fd(client->fd); + struct json_object *request_str = NULL; + + if (!json_object_object_get_ex(request_json, "request", &request_str)) assert(false); - switch (request) { - case REQUEST_NAME: + const char *request = json_object_get_string(request_str); + assert(request); + + if (!strcmp(request, "name")) { respond_name(client->fd); - break; - case REQUEST_OPTS: + } else if (!strcmp(request, "opts")) { respond_opts(client->fd); - break; - case REQUEST_HASH: + } else if (!strcmp(request, "hash")) { respond_hash(client->fd); - break; - default: - log_warn("Client made invalid request"); + } else if (!strcmp(request, "data")) { + respond_data(client->fd, request_json); + } else { + assert(false); } + json_object_put(request_json); + log_info("Client disconnected: %s:%d", client_ip, ntohs(client->addr.sin_port)); close(client->fd); @@ -1,7 +1,6 @@ #!/usr/bin/env -S PYTHONDONTWRITEBYTECODE=1 python import ctypes -import enum import json import os import random @@ -194,11 +193,6 @@ def fmt_opts(opts): log.info(f"Called '{prog} {args.command}' with the following options: {fmt_opts(vars(args))}") ns = SimpleNamespace() -class Request(enum.Enum): - REQUEST_NAME = "n" - REQUEST_OPTS = "o" - REQUEST_HASH = "h" - def gen_sim_paths(): ns.sim_dir = os.path.join(args.home, args.name) ns.sim_opts = os.path.join(ns.sim_dir, "opts.json") @@ -219,7 +213,8 @@ def source_from_opts_file(): def request_from_server(request): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client: client.connect((args.ip, args.port)) - client.sendall(request.value.encode()) + client.sendall(json.dumps(request).encode()) + client.shutdown(socket.SHUT_WR) return json.load(client.makefile(mode="r")) # New simulation @@ -263,10 +258,10 @@ if args.command == "server": if args.command == "client": # Pull basic information from the server # Required to build the client with correct flags - args.name = request_from_server(Request.REQUEST_NAME)["name"] + args.name = request_from_server({"request": "name"})["name"] log.info(f"Sourced simulation name from '{args.ip}:{args.port}': {args.name}") - opts = request_from_server(Request.REQUEST_OPTS) + opts = request_from_server({"request": "opts"}) for key, val in opts.items(): setattr(args, key, val) @@ -274,7 +269,7 @@ if args.command == "client": log.info(f"Sourced configuration from '{args.ip}:{args.port}': {fmt_opts(opts)}") # Server and client should be on the same hash - server_hash = request_from_server(Request.REQUEST_HASH)["hash"] + server_hash = request_from_server({"request": "hash"})["hash"] client_hash = subprocess.check_output(["git", "rev-parse", "HEAD"]).decode("utf-8").strip() if server_hash != client_hash: @@ -358,9 +353,6 @@ def pop_sim_path_vars(): def pop_net_vars(): ns.b.defines.add(f"-DPORT={args.port}") ns.b.defines.add(f"-DPORT_STR=\"{args.port}\"") - ns.b.defines.add(f"-DREQUEST_NAME='{Request.REQUEST_NAME.value}'") - ns.b.defines.add(f"-DREQUEST_OPTS='{Request.REQUEST_OPTS.value}'") - ns.b.defines.add(f"-DREQUEST_HASH='{Request.REQUEST_HASH.value}'") ns.b.links.add("-ljson-c") def pop_general(): |
