Interprocess communication

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
jl
Forum Commoner
Posts: 53
Joined: Tue Nov 09, 2004 12:05 am

Interprocess communication

Post by jl »

I'm experimenting with an IRC bot in PHP, and I have some functionality that I'd like to add to it to do things like monitor stuff on my Linux system, and I don't think I want the bot code and the monitoring code running in the same process, so.. what suggestions can I get for ways that two PHP applications running on the same system can communicate?

What I want to achieve is have the bot running as one process, and the monitoring system running as another. Whenever it needs to, the monitoring process will send a message (somehow) to the IRC bot process with a message that should be output.

The box this is running on isn't dedicated to this task, and is running a few sites with apache.

I was looking at webservices, but the problem with that is that the as far as I can tell with using libs like NuSOUP, the server has to be a complete standalone script, and the structure I'm going to have in the bot is a loop constantly checking for data, like:

while (true) {
if (theres data on the IRC socket)
read it and respond to it
** if (data from some other source, e.g. monitoring process)
read it and respond to it
}

So webservices still leaves the problem of how to inject the info. from the webservices server into the point marked ** in the bot's data reading loop.

Any input appreciated.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

You just need to edit the bot code a little so that it has a queue to hold what command it should be doing, and so on each loop the bot can read data from the socket and also do a task that is in the queue making it seem faster to several users at once. Then since the easy way to make this queue it to use a database you can simply have the other programmes run and put the results/data into the queue ready to be processed by the bot.
jl
Forum Commoner
Posts: 53
Joined: Tue Nov 09, 2004 12:05 am

Post by jl »

So you're saying my

if (data from some other source, e.g. monitoring process)
read it and respond to it

portion of code should be to check a database.. and to use a database as a queue of messages that other applications running on the system put messages into.

That would work, but I suppose I was looking for something a little more 'open' somehow, and checking a database is another large level of complexity.. and requires that everything that wants to send a message out through the bot should be able to write to a database, which ties me down to a large technology, as opposed to opening up options for lots of new things like web services seems to.

That would work, and work fine, but I wonder if there's any simpler way of achieving the injection of messages into the queue of the bot than a database?
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Well i think its a fairly simple process to insert something into the database from anything. If the language that "thing" is written in cannot communicate over a network then it can always call a local program to do it for them. And even then those programs dont have to deal with sql if you build a simple API to be used - just have a php script that gets the data from the user and let that communicate with the database.

And then as i said the use of the database to store the objects in the queue is useful for continuing processing if the bot crashes and also in making the bot faster to respond.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

if i wanted to do something like that i would get myself programs screen and irssi. and then i would write a little perl script that writes all incoming text to a database. But probably there are already a gazillion of eggdrop scripts that do this stuff...

and php i would use to generate nice html pages based on the data in the db. i don't like using php for reading sockets etc...
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

there's a number of ways (other than db) for processes to intercommunicate: [php_man]sem[/php_man] (semaphores, shared memory and message queues), [php_man]posix_mkfifo[/php_man] (named pipes), [php_man]fsockopen[/php_man] (Unix/TCP sockets) and, finally, plain old files ;)
jl
Forum Commoner
Posts: 53
Joined: Tue Nov 09, 2004 12:05 am

Post by jl »

I think the SYSV interprocess communicatoin stuff was what I'm looking for, although the structure I'm going to use (I think) will be a dedicated PHP web service application that then relays web service requests to the bot, so the outward facing interface will be web services, and I could have used any method for the 'internal' web service <> bot communication part.

Thanks for the input.
Post Reply