#include <errno.h>
#include <stdlib.h>
+#include <json.h>
+
#include <krb5/krb5.h>
#include <systemd/sd-bus.h>
struct pakfire_meminfo meminfo = {};
struct json_object* stats = NULL;
struct json_object* data = NULL;
- size_t length = 0;
int r;
// If we have any jobs running, we will submit our stats
// Dereference data so it won't be double-freed later
data = NULL;
- // Serialize to string
- const char* buffer = json_object_to_json_string_length(stats,
- JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY, &length);
-
// Send the message
- r = pakfire_xfer_send_message(daemon->control, buffer, length);
+ r = pakfire_daemon_send_message(daemon, stats);
+ if (r < 0) {
+ switch (-r) {
+ // Ignore if we are not connected
+ case ENOTCONN:
+ r = 0;
+ break;
+
+ // Raise anything else
+ default:
+ goto ERROR;
+ }
+ }
ERROR:
if (stats)
return 0;
}
+int pakfire_daemon_send_message(struct pakfire_daemon* self, struct json_object* message) {
+ const char* m = NULL;
+ size_t length = 0;
+ int r;
+
+ // Return an error if we are not connected
+ if (!self->control)
+ return -ENOTCONN;
+
+ // Serialize to string
+ m = json_object_to_json_string_length(message,
+ JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY, &length);
+ if (!m) {
+ ERROR(self->ctx, "Failed to serialize message: %m\n");
+ return -errno;
+ }
+
+ // Send the message
+ r = pakfire_xfer_send_message(self->control, m, length);
+ if (r < 0) {
+ ERROR(self->ctx, "Failed to send message: %s\n", strerror(-r));
+ return r;
+ }
+
+ return 0;
+}
+
#endif /* CURL_HAS_WEBSOCKETS */
#include <pakfire/ctx.h>
#include <pakfire/job.h>
+#include <json.h>
+
int pakfire_daemon_create(struct pakfire_daemon** daemon, struct pakfire_ctx* ctx);
struct pakfire_daemon* pakfire_daemon_ref(struct pakfire_daemon* daemon);
int pakfire_daemon_job_finished(struct pakfire_daemon* daemon, struct pakfire_job* job);
+// Send message
+int pakfire_daemon_send_message(struct pakfire_daemon* self, struct json_object* message);
+
#endif /* CURL_HAS_WEBSOCKETS */
#endif /* PAKFIRE_DAEMON_H */