Module jls.util.Channel
Allows to send and receive messages.
Provides a local message passing interface suitable for process and thread event based message passing.
The messages are sent and received as string on a channel. The goal is to abstract the message transport implementation, that can internally be a queue, a pipe or a socket.
The channel resource is represented by an opaque string and can be generated automatically. Internally using URI with authentication keys, pipe://pub.priv@local/p12345 or tcp://pub.priv@localhost:12345.
This interface is used for worker that abstract the thread.
Usage:
local channelServer = Channel:new() channelServer:acceptAndClose():next(function(acceptedChannel) acceptedChannel:receiveStart(function(message) print(message) acceptedChannel:close() end) end) local channel = Channel:new() channelServer:bind():next(function() local name = channelServer:getName() -- after bind the server provides a name for connection return channel:connect(name) end):next(function() channel:writeMessage('Hello') end) event:loop()
Class Channel
Channel:new () | Creates a new Channel. |
channel:close ([callback]) | Closes this channel. |
channel:onClose () | Returns a promise that resolves once the channel is closed. |
channel:getName () | Returns the name of this channel. |
channel:bind ([closeWithAccepted[, scheme]]) | Binds this channel. |
channel:onAccept (ch) | Accepts a new channel. |
channel:connect (name) | Connects this channel to the specified name. |
channel:receiveStart (handleMessage) | Starts receiving messages on this channel. |
channel:receiveStop () | Stops receiving messages on this channel. |
channel:writeMessage (payload[, id[, callback]]) | Writes a message on this channel. |
Class Channel
The Channel class.
- Channel:new ()
- Creates a new Channel. A channel can be a server using the bind method or a client using the connect method but not both.
- channel:close ([callback])
-
Closes this channel.
Parameters:
- callback function an optional callback function to use in place of promise. (optional)
Returns:
-
jls.lang.Promise
a promise that resolves once the channel is closed.
- channel:onClose ()
-
Returns a promise that resolves once the channel is closed.
Returns:
-
jls.lang.Promise
a promise that resolves once the channel is closed.
- channel:getName ()
-
Returns the name of this channel.
Returns:
-
string
the name of this channel.
- channel:bind ([closeWithAccepted[, scheme]])
-
Binds this channel.
When bound, the channel name can be used for connection.
Parameters:
- closeWithAccepted boolean true to indicate this channel shall be closed after all the accepted channels are closed. (optional)
- scheme string the scheme to use. (optional)
Returns:
-
jls.lang.Promise
a promise that resolves once the server channel is bound.
- channel:onAccept (ch)
-
Accepts a new channel.
This method should be overriden, the default implementation closes the channel.
Parameters:
- ch the channel to accept.
- channel:connect (name)
-
Connects this channel to the specified name.
Parameters:
- name string the name of the channel.
Returns:
-
jls.lang.Promise
a promise that resolves once the channel is connected.
- channel:receiveStart (handleMessage)
-
Starts receiving messages on this channel.
The handler will be called with the payload and the message type.
Parameters:
- handleMessage function a function that will be called when a message is received.
- channel:receiveStop ()
- Stops receiving messages on this channel. This server channel shall not be used anymore.
- channel:writeMessage (payload[, id[, callback]])
-
Writes a message on this channel.
Parameters:
- payload string the message to send
- id
number
the message identifier, default is
Channel.MESSAGE_ID_USER
. You are free to use ids greater than or equal toMESSAGE_ID_USER
. (optional) - callback function an optional callback function to use in place of promise. (optional)
Returns:
-
jls.lang.Promise
a promise that resolves once the message has been sent.