lua-users home
lua-l archive

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


On Tue, 9 Jun 2015 16:29:43 -0700
Brigham Toskin <...> wrote:
> Rather than trying to share access to your globals with a subprocess, it
> might be better to use a configuration chunk, command line options, or
> piped input.
> 
> What are you actually trying to do, though?

This is all about client-server interconnection in a casual task.
As I am not about to deal with relatively low-level socket programming
I use socat (http://www.dest-unreach.org/socat/) utility to perform
networking I/O. It is a cool tool.

socat can serve as unix' inetd forking a new instance of the some $program
on every incoming client connection. The $program interacts with the client
through its stdin/stdout streams.
In the meantime the $program can launch a child socat process which redirects
its stdin/stdout to/from a specified outgoing network connection to
connect to the server. By using streams to/form this subprocess'
stdin/stdout the $program can interconnect the client and the server.
That way, I need a simple "glue" code in the $program to cross join the
streams from the both sides.

I've written this task currently in a bash script.
The following is a very simplified but full functional example for the case
when the client-server interaction is just a one text string request-response.

On bash command line:
$ ListenPort=...
$ ListenAddress=...
$ ServerPort=...
$ ServerAddress=...
$ socat tcp4-listen:$ListenPort,bind=$ListenAddress,reuseaddr,fork \
        exec:"interconnect.sh $ServerAddress $ServerPort"

------------- interconnect.sh -------------
#! /bin/bash

declare server_address="$1" server_port="$2"
declare client_request server_response

forward-to()
{
  echo "$client_request" | socat stdio "$@"
}

read client_request
read server_response < <(forward-to tcp4:"$server_address":"$server_port")
echo "$server_response"
-------------------------------------------

The above script is really simple and clean. [1]

I'm leaning lua now and the "PiL" book phrase:
"More than an extensible language, Lua is also a glue language.
Lua supports a component-based approach to software development,
where we create an application by gluing together existing high-level
components."
has inspired me to try reimplement this task in lua:
------------- interconnect.lua -------------
#! /bin/lua

local function forward2(...)
-- ??? --
end

local client_request = io.read('*L')
local server_response = io.popen(-- ??? --):read('*L')
io.write(server_response):flush()
--------------------------------------------

And I've faced with problems in solving this task for
joining two components through their stdin/stdout streams in lua.
I feel that such an approaches as lua-ex
(http://lua-users.org/wiki/ExtensionProposal) and all the mentioned third
party lua libraries are trying to solve the problems of the same type.

---

[1] But in such a simplified version it is not useful at all
as the same (and more comprehensive) effect can be achieved in a single
socat command without needing any intermediate script:
$ socat tcp4-listen:$ListenPort,bind=$ListenAddress,reuseaddr,fork \
        tcp4:$ServerAddress:$ServerPort
---

> Does it actually need to be in another process?
> Using loadstring (or its cousins) would probably work just
> fine in most cases.

Of course it does not need to be.
How can I load a chunk with the different io.stdin/stdout stream descriptors
for that chunk?

Thanks.

-- 
Mike