summaryrefslogtreecommitdiffstats
path: root/src/daemon/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/daemon/main.c')
-rw-r--r--src/daemon/main.c79
1 files changed, 59 insertions, 20 deletions
diff --git a/src/daemon/main.c b/src/daemon/main.c
index 07da36d..fd7932e 100644
--- a/src/daemon/main.c
+++ b/src/daemon/main.c
@@ -37,6 +37,7 @@ static struct _state {
static void cleanup(void);
static int __main_loop(const struct timespec * const iter_len);
+static int does_my_kokoro_go_doki_doki(struct tlv *tlv);
/* the logic is a placeholder right now */
int main(int argc, char **argv)
@@ -79,40 +80,49 @@ int main(int argc, char **argv)
/* TODO: semi-stub */
static int __main_loop(const struct timespec * const iter_len)
{
- struct tlv field;
struct tlv_packet pack;
+ struct tlv field;
struct timespec t;
char *buf = NULL;
size_t recvsize;
int ret = A_OK;
t = *iter_len;
- tlv_init(&field, HEARTBEAT);
- field.data = strdup(heartbeat_server);
- field.head.size = sizeof(heartbeat_server);
tlv_packet_init(&pack);
- tlv_pack(&pack, &field);
- tlv_packet_finalize(&pack);
- tlv_destroy(&field);
-
- /* flush any pending discovery packets from the socker,
- * as those can be quite numerous on startup.
- */
- net_flush_read_buffer(__progstate.nd);
while (ret != ERROR) {
ret = net_getlastdata(__progstate.nd, &buf, &recvsize);
- if (ret == A_OK) {
- /* we're only sending one TLV per packet as of now,
- * so no need to save the pointer for further parsing
- */
- ret = tlv_get(buf, &field, NULL);
- if (ret == A_OK && field.head.type == HEARTBEAT && !(strcmp(field.data, heartbeat_device))) {
- /* error checking? what error checking? */
- ret = net_send(__progstate.nd, pack.data, pack.cursor + 1);
+ if (ret != A_OK) {
+ goto skiprecv;
+ }
+ /* we're only sending one TLV per packet as of now,
+ * so no need to save the pointer for further parsing
+ */
+ ret = tlv_get(buf, &field, NULL);
+
+ /* handles discovery */
+ if (ret != A_OK || !(does_my_kokoro_go_doki_doki(&field))) {
+ goto skiprecv;
+ }
+
+ /* handles receiving messages */
+ if (field.head.type == TEXT) {
+ message_receive(field.data);
+ }
+/* jumping here if did not receive any data */
+skiprecv:
+ /* handles sending messages */
+ if (message_send(&field)) {
+ if (field.head.size) {
+ tlv_pack(&pack, &field);
+ tlv_packet_finalize(&pack);
+ tlv_destroy(&field);
+ net_send(__progstate.nd, pack.data, pack.cursor + 1);
+ tlv_packet_reset(&pack);
}
}
+
nanosleep(&t, NULL);
}
@@ -121,6 +131,35 @@ static int __main_loop(const struct timespec * const iter_len)
return ret;
}
+static int does_my_kokoro_go_doki_doki(struct tlv *tlv)
+{
+ int ret = ERROR;
+ static struct tlv_packet pack;
+ static size_t firstrun = 1;
+
+ if (firstrun) {
+ struct tlv field;
+ tlv_init(&field, HEARTBEAT);
+ field.data = strdup(heartbeat_server);
+ field.head.size = sizeof(heartbeat_server);
+ tlv_packet_init(&pack);
+ tlv_pack(&pack, &field);
+ tlv_packet_finalize(&pack);
+ tlv_destroy(&field);
+ firstrun = 0;
+ }
+
+ if (tlv->head.type == HEARTBEAT && !(strcmp(tlv->data, heartbeat_device))) {
+ if (setting_verbose() >= INFO) {
+ fprintf(stderr, "heartbeat.\n");
+ }
+
+ ret = net_send(__progstate.nd, pack.data, pack.cursor + 1);
+ }
+
+ return ret;
+}
+
static void cleanup(void)
{
purple_close();