Skip to content

Commit

Permalink
fix: update at_talk (#292)
Browse files Browse the repository at this point in the history
* fix: recv contexts and unnecessary logging on empty monitor

* docs: some typos in docs

* fix: sync key + namespace with dart at_talk and input validation on empty line
  • Loading branch information
Xlin123 authored Jun 6, 2024
1 parent 212f6b7 commit e18fa81
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
47 changes: 31 additions & 16 deletions examples/desktop/at_talk/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,24 @@
#define ROOT_HOST "root.atsign.org"
#define ROOT_PORT 64

#define ATKEY_NAME "at_talk"
#define ATKEY_NAMESPACE "at_talk"
#define ATKEY_NAME "attalk"
#define ATKEY_NAMESPACE "ai6bh"

#define MONITOR_REGEX ".*"

#define TAG "at_talk"

struct thread_args {
atclient *atclient_ctx;
atclient *monitor;
};

static int parse_args(int argc, char **argv, char **from_atsign, char **to_atsign);
static int get_atkeys_path(const char *atsign, const size_t atsignlen, char **atkeyspath);
static void *monitor_handler(atclient *atclient2);
static void *monitor_handler(void *args);
static int attalk_send_message(atclient *ctx, const char *recipient_atsign, const char *message,
const size_t messagelen);
static int attalk_recv_message(atclient *monitor, char **messageptr, char **sender_atsign);
static int attalk_recv_message(atclient *ctx, atclient *monitor, char **messageptr, char **sender_atsign);

int main(int argc, char **argv) {
int ret = 0;
Expand Down Expand Up @@ -94,7 +99,12 @@ int main(int argc, char **argv) {
goto exit;
}

ret = pthread_create(&tid, NULL, (void *) monitor_handler, &monitor);
struct thread_args contexts;
contexts.atclient_ctx = &atclient1;
contexts.monitor = &monitor;


ret = pthread_create(&tid, NULL, (void *) monitor_handler, (void *) &contexts);
if (ret != 0) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create monitor_handler\n");
return ret;
Expand All @@ -108,12 +118,14 @@ int main(int argc, char **argv) {

printf("%s%s%s -> ", HBLU, from_atsign, reset);
while ((read = getline(&line, &linelen, stdin)) != -1) {
if (read > 1){
if (line[read - 1] == '\n') {
line[read - 1] = '\0';
}

if (line[read - 1] == '\n') {
line[read - 1] = '\0';
ret = attalk_send_message(&atclient1, to_atsign, line, linelen);
}

ret = attalk_send_message(&atclient1, to_atsign, line, linelen);
printf("%s%s%s -> ", HBLU, from_atsign, reset);
}

Expand Down Expand Up @@ -167,22 +179,26 @@ static int get_atkeys_path(const char *atsign, const size_t atsignlen, char **at
}

// allocate memory for atkeys path
char *atkeys_path = (char *)malloc(strlen(home) + strlen("/.atsign/keys/") + atsignlen + strlen("_key.atkeys") + 1);
char *atkeys_path = (char *)malloc(strlen(home) + strlen("/.atsign/keys/") + atsignlen + strlen("_key.atKeys") + 1);
if (atkeys_path == NULL) {
return 1;
}

// create atkeys path
sprintf(atkeys_path, "%s/.atsign/keys/%.*s_key.atkeys", home, (int)atsignlen, atsign);
sprintf(atkeys_path, "%s/.atsign/keys/%.*s_key.atKeys", home, (int)atsignlen, atsign);

*atkeyspath = atkeys_path;
return 0;
}

static void *monitor_handler(atclient *atclient2) {
static void *monitor_handler(void* args){
int ret = 0;

if ((ret = atclient_monitor_start(atclient2, MONITOR_REGEX, strlen(MONITOR_REGEX))) != 0) {
struct thread_args *contexts = args;
atclient *atclient_ctx = contexts->atclient_ctx;
atclient *monitor = contexts->monitor;

if ((ret = atclient_monitor_start(monitor, MONITOR_REGEX, strlen(MONITOR_REGEX))) != 0) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to start monitor: %d\n", ret);
goto exit;
}
Expand All @@ -192,7 +208,7 @@ static void *monitor_handler(atclient *atclient2) {
while (loop) {
char *messageptr = NULL;
char *sender_atsign = NULL;
attalk_recv_message(atclient2, &messageptr, &sender_atsign);
attalk_recv_message(atclient_ctx, monitor, &messageptr, &sender_atsign);
if (messageptr != NULL) {
printf("\n%s%s%s: %s\n", HMAG, sender_atsign, reset, messageptr);
free(messageptr);
Expand Down Expand Up @@ -233,13 +249,12 @@ static int attalk_send_message(atclient *ctx, const char *recipient_atsign, cons
exit: { return ret; }
}

static int attalk_recv_message(atclient *monitor, char **messageptr, char **sender_atsign) {
static int attalk_recv_message(atclient *atclient_ctx, atclient *monitor, char **messageptr, char **sender_atsign) {
int ret = 1;

atclient_monitor_message *message = NULL;

if ((ret = atclient_monitor_read(monitor, monitor, &message)) != 0) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_monitor_for_notification: %d\n", ret);
if ((ret = atclient_monitor_read(monitor, atclient_ctx, &message)) != 0) {
goto exit;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/atclient/include/atclient/atsign.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void atclient_atsign_free(atclient_atsign *atsign);

/**
* @brief populates *atsign and *atsignlen with the atsign without the prefixed `@` symbol . Calling this function will
* guarantee that *atsign is always withot a prefixed `@` symbol, whether if it started with one or not.
* guarantee that *atsign is always without a prefixed `@` symbol, whether if it started with one or not.
*
* @param atsign the atsign to populate (there will be no prefixed `@` symbol. example: @bob -> bob)
* @param atsignsize the buffer size
Expand All @@ -31,7 +31,7 @@ int atclient_atsign_without_at_symbol(char *atsign, const size_t atsignsize, siz

/**
* @brief populates *atsign and *atsignlen with the atsign with the prefixed `@` symbol. Calling this function will
*
* guarantee that *atsign is always prefixed with '@' symbol, whether it started with one or not.
* @param atsign the atsign buffer to populate
* @param atsignsize the buffer size
* @param atsignlen the actually written size of the atsign
Expand Down

0 comments on commit e18fa81

Please sign in to comment.