OnPosix is a tiny library to abstract POSIX mechanisms to C++ developers. It offers threading, networking, logging, IPC, etc.
Most features offered by this library can be found either inside the Boost library or in a standard library compliant with the C++11 standard.
Unfortunately, however, for some embedded Linux devices, these libraries cannot represent viable solutions, due to the lack of memory space (for the Boost libraries) and the lack of a new C++ compiler (e.g., on Xilinx MicroBlaze). On these platforms, the OnPosix library represents a good and cheap solution to have object-oriented POSIX mechanisms.
The library is available both as a shared (.so) and a static (.a) library.
The library has been developed in the context of the ERA project, financially supported by the European commission under the FP7 program.
The library is licensed under version 2 of the GNU LIBRARY GENERAL PUBLIC LICENSE (LGPL).
See file LICENSE for more details.
To build the library, just type:
The library is built both as shared library (.so) and static library (.a).
To install the library on your system, type
To install the library in a specific directory, type
Documentation is created through Doxygen. To generate the documentation, type
Unit testing is done through the Google C++ Testing Framework. To test the library, specify the googletest path by setting the GTEST_INCLUDE_DIR and GTEST_LIB_DIR variables in the Makefile and type
Some examples about the characteristics offered by the library:
The library provides the class onposix::Process to run a function or a program through fork() and fork()+execvp(), respectively.
Example of usage to run a function:
Example of usage to run a program — i.e., through for()+execvp()
The library offers basic mechanisms to create, start and stop threads.
On Linux, it allows also to set scheduling parameters, thread affinity and signal handling.
The simplest case to create a thread is to use the onposix::SimpleThread class:
For more complex cases, or if you need a better exchange of arguments (e.g., return values), create your own class by inheriting from onposix::AbstractThread and specifying a run() method. Here we provide a small example. Information about all available methods can be found in the onposix::AbstractThread class.
It is also possible to use asynchronous read/write operations:
The library offers mechanisms for both connection-oriented (e.g., TCP) and connection-less (e.g., UDP) communications. Communications can be either AF_UNIX or AF_INET. Here is a small example about a TCP client-server application. More information can be found in the description of classes onposix::StreamSocketServer, onposix::StreamSocketServerDescriptor, onposix::StreamSocketClientDescriptor, onposix::DgramSocketClientDescriptor and onposix::DgramSocketServerDescriptor.
Log messages can be printed to console and/or to a file with different log levels.
The following log levels can be set for both LOG_LEVEL_CONSOLE and LOG_LEVEL_FILE:
These values can be set directly inside the file include/Logger.hpp, or when including this file as follows:
To log a message, use
Assertions provided by this library work also when code is compiled with the -NDEBUG macro.
Example of usage:
The library also offer Observer designer pattern on descriptors (i.e., onposix::DescriptorsMonitor), buffers (i.e., onposix::Buffer) and shared queues (i.e., onposix::PosixSharedQueue and onposix::PosixPrioritySharedQueue).
A mailing list is available on Sourceforge. Fell free to propose feedback, new features and possible improvements.