Giter VIP home page Giter VIP logo

libmodule's Introduction

Libmodule

CI Build CodeQL Documentation Status License: MIT

What is this?

Libmodule offers a small and simple C implementation of an actor library that aims to let developers easily create modular C projects in a way which is both simple and elegant.
Indeed, libmodule was heavily inspired by my own actor library experience with akka for its API.

What is a module, anyway?

Unsurprisingly, module is the core concept of libmodule architecture.
A module is an Actor that can listen on non-pubsub events too.
Frankly speaking, it is denoted by a M_MOD() macro plus a bunch of mandatory callbacks, eg:

#include <module/mod_easy.h>
#include <module/ctx.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>

M_MOD("Pippo");

static bool m_mod_on_eval(mod_t *mod) {
    /* Should module be started? */
    return true;
}

static bool m_mod_on_start(mod_t *mod) {
    /* Register STDIN fd */
    m_mod_src_register(mod, STDIN_FILENO, 0, NULL);
    return true;
}

static void m_mod_on_stop(mod_t *mod) {
    
}

static void m_mod_on_evt(m_mod_t *mod, const m_queue_t *const evts) {
    m_itr_foreach(evts, {
        m_evt_t *msg = m_itr_get(m_itr);
        switch (msg->type) {
        case M_SRC_TYPE_FD: {
            char c;
            read(msg->fd_evt->fd, &c, sizeof(char));
            switch (tolower(c)) {
                case 'q':
                    m_mod_log(mod, "Leaving...\n");
                    m_mod_tell(mod, mod, "ByeBye", 0);
                    break;
                default:
                    if (c != ' ' && c != '\n') {
                        m_mod_log(mod, "Pressed '%c'\n", c);
                    }
                    break;
            }
            break;
        }
        case M_SRC_TYPE_PS:
            if (strcmp((char *)msg->ps_evt->data, "ByeBye") == 0) {
                m_mod_log("Bye\n"):
                m_ctx_quit(0);
            }
            break;
        }
    }
}

In this example, a "Pippo" module is created and will echo chars from stdin until 'q' is pressed, exiting with 0.
Note that it does not even need a main function, as libmodule already provides a default one as a weak, thus overridable, symbol.

Is it portable?

Yes, it is. Non-portable code is actually compile-time-plugins based.
On linux, libmodule's internal loop will use epoll, while on BSD and MacOS it will use kqueue.
On other OS, cmake will fallback at looking for libkqueue, a drop-in replacement for kqueue.

Finally, it heavily relies upon gcc attributes that may or may not be available for your compiler.

Is there any documentation?

Yes, it is availabe at readthedocs.
There are some simple examples too, see Samples folder.
To see a real project using libmodule, see Clight and Clightd.

CI

Libmodule, samples and tests are built and tested with github actions on ubuntu, freebsd, and macos.
Moreover, tests are executed and valgrind checked too.
Finally, a codeQL workflow ensures code quality.

But...why?

We all know OOP is not a solution to every problem and C is still a beautiful and much used language.
Still, I admit to love code modularity that OOP enforces; moreover, I realized that I was using same code abstractions over and over in my C projects (both side projects and at my job).
So I thought that writing a library to achieve those same abstractions in a cleaner and simpler way was the right thing to do and could help some other dev.

Build dep and how to build

You only need cmake to build libmodule on Linux and BSD/osx; it does not depend upon external software.
On other platforms, you will need libkqueue too.
To build, you only need to issue:

$ mkdir build
$ cd build
$ cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_INSTALL_LIBDIR=lib ../
$ make

Note that libmodule can also be built as static library, by passing -DSTATIC_MODULE=true parameter to cmake.

Installation - Generic OS

# make install

Installation - Red Hat

$ cpack -G RPM

And finally install generated RPM package.

Installation - Debian

$ cpack -G DEB

And finally install generated DEB package.

Libmodule will install a pkg-config script too: use it to link libmodule in your projects, or use "-lmodule" linker flag.
Please note that in order to test examples, there is no need to install the library.

For Archlinux users, Libmodule is available on AUR.

License

Libmodule is made available with a MIT license to encourage people to actually try it out and maybe use it inside their project, no matter if open or closed source.

Are you using libmodule in your project? Please let me know and I will gladly list it here.

libmodule's People

Contributors

fededp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

libmodule's Issues

Compiler warnings

Building on Ubuntu 16.04 with stock compiler flags. GCC version 5.4.0.

$ ninja
[5/7] Building C object CMakeFiles/module.dir/Lib/modules.c.o
../Lib/modules.c: In function ‘modules_ctx_loop_events’:
../Lib/modules.c:78:25: warning: ignoring return value of ‘read’, declared with attribute warn_unused_result [-Wunused-result]
                         read(p->fd, (void **)&msg.pubsub_msg, sizeof(pubsub_msg_t *));
                         ^
[5/7] Building C object CMakeFiles/module.dir/Lib/module.c.o
../Lib/module.c: In function ‘tell_if’:
../Lib/module.c:403:9: warning: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Wunused-result]
         write(mod->pubsub_fd[1], &mm, sizeof(pubsub_msg_t *));
         ^
[7/7] Creating library symlink libmodule.so.3 libmodule.so

Doesn't configure with cmake 3.5

$ cmake ..
CMake Error at CMakeLists.txt:3 (project):
  project with VERSION must use LANGUAGES before language names.


-- Configuring incomplete, errors occurred!
$ cmake --version
cmake version 3.5.1

CMake suite maintained and supported by Kitware (kitware.com/cmake).

It looks like cmake 3.5.1 does not support the "DESCRIPTION" attribute to project():

$ cmake --help-command project
[...]
 project(<PROJECT-NAME> [LANGUAGES] [<language-name>...])
 project(<PROJECT-NAME>
         [VERSION <major>[.<minor>[.<patch>[.<tweak>]]]]
         [LANGUAGES <language-name>...])

[...]

I see two solutions: either change cmake_minimum_required (which will mean more work for me, since I will have to build a separate cmake) or change the file to be compatible with the currently specified minimum cmake version.

I tried adding "LANGUAGES C" in the middle as well as the end of that list, but that doesn't work, getting errors such as "Could not find cmake module file: CMakeDetermineDESCRIPTIONCompiler.cmake".

Removing the description does work however, I'm not sure if it can be added in some other way instead perhaps. It might be possible to do an if statement on cmake version and do two different project clauses as well.

pkg-config file is broken when CMAKE_INSTALL_{INCLUDE,LIB}DIR is absolute

As per title: Extra/libmodule.pc.in has

prefix=@CMAKE_INSTALL_PREFIX@
exec_prefix=@CMAKE_INSTALL_PREFIX@
libdir=${exec_prefix}/@CMAKE_INSTALL_LIBDIR@
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@

and so can’t handle absolute paths in CMAKE_INSTALL_{INCLUDE,LIB}DIR. This leads to broken .pc files on NixOS in particular.

See “Concatenating paths when building pkg-config files” for a thorough discussion of the problem and a suggested fix, or KDE’s extra-cmake-modules for a simpler approach.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.