bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gawk] Adding an API hook to enable a select extension


From: Manuel Collado
Subject: Re: [bug-gawk] Adding an API hook to enable a select extension
Date: Fri, 31 May 2013 23:57:42 +0200
User-agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/17.0 Thunderbird/17.0

El 31/05/2013 20:48, Eli Zaretskii escribió:
Date: Fri, 31 May 2013 14:15:42 -0400
From: "Andrew J. Schorr" <address@hidden>
Cc: address@hidden, address@hidden

BEGIN {
    server["/inet4/tcp/0/127.0.0.1/12345"] = ""
    server["/inet4/tcp/0/127.0.0.1/23456"] = ""
    for (i in server)
       PROCINFO[i, "READ_TIMEOUT"] = 0
    while (length(server) > 0) {
       for (i in server) {
         if ((rc = (i |& getline x)) > 0)
            printf "Server %s says: %s\n", i, x
         else if (rc != TIMEOUT) {
            printf "Error reading from %s: %s\n", i, ERRNO
            close(i)
            delete server[i]
         }
       }
       sleep(1)
    }
}
[snipped]

IOW, if the Awk programmer wants an event loop, she should just say
so, and Gawk will DTRT.  Let's not ask the programmer to program all
that explicitly.  Because if we do, we will next need to tell them to
handle interrupted system calls, signals, and whatnot.  If I need to
do all that, I'd rather code it all in C.

You asked for ideas. Well, here is one. Hope not to make too noise.

There could be an analogy between serial input from a list of files (the default awk behavior) and concurrent input from a set of sockets, one record at a time:

FILENAME  <-> SOCKETNAME
ARGIND    <-> SOCKET
$0        <-> $0
...       <-> ...

The set of active sockets could be managed by opensocket() and closesocket() primitives.

Both modes (input files or sockets) could be intermixed. If there are no active sockets, awk will behave as usual, reading from argument files.

Sample user code, no explicit loops nor sleep():

BEGIN {
   # set socket handles
   s1 = opensocket("/inet4/tcp/0/127.0.0.1/12345")
   s2 = opensocket("/inet4/tcp/0/127.0.0.1/23456")
   TIMEOUT = 1.0
}

TIMEDOUT {
   print "No response for " TIMEOUT " seconds, retrying"
   next
}

ERRNO {
   print "Error " ERRNO " reading from current socket set"
   closesocket(s1)
   closesocket(s2)
   exit
}

{
   print "Received from " SOCKETNAME ": " $0
}

SOCKET == s1 {
   # one record from s1 is enough,
   # stop reading from s1, but
   # keep reading from s2
   closesocket(s1)
}

SOCKET == s2 && /exit/ {
   print "Exit requested from " SOCKETNAME
   closesocket(s2)
}

Hope you get the idea. Perhaps too ambitious.


Awk is not a system programming language.  It is higher-level, so it
should conceal such details from the programmer.  Like it does with
memory allocation, for example.  IMO, at least.

Agreed.

Regards.
--
Manuel Collado - http://lml.ls.fi.upm.es/~mcollado




reply via email to

[Prev in Thread] Current Thread [Next in Thread]