Giter Site home page Giter Site logo

Comments (5)

ergl avatar ergl commented on July 17, 2024

Hi! If I'm understanding you correctly, you don't know how to send the reply back to the original sender of the data inside the handle_data/2, right?

That function expects you reply with {ok, [response()], State}, where response() is {external_request_id(), term()}. This means that you have to return whatever request identifier you used to originally send your message to the server.

Check out an example with the arithmetic_tcp_client module under the test folder:

handle_request({Operation, A, B}, #state {
request_counter = RequestCounter
} = State) ->
RequestId = arithmetic_protocol:request_id(RequestCounter),
Data = arithmetic_protocol:request(RequestId, Operation, A, B),
{ok, RequestId, Data, State#state {
request_counter = RequestCounter + 1
}}.

The client creates a new request id, and generates a new message with it:

-spec request(int(), operation(), tiny_int(), tiny_int()) -> binary().
request(ReqId, Operation, A, B) ->
<<ReqId:32/integer, (opcode(Operation)), A:8/integer, B:8/integer>>.

As you can see, the original request id is part of the binary we send to the server, which should reply back with the id so that the client can re-use it:

handle_data(Data, #state {
buffer = Buffer
} = State) ->
Data2 = <<Buffer/binary, Data/binary>>,
{Replies, Buffer2} = arithmetic_protocol:parse_replies(Data2),
{ok, Replies, State#state {
buffer = Buffer2
}}.

Inside parse_replies, we extract the request identifier from the server message:

parse_replies(<<ReqId:32/integer, A:16/integer, Rest/binary>>, Acc) ->
parse_replies(Rest, [{ReqId, A} | Acc]);
parse_replies(Buffer, Acc) ->
{Acc, Buffer}.

As such, when arithmetic_tcp_client:handle_data/2 returns, the pool is able to match the original request id to the original sender, and return it.

Hope this helps!

from shackle.

nayibor avatar nayibor commented on July 17, 2024

hi.
thanks very much for the help and sorry for late reply.
thanks for the samples and its shed a lot of light on how the library works.
from our end whats happening is that the processes themselves are the direct connection between themselves and the tcp server.
there is no client sending a message with shackle acting as sort of a proxy.
i think thats the major difference.
so the thing is that when the shackle process gets the data,it just processes it and sends it back if necessary.
so i was confused because there is no way to know how to send data directly to the requesting party.
thanks!!

from shackle.

lpgauth avatar lpgauth commented on July 17, 2024

You can still use shackle without a request_id() if your response are in order. All you have to do it keep a counter of requests out and requests in.

from shackle.

nayibor avatar nayibor commented on July 17, 2024

please can u give me an example of that.
there is an example you cited above containing the counter but that also contains the request id.

from shackle.

lpgauth avatar lpgauth commented on July 17, 2024

You need to keep two counters (one for requests out and one for requests in).

See https://github.com/lpgauth/buoy/blob/master/src/buoy_client.erl

from shackle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.