Reputation: 2990
Unlike boost.asio, netty has no read-like method. It is unconvenient in following situation: A management node manage some nodes, and clients connect to management node to retrieve information resides in nodes. When management node receive client's request, it sends a request to the correspond node and waiting for the node response. When the node response and management node gets the information in its ‘messageReceived’ function, how to send the information to the channel which belong to client? It needs send a event to Handler of client request.
1.get client request
2.send request to a node
3.read response of that node
4.send response to client
all these 4 step can be done in one function in boost.asio. But netty does not support step3, the read function is independent, user can not call it directly. The only way is after receive the response in handler between management node and node, re-send it to handler between management node and client. What is the typical way to do this?
Upvotes: 3
Views: 2211
Reputation: 9061
Netty uses async i/o operations hence step 2 is async and yes, you cannot block till you get step 3.
In most cases like this,(not specific to Netty) what you can do is:
1) provide unique id's to your requests, the node's are supposed to echo back the unique id's in their responses.
2) Store them in a hashmap with key as unique id of request and channel(between management node and client) as its value
3) When you receive the response from the node in management node, you can lookup the channel from hashmap using unique id and then send the response to client.
You can also take a look at Bruno de carvalho's Netty load balancer which will give you another perspective to the same issue. He uses tunneling to achieve the same effect.
Upvotes: 3