"ratchet" socket control library

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

"ratchet" socket control library

Ian Good
Hey all,

Wanted to introduce ratchet, a new library I've been working on. Ratchet is a socket mechanism similar to Python's gevent or a combination of Lua's luasocket+copas+ssl. The idea is, your code looks synchronous, but while waiting for one event it works on another. I attached an example at the bottom of this message.

Asynchronous, "thread-like" access to:
 * TCP/UDP socket client and server support
 * SSL socket wrapper
 * DNS queries (thanks to dns.c: http://25thandclement.com/~william/projects/dns.c.html)
 * Timing mechanisms (via timerfd)
 * ZeroMQ messaging library

Extra mechanisms:
 * Buffered sockets
 * HTTP/1.0 client and server sample library
 * SMTP client and server sample library

The code includes 20 integration-style tests covering all major features and extensive API documentation. The website includes an extensive usage manual and the API docs.

Website: http://ratchet.icgood.net/
Source: http://github.com/icgood/ratchet

Ian Good



------------------------------------------------------------------------

require "ratchet"
require "ratchet.http.client"

function http_query(i)
    local rec = ratchet.socket.prepare_uri("tcp://google.com:80")
    local socket = ratchet.socket.new(rec.family, rec.socktype, rec.protocol)
    if not socket:connect(rec.addr) then
        error("Could not connect to google.com on port 80!")
    end

    local client = ratchet.http.client.new(socket)
    local code, message, headers, data = client:query("GET", "/")

    assert(200 == code, "[200] != [%s] (%s)":format(code, message))
    print(i .. ": Received html data from google.com.")
end

kernel = ratchet.new()
for i=1,10 do
    kernel:attach(http_query, i)
end
kernel:loop()



Reply | Threaded
Open this post in threaded view
|

Re: "ratchet" socket control library

Josh Simmons
On Thu, Sep 29, 2011 at 12:10 AM, Ian Good <[hidden email]> wrote:

> Hey all,
>
> Wanted to introduce ratchet, a new library I've been working on. Ratchet is a socket mechanism similar to Python's gevent or a combination of Lua's luasocket+copas+ssl. The idea is, your code looks synchronous, but while waiting for one event it works on another. I attached an example at the bottom of this message.
>
> Asynchronous, "thread-like" access to:
>  * TCP/UDP socket client and server support
>  * SSL socket wrapper
>  * DNS queries (thanks to dns.c: http://25thandclement.com/~william/projects/dns.c.html)
>  * Timing mechanisms (via timerfd)
>  * ZeroMQ messaging library
>
> Extra mechanisms:
>  * Buffered sockets
>  * HTTP/1.0 client and server sample library
>  * SMTP client and server sample library
>
> The code includes 20 integration-style tests covering all major features and extensive API documentation. The website includes an extensive usage manual and the API docs.
>
> Website: http://ratchet.icgood.net/
> Source: http://github.com/icgood/ratchet
>
> Ian Good
>
>
>
> ------------------------------------------------------------------------
>
> require "ratchet"
> require "ratchet.http.client"
>
> function http_query(i)
>    local rec = ratchet.socket.prepare_uri("tcp://google.com:80")
>    local socket = ratchet.socket.new(rec.family, rec.socktype, rec.protocol)
>    if not socket:connect(rec.addr) then
>        error("Could not connect to google.com on port 80!")
>    end
>
>    local client = ratchet.http.client.new(socket)
>    local code, message, headers, data = client:query("GET", "/")
>
>    assert(200 == code, "[200] != [%s] (%s)":format(code, message))
>    print(i .. ": Received html data from google.com.")
> end
>
> kernel = ratchet.new()
> for i=1,10 do
>    kernel:attach(http_query, i)
> end
> kernel:loop()
>
>
>
>

This looks really fantastic, I'm getting it set up to have a play now.

One minor and bloody annoying (damn you lua package maintainers
everywhere) thing is that the .pc file is named lua5.1 on debian based
systems, and lua51 on some others iirc. Not sure if there's an easy
way to support these various names in the autotools script.

The lua detection seems a bit hairy too, I had to patch it slightly to
get it to detect interpreter version properly, would it not be easier
to just search with pkg-config and be done with it? It wasn't finding
the right headers either but I gave up on that problem and just
inserted a pkg-config lua check into the build.

I had to disable SSLv2 as well because it has been removed from debian
OpenSSL due to vulnerabilities.

It has built now though! So I'll get to the real business.

Cheers,
Josh.

Reply | Threaded
Open this post in threaded view
|

Re: "ratchet" socket control library

Miles Bader-2
Josh Simmons <[hidden email]> writes:
> One minor and bloody annoying (damn you lua package maintainers
> everywhere) thing is that the .pc file is named lua5.1 on debian based
> systems, and lua51 on some others iirc. Not sure if there's an easy
> way to support these various names in the autotools script.

Just check for 'em all... :]

Also remember that "configure.ac" files are actually shell scripts, so
you can put your tests in loops... (with code to break out of the loop
as soon as a test finds something)

So maybe something  like:

   have_liblua=no
   for lua_pc_name in lua lua5.1 lua-5.1 lua51; do
     PKG_CHECK_MODULES([liblua], [$lua_pc_name], [have_liblua=yes], [:])
     test $have_liblua = yes && break
   done

Would work...  The PKG_CHECK_MODULES which succeeded will have set
liblua_CFLAGS and liblua_LIBS (if none succeeded, have_liblua will still
be "no").

Of course, many systems have Lua libraries installed, but no .pc file
for them, so if have_liblua is still "no" after that, it's probably a
good idea to have _another_ loop which checks for lua libraries the
traditional way (AC_CHECK_LIB, etc).

[I use something similar, but not exactly this, so I haven't tested
the above.]

-miles

--
Mayonnaise, n. One of the sauces that serve the French in place of a state
religion.

Reply | Threaded
Open this post in threaded view
|

Re: "ratchet" socket control library

David Manura
In reply to this post by Ian Good
On Wed, Sep 28, 2011 at 10:10 AM, Ian Good
> Wanted to introduce ratchet, a new library

One style point: The code [1] defines modules like

  require "package"
  local common = require "ratchet.http.common"
  module("ratchet.http.server", package.seeall)
  local class = getfenv()
  __index = class
  function new(socket, from, handlers, send_size)
     local self = {}
     setmetatable(self, class)
     .....
  end

I think you can replace getfenv() here with _M.

[1] https://github.com/icgood/ratchet/blob/master/src/http/server.lua