]> git.ipfire.org Git - pakfire.git/commitdiff
builder: Handle any incoming messages here
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 25 Jun 2025 17:34:22 +0000 (17:34 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 25 Jun 2025 17:34:22 +0000 (17:34 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/builder.c
src/pakfire/builder.h
src/pakfire/client.c
src/pakfire/daemon.c

index 9d311de875f5c918333df42618db02e67c929ad3..3c5eae3cdcf34a4166848bfdfbe357f9fe0f9039 100644 (file)
@@ -436,6 +436,79 @@ int pakfire_builder_close(struct pakfire_xfer* xfer, int code, void* data) {
        return 0;
 }
 
+static int pakfire_builder_handle_job_message(struct pakfire_builder* self, const char* job_id, struct json_object* message) {
+       return -EINVAL; // XXX TODO
+}
+
+static int pakfire_builder_handle_message(struct pakfire_builder* self, struct json_object* message) {
+       const char* job_id = NULL;
+       const char* type = NULL;
+       int r;
+
+       // Does this message have a build job ID?
+       r = pakfire_json_get_string(message, "job_id", &job_id);
+       if (r < 0) {
+               switch (-r) {
+                       // Fall through if the key did not exist
+                       case ENOMSG:
+                               break;
+
+                       // Otherwise raise the error
+                       default:
+                               return r;
+               }
+       }
+
+       // If we have a job ID, this message is for a specific job
+       if (job_id)
+               return pakfire_builder_handle_job_message(self, job_id, message);
+
+       // Fetch the message type
+       r = pakfire_json_get_string(message, "type", &type);
+       if (r < 0) {
+               ERROR(self->ctx, "Invalid message that is missing its type\n");
+               return r;
+       }
+
+       // Handle new jobs
+#if 0
+       if (pakfire_string_equals(type, "job"))
+               return pakfire_builder_job(self, message);
+#endif
+
+       // Log an error for any unknown messages
+       ERROR(self->ctx, "Unknown message. Ignoring:\n%s\n",
+               pakfire_json_to_string(message, NULL));
+
+       return 0;
+}
+
+int pakfire_builder_recv(struct pakfire_xfer* xfer,
+               const char* message, const size_t size, void* data) {
+       struct pakfire_builder* self = data;
+       struct json_object* m = NULL;
+       char* error = NULL;
+       int r;
+
+       // Parse the JSON message
+       r = pakfire_json_parse(&m, &error, message, size);
+       if (r < 0) {
+               ERROR(self->ctx, "Failed to parse JSON message: %s\n", error);
+               goto ERROR;
+       }
+
+       // Handle the message
+       r = pakfire_builder_handle_message(self, m);
+
+ERROR:
+       if (m)
+               json_object_put(m);
+       if (error)
+               free(error);
+
+       return r;
+}
+
 int pakfire_builder_send_message(struct pakfire_builder* self, struct json_object* message) {
        const char* m = NULL;
        size_t length = 0;
index 1b722d8629bcbd8c53abaea6febcd6473e5db469..508f711349a4211945c0118ec67ff254bcb0cc33 100644 (file)
@@ -23,6 +23,9 @@
 
 struct pakfire_builder;
 
+// json-c
+#include <json.h>
+
 #include <pakfire/client.h>
 #include <pakfire/ctx.h>
 
@@ -38,6 +41,7 @@ int pakfire_builder_connect(struct pakfire_builder* self);
 // Socket Callbacks
 int pakfire_builder_connected(struct pakfire_xfer* xfer, void* data);
 int pakfire_builder_close(struct pakfire_xfer* xfer, int code, void* data);
+int pakfire_builder_recv(struct pakfire_xfer* xfer, const char* message, const size_t size, void* data);
 
 int pakfire_builder_send_message(struct pakfire_builder* self, struct json_object* message);
 
index 63f7be4c786df2c58404d5205a3261070c425356..cabe8cd31f13b1c5b9625260dd2b1aa4d112293d 100644 (file)
@@ -814,7 +814,7 @@ int pakfire_client_builder_connect(struct pakfire_client* self, struct pakfire_b
        // Make this a WebSocket connection
        r = pakfire_xfer_socket(xfer,
                        pakfire_builder_connected,
-                       NULL,
+                       pakfire_builder_recv,
                        NULL,
                        pakfire_builder_close,
                        builder);
index 0ed5c6ed3dd7c1552d525f5794919047ba5a22d0..ae99ef86dd3e19bddfb10b1cfbad2f52db41e3f4 100644 (file)
@@ -266,77 +266,6 @@ ERROR:
        return r;
 }
 
-static int pakfire_daemon_recv(struct pakfire_xfer* xfer,
-               const char* message, const size_t size, void* data) {
-       struct pakfire_daemon* daemon = data;
-       struct json_object* type = NULL;
-       struct json_object* m = NULL;
-       char* error = NULL;
-       int r;
-
-       const char* job_id = NULL;
-
-       // Parse the JSON message
-       r = pakfire_json_parse(&m, &error, message, size);
-       if (r < 0) {
-               ERROR(daemon->ctx, "Failed to parse JSON message: %s\n", error);
-               goto ERROR;
-       }
-
-       // Does this message have a build job ID?
-       r = pakfire_json_get_string(m, "job_id", &job_id);
-       if (r < 0) {
-               switch (-r) {
-                       // Fall through if the key did not exist
-                       case ENOMSG:
-                               break;
-
-                       // Otherwise raise the error
-                       default:
-                               goto ERROR;
-               }
-       }
-
-       // If we have a job ID, this message is for a specific job
-       if (job_id) {
-               r = pakfire_daemon_handle_job_message(daemon, job_id, m);
-               if (r < 0)
-                       goto ERROR;
-
-               goto ERROR;
-       }
-
-       // Fetch the message type
-       if (!json_object_object_get_ex(m, "type", &type)) {
-               ERROR(daemon->ctx, "Invalid message with missing type\n");
-
-               return -EINVAL;
-       }
-
-       // Fetch the type
-       const char* t = json_object_get_string(type);
-
-       // Handle new jobs
-       if (pakfire_string_equals(t, "job")) {
-               r = pakfire_daemon_job(daemon, m);
-               if (r < 0)
-                       goto ERROR;
-
-       } else {
-               ERROR(daemon->ctx, "Unknown message. Ignoring:\n%s\n", pakfire_json_to_string(m, NULL));
-
-               r = 0;
-       }
-
-ERROR:
-       if (m)
-               json_object_put(m);
-       if (error)
-               free(error);
-
-       return r;
-}
-
 /*
        Called when we are ready to stream the log.