You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
Sami Vaarala d79f10bd99 fix eventloop example callback argument handling (fixes #3) 11 years ago
..
Makefile implement a C eventloop example comparable to the Ecmascript one (still no sockets); simplify the Ecmascript eventloop version also; C code cleanup and reorganization 11 years ago
README.txt eventloop cleanups 11 years ago
basic-test.js fix eventloop example callback argument handling (fixes #3) 11 years ago
c_eventloop.c change eventloop example to use the function/number list helpers 11 years ago
c_eventloop.js fix eventloop example callback argument handling (fixes #3) 11 years ago
client-socket-test.js change Ecmascript eventloop to a singleton (no explicit instantiation which is pointless), other consistency fixes 11 years ago
curses-timers.js merge Ecmascript EventLoop and TimerManager classes to align better with the C example; exit primitive for easier valgrinding 11 years ago
ecma_eventloop.js fix eventloop example callback argument handling (fixes #3) 11 years ago
fileio.c change eventloop example to use the function/number list helpers 11 years ago
main.c fix protected/safe call sites in code examples to omit errhandler_index 11 years ago
ncurses.c change eventloop example to use the function/number list helpers 11 years ago
poll.c change eventloop example to use the function/number list helpers 11 years ago
server-socket-test.js eventloop cleanups for buffer handling, now using buffer virtual props 11 years ago
socket.c change eventloop example to use the function/number list helpers 11 years ago

README.txt

==================
Eventloop examples
==================

Overview and usage
==================

A few examples on how an event loop can be implemented with Duktape, mainly
illlustrating how the Duktape interface works (not how event loops should be
built otherwise).

To test (Linux only, perhaps other Unix)::

$ make
$ ./evloop curses-timers.js # run with Ecmascript eventloop
$ ./evloop -c curses-timers.js # run with C eventloop

Implementation approaches
=========================

There are several approaches to implementation timers. Here we demonstrate
two main approaches:

1. Using a C eventloop which calls into Javascript. All the event loop state
like timers, sockets, etc, is held in C structures.
(See ``c_eventloop.c`` and ``c_eventloop.js``.)

2. Using an Ecmascript eventloop which never returns. All the event loop state
can be managed with Ecmascript code instead of C structures. The Ecmascript
eventloop calls a Duktape/C helper to do the lowest level poll() call.
(See ``ecma_eventloop.js``.)

Services provided
=================

The event loop API provided by both examples is the same, and includes:

* Timers: setTimeout, clearTimeout, setInterval, clearInterval

* Sockets: simple network sockets

In addition there are a few synchronous API bindings which are not event loop
related:

* File I/O

* Curses, for doing beautiful character graphics

Limitations
===========

This is **not** a production quality event loop. This is on purpose, to
keep the example somewhat simple. Some shortcomings include:

* A production quality event loop would track its internal state (active
timers and sockets) much more efficiently. In general memory usage and
code footprint can be reduced.

* Buffer churn caused by allocating a new buffer for every socket read
should be eliminated by reusing buffers where appropriate. Although
churn doesn't increase memory footprint with reference counting, it
is slower than reusing buffers and might increase memory fragmentation.

* There is no way to suspend reading or writing in the example. Adding
them is straightforward: the poll set needs to be managed dynamically.

* The example uses poll() while one should use epoll() on Linux, kqueue()
on BSD systems, etc.

* Error handling is mostly missing. Debug prints don't interact well
with curses.