ONPOSIX
2.0
Main Page
Namespaces
Classes
Files
File List
File Members
ONPOSIX
OnPosix Library
Namespaces
Classes
Files
File List
include
src
AbstractThread.cpp
Buffer.cpp
DescriptorsMonitor.cpp
DgramSocketClientDescriptor.cpp
DgramSocketServerDescriptor.cpp
FifoDescriptor.cpp
FileDescriptor.cpp
Logger.cpp
main.cpp
Pipe.cpp
PosixCondition.cpp
PosixDescriptor.cpp
PosixMutex.cpp
Process.cpp
StreamSocketClientDescriptor.cpp
StreamSocketServer.cpp
StreamSocketServerDescriptor.cpp
Time.cpp
File Members
•
All
Classes
Namespaces
Files
Functions
Variables
Enumerator
Friends
Macros
Pages
main.cpp
Go to the documentation of this file.
1
/*
2
* main.cpp
3
*
4
* Copyright (C) 2012 Evidence Srl - www.evidence.eu.com
5
*
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Library General Public
8
* License as published by the Free Software Foundation; either
9
* version 2 of the License, or (at your option) any later version.
10
*
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Library General Public License for more details.
15
*
16
* You should have received a copy of the GNU Library General Public
17
* License along with this library; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
*/
20
21
/**
22
*
23
* \htmlonly
24
* <br>
25
* <center>
26
* <img src="onposix.png" width="250"></img><br>
27
* Minimal Posix C++ library
28
* </center>
29
* <br>
30
* \endhtmlonly
31
*
32
* @mainpage OnPosix Library
33
*
34
* @author Evidence Srl - <a href="http://www.evidence.eu.com" target="_blank">www.evidence.eu.com</a>
35
*
36
* <br>
37
* <br>
38
* <h1>Introduction</h1>
39
*
40
* OnPosix is a tiny library to abstract POSIX mechanisms to C++
41
* developers. It offers threading, networking, logging, IPC, etc. <br>
42
* <br>
43
* Most features offered by this library can be found either inside the
44
* <a href="http://www.boost.org" target="_blank">Boost library</a>
45
* or in a standard library compliant with the
46
* <a href="http://www.stroustrup.com/C++11FAQ.html">C++11 standard</a>.<br>
47
* Unfortunately, however, for some embedded Linux devices, these libraries
48
* cannot represent viable solutions, due to the lack of memory space
49
* (for the Boost libraries) and the lack of a new C++ compiler
50
* (e.g., on Xilinx MicroBlaze).
51
* On these platforms, the OnPosix library represents a good and cheap solution
52
* to have object-oriented POSIX mechanisms.<br>
53
* <br>
54
* The library is available both as a shared (.so) and a static
55
* (.a) library.
56
*
57
*
58
* <br>
59
* <br>
60
* <br>
61
* <h1>License</h1>
62
*
63
* The library has been developed in the context of the
64
* <a href="http://www.era-project.org">ERA</a> project, financially supported
65
* by the European commission under the FP7 program.
66
* <br>
67
* <br>
68
* The library is licensed under version 2 of the
69
* <a href="http://www.gnu.org/licenses/old-licenses/lgpl-2.0.html">GNU LIBRARY
70
* GENERAL PUBLIC LICENSE (LGPL)</a>.<br>
71
* See file LICENSE for more details.
72
*
73
* <br>
74
* <br>
75
* <br>
76
* <h1>Download</h1>
77
*
78
* Type
79
* \code
80
* git clone git://git.code.sf.net/p/onposix/code onposix-code
81
* \endcode
82
*
83
* or <a href="http://sourceforge.net/projects/onposix/files/">
84
* download a zip archive from Sourceforge</a>.
85
*
86
* <br>
87
* <br>
88
* <h1>Installation</h1>
89
*
90
* To build the library, just type:
91
*
92
* \code
93
* make
94
* \endcode
95
*
96
* The library is built both as shared library (.so) and static library (.a).
97
*
98
* To install the library on your system, type
99
*
100
* \code
101
* make install
102
* \endcode
103
*
104
* To install the library in a specific directory, type
105
*
106
* \code
107
* make TARGET_DIR=/absolute/path install
108
* \endcode
109
*
110
* Documentation is created through <a href="http://www.doxygen.org">Doxygen</a>.
111
* To generate the documentation, type
112
*
113
* \code
114
* make doc
115
* \endcode
116
*
117
*
118
* Unit testing is done through the <a href="http://code.google.com/p/googletest/">Google C++ Testing Framework</a>.
119
* To test the library, specify the googletest path by setting the GTEST_INCLUDE_DIR and GTEST_LIB_DIR variables in the Makefile and type
120
*
121
* \code
122
* make test
123
* ./test
124
* \endcode
125
*
126
* <br>
127
* <br>
128
* <h1>Examples of usage</h1>
129
*
130
* Some examples about the characteristics offered by the library:
131
*
132
* <h2>Processes</h2>
133
*
134
* The library provides the class \ref onposix::Process to run a function
135
* or a program through fork() and fork()+execvp(), respectively.
136
*
137
* Example of usage to run a function:
138
* \code
139
*
140
* void function ()
141
* {
142
* //...
143
* }
144
*
145
* int main ()
146
* {
147
* Process p(function);
148
* }
149
* \endcode
150
*
151
* Example of usage to run a program --- i.e., through for()+execvp()
152
* \code
153
*
154
* int main ()
155
* {
156
* std::vector<std::string> args;
157
* args.push_back("-l");
158
* args.push_back("*.cpp");
159
*
160
* Process p("ls", args);
161
* }
162
* \endcode
163
*
164
* <h2>Threads</h2>
165
*
166
* The library offers basic mechanisms to create, start and stop
167
* threads.<br>
168
* On Linux, it allows also to set scheduling parameters, thread affinity
169
* and signal handling.<br>
170
* <br>
171
* The simplest case to create a thread is to use the
172
* \ref onposix::SimpleThread class:
173
*
174
* \code
175
* void myfunction (void* arg);
176
*
177
* int main ()
178
* {
179
* int b;
180
* SimpleThread t (myfunction, (void*) b);
181
* t.start();
182
* t.waitForTermination();
183
* }
184
* \endcode
185
*
186
* For more complex cases, or if you need a better exchange of arguments
187
* (e.g., return values), create your own class by inheriting from
188
* \ref onposix::AbstractThread and specifying a run() method.
189
* Here we provide a small example. Information about all available methods
190
* can be found in the \ref onposix::AbstractThread class.<br>
191
*
192
* \code
193
* class MyThread: public AbstractThread {
194
* // Put thread arguments here
195
* public:
196
* MyThread() {
197
* // Initialize arguments here
198
* }
199
* void run() {
200
* // Put thread code here
201
* // Access arguments as normal attributes
202
* }
203
* };
204
*
205
* int main ()
206
* {
207
* MyThread t;
208
* t.blockSignal(SIGTERM);
209
* t.start();
210
* t.waitForTermination();
211
* }
212
* \endcode
213
*
214
* <h2>Mutual exclusion</h2>
215
*
216
* \code
217
* PosixMutex m;
218
* m.lock();
219
* //....
220
* m.unlock();
221
* \endcode
222
*
223
* <h2>Condition variables</h2>
224
*
225
* \code
226
* PosixCondition c;
227
* PosixMutex m;
228
* c.wait(&m);
229
* //....
230
* c.signal();
231
* \endcode
232
*
233
* <h2>File descriptors</h2>
234
*
235
* \code
236
* FileDescriptor fd ("/tmp/myfile", O_RDONLY);
237
* Buffer b (10);
238
* fd.read (&b, b.getSize());
239
* fd.close();
240
* \endcode
241
*
242
* It is also possible to use asynchronous read/write operations:
243
*
244
*
245
* \code
246
* void read_handler(Buffer* b, size_t size)
247
* {
248
* //...
249
* }
250
*
251
* FileDescriptor fd ("/tmp/myfile", O_RDONLY);
252
* Buffer b (10);
253
* fd.async_read (read_handler, &b, b.getSize());
254
* fd.close();
255
* \endcode
256
*
257
* <h2>Socket descriptors</h2>
258
*
259
* The library offers mechanisms for both connection-oriented (e.g., TCP) and
260
* connection-less (e.g., UDP) communications.
261
* Communications can be either AF_UNIX or AF_INET.
262
* Here is a small example about a TCP client-server application.
263
* More information can be found in the description of classes
264
* \ref onposix::StreamSocketServer, \ref onposix::StreamSocketServerDescriptor,
265
* \ref onposix::StreamSocketClientDescriptor,
266
* \ref onposix::DgramSocketClientDescriptor and
267
* \ref onposix::DgramSocketServerDescriptor.<br>
268
*
269
* Server-side:
270
* \code
271
* void read_handler(Buffer* b, size_t size)
272
* {
273
* //...
274
* }
275
*
276
* int port = 1234;
277
* StreamSocketServer serv (port);
278
* while (true) {
279
* StreamSocketServerDescriptor des (serv);
280
* Buffer b (10);
281
*
282
* // Synchronous read:
283
* des.read(&b, b.getSize());
284
*
285
* //...or asynchronous read:
286
* fd.async_read (read_handler, &b, b.getSize());
287
*
288
* // ...
289
* }
290
* \endcode
291
*
292
* Client-side:
293
* \code
294
* int port = 1234;
295
* std::string address = "192.168.10.133";
296
* StreamSocketClientDescriptor des(address, port);
297
* Buffer b (10);
298
* des.write(&b, b.getSize());
299
* \endcode
300
*
301
* <h2>Pipes</h2>
302
*
303
* \code
304
* Pipe p;
305
* Buffer b (10);
306
* p.read(&b, b.getSize());
307
* p.close();
308
* \endcode
309
*
310
*
311
* <h2>FIFOs (AKA "named pipes")</h2>
312
*
313
* \code
314
* FifoDescriptor fd ("/tmp/myfifo", O_RDONLY);
315
* Buffer b (10);
316
* fd.read (&b, b.getSize());
317
* \endcode
318
*
319
*
320
*
321
* <h2>Logging</h2>
322
*
323
* Log messages can be printed to console and/or to a file with different
324
* log levels.
325
*
326
* The following log levels can be set for both LOG_LEVEL_CONSOLE and LOG_LEVEL_FILE:
327
* <ul>
328
* <li> LOG_NOLOG: No logging
329
* <li> LOG_ERRORS: Only log errors
330
* <li> LOG_WARNINGS: Log warnings and errors
331
* <li> LOG_ALL: Log all messages (debug messages included)
332
* </ul>
333
*
334
* These values can be set directly inside the file include/Logger.hpp, or when including this file as follows:
335
*
336
* \code
337
* #define LOG_LEVEL_CONSOLE LOG_WARNINGS
338
* #define LOG_LEVEL_FILE LOG_ALL
339
* #include "Logger.hpp"
340
* \endcode
341
*
342
* To log a message, use
343
* \code
344
* LOG_FILE("/tmp/myproject");
345
* DEBUG("hello " << "world");
346
* DEBUG("something " << "strange");
347
* DEBUG("this is an error");
348
* \endcode
349
*
350
* <h2>Timing</h2>
351
*
352
* \code
353
* Time t1;
354
* std::cout << t1.getSeconds() << " " << t1.getNSeconds() << std::endl;
355
* \endcode
356
*
357
* <h2>Watching multiple descriptors</h2>
358
*
359
* \code
360
* class FileReader: public AbstractDescriptorReader {
361
* FileDescriptor fd1_;
362
* FileDescriptor fd2_;
363
* public:
364
* FileReader(){
365
* //...
366
* monitorDescriptor(fd1_);
367
* monitorDescriptor(fd2_);
368
* }
369
* void dataAvailable(PosixDescriptor& des) {
370
* std::cout << des.getDescriptorNumber() << "ready" << std::endl;
371
* }
372
* };
373
* \endcode
374
*
375
* <h2>Assertions</h2>
376
*
377
* Assertions provided by this library work also when code is compiled with the
378
* -NDEBUG macro.
379
*
380
* Example of usage:
381
* \code
382
* int i;
383
* VERIFY_ASSERTION(i == 0);
384
* \endcode
385
*
386
*
387
* <h2>Much more...</h2>
388
*
389
* The library also offer Observer designer pattern on descriptors (i.e., \ref onposix::DescriptorsMonitor), buffers (i.e., \ref onposix::Buffer)
390
* and shared queues (i.e., \ref onposix::PosixSharedQueue and \ref onposix::PosixPrioritySharedQueue).
391
*
392
* <br>
393
* <br>
394
* <h1>Support</h1>
395
*
396
* A <a href="http://sourceforge.net/p/onposix/mailman/">mailing list</a>
397
* is available on Sourceforge. Fell free to propose feedback, new features and possible improvements.
398
*/
399
400
src
main.cpp
Generated on Wed Apr 30 2014 15:27:24 for ONPOSIX by
1.8.1.2