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;
struct pakfire_builder;
+// json-c
+#include <json.h>
+
#include <pakfire/client.h>
#include <pakfire/ctx.h>
// 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);
// Make this a WebSocket connection
r = pakfire_xfer_socket(xfer,
pakfire_builder_connected,
- NULL,
+ pakfire_builder_recv,
NULL,
pakfire_builder_close,
builder);
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.