Running Node.js on STM32 - c

Iam working yet with C program on STM32 microproc, what contains a web server, accesible by the user via web gui(HTML and javascript files). The web gui part became more complex, and it needs higher level operations.
The questions: is it possible to embed a node.js program with some node modules? Does it work with the C webserver, or the node program have to make the webserver, and communicate with the C program?
Or there is an other solution what is better in this case?
This question maybe seems dumb, but i didnt find documentation about it.

After a research I found some solutions:
Node.js for Embedded Systems
The book can then guide you to jerryscript which:
is a lightweight JavaScript engine for resource-constrained devices
such as microcontrollers
You can find there that it's also used with the STM32-Discovery board.
Node.js on clientside
This article guides to Browserify which allows to run node on clientside.
Just make it simpler
You could use HTTPD implementation shipped with LwIP. There is a script called makefsdata which allows to convert html, js, css ... files into c-arrays. This implementation also supports POST method.

Related

Custom protocol support

I am not finding documentation for custom protocol support.
From what I understand, Gatling has core engine that does scheduling, thread management etc, and protocol support is designed as an Actor ?
I am trying to develop a custom protocol (thats basically a shell script that will talk to an external service). The latest reference documentation does not seem to have any reference to how to do this ? Any pointers will be greatly appreciated.
If you need to stress test something that is implemented in a shell script, then Gatling probably isn't the best fit. Gatling is designed for stress testing networking protocols. So unless you can duplicate what your shell script is doing in Gatling expressed in networking protocols, you then might want to use something else.
Secondly, if you did implement it, I would check with the core developers of Gatling if it's something that they would consider including (use a github issue to ask). Since the applications of this might not be widespread, they may choose to not include it in their project. If that's the case you would have to either run your own fork with the implementation or add some sort of plugin architecture to Gatling for 3rd part extensibility.
So my suggestions are:
Decompose your shell script into the specific network protocol parts you're interested in stress testing implementing in Gatling.
Use a different tool that's designed to running multiple shell scripts at once for stress testings. Something like GNU Parallel if you're on a Linux box.
Implement it yourself. There's no documentation on how to do this. However a good starting example would be the JMS Protocol Implementation to give you an idea of all that's involved.

embedding a headless browser in C

Is any of env-js, phantom JS, slimmerJS or any of the headless browsers embeddable in a C application? This is what I need to do: I have a C application that connects to a couple of servers through HTTP & HTTPS. Up until now, I got every page, figured out what it did, (mainly Javascript), extracted the parts of code that I needed (I also implemented a very simple string parser/extracter) and implemented the flow by sending the HTML code through a (secure)Socket and reading back the response. That part still works smoothly.
That was until I bumped into a set of very complex (read: obfuscated and looong) javascript pages linked one after the other, with several scripts included, and server side programming, and then I realized that I wouldn't be able to get a "maintainable" program with the procedure I've taken.
So I have spent the last several days looking for an embeddable Javascript interpreter. Bumped into spider monkey which is embeddable in C, but since I don't have any control over the scripts received, its lack of the DOM implementation makes it unsuitable. I also considered implementing the DOM interface, but honestly is a way too long distraction from my main proyect.
Then I considered the headless browsers mentioned above. Have read all the information I found about them, looked for some sort of library to interface with them, and in abscence of such librarys, have considedered hacking the code, only to realize that even trying to hack phantomJS to embedded it in my C system would be even crazier than implementing the DOM interface in spider monkey.
The system currently works in Windows and I'm using MinGW to develop it, but its final target is to be implemented in a Raspberry PI, so the more I can have plain straight C source code, the easier it will be to move the system to its final destination. With this I mean: I can use windows libraries in the meantime if they are pre-built, but can't miss the need that they must be compilable and generated in a plain compiler. I don't have yet the Raspberry PI, but I'm not expecting any fancy development tool set (I might be wrong on this)
Lastly, for the curious inside, the system is a stock screener, generates graphics with indicators which are put in a web server and generates alerts (to send notifications of price conditions) through Yahoo Messenger (this choice was mainly due to portability and availability of source code).
I will really appreciate your help in find a way to implement/embed in C *any* Javascript interpreter that has a DOM interface implemented.
Regards.
Alfredo Meraz

I'm searching a cgi lib in C to build a restful web service

I want to build a restful (CoAP) web service which can execute c code to handle events.
Therefore I'm searching a lib which provides me with a rest api in C and cgi similar to
restcgi which is sadly in c++ or CGI-Simple which is in perl.
The server is running on a embedded device so it has very limited resources and the services will be accessed only by machines.
Thank you very much.
You may be interested in Raphters framework and its architecture. It's pretty small, so you can examine the code, the framework itself can be used as a FastCGI backend for some web server, e.g. for nginx.
I have recently came across one quite interesting CoAP library which uses libevent. You will aslo want to check Klone embeddable HTTP server by the same guys at KoanLogic. I have previously looked at libcoap, but it didn't find it very usable at the time. You may also wish to try using either libuv, libev or libevent. But I guess it's probably gonna be much easier to adopt some of the code from WT repository and get your CoAP/HTTP server done.

high performance application webserver in C/C++

Is there any high performance (ideally evented and open source) web server in C or C++?
I'd like to be able to use it in that it calls a method/function in my application with a filled out HTTP Request class/struct, and then I can return a filled out HTTP Response class/struct to it.
If it isn't open source, I'd need built in support for long-polling connections, keep-alive, etc—otherwise, I think that I can add these things myself.
If you don't know of any such servers available, would you recommend writing my own web server to fit the task? It cannot be file-based, and must be written in high-performance C/C++.
Edit: I'm thinking something like the Ruby Mongrel for C, if that helps.
I had the very same requirements for my job, so I evaluated a number of solutions: mongoose, libmicrohttpd, libevent. And I also was thinking about writing nginx modules. Here is the summary of my findings:
nginx
nginx project page
I love this server and use it a lot. Its performance and resource usage is much better than that of Apache, which I also still use but plan migrating to nginx.
Very good tunable performance. Rich functionality. Portability.
Module API is not documented and seems to be very verbose. See this nginx hello world module as example.
Nginx does not use threads but uses multiple processes. This makes writing modules harder, need to learn nginx API for shared memory, etc.
mongoose
mongoose project page
All server's code is in single mongoose.c file (about 130K), no dependencies. This is good.
One thread per connection, so if you need concurrency you've got to configure lots of threads, ie. high RAM usage. Not too good.
Performance is good, although not exceptional.
API is simple but you have to compose all response HTTP headers yourself, ie. learn HTTP protocol in detail.
libmicrohttpd
libmicrohttpd project page
Official GNU project.
Verbose API, seems awkward to me, although much more simple than writing nginx modules.
Good performance in keep-alive mode (link to my benchmarks below), not so good without keep-alive.
libevent
libevent project page
Libevent library has built-in web server called evhttp.
It is event based, uses libevent for that.
Easy API. Constructs HTTP headers automatically.
Officially single-threaded. This is major disadvantage. I've found a hack, which makes several instances of evhttp run simultaneously accepting connections from the same socket. Not sure if it is all safe and robust.
Performance of single-threaded evhttp is surprisingly poor. Multi-threaded hack works better, but still not good.
G-WAN
G-WAN project is not open source, but I'd like to say a few words about it.
Very good performance, low memory usage, 150 KB executable.
Very convenient 'servlet' deployment: just copy .c file into csp directory, and running server automatically compiles it. Code modifications also compiled on the fly.
Simple API. Although constrained in some ways. Rich functionality (json, key-value store, etc.).
Unstable. I had segfaults on static files. Hangs on some sample scripts. (Experienced on clean install. Never mixed files of different versions).
Only 32-bit binary (not anymore).
So as you can see, none of existing alternatives have fully satisfied me. So I have developed my own server, which is ...
NXWEB
NXWEB project page
Feature highlights:
Very good performance; see benchmarks on project page
Can serve tens of thousands concurrent requests
Small memory footprint
Multi-threaded model designed to scale
Exceptionally light code base
Simple API
Decent HTTP protocol handling
Keep-alive connections
SSL support (via GNUTLS)
HTTP proxy (with keep-alive connection pooling)
Non-blocking sendfile support (with configurable small file memory cache; gzip pre-encoded file serving)
Modular design for developers
Can be run as daemon; relaunches itself on error
Open source
Limitations:
Depends on libev library (not anymore)
Only tested on Linux
I would suggest to write a FastCGI executable that can be used with many high performance web servers (even closed source ones).
mongoose: one file. simple and easy to use. not an asycn io but perfect for embedded and particular purposes.
gwan. excellent. no crashes. ultra well planned configuration. very smart and easy for c/c++ development in other words, very clean sensible api compared to nginx. provides a thread per core. or whatever you specify. a great choice. largest disadvantage (maybe im lacking in this area): cannot step thru code.
libevent: single thread is not a disadvantage on a single core machine. afterall its point is an async i/o. does have multithreads for other cores.
nginx: no personal experience. gaining serious ground on a-patchy server. (terribly confusing api)
boost asio: a c++ library for asynchio (asio). awesome. needs a friendly higher-level api for simpletons like myself. and others who come from php, java, javascript, node.js and other web languages.
python bottle: awesome 1 file lib (framework/system) that makes it easy to build python web apps. has/is a built in httpd server, like libevent and node.js
node.js: javascript asyncio server. an excellent selection. unfortunately, have to program in javascript that does become tedious. while there is something to be said for getting the job done; there is also something to be said for enjoying yourself during the process. hopefully no ones comes up with node.php
I'm going to suggest the same thing as Axel Gneiting - but have provided an answer with my reasons for taking this approach:
1) HTTP is not trivial as a protocol - writing your own server or amending an off-the-shelf solution is a very complex task - a lot more complex than using the available APIs for implementing a separate processing engine
2) Using (an unmodified) mainstream webserver should provide you with more functionality than you require (so you've got growing room).
3) Using (an unmodified) mainstream webserver will usually mean that it has been far more extensively tested and documented than a homebrew system.
4) .. and its more likely to be secure and stable.
5) Using fastCGI you can use all sorts of languages to develop your back-end processing in - including C++ and C. There are standard toolkits available to facilitate this.
6) alternatively many webservers provide support for running interpreter engines in-process (e.g. mod_php, mod_perl). I'd advise against running your own native code as a module though.
It cannot be file-based.
Eh? What does that mean?
I'm an avid nginx user; nginx is written in C; nginx seems like it could work for you. If you want the very best speed out of nginx, I would make a nginx module. Here are 3rd party modules which you can examine to get an idea of what it requires.
As for the long polling requirement, you might want to have a look at the http push modules.

c code for web pages in rl-rtx

we have to work with RL-RTX (RTOS) in our project.in that we have to do some web pages.we have experience in building web pages in linux using "go-ahead webserver".
can we code in c language and store that executable in .cgi extension and call from the browser?
Yes, you can. Almost all web servers can be configured to serve cgi. You could use something like libcgi to handle the interface in your c code. Still, it would probably be more efficient to use some kind of scripting language rather than c.

Resources