Re: Daemonizing CRM114 using FIFO

Bill Yerazunis <[email protected]>
Newsgroups gmane.mail.spam.crm114
Message-ID <[email protected]>
   From: James Lee <csejl-/[email protected]>

   Thanks for your detailed response.  However, my question wasn't so
   much about how to solve this sort of problem in general but about
   how to do it specifically in CRM114.

   First, let me tell you that this will be an asynchronous solution.
   When it's all said and done, the client will send a unique ID along
   with every message and the :do_it: CRM function will parse the
   classify result and write the unique ID and its category number to
   result.txt.  There will be another process (a thread actually) that
   reads the result file at a later time.

   I thought about using WINDOW statement to parse the content into
   messages but it can only read from stdin, correct?  In other words,
   can I use WINDOW statement to read the input from a FIFO?  Reading
   from a file is not an option for me...

   Let's say I go with the lengh+content solution where 1+ message(s)
   are delivered at a time via a FIFO.  How would I write my :do_it:
   function so that when it's called (close() is received) it outputs
   the correct number of results before exiting?  Would I use MATCH
   and LIAF?  An example would help a lot since this would be my first
   attempt at writing a CRM114 script.

James:

Here you go - one example script.  This one is meant to 
read from a pipe.  Just do a 

  mkfifo /tmp/myfifo

and then run the pipesucker program as in

  crm pipesucker.crm < /tmp/myfifo.

in one terminal window, then

   cat > /tmp/myfifo

to push stuff in from another terminal window.

It seems to work fine for me, modulo the sleep time (and no, I don't
see an easy way to not sleep() given that select() can't even detect
when a pipe is unconnected at the other end.).  So, the problem 
seems to be with Unix itself; a pipe once opened and then closed
remains available to read immediately forevermore.  

If I can come up with a workaround, I'll put it in.  Until then,
here's the "pipesucker.crm" example program.  

   - Bill Yerazunis

#! /usr/bin/crm114
#   Program to read from a pipe to EOF, process it, and go 'round again.
#   use "mkfifo" to make a pipe for this program to suck on.  (yes, you
#   write to the pipe just like it was a file.)
#
#   Note however that there is (as far as I know) no capability in *nix
#   to do a "wait for read" past the initial pipe/file creation/opening.
#   Specifically, select() will NOT do this.  The result is that a read
#   on an _unconnected_ pipe completes immediately with no warning, but
#   once the other end of the pipe opens, it all works fine.
#
#   So, we read in a spin-loop with a 0.5 second sleep and that gives us
#   both an adequately quick response without consuming all available CPU.
#
#!
window
output /Starting up the pipesucker\n/
isolate (:in_text:)
{
    {
        input ( :in_text: )
	match <absent> [:in_text:] /./
	output /./                        ### DELETE THIS FOR SILENT OPERATION
	syscall /sleep 0.5/
	liaf
    }
    {	
       ####  Here's where we do some basic processing on the
       ####  input.  Right now, that is just "contains a foo" and "stop"
       ####  or not.  Put your demonic code here.

        output /--- Read from input --- \n :*:in_text:\n --- END ---/
	{
		match [:in_text:] /foo/
		output / ... and it contains a "foo"\n/
	}
	alius
	{
		match [:in_text:] /stop/
		output / All done for now. \n/
		exit
	}
	alius
	{
		output / ... and it did not contain a "foo"\n/
	}	
    }	
    liaf
}



-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.