How to IPC between PHP clients and a C Daemon Server?
I suspect Thrift is what you want. You'd have to write a little glue code to do PHP <-thrift-> C++ <-> C, but that would probably be more robust than rolling your own.
What is a good Inter-Process Communication method between C and PHP in Linux
Possibly the simplest solution you can find is to use pipes. The processes would have an open pipe for reading "calls" and answering them in the same fashion.
One possible way of setting this up is to have a pair of named pipes (mkfifo
) in a specific or variable location. Such pipes are known to both this process and PHP. The process would block reading a loop for requests/commands in some textual "protocol" and write back to PHP through the other pipe. In this way both PHP and the external processes could be stopped/killed and restarted and the communications path would still be stable.
You might need to do something else in order to verify whether the process is actually running, if this is needed, but a simple "ping" command over this "protocol" would be enough.
This assumes:
- you have the possibility of making improvements to the processes that communicates to the hardware (otherwise, your are bound to whatever it already offers)
- you don't have high performance requirements (pipes are relatively slow)
- there's no parallelism problem in case of concurrent accesses from the PHP script to process (unless you do some locking, 2 concurrent requests would be written mixed in the pipe)
There are off course other ways of achieving this, but I find hard to consider other way that is as simple as this. Queueing (d-bus, others), as suggested in some comments, are just building on top of this idea but adding more complexity IMHO, therefore, if no functionality provided by these other services is needed, pipes should be enough.
communicate c program and php
Some simple solutions I can think of are:
Redis
You could use redis as your ipc using hiredis as your c client library. I never used hiredis library before but did it just now for you to test and the library is really good. I could have known it because redis is the best piece of C code I know :).
modify example.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hiredis.h"
int main(void) {
redisContext *c;
redisReply *reply;
c = redisConnect((char*)"127.0.0.1", 6379);
if (c->err) {
printf("Connection error: %s\n", c->errstr);
redisFree(c);
exit(1);
}
/* Blocking pop. */
reply = redisCommand(c, "BLPOP php 0");
if (reply->type == REDIS_REPLY_ARRAY) {
if (reply->elements == 2) {
printf("%s\n", reply->element[1]->str);
}
}
freeReplyObject(reply);
redisFree(c);
return 0;
}
Compile and run example:
make
./hiredis-example
from ./redis-cli:
from another tab start start redis-cli(for prototyping) and issue the following command. You should replace this with predis as php client library, but that is going to be very easy:
lpush php "Hello from PHP"
Inside running hiredis-example:
You should see the message "Hello from PHP". Easy as pie if you ask me :).
Named pipes
You could use a named pipes.
Sockets
You could read the Beej's Guide to Network Programming Using Internet Sockets. In my opinion this is a very good read.
How best to control (issue command signals to) a PHP-based Daemon script?
A possible solution would be to use signals -- see pcntl_signal
, for instance : your PHP daemon would listen for signals, and you'd only have to send signals from "outside".
That's a way that is quite often used on UNIX/Linux -- but note that pcntl_*
functions will not be available on windows. (The class you posted is already using pcntl*
functions, so nothing new here)
An elegant API for a C-based remote-control / daemon (IPC / RPC)?
I can't recommend a ready-to-go library - Instead I suggest you look at Music Player Daemon. It uses a very simple text protocol to control the entire application remotely. There are several clients (with code) you can look at too.
Linux daemon as external agent polling PHP webapp
Question 1: What programming language would you suggest for this daemon?
For now I started to use PHP (PHP-CLI) for the agent. (Because I use PHP on the server side as well, this was easiest)
Question 2: Is there a standard framework or polling protocol which I can use?
I started with Laravel Passport API: https://laravel.com/docs/master/passport
In combination with Guzzle: http://docs.guzzlephp.org/en/stable/
Related Topics
Remove/Replace the Username Field with Email Using Fosuserbundle in Symfony2/Symfony3
Static Class Initializer in PHP
Codeigniter $This->Db->Get(), How to Return Values for a Specific Row
Search for Highest Key/Index in an Array
Install PHP-Mcrypt on Centos 6
Can Anyone Give Me an Example for PHP's Curlfile Class
Get All Records from MySQL Database That Are Within Google Maps .Getbounds
PHP Notice: Undefined Offset: 1 with Array When Reading Data
What's the Best Way to Create a Single-File Upload Form Using PHP
Using Clean Urls in Restful API
How to Stop Gd2 from Washing Away the Colors Upon Resizing Images
Why Do I Get "Resource Id #4" When I Apply Print_R() to an Array in PHP
Sending Post Parameters with Postman Doesn't Work, But Sending Get Parameters Does
Extract Floating Point Numbers from a Delimited String in PHP