Skip to content

Conversation

@devprabal
Copy link
Owner

@devprabal devprabal commented Mar 31, 2025

⚠️ Please DO NOT MERGE.

This PR is a Work In Progress.
This PR is only to test out sourcery gh ai code review

Summary by Sourcery

Implement a basic publish-subscribe (pubsub) messaging system with topic-based subscription and event publishing

New Features:

  • Add a publish-subscribe messaging mechanism with support for creating a subscriber list, subscribing to topics, and publishing events

Enhancements:

  • Modify main function to include a pubsub test case
  • Create pubsub infrastructure with topic-based event handling

Tests:

  • Add test cases for pubsub functionality including subscriber creation, subscription, and event publishing

@sourcery-ai
Copy link

sourcery-ai bot commented Mar 31, 2025

Reviewer's Guide by Sourcery

This pull request introduces a basic pubsub mechanism. It includes functions for creating a pubsub system, subscribing to topics, and publishing messages to topics. The implementation uses a linked list to store subscribers. A test case is added to demonstrate the functionality.

Sequence diagram for pubsub publish

sequenceDiagram
  participant Publisher
  participant Pubsub
  participant Subscriber1
  participant Subscriber2

  Publisher->>Pubsub: publish(event)
  Pubsub->>Pubsub: find_in_list(event.topic)
  alt Topic found
    Pubsub->>Subscriber1: subscriber_cb_fptr(event.data)
    Pubsub->>Subscriber2: subscriber_cb_fptr(event.data)
  else Topic not found
    Pubsub-->>Publisher: return
  end
Loading

Class diagram for Pubsub

classDiagram
  class PubsubPublisher {
    char topic[20]
    void* data
  }
  class PubsubSubscriber {
    char topic[20]
    subscriber_cb_t subscriber_cb_fptr
    void* sub_data
  }
  class Pubsub {
    Head* subscriber_list
    bool pubsub_create()
    bool pubsub_publish(PubsubPublisher event)
    bool pubsub_subscribe(PubsubSubscriber sub)
  }
  Pubsub -- PubsubPublisher : Uses
  Pubsub -- PubsubSubscriber : Manages
Loading

File-Level Changes

Change Details Files
Introduces a pubsub mechanism with subscribe and publish capabilities.
  • Added pubsub.h and pubsub.c files.
  • Implemented pubsub_create to initialize the subscriber list.
  • Implemented pubsub_subscribe to add subscribers to a list.
  • Implemented pubsub_publish to notify subscribers when a message is published to a topic.
  • Added PubsubPublisher and PubsubSubscriber structs to define the structure of publishers and subscribers.
  • Added test case pubsub_test_case to test the pubsub mechanism.
main.c
pubsub.c
pubsub.h
Refactors main.c to include and test the new pubsub functionality.
  • Added #include "pubsub.h".
  • Created pubsub_test_case function to test pubsub functionality.
  • Modified main function to call pubsub_test_case instead of sll_test_case.
main.c

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @devprabal - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider using a more robust string comparison function like strcmp instead of comparing lengths first.
  • Think about how you will handle concurrent access to the subscriber list.
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines 225 to 229
snprintf(sub1.topic, strlen("/topic1") + 1, "%s", "/topic1");
pubsub_subscribe(sub1);

snprintf(sub1.topic, strlen("/topic1") + 1, "%s", "/topic1");
pubsub_subscribe(sub2);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Potential mix-up in topic assignment for sub2.

The second subscription uses 'snprintf' to write into sub1.topic instead of sub2.topic before subscribing sub2. This may lead to unexpected behavior if sub2's topic is not set properly. Please verify if this is intentional or update it to 'snprintf(sub2.topic, ...)' to match the subscriber.

pubsub.c Outdated
return false;
}

bool pubsub_publish(PubsubPublisher event) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question (bug_risk): No callback invocation in pubsub_publish.

Currently, pubsub_publish only checks and prints a message when a matching topic is found. If the design intends to notify subscribers, iterating over the list and invoking each subscriber's callback function might be required. Ensure that this behavior aligns with the intended functionality of the publish method.

return false;
}

static bool compare_topic(void* t1, void* t2)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider simplifying compare_topic by using strcmp and refactoring pubsub_publish to use early returns instead of nested ifs to improve readability and reduce unnecessary checks and nesting, while keeping all functionality intact, which reduces complexity

**Consider simplifying both `compare_topic` and the nested flow in `pubsub_publish`.**

For example, in `compare_topic`, you can simplify string comparisons by using `strcmp` directly:

```c
static bool compare_topic(void* t1, void* t2) {
    char* topic1 = (char*)t1;
    char* topic2 = (char*)t2;
    if (topic1 && topic2) {
        return strcmp(topic1, topic2) == 0;
    }
    return false;
}

For pubsub_publish, refactor to use early returns instead of nested ifs:

bool pubsub_publish(PubsubPublisher event) {
    if (!subscriber_list) {
        DBG;
        return true;
    }
    if (find_in_list(subscriber_list, event.topic, compare_topic)) {
        printf("\nfound\n");
    } else {
        DBG;
    }
    return true;
}

These changes reduce the extra checks and nesting while keeping all functionality intact.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants