How to design a flexible Erlang protocol stack creation API

好久不见. 提交于 2019-11-30 19:39:10

I'll throw in what I have planned so far as part of the answers:

  • connect gets passed a List of modules to stack, looking like a proplist in case of params e.g:

    connect([module1, module2, {module3, [params3]}], param0, further_params)
    

    each layer strips off the head and calls the next layers connect.

  • connect() "somehow" passes fun references up and/or down the layers

    • send for async sending down the stack will be returned by the lower level connect
    • recv for async receiving up the stack will be passed as param to the lower level connect
    • call for sync sending and waiting for an reply returned -- not sure how to handle these, probably also returned from the lower level connect
  • Multiplexers routing lists might look like these

    connect([module1, multiplexer, [[m_a_1, m_a_2, {m_a_3, [param_a_3]}], 
                                    [m_b_1, m_b_2],
                                    [{m_c_1, [param_c_1]}, m_c_2]], param0, 
                                                                    further_params]).
    

Currently I decided there will not be a extra function for synchronous calls, I'm just using send for it.

There is implementation example of the idea in this case for a module which is stateless: encode/1 and decode/1 do some for and back transformation on packets e.g. parse binary representation into a record and back:

connect(Chan, [Down|Rest], [], Recv_fun) ->
    {Down_module, Param} = case Down of
                               {F, P} -> {F, P};
                               F when is_atom (F) -> {F, []}
                           end,
    Send_fun = Down_module:connect(Chan, Rest, Param,
                                   fun(Packet) -> recv(Packet, Recv_fun) end),
    {ok, fun(Packet) -> send(Packet, Send_fun) end}.

send(Packet, Send_fun) ->
    Send_fun(encode(Packet)).

recv(Packet, Recv_fun) ->
    Recv_fun(decode(Packet)).

As soon as I have a stateful example I'll post it too.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!