Last active
August 29, 2015 13:57
-
-
Save pauldub/9879606 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- This is my current thinkings about the main interface provided by the library and | |
-- there are a few things I am unable to do at the moment. | |
-- So my problem is about how can I get the function "dnode:connect" and "dnode" callable | |
-- at the same time. I tried both with tables and functions but without success. | |
-- Helper tcp connect and listen interface. | |
local dnode = require('dnode').dnode | |
-- or, depending on how I get around the problem stated above | |
local dnode = require('dnode') | |
-- Starting a dnode server. | |
-- This should not be a problem to implement. | |
local server = dnode({ | |
foo = function(reply) | |
reply("very foo") | |
end | |
}) | |
server:listen(port) -- Returns dnode instance with self.stream = tcp listener or | |
-- just the tcp listener? | |
-- | |
-- Maybe it can always return the listener but also set self.stream | |
server:listen(port, ... --[[ host, listening_cb ]]) -- There is only a callback because | |
-- the underlying library doesn't | |
-- propagate emit the 'listening' | |
-- event to the caller (maybe i'm | |
-- wrong or should use a socket | |
-- directly). | |
-- | |
-- Maybe I should propagate it | |
-- by wrapping / hooking the | |
-- callback? | |
-- You can also use this syntax. | |
server:listen({ port = number, host = string}--[[ , listening_cb ]]) | |
-- Calling the destroy method will end the tcp server too. | |
server:destroy() | |
-- Starting a dnode client. | |
-- node.js dnode's documentation example: | |
local d = dnode:connect(port) | |
d:on('remote', function(remote) | |
-- ... call remote functions | |
end) | |
-- Object oriented interface. DNode can be :extend() 'ed | |
-- | |
-- This is how it works right now and should be easy to maintain | |
-- along with the other one. | |
-- | |
-- It will probable move be fully implemented by the current Server class | |
-- under the file lib/dnode/dnode.lua | |
-- init.lua only providing helper functions and quick access to interfaces. | |
local DNode = require('dnode').DNode | |
d = DNode:new({ | |
foo = function(reply) | |
reply("very foo") | |
end | |
}) | |
-- Helpers are also available | |
server = d:listen(port, ... --[[ host, listening_cb ]]) | |
server = d:listen({ port = number, host = string}--[[ , listening_cb ]]) | |
-- Client usage | |
d = DNode:new() | |
d:connect(port, ... --[[ host, cb ]]) | |
d:on('remote', function(remote) | |
remote.foo(function(response) | |
assert(response == 'very foo') | |
end) | |
end) | |
-- I think both interfaces are in very similar and quite close to the original. | |
-- But I'm not yet profficient enough in Lua so I'm still looking into it. | |
-- | |
-- Also there are other things I don't talk here, like client-side refs. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment